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