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.transaction.jdbc;
17  
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.when;
22  
23  import java.util.Properties;
24  
25  import javax.sql.DataSource;
26  
27  import org.apache.ibatis.session.TransactionIsolationLevel;
28  import org.apache.ibatis.transaction.Transaction;
29  import org.junit.jupiter.api.Test;
30  
31  class JdbcTransactionFactoryTest {
32  
33    @Test
34    void testNullProperties() throws Exception {
35      TestConnection connection = new TestConnection(false);
36      JdbcTransactionFactory factory = new JdbcTransactionFactory();
37      factory.setProperties(null);
38      Transaction transaction = factory.newTransaction(connection);
39      transaction.getConnection();
40      transaction.close();
41      assertTrue(connection.getAutoCommit());
42    }
43  
44    @Test
45    void testSkipSetAutoCommitOnClose() throws Exception {
46      TestConnection connection = new TestConnection(false);
47      DataSource ds = mock(DataSource.class);
48      when(ds.getConnection()).thenReturn(connection);
49  
50      JdbcTransactionFactory factory = new JdbcTransactionFactory();
51      Properties properties = new Properties();
52      properties.setProperty("skipSetAutoCommitOnClose", "true");
53      factory.setProperties(properties);
54      Transaction transaction = factory.newTransaction(ds, TransactionIsolationLevel.NONE, false);
55      transaction.getConnection();
56      transaction.close();
57      assertFalse(connection.getAutoCommit());
58    }
59  
60  }