View Javadoc
1   /**
2    *    Copyright 2009-2019 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.getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml")) {
52        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
53  
54        Configuration configuration = sqlSessionFactory.getConfiguration();
55        configuration.getMappedStatementNames();
56  
57        MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select");
58        MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select");
59        Cache cache = selectPetStatement.getCache();
60        assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId());
61        assertSame(cache, selectPersonStatement.getCache());
62      }
63    }
64  
65    private void testCrossReference(SqlSessionFactory sqlSessionFactory) {
66      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
67        MultipleCrossIncludePersonMapper personMapper = sqlSession.getMapper(MultipleCrossIncludePersonMapper.class);
68        Person person = personMapper.select(1);
69        assertEquals((Integer) 1, person.getId());
70        assertEquals(2, person.getPets().size());
71        assertEquals((Integer) 2, person.getPets().get(1).getId());
72  
73        Pet pet = personMapper.selectPet(1);
74        assertEquals(Integer.valueOf(1), pet.getId());
75  
76        MultipleCrossIncludePetMapper petMapper = sqlSession.getMapper(MultipleCrossIncludePetMapper.class);
77        Pet pet2 = petMapper.select(3);
78        assertEquals((Integer)3, pet2.getId());
79        assertEquals((Integer)2, pet2.getOwner().getId());
80      }
81    }
82  
83    private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
84      try (Reader configReader = Resources
85          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml")) {
86        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
87  
88        initDb(sqlSessionFactory);
89  
90        return sqlSessionFactory;
91      }
92    }
93  
94    private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
95      Configuration configuration = new Configuration();
96      Environment environment = new Environment("development", new JdbcTransactionFactory(), new UnpooledDataSource(
97          "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
98      configuration.setEnvironment(environment);
99      configuration.addMapper(MultipleCrossIncludePersonMapper.class);
100     configuration.addMapper(MultipleCrossIncludePetMapper.class);
101     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
102 
103     initDb(sqlSessionFactory);
104 
105     return sqlSessionFactory;
106   }
107 
108   private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
109     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
110             "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
111   }
112 
113 }