Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Map;

import org.apache.ibatis.domain.blog.Author;
import org.apache.ibatis.domain.misc.RichType;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
Expand All @@ -35,7 +34,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see BeanWrapper
*/
class BeanWrapperUnitTest extends ObjectWrapperBaseTest {
class BeanWrapperUnitTest extends ObjectWrapperBase {

private RichType richType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @see CollectionWrapper
*/
@ExtendWith(MockitoExtension.class)
class CollectionWrapperUnitTest extends ObjectWrapperBaseTest {
class CollectionWrapperUnitTest extends ObjectWrapperBase {

@Mock
private Collection<Object> collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see MapWrapper
*/
class MapWrapperUnitTest extends ObjectWrapperBaseTest {
class MapWrapperUnitTest extends ObjectWrapperBase {

@Mock
private Map<String, Object> map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @see ObjectWrapper
*/
@ExtendWith(MockitoExtension.class)
abstract class ObjectWrapperBaseTest {
abstract class ObjectWrapperBase {

abstract void shouldGet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#choose-when-otherwise">choose</a>
*/
class ChooseSqlNodeTest extends SqlNodeTest{
class ChooseSqlNodeTest extends SqlNodeBase {

private static final String FIRST_TEXT = " AND title like #{title}";
private static final String SECOND_TEXT = " AND author_name like #{author.username}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#foreach">foreach</a>
*/
class ForEachSqlNodeTest extends SqlNodeTest{
class ForEachSqlNodeTest extends SqlNodeBase {

private SqlNode sqlNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#if">if</a>
*/
class IfSqlNodeTest extends SqlNodeTest {
class IfSqlNodeTest extends SqlNodeBase {

private static final String CONDITION = "title != null";
private static final String TEXT = "AND title like #{title}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @author <a href="[email protected]">mawen12</a>
*/
class MixedSqlNodeTest extends SqlNodeTest{
class MixedSqlNodeTest extends SqlNodeBase {

private static final String FIRST_TEXT = "abc";
private static final String SECOND_TEXT = "bcd";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#trim-where-set">trim-where-set</a>
*/
class SetSqlNodeTest extends SqlNodeTest {
class SetSqlNodeTest extends SqlNodeBase {

private static final String FIRST_TEXT = " username = #{username},";
private static final String SECOND_TEXT = " password = #{password}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

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

@Mock
protected Configuration configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* @author <a href="[email protected]">mawen12</a>
*/
class StaticTextSqlNodeTest extends SqlNodeTest {
class StaticTextSqlNodeTest extends SqlNodeBase {

private static final String TEXT = "select 1 from dual";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @author <a href="[email protected]">mawen12</a>
*/
class TextSqlNodeTest extends SqlNodeTest {
class TextSqlNodeTest extends SqlNodeBase {

private static final String TEXT = "select 1 from dual";
private static final String DYNAMIC_TEXT = "select * from user where id = ${id}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#trim-where-set">trim-where-set</a>
*/
class TrimSqlNodeTest extends SqlNodeTest {
class TrimSqlNodeTest extends SqlNodeBase {

private static final String FIRST_TEXT = " AND id = #{id}";
private static final String SECOND_TEXT = " AND name = #{name}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#bind">bind</a>
*/
class VarDeclSqlNodeTest extends SqlNodeTest {
class VarDeclSqlNodeTest extends SqlNodeBase {

private SqlNode sqlNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#trim-where-set">trim-where-set</a>
*/
class WhereSqlNodeTest extends SqlNodeTest {
class WhereSqlNodeTest extends SqlNodeBase {

private static final String FIRST_TEXT = " AND id = #{id}";
private static final String SECOND_TEXT = " AND name = #{name}";
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/apache/ibatis/transaction/TransactionBase.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 TransactionBase {

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,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 TransactionFactoryBase {

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);
}

}
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.TransactionFactoryBase;
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 TransactionFactoryBase {

@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.

Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with how this is for now. Yes assertj makes it a lot more readable. Possibly we can run open rewrite against this for tests to clean them up. Will check into that separately.

}

@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));
}

}
Loading