1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/stringlist/mybatis-config.xml")) {
43 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44 }
45
46
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 }