View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
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.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       * Custom collections with nested select.
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  }