Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TransactionFactory, Transaction test cases #3277

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.transaction;

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.lang.reflect.Field;
import java.sql.SQLException;

/**
* @author <a href="[email protected]">mawen12</a>
* @see TransactionFactory
*/
@ExtendWith(MockitoExtension.class)
public abstract class TransactionFactoryTest {

public abstract void shouldSetProperties() throws Exception;

public abstract void shouldNewTransactionWithConnection() throws SQLException;

public abstract void shouldNewTransactionWithDataSource() throws Exception;

public static Object getValue(Field field, Object object) throws Exception {
field.setAccessible(true);
return field.get(object);
}

}
39 changes: 39 additions & 0 deletions src/test/java/org/apache/ibatis/transaction/TransactionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.transaction;

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.sql.SQLException;

/**
* @author <a href="[email protected]">mawen12</a>
* @see Transaction
*/
@ExtendWith(MockitoExtension.class)
public abstract class TransactionTest {

public abstract void shouldGetConnection() throws SQLException;

public abstract void shouldCommit() throws SQLException;

public abstract void shouldRollback() throws SQLException;

public abstract void shouldClose() throws SQLException;

public abstract void shouldGetTimeout() throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.transaction.jdbc;

import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.TransactionFactoryTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author <a href="[email protected]">mawen12</a>
* @see JdbcTransactionFactory
*/
class JdbcTransactionFactoryUnitTest extends TransactionFactoryTest {

@Mock
private Properties properties;

@Mock
private Connection connection;

@Mock
private DataSource dataSource;

private TransactionFactory transactionFactory;

@BeforeEach
void setup() {
this.transactionFactory = new JdbcTransactionFactory();
}

@Test
@Override
public void shouldSetProperties() throws Exception {
when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");

transactionFactory.setProperties(properties);

assertTrue((Boolean) getValue(transactionFactory.getClass().getDeclaredField("skipSetAutoCommitOnClose"), transactionFactory));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, using constructs like assertTrue, or assertEquals(true, xxx) does not make it that clear what went wrong initially in test output.

It's more a personal thing, but I prefer using assertj, as the failure messages are more descriptive. Not sure what others think.

}

@Test
@Override
public void shouldNewTransactionWithConnection() throws SQLException {
Transaction result = transactionFactory.newTransaction(connection);

assertNotNull(result);
assertInstanceOf(JdbcTransaction.class, result);
assertEquals(connection, result.getConnection());
}

@Test
@Override
public void shouldNewTransactionWithDataSource() throws Exception {
when(dataSource.getConnection()).thenReturn(connection);

Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false);

assertNotNull(result);
assertInstanceOf(JdbcTransaction.class, result);
assertEquals(connection, result.getConnection());
verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result));
assertEquals(false, getValue(result.getClass().getDeclaredField("autoCommit"), result));
assertEquals(false, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
}

@Test
void shouldNewTransactionWithDataSourceAndCustomProperties() throws Exception {
when(dataSource.getConnection()).thenReturn(connection);
when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");

transactionFactory.setProperties(properties);
Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);

assertNotNull(result);
assertInstanceOf(JdbcTransaction.class, result);
assertEquals(connection, result.getConnection());
verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result));
assertEquals(true, getValue(result.getClass().getDeclaredField("autoCommit"), result));
assertEquals(true, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.transaction.jdbc;

import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.sql.Connection;
import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.*;

/**
* @author <a href="[email protected]">mawen12</a>
* @see JdbcTransaction
*/
class JdbcTransactionWithConnectionTest extends TransactionTest {

@Mock
private Connection connection;

private Transaction transaction;

@BeforeEach
void setup() {
this.transaction = new JdbcTransaction(connection);
}

@Test
@Override
public void shouldGetConnection() throws SQLException {
Connection result = transaction.getConnection();

assertEquals(connection, result);
}

@Test
@Override
public void shouldCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(false);

transaction.commit();

verify(connection).commit();
}

@Test
void shouldAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(true);

transaction.commit();

verify(connection, never()).commit();
}

@Test
@Override
public void shouldRollback() throws SQLException {
when(connection.getAutoCommit()).thenReturn(false);

transaction.rollback();

verify(connection).rollback();
}

@Test
void shouldAutoRollback() throws SQLException {
when(connection.getAutoCommit()).thenReturn(true);

transaction.rollback();

verify(connection, never()).rollback();
}

@Test
@Override
public void shouldClose() throws SQLException {
when(connection.getAutoCommit()).thenReturn(false);

transaction.close();

verify(connection).close();
verify(connection).setAutoCommit(true);
}

@Test
void shouldCloseWithAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(true);

transaction.close();

verify(connection).close();
verify(connection, never()).setAutoCommit(true);
}

@Test
@Override
public void shouldGetTimeout() throws SQLException {
assertNull(transaction.getTimeout());
}

}
Loading