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