View Javadoc
1   /*
2    *    Copyright 2009-2024 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.managed;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.mockito.Mockito.verify;
23  import static org.mockito.Mockito.when;
24  
25  import java.sql.Connection;
26  import java.sql.SQLException;
27  import java.util.Properties;
28  
29  import javax.sql.DataSource;
30  
31  import org.apache.ibatis.session.TransactionIsolationLevel;
32  import org.apache.ibatis.transaction.Transaction;
33  import org.apache.ibatis.transaction.TransactionFactory;
34  import org.apache.ibatis.transaction.TransactionFactoryBase;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  import org.mockito.Mock;
38  
39  /**
40   * @author <a href="1181963012mw@gmail.com">mawen12</a>
41   *
42   * @see ManagedTransactionFactory
43   */
44  class ManagedTransactionFactoryUnitTest extends TransactionFactoryBase {
45  
46    @Mock
47    private Properties properties;
48  
49    @Mock
50    private Connection connection;
51  
52    @Mock
53    private DataSource dataSource;
54  
55    private TransactionFactory transactionFactory;
56  
57    @BeforeEach
58    void setup() {
59      this.transactionFactory = new ManagedTransactionFactory();
60    }
61  
62    @Test
63    @Override
64    public void shouldSetProperties() throws Exception {
65      when(properties.getProperty("closeConnection")).thenReturn("false");
66  
67      transactionFactory.setProperties(properties);
68  
69      assertFalse(
70          (Boolean) getValue(transactionFactory.getClass().getDeclaredField("closeConnection"), transactionFactory));
71    }
72  
73    @Test
74    @Override
75    public void shouldNewTransactionWithConnection() throws SQLException {
76      Transaction result = transactionFactory.newTransaction(connection);
77  
78      assertNotNull(result);
79      assertInstanceOf(ManagedTransaction.class, result);
80      assertEquals(connection, result.getConnection());
81    }
82  
83    @Test
84    @Override
85    public void shouldNewTransactionWithDataSource() throws Exception {
86      when(dataSource.getConnection()).thenReturn(connection);
87      when(properties.getProperty("closeConnection")).thenReturn("false");
88  
89      transactionFactory.setProperties(properties);
90      Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);
91  
92      assertNotNull(result);
93      assertInstanceOf(ManagedTransaction.class, result);
94      assertEquals(connection, result.getConnection());
95      verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
96  
97      assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
98      assertEquals(TransactionIsolationLevel.READ_COMMITTED,
99          getValue(result.getClass().getDeclaredField("level"), result));
100     assertEquals(false, getValue(result.getClass().getDeclaredField("closeConnection"), result));
101   }
102 }