1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.repeatable;
17
18 import java.io.IOException;
19 import java.io.Reader;
20 import java.sql.SQLException;
21
22 import org.apache.ibatis.BaseDataTest;
23 import org.apache.ibatis.io.Resources;
24 import org.apache.ibatis.session.SqlSession;
25 import org.apache.ibatis.session.SqlSessionFactory;
26 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29
30 class RepeatableInsertTest {
31
32 @Test
33 void hsql() throws IOException, SQLException {
34 SqlSessionFactory sqlSessionFactory;
35 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
36 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-hsql");
37 }
38
39 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
40 "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
41
42 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
43 Mapper mapper = sqlSession.getMapper(Mapper.class);
44 int count = mapper.count();
45 User newUser = new User();
46 newUser.setName("Test");
47 mapper.insertUser(newUser);
48
49 User user = mapper.getUser(newUser.getId());
50 Assertions.assertEquals(Integer.valueOf(count + 1), user.getId());
51 Assertions.assertEquals("Test HSQL", user.getName());
52 }
53 }
54
55 @Test
56 void hsqlUsingProvider() throws IOException, SQLException {
57 SqlSessionFactory sqlSessionFactory;
58 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
59 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-hsql");
60 }
61
62 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
63 "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
64
65 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
66 Mapper mapper = sqlSession.getMapper(Mapper.class);
67 int count = mapper.count();
68 User newUser = new User();
69 newUser.setName("Test");
70 mapper.insertUserUsingProvider(newUser);
71
72 User user = mapper.getUser(newUser.getId());
73 Assertions.assertEquals(Integer.valueOf(count + 1), user.getId());
74 Assertions.assertEquals("Test HSQL", user.getName());
75 }
76 }
77
78 @Test
79 void derby() throws IOException, SQLException {
80 SqlSessionFactory sqlSessionFactory;
81 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
82 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-derby");
83 }
84
85
86 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
87 "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
88
89 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
90 Mapper mapper = sqlSession.getMapper(Mapper.class);
91
92 int count = mapper.count();
93 User newUser = new User();
94 newUser.setName("Test");
95 mapper.insertUser(newUser);
96
97 User user = mapper.getUser(newUser.getId());
98 Assertions.assertEquals(Integer.valueOf(count + 1001), user.getId());
99 Assertions.assertEquals("Test DERBY", user.getName());
100 }
101 }
102
103 @Test
104 void derbyUsingProvider() throws IOException, SQLException {
105 SqlSessionFactory sqlSessionFactory;
106 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
107 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-derby");
108 }
109
110
111 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
112 "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
113
114 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
115 Mapper mapper = sqlSession.getMapper(Mapper.class);
116
117 int count = mapper.count();
118 User newUser = new User();
119 newUser.setName("Test");
120 mapper.insertUserUsingProvider(newUser);
121
122 User user = mapper.getUser(newUser.getId());
123 Assertions.assertEquals(Integer.valueOf(count + 1001), user.getId());
124 Assertions.assertEquals("Test DERBY", user.getName());
125 }
126 }
127
128 @Test
129 void h2() throws IOException, SQLException {
130 SqlSessionFactory sqlSessionFactory;
131 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
132 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-h2");
133 }
134
135
136 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
137 "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
138
139 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
140 Mapper mapper = sqlSession.getMapper(Mapper.class);
141
142 int count = mapper.count();
143 User newUser = new User();
144 newUser.setName("Test");
145 mapper.insertUser(newUser);
146
147 User user = mapper.getUser(newUser.getId());
148 Assertions.assertEquals(Integer.valueOf(count + 10001), user.getId());
149 Assertions.assertEquals("Test DEFAULT", user.getName());
150 }
151 }
152
153 @Test
154 void h2UsingProvider() throws IOException, SQLException {
155 SqlSessionFactory sqlSessionFactory;
156 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
157 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-h2");
158 }
159
160
161 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
162 "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
163
164 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
165 Mapper mapper = sqlSession.getMapper(Mapper.class);
166
167 int count = mapper.count();
168 User newUser = new User();
169 newUser.setName("Test");
170 mapper.insertUserUsingProvider(newUser);
171
172 User user = mapper.getUser(newUser.getId());
173 Assertions.assertEquals(Integer.valueOf(count + 10001), user.getId());
174 Assertions.assertEquals("Test DEFAULT", user.getName());
175 }
176 }
177
178 }