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.mapper_type_parameter;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.io.Reader;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.ibatis.BaseDataTest;
27  import org.apache.ibatis.io.Resources;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  class MapperTypeParameterTest {
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create an SqlSessionFactory
40      try (Reader reader = Resources
41          .getResourceAsReader("org/apache/ibatis/submitted/mapper_type_parameter/mybatis-config.xml")) {
42        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43      }
44  
45      // populate in-memory database
46      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47          "org/apache/ibatis/submitted/mapper_type_parameter/CreateDB.sql");
48    }
49  
50    @Test
51    void shouldResolveReturnType() {
52      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
54        Person person = mapper.select(new Person(1));
55        assertEquals("Jane", person.getName());
56      }
57    }
58  
59    @Test
60    void shouldResolveListTypeParam() {
61      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
63        List<Person> persons = mapper.selectList(null);
64        assertEquals(2, persons.size());
65        assertEquals("Jane", persons.get(0).getName());
66        assertEquals("John", persons.get(1).getName());
67      }
68    }
69  
70    @Test
71    void shouldResolveMultipleTypeParam() {
72      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
74        Map<Long, Country> results = mapper.selectMap(new Country());
75        assertEquals(2, results.size());
76        assertEquals("Japan", results.get(1L).getName());
77        assertEquals("New Zealand", results.get(2L).getName());
78      }
79    }
80  
81    @Test
82    void shouldResolveParameterizedReturnType() {
83      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
84        PersonListMapper mapper = sqlSession.getMapper(PersonListMapper.class);
85        List<Person> persons = mapper.select(null);
86        assertEquals(2, persons.size());
87        assertEquals("Jane", persons.get(0).getName());
88        assertEquals("John", persons.get(1).getName());
89      }
90    }
91  
92    @Test
93    void shouldResolveParam() {
94      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
95        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
96        assertEquals(1, mapper.update(new Country(2L, "Greenland")));
97      }
98    }
99  
100   @Test
101   void shouldResolveListParam() {
102     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
103       PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
104       Person person1 = new Person("James");
105       assertEquals(1, mapper.insert(Collections.singletonList(person1)));
106       assertNotNull(person1.getId());
107     }
108   }
109 }