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  
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.datasource.unpooled.UnpooledDataSource;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.mapping.Environment;
28  import org.apache.ibatis.session.Configuration;
29  import org.apache.ibatis.session.SqlSession;
30  import org.apache.ibatis.session.SqlSessionFactory;
31  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
32  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
33  import org.junit.jupiter.api.Test;
34  
35  class MultipleReverseIncludeTest {
36  
37    @Test
38    void testMultipleReverseIncludeXmlConfig() throws Exception {
39      testMultipleReverseIncludes(getSqlSessionFactoryXmlConfig());
40    }
41  
42    @Test
43    void testMultipleReverseIncludeJavaConfig() throws Exception {
44      testMultipleReverseIncludes(getSqlSessionFactoryJavaConfig());
45    }
46  
47    private void testMultipleReverseIncludes(SqlSessionFactory sqlSessionFactory) {
48      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49        MultipleReverseIncludePersonMapper personMapper = sqlSession.getMapper(MultipleReverseIncludePersonMapper.class);
50        Person person = personMapper.select(1);
51        assertEquals((Integer) 1, person.getId());
52        assertEquals("John", person.getName());
53      }
54    }
55  
56    private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
57      try (Reader configReader = Resources
58          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleReverseIncludeMapperConfig.xml")) {
59        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
60  
61        initDb(sqlSessionFactory);
62  
63        return sqlSessionFactory;
64      }
65    }
66  
67    private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
68      Configuration configuration = new Configuration();
69      Environment environment = new Environment("development", new JdbcTransactionFactory(),
70          new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
71      configuration.setEnvironment(environment);
72      configuration.addMapper(MultipleReverseIncludePersonMapper.class);
73  
74      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
75  
76      initDb(sqlSessionFactory);
77  
78      return sqlSessionFactory;
79    }
80  
81    private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
82      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
83          "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
84    }
85  
86  }