View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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     * Custom collections with nested resultMap.
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     * Custom collections with nested select.
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  }