Skip to content

Commit 4b5896c

Browse files
committed
Added a test case
1 parent cf23454 commit 4b5896c

File tree

1 file changed

+338
-0
lines changed

1 file changed

+338
-0
lines changed
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
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+
* http://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.transaction.jdbc;
17+
18+
import static org.junit.Assert.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import java.sql.Array;
22+
import java.sql.Blob;
23+
import java.sql.CallableStatement;
24+
import java.sql.Clob;
25+
import java.sql.Connection;
26+
import java.sql.DatabaseMetaData;
27+
import java.sql.NClob;
28+
import java.sql.PreparedStatement;
29+
import java.sql.SQLClientInfoException;
30+
import java.sql.SQLException;
31+
import java.sql.SQLWarning;
32+
import java.sql.SQLXML;
33+
import java.sql.Savepoint;
34+
import java.sql.Statement;
35+
import java.sql.Struct;
36+
import java.util.Map;
37+
import java.util.Properties;
38+
import java.util.concurrent.Executor;
39+
40+
import javax.sql.DataSource;
41+
42+
import org.apache.ibatis.session.TransactionIsolationLevel;
43+
import org.junit.jupiter.api.Test;
44+
45+
class JdbcTransactionTest {
46+
@Test
47+
void testSetAutoCommitOnClose() throws Exception {
48+
testAutoCommit(true, false, true, false);
49+
testAutoCommit(false, false, true, false);
50+
testAutoCommit(true, true, true, false);
51+
testAutoCommit(false, true, true, false);
52+
testAutoCommit(true, false, false, true);
53+
testAutoCommit(false, false, false, true);
54+
testAutoCommit(true, true, true, true);
55+
testAutoCommit(false, true, true, true);
56+
}
57+
58+
private void testAutoCommit(boolean initialAutoCommit, boolean desiredAutoCommit, boolean resultAutoCommit,
59+
boolean skipSetAutoCommitOnClose) throws Exception {
60+
TestConnection con = new TestConnection(initialAutoCommit);
61+
DataSource ds = mock(DataSource.class);
62+
when(ds.getConnection()).thenReturn(con);
63+
64+
JdbcTransaction transaction = new JdbcTransaction(ds, TransactionIsolationLevel.NONE, desiredAutoCommit, skipSetAutoCommitOnClose);
65+
transaction.getConnection();
66+
transaction.commit();
67+
transaction.close();
68+
69+
assertEquals(resultAutoCommit, con.getAutoCommit());
70+
}
71+
72+
private class TestConnection implements Connection {
73+
private boolean autoCommit;
74+
75+
TestConnection(boolean autoCommit) {
76+
super();
77+
this.autoCommit = autoCommit;
78+
}
79+
80+
@Override
81+
public void setAutoCommit(boolean autoCommit) throws SQLException {
82+
this.autoCommit = autoCommit;
83+
}
84+
85+
@Override
86+
public boolean getAutoCommit() throws SQLException {
87+
return autoCommit;
88+
}
89+
90+
@Override
91+
public <T> T unwrap(Class<T> iface) throws SQLException {
92+
return null;
93+
}
94+
95+
@Override
96+
public boolean isWrapperFor(Class<?> iface) throws SQLException {
97+
return false;
98+
}
99+
100+
@Override
101+
public Statement createStatement() throws SQLException {
102+
return null;
103+
}
104+
105+
@Override
106+
public PreparedStatement prepareStatement(String sql) throws SQLException {
107+
return null;
108+
}
109+
110+
@Override
111+
public CallableStatement prepareCall(String sql) throws SQLException {
112+
return null;
113+
}
114+
115+
@Override
116+
public String nativeSQL(String sql) throws SQLException {
117+
return null;
118+
}
119+
120+
@Override
121+
public void commit() throws SQLException {
122+
}
123+
124+
@Override
125+
public void rollback() throws SQLException {
126+
}
127+
128+
@Override
129+
public void close() throws SQLException {
130+
}
131+
132+
@Override
133+
public boolean isClosed() throws SQLException {
134+
return false;
135+
}
136+
137+
@Override
138+
public DatabaseMetaData getMetaData() throws SQLException {
139+
return null;
140+
}
141+
142+
@Override
143+
public void setReadOnly(boolean readOnly) throws SQLException {
144+
}
145+
146+
@Override
147+
public boolean isReadOnly() throws SQLException {
148+
return false;
149+
}
150+
151+
@Override
152+
public void setCatalog(String catalog) throws SQLException {
153+
}
154+
155+
@Override
156+
public String getCatalog() throws SQLException {
157+
return null;
158+
}
159+
160+
@Override
161+
public void setTransactionIsolation(int level) throws SQLException {
162+
}
163+
164+
@Override
165+
public int getTransactionIsolation() throws SQLException {
166+
return 0;
167+
}
168+
169+
@Override
170+
public SQLWarning getWarnings() throws SQLException {
171+
return null;
172+
}
173+
174+
@Override
175+
public void clearWarnings() throws SQLException {
176+
}
177+
178+
@Override
179+
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
180+
return null;
181+
}
182+
183+
@Override
184+
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
185+
throws SQLException {
186+
return null;
187+
}
188+
189+
@Override
190+
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
191+
return null;
192+
}
193+
194+
@Override
195+
public Map<String, Class<?>> getTypeMap() throws SQLException {
196+
return null;
197+
}
198+
199+
@Override
200+
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
201+
}
202+
203+
@Override
204+
public void setHoldability(int holdability) throws SQLException {
205+
}
206+
207+
@Override
208+
public int getHoldability() throws SQLException {
209+
return 0;
210+
}
211+
212+
@Override
213+
public Savepoint setSavepoint() throws SQLException {
214+
return null;
215+
}
216+
217+
@Override
218+
public Savepoint setSavepoint(String name) throws SQLException {
219+
return null;
220+
}
221+
222+
@Override
223+
public void rollback(Savepoint savepoint) throws SQLException {
224+
}
225+
226+
@Override
227+
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
228+
}
229+
230+
@Override
231+
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
232+
throws SQLException {
233+
return null;
234+
}
235+
236+
@Override
237+
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
238+
int resultSetHoldability) throws SQLException {
239+
return null;
240+
}
241+
242+
@Override
243+
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
244+
int resultSetHoldability) throws SQLException {
245+
return null;
246+
}
247+
248+
@Override
249+
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
250+
return null;
251+
}
252+
253+
@Override
254+
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
255+
return null;
256+
}
257+
258+
@Override
259+
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
260+
return null;
261+
}
262+
263+
@Override
264+
public Clob createClob() throws SQLException {
265+
return null;
266+
}
267+
268+
@Override
269+
public Blob createBlob() throws SQLException {
270+
return null;
271+
}
272+
273+
@Override
274+
public NClob createNClob() throws SQLException {
275+
return null;
276+
}
277+
278+
@Override
279+
public SQLXML createSQLXML() throws SQLException {
280+
return null;
281+
}
282+
283+
@Override
284+
public boolean isValid(int timeout) throws SQLException {
285+
return false;
286+
}
287+
288+
@Override
289+
public void setClientInfo(String name, String value) throws SQLClientInfoException {
290+
}
291+
292+
@Override
293+
public void setClientInfo(Properties properties) throws SQLClientInfoException {
294+
}
295+
296+
@Override
297+
public String getClientInfo(String name) throws SQLException {
298+
return null;
299+
}
300+
301+
@Override
302+
public Properties getClientInfo() throws SQLException {
303+
return null;
304+
}
305+
306+
@Override
307+
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
308+
return null;
309+
}
310+
311+
@Override
312+
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
313+
return null;
314+
}
315+
316+
@Override
317+
public void setSchema(String schema) throws SQLException {
318+
}
319+
320+
@Override
321+
public String getSchema() throws SQLException {
322+
return null;
323+
}
324+
325+
@Override
326+
public void abort(Executor executor) throws SQLException {
327+
}
328+
329+
@Override
330+
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
331+
}
332+
333+
@Override
334+
public int getNetworkTimeout() throws SQLException {
335+
return 0;
336+
}
337+
}
338+
}

0 commit comments

Comments
 (0)