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.stringlist;
17  
18  import static org.junit.jupiter.api.Assertions.*;
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.exceptions.PersistenceException;
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.Assertions;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  class StringListTest {
35  
36    private static SqlSessionFactory sqlSessionFactory;
37  
38    @BeforeAll
39    static void setUp() throws Exception {
40      // create a SqlSessionFactory
41      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/stringlist/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/stringlist/CreateDB.sql");
48    }
49  
50    @Test
51    void shouldMapListOfStrings() {
52      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53        Mapper mapper = sqlSession.getMapper(Mapper.class);
54        List<User> users = mapper.getUsersAndGroups(1);
55        Assertions.assertEquals(1, users.size());
56        Assertions.assertEquals(2, users.get(0).getGroups().size());
57        Assertions.assertEquals(2, users.get(0).getRoles().size());
58      }
59    }
60  
61    @Test
62    void shouldMapListOfStringsToMap() {
63      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
64        Mapper mapper = sqlSession.getMapper(Mapper.class);
65        List<Map<String, Object>> results = mapper.getUsersAndGroupsMap(1);
66        Assertions.assertEquals(1, results.size());
67        Assertions.assertEquals(2, ((List<?>) results.get(0).get("groups")).size());
68        Assertions.assertEquals(2, ((List<?>) results.get(0).get("roles")).size());
69      }
70    }
71  
72    @Test
73    void shouldFailFastIfCollectionTypeIsAmbiguous() throws Exception {
74      try (Reader reader = Resources
75          .getResourceAsReader("org/apache/ibatis/submitted/stringlist/mybatis-config-invalid.xml")) {
76        new SqlSessionFactoryBuilder().build(reader);
77        fail("Should throw exception when collection type is unresolvable.");
78      } catch (PersistenceException e) {
79        assertTrue(e.getMessage()
80            .contains("Ambiguous collection type for property 'groups'. You must specify 'javaType' or 'resultMap'."));
81      }
82    }
83  }