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.*;
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.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml")) {
43 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44 }
45
46
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 }