1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
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.List;
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.BeforeAll;
30 import org.junit.jupiter.api.Test;
31
32 class OneManyResultMapTest {
33
34 private static SqlSessionFactory sqlSessionFactory;
35
36 @BeforeAll
37 static void setUp() throws Exception {
38
39 try (Reader reader = Resources
40 .getResourceAsReader("org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/SqlMapConfig.xml")) {
41 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42 }
43
44
45 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46 "org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/CreateDB.sql");
47 }
48
49 @Test
50 void shouldUseResultMapWithMany() {
51 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52 UserDao mapper = sqlSession.getMapper(UserDao.class);
53 List<User> users = mapper.findAll();
54 assertNotNull(users);
55 assertEquals(4, users.size());
56 assertEquals(2, users.get(0).getRoles().size());
57 }
58 }
59
60 @Test
61 void shouldUseResultMapInXmlWithMany() {
62 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63 UserDao mapper = sqlSession.getMapper(UserDao.class);
64 List<User> users = mapper.findAll2();
65 assertNotNull(users);
66 assertEquals(4, users.size());
67 assertEquals(2, users.get(0).getRoles().size());
68 }
69 }
70
71 @Test
72 void shouldUseResultMapWithOne() {
73 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
74 UserDao mapper = sqlSession.getMapper(UserDao.class);
75 List<User> users = mapper.findAll3();
76 assertNotNull(users);
77 assertEquals(2, users.size());
78 assertNotNull(users.get(0).getRole());
79 assertEquals("teacher", users.get(0).getRole().getRoleName());
80 }
81 }
82
83 @Test
84 void shouldResolveResultMapInTheSameNamespace() {
85 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
86 UserDao mapper = sqlSession.getMapper(UserDao.class);
87 User headmaster = mapper.findHeadmaster();
88 assertNotNull(headmaster);
89 assertEquals(3, headmaster.getTeachers().size());
90 assertEquals("Doug Lea", headmaster.getTeachers().get(0).getUsername());
91 }
92 }
93
94 }