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.result_handler_type;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.Reader;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.junit.jupiter.api.Test;
30  
31  class DefaultResultHandlerTypeTest {
32  
33    @Test
34    void testSelectList() throws Exception {
35      String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml";
36      SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
37      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
38        List<Person> list = sqlSession
39            .selectList("org.apache.ibatis.submitted.result_handler_type.PersonMapper.doSelect");
40        assertEquals(list.size(), 2);
41        assertEquals("java.util.LinkedList", list.getClass().getCanonicalName());
42      }
43    }
44  
45    @Test
46    void testSelectMap() throws Exception {
47      String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml";
48      SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
49      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50        Map<Integer, Person> map = sqlSession
51            .selectMap("org.apache.ibatis.submitted.result_handler_type.PersonMapper.doSelect", "id");
52        assertEquals(map.size(), 2);
53        assertEquals("java.util.LinkedHashMap", map.getClass().getCanonicalName());
54      }
55    }
56  
57    @Test
58    void testSelectMapAnnotation() throws Exception {
59      String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml";
60      SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig);
61      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
63        Map<Integer, Person> map = mapper.selectAsMap();
64        assertEquals(map.size(), 2);
65        assertEquals("java.util.LinkedHashMap", map.getClass().getCanonicalName());
66      }
67    }
68  
69    private SqlSessionFactory getSqlSessionFactoryXmlConfig(String resource) throws Exception {
70      try (Reader configReader = Resources.getResourceAsReader(resource)) {
71        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
72        BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
73            "org/apache/ibatis/submitted/result_handler_type/CreateDB.sql");
74  
75        return sqlSessionFactory;
76      }
77    }
78  
79  }