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 RepeatableUpdateTest {
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        mapper.updateUserName(1);
45  
46        User user = mapper.getUser(1);
47        Assertions.assertEquals("User1 HSQL", user.getName());
48      }
49    }
50  
51    @Test
52    void hsqlUsingProvider() throws IOException, SQLException {
53      SqlSessionFactory sqlSessionFactory;
54      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
55        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-hsql");
56      }
57  
58      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
59          "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
60  
61      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62        Mapper mapper = sqlSession.getMapper(Mapper.class);
63        mapper.updateUserNameUsingProvider(1);
64  
65        User user = mapper.getUser(1);
66        Assertions.assertEquals("User1 HSQL", user.getName());
67      }
68    }
69  
70    @Test
71    void derby() throws IOException, SQLException {
72      SqlSessionFactory sqlSessionFactory;
73      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
74        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-derby");
75      }
76  
77      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
78          "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
79  
80      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
81        Mapper mapper = sqlSession.getMapper(Mapper.class);
82  
83        mapper.updateUserName(1);
84  
85        User user = mapper.getUser(1);
86        Assertions.assertEquals("User1 DERBY", user.getName());
87      }
88    }
89  
90    @Test
91    void derbyUsingProvider() throws IOException, SQLException {
92      SqlSessionFactory sqlSessionFactory;
93      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
94        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-derby");
95      }
96  
97      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
98          "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
99  
100     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
101       Mapper mapper = sqlSession.getMapper(Mapper.class);
102 
103       mapper.updateUserNameUsingProvider(1);
104 
105       User user = mapper.getUser(1);
106       Assertions.assertEquals("User1 DERBY", user.getName());
107     }
108   }
109 
110   @Test
111   void h2() throws IOException, SQLException {
112     SqlSessionFactory sqlSessionFactory;
113     try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
114       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-h2");
115     }
116 
117     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
118         "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
119 
120     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
121       Mapper mapper = sqlSession.getMapper(Mapper.class);
122 
123       mapper.updateUserName(1);
124 
125       User user = mapper.getUser(1);
126       Assertions.assertEquals("User1 DEFAULT", user.getName());
127     }
128   }
129 
130   @Test
131   void h2UsingProvider() throws IOException, SQLException {
132     SqlSessionFactory sqlSessionFactory;
133     try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/repeatable/mybatis-config.xml")) {
134       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, "development-h2");
135     }
136 
137     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
138         "org/apache/ibatis/submitted/repeatable/CreateDB.sql");
139 
140     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
141       Mapper mapper = sqlSession.getMapper(Mapper.class);
142 
143       mapper.updateUserNameUsingProvider(1);
144 
145       User user = mapper.getUser(1);
146       Assertions.assertEquals("User1 DEFAULT", user.getName());
147     }
148   }
149 
150 }