1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }