View Javadoc
1   /*
2    *    Copyright 2009-2022 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.result_set_type;
17  
18  import java.io.Reader;
19  import java.util.List;
20  import java.util.function.Function;
21  
22  import org.apache.ibatis.BaseDataTest;
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.jdbc.ScriptRunner;
25  import org.apache.ibatis.session.RowBounds;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  class ResultSetTypeTest {
34  
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create an SqlSessionFactory
40      try (Reader reader = Resources
41          .getResourceAsReader("org/apache/ibatis/submitted/result_set_type/mybatis-config.xml")) {
42        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43      }
44  
45      // populate in-memory database
46      ScriptRunner runner = new ScriptRunner(
47          sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection());
48      runner.setDelimiter("go");
49      runner.setLogWriter(null);
50      runner.setErrorLogWriter(null);
51      BaseDataTest.runScript(runner, "org/apache/ibatis/submitted/result_set_type/CreateDB.sql");
52    }
53  
54    @Test
55    void testWithStatement() {
56      test(mapper -> mapper.getUserWithStatementAndUnset(new RowBounds(5, 3)), 0);
57      test(mapper -> mapper.getUserWithStatementAndDefault(new RowBounds(4, 3)), 1);
58      test(mapper -> mapper.getUserWithStatementAndForwardOnly(new RowBounds(3, 3)), 2);
59      test(mapper -> mapper.getUserWithStatementAndScrollInsensitive(new RowBounds(2, 2)), 2);
60      test(mapper -> mapper.getUserWithStatementAndScrollSensitive(new RowBounds(1, 1)), 1);
61    }
62  
63    @Test
64    void testWithPrepared() {
65      test(mapper -> mapper.getUserWithPreparedAndUnset(new RowBounds(5, 3)), 0);
66      test(mapper -> mapper.getUserWithPreparedAndDefault(new RowBounds(4, 3)), 1);
67      test(mapper -> mapper.getUserWithPreparedAndForwardOnly(new RowBounds(3, 3)), 2);
68      test(mapper -> mapper.getUserWithPreparedAndScrollInsensitive(new RowBounds(2, 2)), 2);
69      test(mapper -> mapper.getUserWithPreparedAndScrollSensitive(new RowBounds(1, 1)), 1);
70    }
71  
72    @Test
73    void testWithCallable() {
74      test(mapper -> mapper.getUserWithCallableAndUnset(new RowBounds(5, 3)), 0);
75      test(mapper -> mapper.getUserWithCallableAndDefault(new RowBounds(4, 3)), 1);
76      test(mapper -> mapper.getUserWithCallableAndForwardOnly(new RowBounds(3, 3)), 2);
77      test(mapper -> mapper.getUserWithCallableAndScrollInsensitive(new RowBounds(2, 2)), 2);
78      test(mapper -> mapper.getUserWithCallableAndScrollSensitive(new RowBounds(1, 1)), 1);
79    }
80  
81    private void test(Function<Mapper, List<User>> usersSupplier, int expectedSize) {
82      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83        Mapper mapper = sqlSession.getMapper(Mapper.class);
84        List<User> users = usersSupplier.apply(mapper);
85        Assertions.assertEquals(expectedSize, users.size());
86      }
87    }
88  
89  }