You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by tu...@apache.org on 2022/12/09 08:23:53 UTC
[shardingsphere] branch master updated: Fix the transaction IT test assert usage specification (#22764)
This is an automated email from the ASF dual-hosted git repository.
tuichenchuxin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 04525dc4129 Fix the transaction IT test assert usage specification (#22764)
04525dc4129 is described below
commit 04525dc41299781e97850871671b8e03a6f779bb
Author: ZhangCheng <fl...@outlook.com>
AuthorDate: Fri Dec 9 16:23:44 2022 +0800
Fix the transaction IT test assert usage specification (#22764)
---
.../cases/autocommit/MySQLAutoCommitTestCase.java | 14 ++++----------
.../autocommit/PostgresSQLAutoCommitTestCase.java | 14 ++++----------
.../ExceptionInTransactionTestCase.java | 2 --
.../cases/cursor/OpenGaussCursorTestCase.java | 19 +++++--------------
.../cases/nested/NestedTransactionTestCase.java | 16 +++++++++-------
.../cases/readonly/SetReadOnlyTestCase.java | 6 ++----
.../cases/truncate/MySQLXATruncateTestCase.java | 21 +++++++++++++--------
7 files changed, 37 insertions(+), 55 deletions(-)
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
index b791428d190..e674c7f1bd2 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
@@ -25,11 +25,11 @@ import org.apache.shardingsphere.test.e2e.transaction.engine.constants.Transacti
import javax.sql.DataSource;
import java.sql.Connection;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
/**
* MySQL auto commit transaction integration test.
@@ -54,15 +54,9 @@ public final class MySQLAutoCommitTestCase extends BaseTransactionTestCase {
executeWithLog(connection1, "set autocommit=0;");
executeWithLog(connection2, "begin;");
executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1)");
- ResultSet emptyResultSet = executeQueryWithLog(connection2, "select * from account;");
- if (emptyResultSet.next()) {
- fail("There should not be result.");
- }
+ assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
executeWithLog(connection1, "commit;");
ThreadUtil.sleep(1, TimeUnit.SECONDS);
- ResultSet notEmptyResultSet = executeQueryWithLog(connection2, "select * from account");
- if (!notEmptyResultSet.next()) {
- fail("There should be result.");
- }
+ assertTrue(executeQueryWithLog(connection2, "select * from account").next());
}
}
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgresSQLAutoCommitTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgresSQLAutoCommitTestCase.java
index 90cfadbd7de..5cba86ae5fe 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgresSQLAutoCommitTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgresSQLAutoCommitTestCase.java
@@ -25,11 +25,11 @@ import org.apache.shardingsphere.test.e2e.transaction.engine.constants.Transacti
import javax.sql.DataSource;
import java.sql.Connection;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
/**
* PostgresSQL auto commit transaction integration test.
@@ -52,16 +52,10 @@ public final class PostgresSQLAutoCommitTestCase extends BaseTransactionTestCase
executeWithLog(connection1, "begin;");
executeWithLog(connection2, "begin;");
executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1)");
- ResultSet emptyResultSet = executeQueryWithLog(connection2, "select * from account;");
- if (emptyResultSet.next()) {
- fail("There should not be result.");
- }
+ assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
executeWithLog(connection1, "commit;");
ThreadUtil.sleep(1, TimeUnit.SECONDS);
- ResultSet notEmptyResultSet = executeQueryWithLog(connection2, "select * from account");
- if (!notEmptyResultSet.next()) {
- fail("There should be result.");
- }
+ assertTrue(executeQueryWithLog(connection2, "select * from account").next());
executeWithLog(connection2, "commit;");
}
}
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java
index f6fadd070f5..b34f65ba8f5 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java
@@ -63,7 +63,6 @@ public final class ExceptionInTransactionTestCase extends BaseTransactionTestCas
}
}
Thread queryThread = new Thread(() -> {
- log.info("Execute query in new thread.");
try (Connection connection2 = getDataSource().getConnection()) {
assertAccountRowCount(connection2, 0);
} catch (final SQLException ignored) {
@@ -72,7 +71,6 @@ public final class ExceptionInTransactionTestCase extends BaseTransactionTestCas
queryThread.start();
try {
queryThread.join();
- log.info("ExceptionInTransactionTestCase test over.");
} catch (final InterruptedException ignored) {
}
}
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java
index bf67b2ad052..6af1fd11232 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java
@@ -35,6 +35,7 @@ import java.util.Objects;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
/**
@@ -175,31 +176,21 @@ public final class OpenGaussCursorTestCase extends BaseTransactionTestCase {
private void fetch(final Connection connection, final int expectedId) throws SQLException {
ResultSet resultSet = executeQueryWithLog(connection, "fetch test;");
if (resultSet.next()) {
- int id = resultSet.getInt("id");
- assertThat(id, is(expectedId));
+ assertThat(resultSet.getInt("id"), is(expectedId));
} else {
fail("Expected has result.");
}
}
private void fetchOver(final Connection connection) throws SQLException {
- ResultSet resultSet = executeQueryWithLog(connection, "fetch test;");
- while (resultSet.next()) {
- fail("Expected fetch nothing.");
- }
+ assertFalse(executeQueryWithLog(connection, "fetch test;").next());
}
private void fetchForwardOver(final Connection connection) throws SQLException {
- ResultSet resultSet = executeQueryWithLog(connection, "fetch forward from test;");
- while (resultSet.next()) {
- fail("Expected fetch nothing.");
- }
+ assertFalse(executeQueryWithLog(connection, "fetch forward from test;").next());
}
private void fetchForwardAllOver(final Connection connection) throws SQLException {
- ResultSet resultSet = executeQueryWithLog(connection, "fetch forward all from test;");
- while (resultSet.next()) {
- fail("Expected fetch nothing.");
- }
+ assertFalse(executeQueryWithLog(connection, "fetch forward all from test;").next());
}
}
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java
index 6cdf6512e81..6979fcc2ed2 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java
@@ -18,16 +18,18 @@
package org.apache.shardingsphere.test.e2e.transaction.cases.nested;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
+import org.apache.shardingsphere.test.e2e.env.container.atomic.constants.AdapterContainerConstants;
import org.apache.shardingsphere.test.e2e.transaction.cases.base.BaseTransactionTestCase;
import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionBaseE2EIT;
import org.apache.shardingsphere.test.e2e.transaction.engine.base.TransactionTestCase;
-import org.apache.shardingsphere.test.e2e.env.container.atomic.constants.AdapterContainerConstants;
import org.apache.shardingsphere.transaction.api.TransactionType;
-import org.junit.Assert;
import javax.sql.DataSource;
import java.sql.SQLException;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
/**
* Nested transaction test case.
*/
@@ -41,19 +43,19 @@ public class NestedTransactionTestCase extends BaseTransactionTestCase {
@Override
protected void executeTest() throws SQLException {
ShardingSphereConnection connection = (ShardingSphereConnection) getDataSource().getConnection();
- Assert.assertFalse(connection.isHoldTransaction());
+ assertFalse(connection.isHoldTransaction());
connection.setAutoCommit(false);
- Assert.assertTrue(connection.isHoldTransaction());
+ assertTrue(connection.isHoldTransaction());
requiresNewTransaction();
- Assert.assertTrue(connection.isHoldTransaction());
+ assertTrue(connection.isHoldTransaction());
connection.commit();
}
private void requiresNewTransaction() throws SQLException {
ShardingSphereConnection connection = (ShardingSphereConnection) getDataSource().getConnection();
- Assert.assertFalse(connection.isHoldTransaction());
+ assertFalse(connection.isHoldTransaction());
connection.setAutoCommit(false);
- Assert.assertTrue(connection.isHoldTransaction());
+ assertTrue(connection.isHoldTransaction());
connection.commit();
}
}
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java
index 35070fa01ca..8595d8f2486 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java
@@ -27,7 +27,7 @@ import java.sql.SQLException;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
/**
* Set read only transaction integration test.
@@ -43,9 +43,7 @@ public abstract class SetReadOnlyTestCase extends BaseTransactionTestCase {
assertQueryBalance(connection);
executeUpdateWithLog(connection, "update account set balance = 101 where id = 2;");
ResultSet resultSet = executeQueryWithLog(connection, "select * from account where id = 2");
- if (!resultSet.next()) {
- fail("Should have a result.");
- }
+ assertTrue(resultSet.next());
int balanceResult = resultSet.getInt("balance");
assertThat(String.format("Balance is %s, should be 101.", balanceResult), balanceResult, is(101));
}
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java
index 65ca3e7b08a..35b793d860d 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java
@@ -43,6 +43,19 @@ public final class MySQLXATruncateTestCase extends BaseTransactionTestCase {
super(baseTransactionITCase, dataSource);
}
+ @Override
+ protected void beforeTest() throws SQLException {
+ super.beforeTest();
+ prepare();
+ }
+
+ private void prepare() throws SQLException {
+ Connection connection = getDataSource().getConnection();
+ executeWithLog(connection, "delete from account;");
+ executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
+ connection.close();
+ }
+
@Override
public void executeTest() throws SQLException {
assertTruncateInMySQLXATransaction();
@@ -50,7 +63,6 @@ public final class MySQLXATruncateTestCase extends BaseTransactionTestCase {
private void assertTruncateInMySQLXATransaction() throws SQLException {
// TODO This test case may cause bad effects to other test cases in JDBC adapter
- prepare();
Connection connection = getDataSource().getConnection();
connection.setAutoCommit(false);
assertAccountRowCount(connection, 8);
@@ -66,11 +78,4 @@ public final class MySQLXATruncateTestCase extends BaseTransactionTestCase {
connection.close();
}
}
-
- private void prepare() throws SQLException {
- Connection connection = getDataSource().getConnection();
- executeWithLog(connection, "delete from account;");
- executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
- connection.close();
- }
}