1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }