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.*;
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.builder.BuilderException;
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.Assertions;
37 import org.junit.jupiter.api.Test;
38
39 class XmlExternalRefTest {
40
41 @Test
42 void testCrossReferenceXmlConfig() throws Exception {
43 testCrossReference(getSqlSessionFactoryXmlConfig());
44 }
45
46 @Test
47 void testCrossReferenceJavaConfig() throws Exception {
48 testCrossReference(getSqlSessionFactoryJavaConfig());
49 }
50
51 @Test
52 void testFailFastOnBuildAll() {
53 Configuration configuration = new Configuration();
54 try {
55 configuration.addMapper(InvalidMapper.class);
56 } catch (Exception e) {
57 fail("No exception should be thrown before parsing statement nodes.");
58 }
59 Assertions.assertThrows(BuilderException.class, configuration::getMappedStatementNames);
60 }
61
62 @Test
63 void testFailFastOnBuildAllWithInsert() {
64 Configuration configuration = new Configuration();
65 try {
66 configuration.addMapper(InvalidWithInsertMapper.class);
67 configuration.addMapper(InvalidMapper.class);
68 } catch (Exception e) {
69 fail("No exception should be thrown before parsing statement nodes.");
70 }
71 Assertions.assertThrows(BuilderException.class, configuration::getMappedStatementNames);
72 }
73
74 @Test
75 void testMappedStatementCache() throws Exception {
76 try (Reader configReader = Resources
77 .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml")) {
78 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
79
80 Configuration configuration = sqlSessionFactory.getConfiguration();
81 configuration.getMappedStatementNames();
82
83 MappedStatement selectPetStatement = configuration
84 .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PetMapper.select");
85 MappedStatement selectPersonStatement = configuration
86 .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PersonMapper.select");
87 Cache cache = selectPetStatement.getCache();
88 assertEquals("org.apache.ibatis.submitted.xml_external_ref.PetMapper", cache.getId());
89 assertSame(cache, selectPersonStatement.getCache());
90 }
91 }
92
93 private void testCrossReference(SqlSessionFactory sqlSessionFactory) {
94 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
95 PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
96 Person person = personMapper.select(1);
97 assertEquals((Integer) 1, person.getId());
98 assertEquals(2, person.getPets().size());
99 assertEquals((Integer) 2, person.getPets().get(1).getId());
100
101 Pet pet = personMapper.selectPet(1);
102 assertEquals(Integer.valueOf(1), pet.getId());
103
104 PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
105 Pet pet2 = petMapper.select(3);
106 assertEquals((Integer) 3, pet2.getId());
107 assertEquals((Integer) 2, pet2.getOwner().getId());
108 }
109 }
110
111 private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
112 try (Reader configReader = Resources
113 .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml")) {
114 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
115
116 initDb(sqlSessionFactory);
117
118 return sqlSessionFactory;
119 }
120 }
121
122 private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
123 Configuration configuration = new Configuration();
124 Environment environment = new Environment("development", new JdbcTransactionFactory(),
125 new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
126 configuration.setEnvironment(environment);
127 configuration.addMapper(PersonMapper.class);
128 configuration.addMapper(PetMapper.class);
129
130 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
131
132 initDb(sqlSessionFactory);
133
134 return sqlSessionFactory;
135 }
136
137 private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
138 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
139 "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
140 }
141
142 }