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.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      // populate in-memory database
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     // populate in-memory database
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     // populate in-memory database
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     // populate in-memory database
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 }