1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.cursor_nested;
17
18 import java.io.Reader;
19 import java.util.Iterator;
20
21 import org.apache.ibatis.BaseDataTest;
22 import org.apache.ibatis.cursor.Cursor;
23 import org.apache.ibatis.io.Resources;
24 import org.apache.ibatis.session.RowBounds;
25 import org.apache.ibatis.session.SqlSession;
26 import org.apache.ibatis.session.SqlSessionFactory;
27 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31
32 class CursorNestedTest {
33
34 private static SqlSessionFactory sqlSessionFactory;
35
36 @BeforeAll
37 static void setUp() throws Exception {
38
39 try (
40 Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/cursor_nested/mybatis-config.xml")) {
41 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42 }
43
44
45 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46 "org/apache/ibatis/submitted/cursor_nested/CreateDB.sql");
47 }
48
49 @Test
50 void shouldGetAllUser() {
51 Cursor<User> usersCursor;
52 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53 Mapper mapper = sqlSession.getMapper(Mapper.class);
54 usersCursor = mapper.getAllUsers();
55
56 Assertions.assertFalse(usersCursor.isOpen());
57
58 Iterator<User> iterator = usersCursor.iterator();
59
60
61 Assertions.assertTrue(iterator.hasNext());
62 Assertions.assertTrue(usersCursor.isOpen());
63 Assertions.assertFalse(usersCursor.isConsumed());
64
65 User user = iterator.next();
66 Assertions.assertEquals(2, user.getGroups().size());
67 Assertions.assertEquals(3, user.getRoles().size());
68
69 user = iterator.next();
70 Assertions.assertEquals(1, user.getGroups().size());
71 Assertions.assertEquals(3, user.getRoles().size());
72
73 user = iterator.next();
74 Assertions.assertEquals(3, user.getGroups().size());
75 Assertions.assertEquals(1, user.getRoles().size());
76
77 user = iterator.next();
78 Assertions.assertEquals(2, user.getGroups().size());
79 Assertions.assertEquals(2, user.getRoles().size());
80
81 Assertions.assertTrue(usersCursor.isOpen());
82 Assertions.assertFalse(usersCursor.isConsumed());
83
84
85 Assertions.assertFalse(iterator.hasNext());
86 Assertions.assertFalse(usersCursor.isOpen());
87 Assertions.assertTrue(usersCursor.isConsumed());
88 }
89 Assertions.assertFalse(usersCursor.isOpen());
90 }
91
92 @Test
93 void testCursorWithRowBound() {
94 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
95 Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(2, 1));
96
97 Iterator<User> iterator = usersCursor.iterator();
98
99 Assertions.assertTrue(iterator.hasNext());
100 User user = iterator.next();
101 Assertions.assertEquals("User3", user.getName());
102 Assertions.assertEquals(2, usersCursor.getCurrentIndex());
103
104 Assertions.assertFalse(iterator.hasNext());
105 Assertions.assertFalse(usersCursor.isOpen());
106 Assertions.assertTrue(usersCursor.isConsumed());
107 }
108 }
109 }