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.nestedresulthandler;
17  
18  import java.io.Reader;
19  import java.util.List;
20  
21  import org.apache.ibatis.BaseDataTest;
22  import org.apache.ibatis.exceptions.PersistenceException;
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Test;
30  
31  class NestedResultHandlerTest {
32    private static SqlSessionFactory sqlSessionFactory;
33  
34    @BeforeAll
35    static void setUp() throws Exception {
36      // create a SqlSessionFactory
37      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler/mybatis-config.xml")) {
38        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39      }
40  
41      // populate in-memory database
42      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
43          "org/apache/ibatis/submitted/nestedresulthandler/CreateDB.sql");
44    }
45  
46    @Test
47    void testGetPerson() {
48      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49        Mapper mapper = sqlSession.getMapper(Mapper.class);
50  
51        List<Person> persons = mapper.getPersons();
52  
53        Person person = persons.get(0);
54        Assertions.assertEquals("grandma", person.getName());
55        Assertions.assertTrue(person.owns("book"));
56        Assertions.assertTrue(person.owns("tv"));
57        Assertions.assertEquals(2, person.getItems().size());
58  
59        person = persons.get(1);
60        Assertions.assertEquals("sister", person.getName());
61        Assertions.assertTrue(person.owns("phone"));
62        Assertions.assertTrue(person.owns("shoes"));
63        Assertions.assertEquals(2, person.getItems().size());
64  
65        person = persons.get(2);
66        Assertions.assertEquals("brother", person.getName());
67        Assertions.assertTrue(person.owns("car"));
68        Assertions.assertEquals(1, person.getItems().size());
69      }
70    }
71  
72    @Test
73    // issue #542
74    void testGetPersonWithHandler() {
75      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76        sqlSession.select("getPersons", context -> {
77          Person person = (Person) context.getResultObject();
78          if ("grandma".equals(person.getName())) {
79            Assertions.assertEquals(2, person.getItems().size());
80          }
81        });
82      }
83    }
84  
85    @Test
86    void testUnorderedGetPersonWithHandler() {
87      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88        Assertions.assertThrows(PersistenceException.class, () -> sqlSession.select("getPersonsWithItemsOrdered", context -> {
89          Person person = (Person) context.getResultObject();
90          if ("grandma".equals(person.getName())) {
91            person.getItems().size();
92          }
93        }));
94      }
95    }
96  
97    /**
98     * Fix bug caused by issue #542, see new issue #22 on github If we order by a
99     * nested result map attribute we can miss some records and end up with
100    * duplicates instead.
101    */
102   @Test
103   void testGetPersonOrderedByItem() {
104     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
105       Mapper mapper = sqlSession.getMapper(Mapper.class);
106 
107       List<Person> persons = mapper.getPersonsWithItemsOrdered();
108 
109       Person person = persons.get(0);
110       Assertions.assertEquals("grandma", person.getName());
111       Assertions.assertTrue(person.owns("book"));
112       Assertions.assertTrue(person.owns("tv"));
113       Assertions.assertEquals(2, person.getItems().size());
114 
115       person = persons.get(1);
116       Assertions.assertEquals("brother", person.getName());
117       Assertions.assertTrue(person.owns("car"));
118       Assertions.assertEquals(1, person.getItems().size());
119 
120       person = persons.get(2);
121       Assertions.assertEquals("sister", person.getName());
122       Assertions.assertTrue(person.owns("phone"));
123       Assertions.assertTrue(person.owns("shoes"));
124       Assertions.assertEquals(2, person.getItems().size());
125     }
126   }
127 
128   @Test // reopen issue 39? (not a bug?)
129   void testGetPersonItemPairs() {
130     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
131       Mapper mapper = sqlSession.getMapper(Mapper.class);
132       List<PersonItemPair> pairs = mapper.getPersonItemPairs();
133 
134       Assertions.assertNotNull(pairs);
135       // System.out.println( new StringBuilder().append("selected pairs: ").append(pairs) );
136 
137       Assertions.assertEquals(5, pairs.size());
138       Assertions.assertNotNull(pairs.get(0).getPerson());
139       Assertions.assertEquals(pairs.get(0).getPerson().getId(), Integer.valueOf(1));
140       Assertions.assertNotNull(pairs.get(0).getItem());
141       Assertions.assertEquals(pairs.get(0).getItem().getId(), Integer.valueOf(1));
142     }
143   }
144 
145 }