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_simple;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.IOException;
21  import java.util.Iterator;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.cursor.Cursor;
25  import org.apache.ibatis.mapping.Environment;
26  import org.apache.ibatis.session.Configuration;
27  import org.apache.ibatis.session.ExecutorType;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.apache.ibatis.testcontainers.PgContainer;
32  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
33  import org.junit.jupiter.api.BeforeAll;
34  import org.junit.jupiter.api.Tag;
35  import org.junit.jupiter.api.Test;
36  
37  @Tag("TestcontainersTests")
38  class PostgresCursorTest {
39  
40    private static SqlSessionFactory sqlSessionFactory;
41  
42    @BeforeAll
43    static void setUp() throws Exception {
44      Configuration configuration = new Configuration();
45      Environment environment = new Environment("development", new JdbcTransactionFactory(),
46          PgContainer.getUnpooledDataSource());
47      configuration.setEnvironment(environment);
48      configuration.addMapper(Mapper.class);
49      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
50  
51      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
52          "org/apache/ibatis/submitted/cursor_simple/CreateDB.sql");
53    }
54  
55    @Test
56    void shouldBeAbleToReuseStatement() throws IOException {
57      // #1351
58      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE)) {
59        Mapper mapper = sqlSession.getMapper(Mapper.class);
60        {
61          Cursor<User> usersCursor = mapper.getAllUsers();
62          Iterator<User> iterator = usersCursor.iterator();
63          User user = iterator.next();
64          assertEquals("User1", user.getName());
65          usersCursor.close();
66        }
67        {
68          Cursor<User> usersCursor = mapper.getAllUsers();
69          Iterator<User> iterator = usersCursor.iterator();
70          User user = iterator.next();
71          assertEquals("User1", user.getName());
72          usersCursor.close();
73        }
74      }
75    }
76  }