View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertSame;
20  
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.sql.SQLException;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.cache.Cache;
27  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
28  import org.apache.ibatis.io.Resources;
29  import org.apache.ibatis.mapping.Environment;
30  import org.apache.ibatis.mapping.MappedStatement;
31  import org.apache.ibatis.session.Configuration;
32  import org.apache.ibatis.session.SqlSession;
33  import org.apache.ibatis.session.SqlSessionFactory;
34  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
35  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
36  import org.junit.jupiter.api.Test;
37  
38  class MultipleCrossIncludeTest {
39  
40    @Test
41    void testMultipleCrossIncludeXmlConfig() throws Exception {
42      testCrossReference(getSqlSessionFactoryXmlConfig());
43    }
44  
45    @Test
46    void testMultipleCrossIncludeJavaConfig() throws Exception {
47      testCrossReference(getSqlSessionFactoryJavaConfig());
48    }
49  
50    @Test
51    void testMappedStatementCache() throws Exception {
52      try (Reader configReader = Resources
53          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml")) {
54        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
55  
56        Configuration configuration = sqlSessionFactory.getConfiguration();
57        configuration.getMappedStatementNames();
58  
59        MappedStatement selectPetStatement = configuration
60            .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select");
61        MappedStatement selectPersonStatement = configuration
62            .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select");
63        Cache cache = selectPetStatement.getCache();
64        assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId());
65        assertSame(cache, selectPersonStatement.getCache());
66      }
67    }
68  
69    private void testCrossReference(SqlSessionFactory sqlSessionFactory) {
70      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
71        MultipleCrossIncludePersonMapper personMapper = sqlSession.getMapper(MultipleCrossIncludePersonMapper.class);
72        Person person = personMapper.select(1);
73        assertEquals((Integer) 1, person.getId());
74        assertEquals(2, person.getPets().size());
75        assertEquals((Integer) 2, person.getPets().get(1).getId());
76  
77        Pet pet = personMapper.selectPet(1);
78        assertEquals(Integer.valueOf(1), pet.getId());
79  
80        MultipleCrossIncludePetMapper petMapper = sqlSession.getMapper(MultipleCrossIncludePetMapper.class);
81        Pet pet2 = petMapper.select(3);
82        assertEquals((Integer) 3, pet2.getId());
83        assertEquals((Integer) 2, pet2.getOwner().getId());
84      }
85    }
86  
87    private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
88      try (Reader configReader = Resources
89          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml")) {
90        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
91  
92        initDb(sqlSessionFactory);
93  
94        return sqlSessionFactory;
95      }
96    }
97  
98    private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
99      Configuration configuration = new Configuration();
100     Environment environment = new Environment("development", new JdbcTransactionFactory(),
101         new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
102     configuration.setEnvironment(environment);
103     configuration.addMapper(MultipleCrossIncludePersonMapper.class);
104     configuration.addMapper(MultipleCrossIncludePetMapper.class);
105     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
106 
107     initDb(sqlSessionFactory);
108 
109     return sqlSessionFactory;
110   }
111 
112   private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
113     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
114         "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
115   }
116 
117 }