1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.custom_collection_handling;
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 import java.util.List;
24
25 import org.apache.ibatis.BaseDataTest;
26 import org.apache.ibatis.io.Resources;
27 import org.apache.ibatis.session.SqlSession;
28 import org.apache.ibatis.session.SqlSessionFactory;
29 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30 import org.junit.jupiter.api.Test;
31
32 class CustomCollectionHandlingTest {
33
34
35
36
37 @Test
38 void testSelectListWithNestedResultMap() throws Exception {
39 String xmlConfig = "org/apache/ibatis/submitted/custom_collection_handling/MapperConfig.xml";
40 SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
41 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
42 List<Person> list = sqlSession
43 .selectList("org.apache.ibatis.submitted.custom_collection_handling.PersonMapper.findWithResultMap");
44 assertEquals(2, list.size());
45 assertEquals(2, list.get(0).getContacts().size());
46 assertEquals(1, list.get(1).getContacts().size());
47 assertEquals("3 Wall Street", list.get(0).getContacts().get(1).getAddress());
48 }
49 }
50
51
52
53
54 @Test
55 void testSelectListWithNestedSelect() throws Exception {
56 String xmlConfig = "org/apache/ibatis/submitted/custom_collection_handling/MapperConfig.xml";
57 SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
58 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59 List<Person> list = sqlSession
60 .selectList("org.apache.ibatis.submitted.custom_collection_handling.PersonMapper.findWithSelect");
61 assertEquals(2, list.size());
62 assertEquals(2, list.get(0).getContacts().size());
63 assertEquals(1, list.get(1).getContacts().size());
64 assertEquals("3 Wall Street", list.get(0).getContacts().get(1).getAddress());
65 }
66 }
67
68 private SqlSessionFactory getSqlSessionFactoryXmlConfig(String resource) throws Exception {
69 try (Reader configReader = Resources.getResourceAsReader(resource)) {
70 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
71
72 initDb(sqlSessionFactory);
73
74 return sqlSessionFactory;
75 }
76 }
77
78 private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
79 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
80 "org/apache/ibatis/submitted/custom_collection_handling/CreateDB.sql");
81 }
82 }