1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }