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.dirty_select;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  
22  import java.util.Iterator;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.cursor.Cursor;
26  import org.apache.ibatis.mapping.Environment;
27  import org.apache.ibatis.session.Configuration;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.apache.ibatis.testcontainers.PgContainer;
32  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
33  import org.junit.jupiter.api.BeforeAll;
34  import org.junit.jupiter.api.Tag;
35  import org.junit.jupiter.api.Test;
36  
37  @Tag("TestcontainersTests")
38  class DirtySelectTest {
39  
40    private static SqlSessionFactory sqlSessionFactory;
41  
42    @BeforeAll
43    static void setUp() throws Exception {
44      Configuration configuration = new Configuration();
45      Environment environment = new Environment("development", new JdbcTransactionFactory(),
46          PgContainer.getUnpooledDataSource());
47      configuration.setEnvironment(environment);
48      configuration.addMapper(Mapper.class);
49      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
50  
51      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
52          "org/apache/ibatis/submitted/dirty_select/CreateDB.sql");
53    }
54  
55    @Test
56    void shouldRollbackIfCalled() {
57      Integer id;
58      try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
59        Mapper mapper = sqlSession.getMapper(Mapper.class);
60        User user = mapper.insertReturn("Jimmy");
61        id = user.getId();
62        assertNotNull(id);
63        assertEquals("Jimmy", user.getName());
64        sqlSession.rollback();
65      }
66      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
67        Mapper mapper = sqlSession.getMapper(Mapper.class);
68        User user = mapper.selectById(id);
69        assertNull(user);
70      }
71    }
72  
73    @Test
74    void shouldRollbackIfCalled_Xml() {
75      Integer id;
76      try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
77        Mapper mapper = sqlSession.getMapper(Mapper.class);
78        User user = mapper.insertReturnXml("Jimmy");
79        id = user.getId();
80        assertNotNull(id);
81        assertEquals("Jimmy", user.getName());
82        sqlSession.rollback();
83      }
84      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85        Mapper mapper = sqlSession.getMapper(Mapper.class);
86        User user = mapper.selectById(id);
87        assertNull(user);
88      }
89    }
90  
91    @Test
92    void shouldRollbackIfCalled_Provider() {
93      Integer id;
94      try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
95        Mapper mapper = sqlSession.getMapper(Mapper.class);
96        User user = mapper.insertReturnProvider("Jimmy");
97        id = user.getId();
98        assertNotNull(id);
99        assertEquals("Jimmy", user.getName());
100       sqlSession.rollback();
101     }
102     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
103       Mapper mapper = sqlSession.getMapper(Mapper.class);
104       User user = mapper.selectById(id);
105       assertNull(user);
106     }
107   }
108 
109   @Test
110   void shouldRollbackIfCalled_Cursor() throws Exception {
111     Integer id;
112     try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
113       Mapper mapper = sqlSession.getMapper(Mapper.class);
114       try (Cursor<User> cursor = mapper.insertReturnCursor("Kate")) {
115         Iterator<User> iterator = cursor.iterator();
116         User user = iterator.next();
117         id = user.getId();
118         assertNotNull(id);
119         assertEquals("Kate", user.getName());
120       }
121       sqlSession.rollback();
122     }
123     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
124       Mapper mapper = sqlSession.getMapper(Mapper.class);
125       User user = mapper.selectById(id);
126       assertNull(user);
127     }
128   }
129 
130   @Test
131   void shouldNonDirtySelectNotUnsetDirtyFlag() {
132     Integer id;
133     try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
134       Mapper mapper = sqlSession.getMapper(Mapper.class);
135       // INSERT
136       User user = new User();
137       user.setName("Bobby");
138       mapper.insert(user);
139       id = user.getId();
140       assertNotNull(id);
141       assertEquals("Bobby", user.getName());
142       // Non-dirty SELECT
143       mapper.selectById(id);
144       sqlSession.rollback();
145     }
146     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
147       Mapper mapper = sqlSession.getMapper(Mapper.class);
148       User user = mapper.selectById(id);
149       assertNull(user);
150     }
151   }
152 
153   @Test
154   void shouldNonDirtySelectNotUnsetDirtyFlag_Cursor() throws Exception {
155     Integer id;
156     try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
157       Mapper mapper = sqlSession.getMapper(Mapper.class);
158       // INSERT
159       User user = new User();
160       user.setName("Bobby");
161       mapper.insert(user);
162       id = user.getId();
163       assertNotNull(id);
164       assertEquals("Bobby", user.getName());
165       // Non-dirty SELECT
166       try (Cursor<User> cursor = mapper.selectCursorById(id)) {
167         Iterator<User> iterator = cursor.iterator();
168         iterator.next();
169       }
170       sqlSession.rollback();
171     }
172     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
173       Mapper mapper = sqlSession.getMapper(Mapper.class);
174       User user = mapper.selectById(id);
175       assertNull(user);
176     }
177   }
178 
179 }