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 RepeatableSelectTest {
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      User user;
43      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
44        Mapper mapper = sqlSession.getMapper(Mapper.class);
45        user = mapper.getUser(1);
46        sqlSession.commit();
47        Assertions.assertEquals("User1", user.getName());
48        Assertions.assertEquals("HSQL", user.getDatabaseName());
49      }
50      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51        Mapper mapper = sqlSession.getMapper(Mapper.class);
52        Assertions.assertNotSame(user, mapper.getUser(1));
53      }
54    }
55  
56    @Test
57    void hsqlUsingProvider() throws IOException, SQLException {
58      SqlSessionFactory sqlSessionFactory;
59      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
60        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-hsql");
61      }
62  
63      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
64          "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
65  
66      User user;
67      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68        Mapper mapper = sqlSession.getMapper(Mapper.class);
69        user = mapper.getUserUsingProvider(1);
70        Assertions.assertEquals("User1", user.getName());
71        Assertions.assertEquals("HSQL", user.getDatabaseName());
72      }
73      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
74        Mapper mapper = sqlSession.getMapper(Mapper.class);
75        Assertions.assertSame(user, mapper.getUserUsingProvider(1));
76      }
77    }
78  
79    @Test
80    void derby() throws IOException, SQLException {
81      SqlSessionFactory sqlSessionFactory;
82      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
83        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-derby");
84      }
85  
86      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
87          "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
88  
89      User user;
90      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
91        Mapper mapper = sqlSession.getMapper(Mapper.class);
92        user = mapper.getUser(1);
93        sqlSession.commit();
94        Assertions.assertEquals("User1", user.getName());
95        Assertions.assertEquals("DERBY", user.getDatabaseName());
96      }
97      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
98        Mapper mapper = sqlSession.getMapper(Mapper.class);
99        Assertions.assertNotSame(user, mapper.getUser(1));
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     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
111         "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
112 
113     User user;
114     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
115       Mapper mapper = sqlSession.getMapper(Mapper.class);
116       user = mapper.getUserUsingProvider(1);
117       Assertions.assertEquals("User1", user.getName());
118       Assertions.assertEquals("DERBY", user.getDatabaseName());
119     }
120     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
121       Mapper mapper = sqlSession.getMapper(Mapper.class);
122       Assertions.assertSame(user, mapper.getUserUsingProvider(1));
123     }
124   }
125 
126   @Test
127   void h2() throws IOException, SQLException {
128     SqlSessionFactory sqlSessionFactory;
129     try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
130       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-h2");
131     }
132 
133     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
134         "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
135 
136     User user;
137     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
138       Mapper mapper = sqlSession.getMapper(Mapper.class);
139       user = mapper.getUser(1);
140       sqlSession.commit();
141       Assertions.assertEquals("User1", user.getName());
142       Assertions.assertEquals("DEFAULT", user.getDatabaseName());
143     }
144     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
145       Mapper mapper = sqlSession.getMapper(Mapper.class);
146       Assertions.assertSame(user, mapper.getUser(1));
147     }
148   }
149 
150   @Test
151   void h2UsingProvider() throws IOException, SQLException {
152     SqlSessionFactory sqlSessionFactory;
153     try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
154       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-h2");
155     }
156 
157     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
158         "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
159 
160     User user;
161     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
162       Mapper mapper = sqlSession.getMapper(Mapper.class);
163       user = mapper.getUserUsingProvider(1);
164       Assertions.assertEquals("User1", user.getName());
165       Assertions.assertEquals("DEFAULT", user.getDatabaseName());
166     }
167     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
168       Mapper mapper = sqlSession.getMapper(Mapper.class);
169       Assertions.assertNotSame(user, mapper.getUserUsingProvider(1));
170     }
171   }
172 
173   @Test
174   void usingBoth() throws IOException, SQLException {
175     SqlSessionFactory sqlSessionFactory;
176     try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
177       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-derby");
178     }
179 
180     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
181         "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
182 
183     User user;
184     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
185       Mapper mapper = sqlSession.getMapper(Mapper.class);
186       user = mapper.getUserUsingBoth(1);
187       sqlSession.commit();
188       Assertions.assertEquals("User1", user.getName());
189       Assertions.assertEquals("DERBY", user.getDatabaseName());
190     }
191     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
192       Mapper mapper = sqlSession.getMapper(Mapper.class);
193       Assertions.assertNotSame(user, mapper.getUser(1));
194     }
195   }
196 
197   @Test
198   void usingBothProvider() throws IOException, SQLException {
199     SqlSessionFactory sqlSessionFactory;
200     try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
201       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-hsql");
202     }
203 
204     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
205         "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
206 
207     User user;
208     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
209       Mapper mapper = sqlSession.getMapper(Mapper.class);
210       user = mapper.getUserUsingBoth(1);
211       sqlSession.commit();
212       Assertions.assertEquals("User1", user.getName());
213       Assertions.assertEquals("HSQL", user.getDatabaseName());
214     }
215     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
216       Mapper mapper = sqlSession.getMapper(Mapper.class);
217       Assertions.assertNotSame(user, mapper.getUser(1));
218     }
219   }
220 }