View Javadoc
1   /*
2    *    Copyright 2009-2021 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.submitted.xml_external_ref;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.sql.SQLException;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.cache.Cache;
26  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
27  import org.apache.ibatis.io.Resources;
28  import org.apache.ibatis.mapping.Environment;
29  import org.apache.ibatis.mapping.MappedStatement;
30  import org.apache.ibatis.session.Configuration;
31  import org.apache.ibatis.session.SqlSession;
32  import org.apache.ibatis.session.SqlSessionFactory;
33  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
34  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
35  import org.junit.jupiter.api.Test;
36  
37  class MultipleCrossIncludeTest {
38  
39    @Test
40    void testMultipleCrossIncludeXmlConfig() throws Exception {
41      testCrossReference(getSqlSessionFactoryXmlConfig());
42    }
43  
44    @Test
45    void testMultipleCrossIncludeJavaConfig() throws Exception {
46      testCrossReference(getSqlSessionFactoryJavaConfig());
47    }
48  
49    @Test
50    void testMappedStatementCache() throws Exception {
51      try (Reader configReader = Resources
52          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml")) {
53        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
54  
55        Configuration configuration = sqlSessionFactory.getConfiguration();
56        configuration.getMappedStatementNames();
57  
58        MappedStatement selectPetStatement = configuration
59            .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select");
60        MappedStatement selectPersonStatement = configuration
61            .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select");
62        Cache cache = selectPetStatement.getCache();
63        assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId());
64        assertSame(cache, selectPersonStatement.getCache());
65      }
66    }
67  
68    private void testCrossReference(SqlSessionFactory sqlSessionFactory) {
69      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
70        MultipleCrossIncludePersonMapper personMapper = sqlSession.getMapper(MultipleCrossIncludePersonMapper.class);
71        Person person = personMapper.select(1);
72        assertEquals((Integer) 1, person.getId());
73        assertEquals(2, person.getPets().size());
74        assertEquals((Integer) 2, person.getPets().get(1).getId());
75  
76        Pet pet = personMapper.selectPet(1);
77        assertEquals(Integer.valueOf(1), pet.getId());
78  
79        MultipleCrossIncludePetMapper petMapper = sqlSession.getMapper(MultipleCrossIncludePetMapper.class);
80        Pet pet2 = petMapper.select(3);
81        assertEquals((Integer) 3, pet2.getId());
82        assertEquals((Integer) 2, pet2.getOwner().getId());
83      }
84    }
85  
86    private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
87      try (Reader configReader = Resources
88          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml")) {
89        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
90  
91        initDb(sqlSessionFactory);
92  
93        return sqlSessionFactory;
94      }
95    }
96  
97    private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
98      Configuration configuration = new Configuration();
99      Environment environment = new Environment("development", new JdbcTransactionFactory(),
100         new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
101     configuration.setEnvironment(environment);
102     configuration.addMapper(MultipleCrossIncludePersonMapper.class);
103     configuration.addMapper(MultipleCrossIncludePetMapper.class);
104     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
105 
106     initDb(sqlSessionFactory);
107 
108     return sqlSessionFactory;
109   }
110 
111   private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
112     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
113         "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
114   }
115 
116 }