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 MultipleIncludeTest {
36  
37    @Test
38    void testMultipleIncludeXmlConfig() throws Exception {
39      testMultipleIncludes(getSqlSessionFactoryXmlConfig());
40    }
41  
42    @Test
43    void testMultipleIncludeJavaConfig() throws Exception {
44      testMultipleIncludes(getSqlSessionFactoryJavaConfig());
45    }
46  
47    private void testMultipleIncludes(SqlSessionFactory sqlSessionFactory) {
48      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49        MultipleIncludePersonMapper personMapper = sqlSession.getMapper(MultipleIncludePersonMapper.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/MultipleIncludeMapperConfig.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  
69      Configuration configuration = new Configuration();
70      Environment environment = new Environment("development", new JdbcTransactionFactory(),
71          new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
72      configuration.setEnvironment(environment);
73      configuration.addMapper(MultipleIncludePersonMapper.class);
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  }