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.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      // create a SqlSessionFactory
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      // populate in-memory database
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        // Retrieving iterator, fetching is not started
58        Iterator<User> iterator = usersCursor.iterator();
59  
60        // Check if hasNext, fetching is started
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        // Check no more elements
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 }