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.nestedresulthandler_association;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
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
43          .getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml")) {
44        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45      }
46  
47      // populate in-memory database
48      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
49          "org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql");
50    }
51  
52    @Test
53    void shouldHandleRowBounds() throws Exception {
54      final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
55      Date targetMonth = fmt.parse("2014-01-01");
56      final List<Account> accounts = new ArrayList<>();
57      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58        sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), context -> {
59          Account account = (Account) context.getResultObject();
60          accounts.add(account);
61        });
62      }
63      assertEquals(2, accounts.size());
64      assertEquals("Bob2", accounts.get(0).getAccountName());
65      assertEquals("Bob3", accounts.get(1).getAccountName());
66    }
67  
68    @Test
69    void shouldHandleStop() throws Exception {
70      final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
71      final List<Account> accounts = new ArrayList<>();
72      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73        Date targetMonth = fmt.parse("2014-01-01");
74        sqlSession.select("collectPageByBirthMonth", targetMonth, context -> {
75          Account account = (Account) context.getResultObject();
76          accounts.add(account);
77          if (accounts.size() > 1) {
78            context.stop();
79          }
80        });
81      }
82      assertEquals(2, accounts.size());
83      assertEquals("Bob1", accounts.get(0).getAccountName());
84      assertEquals("Bob2", accounts.get(1).getAccountName());
85    }
86  
87  }