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.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      // create an SqlSessionFactory
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      // populate in-memory database
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  }