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.*;
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.selectList("org.apache.ibatis.submitted.custom_collection_handling.PersonMapper.findWithResultMap");
43 assertEquals(2, list.size());
44 assertEquals(2, list.get(0).getContacts().size());
45 assertEquals(1, list.get(1).getContacts().size());
46 assertEquals("3 Wall Street", list.get(0).getContacts().get(1).getAddress());
47 }
48 }
49
50
51
52
53 @Test
54 void testSelectListWithNestedSelect() throws Exception {
55 String xmlConfig = "org/apache/ibatis/submitted/custom_collection_handling/MapperConfig.xml";
56 SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
57 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58 List<Person> list = sqlSession.selectList("org.apache.ibatis.submitted.custom_collection_handling.PersonMapper.findWithSelect");
59 assertEquals(2, list.size());
60 assertEquals(2, list.get(0).getContacts().size());
61 assertEquals(1, list.get(1).getContacts().size());
62 assertEquals("3 Wall Street", list.get(0).getContacts().get(1).getAddress());
63 }
64 }
65
66 private SqlSessionFactory getSqlSessionFactoryXmlConfig(String resource) throws Exception {
67 try (Reader configReader = Resources.getResourceAsReader(resource)) {
68 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
69
70 initDb(sqlSessionFactory);
71
72 return sqlSessionFactory;
73 }
74 }
75
76 private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
77 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
78 "org/apache/ibatis/submitted/custom_collection_handling/CreateDB.sql");
79 }
80 }