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 SameIdTest {
36
37 @Test
38 void testCrossReferenceXmlConfig() throws Exception {
39 testCrossReference(getSqlSessionFactoryXmlConfig());
40 }
41
42 @Test
43 void testCrossReferenceJavaConfig() throws Exception {
44 testCrossReference(getSqlSessionFactoryJavaConfig());
45 }
46
47 private void testCrossReference(SqlSessionFactory sqlSessionFactory) {
48 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49 SameIdPersonMapper personMapper = sqlSession.getMapper(SameIdPersonMapper.class);
50 Person person = personMapper.select(1);
51 assertEquals((Integer) 1, person.getId());
52 assertEquals(2, person.getPets().size());
53 assertEquals((Integer) 2, person.getPets().get(1).getId());
54
55 Pet pet = personMapper.selectPet(1);
56 assertEquals(Integer.valueOf(1), pet.getId());
57
58 SameIdPetMapper petMapper = sqlSession.getMapper(SameIdPetMapper.class);
59 Pet pet2 = petMapper.select(3);
60 assertEquals((Integer) 3, pet2.getId());
61 assertEquals((Integer) 2, pet2.getOwner().getId());
62 }
63 }
64
65 private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
66 try (Reader configReader = Resources
67 .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/SameIdMapperConfig.xml")) {
68 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
69
70 initDb(sqlSessionFactory);
71
72 return sqlSessionFactory;
73 }
74 }
75
76 private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
77 Configuration configuration = new Configuration();
78 Environment environment = new Environment("development", new JdbcTransactionFactory(),
79 new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
80 configuration.setEnvironment(environment);
81 configuration.addMapper(SameIdPersonMapper.class);
82 configuration.addMapper(SameIdPetMapper.class);
83
84 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
85
86 initDb(sqlSessionFactory);
87
88 return sqlSessionFactory;
89 }
90
91 private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
92 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
93 "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
94 }
95
96 }