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