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_association;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.Reader;
21  import java.text.SimpleDateFormat;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.apache.ibatis.BaseDataTest;
27  import org.apache.ibatis.io.Resources;
28  import org.apache.ibatis.session.RowBounds;
29  import org.apache.ibatis.session.SqlSession;
30  import org.apache.ibatis.session.SqlSessionFactory;
31  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Test;
34  
35  class NestedResultHandlerAssociationTest {
36  
37    private static SqlSessionFactory sqlSessionFactory;
38  
39    @BeforeAll
40    static void setUp() throws Exception {
41      // create an SqlSessionFactory
42      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml")) {
43        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44      }
45  
46      // populate in-memory database
47      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
48          "org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql");
49    }
50  
51    @Test
52    void shouldHandleRowBounds() throws Exception {
53      final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
54      Date targetMonth = fmt.parse("2014-01-01");
55      final List<Account> accounts = new ArrayList<>();
56      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57        sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), context -> {
58          Account account = (Account) context.getResultObject();
59          accounts.add(account);
60        });
61      }
62      assertEquals(2, accounts.size());
63      assertEquals("Bob2", accounts.get(0).getAccountName());
64      assertEquals("Bob3", accounts.get(1).getAccountName());
65    }
66  
67    @Test
68    void shouldHandleStop() throws Exception {
69      final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
70      final List<Account> accounts = new ArrayList<>();
71      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72        Date targetMonth = fmt.parse("2014-01-01");
73        sqlSession.select("collectPageByBirthMonth", targetMonth, context -> {
74          Account account = (Account) context.getResultObject();
75          accounts.add(account);
76          if (accounts.size() > 1)
77            context.stop();
78        });
79      }
80      assertEquals(2, accounts.size());
81      assertEquals("Bob1", accounts.get(0).getAccountName());
82      assertEquals("Bob2", accounts.get(1).getAccountName());
83    }
84  
85  }