You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/01/29 03:14:20 UTC
[shardingsphere] branch master updated: Simplify
ConnectionAdapterTest (#9214)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 e167db5 Simplify ConnectionAdapterTest (#9214)
e167db5 is described below
commit e167db56070334d312c27a4cb8db839db98e088b
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Jan 29 11:13:56 2021 +0800
Simplify ConnectionAdapterTest (#9214)
---
.../jdbc/adapter/AbstractConnectionAdapter.java | 3 -
.../driver/jdbc/adapter/ConnectionAdapterTest.java | 262 +++++----------------
2 files changed, 55 insertions(+), 210 deletions(-)
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java
index b034ef2..ec79c5f 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractConnectionAdapter.java
@@ -50,9 +50,6 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
private int transactionIsolation = TRANSACTION_READ_UNCOMMITTED;
- protected AbstractConnectionAdapter() {
- }
-
@Override
public final void close() throws SQLException {
closed = true;
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java
index 06fb10c..1f7de85 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java
@@ -19,274 +19,122 @@ package org.apache.shardingsphere.driver.jdbc.adapter;
import com.google.common.collect.Multimap;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.driver.jdbc.base.AbstractShardingSphereDataSourceForShardingTest;
-import org.apache.shardingsphere.driver.jdbc.core.fixture.BASEShardingTransactionManagerFixture;
-import org.apache.shardingsphere.driver.jdbc.core.fixture.XAShardingTransactionManagerFixture;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
-import org.apache.shardingsphere.transaction.core.TransactionOperationType;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
-import org.junit.After;
import org.junit.Test;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
-public final class ConnectionAdapterTest extends AbstractShardingSphereDataSourceForShardingTest {
-
- private static final String SQL = "SELECT 1";
-
- @After
- public void tearDown() {
- TransactionTypeHolder.clear();
- XAShardingTransactionManagerFixture.getInvocations().clear();
- BASEShardingTransactionManagerFixture.getInvocations().clear();
- }
-
- @Test
- public void assertLocalTransactionAutoCommit() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- assertTrue(actual.getAutoCommit());
- actual.createStatement().executeQuery(SQL);
- actual.setAutoCommit(false);
- assertFalse(actual.getAutoCommit());
- Multimap<String, Connection> cachedConnections = getCachedConnections(actual);
- assertThat(cachedConnections.size(), is(1));
- for (Connection each : cachedConnections.values()) {
- assertFalse(each.getAutoCommit());
- }
- }
- }
-
- @Test
- public void assertShardingTransactionAutoCommit() throws SQLException {
- TransactionTypeHolder.set(TransactionType.XA);
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.createStatement().executeQuery(SQL);
- actual.setAutoCommit(false);
- actual.createStatement().executeQuery(SQL);
- assertTrue(actual.getShardingTransactionManager().isInTransaction());
- Multimap<String, Connection> cachedConnections = getCachedConnections(actual);
- assertThat(cachedConnections.size(), is(1));
- for (Connection each : cachedConnections.values()) {
- assertTrue(each.getAutoCommit());
- }
- } finally {
- TransactionTypeHolder.clear();
- }
- }
-
- @Test
- public void assertShardingTransactionSkipAutoCommit() throws SQLException {
- TransactionTypeHolder.set(TransactionType.XA);
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setAutoCommit(true);
- assertFalse(actual.getShardingTransactionManager().isInTransaction());
- } finally {
- TransactionTypeHolder.clear();
- }
- TransactionTypeHolder.set(TransactionType.XA);
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setAutoCommit(false);
- assertTrue(actual.getShardingTransactionManager().isInTransaction());
- assertThat(XAShardingTransactionManagerFixture.getInvocations().size(), is(1));
- actual.setAutoCommit(false);
- assertThat(XAShardingTransactionManagerFixture.getInvocations().size(), is(1));
- } finally {
- TransactionTypeHolder.clear();
- }
- }
-
- @Test
- public void assertLocalTransactionCommit() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setAutoCommit(false);
- actual.createStatement().executeQuery(SQL);
- actual.commit();
- }
- }
-
- @Test
- public void assertShardingTransactionCommit() throws SQLException {
- TransactionTypeHolder.set(TransactionType.XA);
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.commit();
- assertTrue(XAShardingTransactionManagerFixture.getInvocations().contains(TransactionOperationType.COMMIT));
- } finally {
- TransactionTypeHolder.clear();
- }
- }
-
- @Test
- public void assertShardingTransactionForceCommit() throws SQLException {
- TransactionTypeHolder.set(TransactionType.XA);
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setAutoCommit(false);
- actual.setAutoCommit(true);
- assertTrue(XAShardingTransactionManagerFixture.getInvocations().contains(TransactionOperationType.COMMIT));
- } finally {
- TransactionTypeHolder.clear();
- }
- }
-
- @Test
- public void assertLocalTransactionRollback() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setAutoCommit(false);
- actual.createStatement().executeQuery(SQL);
- actual.rollback();
- }
- }
-
- @Test
- public void assertShardingTransactionRollback() throws SQLException {
- TransactionTypeHolder.set(TransactionType.XA);
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.rollback();
- assertTrue(XAShardingTransactionManagerFixture.getInvocations().contains(TransactionOperationType.ROLLBACK));
- } finally {
- TransactionTypeHolder.clear();
- }
- }
+public final class ConnectionAdapterTest {
@Test
public void assertClose() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.createStatement().executeQuery(SQL);
- actual.close();
- assertClose(actual);
- }
- }
-
- private void assertClose(final ShardingSphereConnection actual) {
+ ShardingSphereConnection actual = mockShardingSphereConnection(mock(Connection.class));
+ actual.close();
assertTrue(actual.isClosed());
- Multimap<String, Connection> cachedConnections = getCachedConnections(actual);
- assertTrue(cachedConnections.isEmpty());
+ assertTrue(getCachedConnections(actual).isEmpty());
}
@Test
public void assertCloseShouldNotClearTransactionType() throws SQLException {
+ ShardingSphereConnection actual = mockShardingSphereConnection(mock(Connection.class));
TransactionTypeHolder.set(TransactionType.XA);
- TransactionType transactionType = TransactionTypeHolder.get();
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.createStatement().executeQuery(SQL);
- }
- assertThat(TransactionTypeHolder.get(), is(transactionType));
+ actual.close();
+ assertTrue(actual.isClosed());
+ assertTrue(getCachedConnections(actual).isEmpty());
+ assertThat(TransactionTypeHolder.get(), is(TransactionType.XA));
}
@Test
public void assertSetReadOnly() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- assertFalse(actual.isReadOnly());
- actual.setReadOnly(true);
- actual.createStatement().executeQuery(SQL);
- assertReadOnly(actual, true);
- actual.setReadOnly(false);
- assertReadOnly(actual, false);
- }
- }
-
- private void assertReadOnly(final ShardingSphereConnection actual, final boolean readOnly) throws SQLException {
- assertThat(actual.isReadOnly(), is(readOnly));
- Multimap<String, Connection> cachedConnections = getCachedConnections(actual);
- assertThat(cachedConnections.size(), is(1));
- for (Connection each : cachedConnections.values()) {
- assertThat(each.isReadOnly(), is(readOnly));
- }
+ Connection connection = mock(Connection.class);
+ ShardingSphereConnection actual = mockShardingSphereConnection(connection);
+ assertFalse(actual.isReadOnly());
+ actual.setReadOnly(true);
+ assertTrue(actual.isReadOnly());
+ verify(connection).setReadOnly(true);
}
@Test
- public void assertGetTransactionIsolation() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.createStatement().executeQuery(SQL);
- assertThat(actual.getTransactionIsolation(), is(Connection.TRANSACTION_READ_COMMITTED));
- }
+ public void assertGetTransactionIsolationWithoutCachedConnections() throws SQLException {
+ assertThat(mockShardingSphereConnection().getTransactionIsolation(), is(Connection.TRANSACTION_READ_UNCOMMITTED));
}
@Test
public void assertSetTransactionIsolation() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- assertThat(actual.getTransactionIsolation(), is(Connection.TRANSACTION_READ_UNCOMMITTED));
- actual.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
- actual.createStatement().executeQuery(SQL);
- assertTransactionIsolation(actual, Connection.TRANSACTION_SERIALIZABLE);
- actual.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- assertTransactionIsolation(actual, Connection.TRANSACTION_READ_COMMITTED);
- }
+ Connection connection = mock(Connection.class);
+ ShardingSphereConnection actual = mockShardingSphereConnection(connection);
+ actual.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
+ verify(connection).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
}
- private void assertTransactionIsolation(final ShardingSphereConnection actual, final int transactionIsolation) throws SQLException {
- assertThat(actual.getTransactionIsolation(), is(transactionIsolation));
- Multimap<String, Connection> cachedConnections = getCachedConnections(actual);
- assertThat(cachedConnections.size(), is(1));
- for (Connection each : cachedConnections.values()) {
- assertThat(each.getTransactionIsolation(), is(transactionIsolation));
- }
+ @Test
+ public void assertGetWarnings() {
+ assertNull(mockShardingSphereConnection().getWarnings());
}
@Test
- public void assertGetWarnings() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- assertNull(actual.getWarnings());
- }
+ public void assertClearWarnings() {
+ mockShardingSphereConnection().clearWarnings();
}
@Test
- public void assertClearWarnings() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.clearWarnings();
- }
+ public void assertGetHoldability() {
+ assertThat(mockShardingSphereConnection().getHoldability(), is(ResultSet.CLOSE_CURSORS_AT_COMMIT));
}
@Test
- public void assertGetHoldability() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- assertThat(actual.getHoldability(), is(ResultSet.CLOSE_CURSORS_AT_COMMIT));
- }
+ public void assertSetHoldability() {
+ mockShardingSphereConnection().setHoldability(ResultSet.CONCUR_READ_ONLY);
+ assertThat(mockShardingSphereConnection().getHoldability(), is(ResultSet.CLOSE_CURSORS_AT_COMMIT));
}
@Test
- public void assertSetHoldability() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setHoldability(ResultSet.CONCUR_READ_ONLY);
- assertThat(actual.getHoldability(), is(ResultSet.CLOSE_CURSORS_AT_COMMIT));
- }
+ public void assertGetCatalog() {
+ assertNull(mockShardingSphereConnection().getCatalog());
}
@Test
- public void assertGetCatalog() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- assertNull(actual.getCatalog());
- }
+ public void assertSetCatalog() {
+ ShardingSphereConnection actual = mockShardingSphereConnection();
+ actual.setCatalog("");
+ assertNull(actual.getCatalog());
}
@Test
- public void assertSetCatalog() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setCatalog("");
- }
+ public void assertGetSchema() {
+ assertNull(mockShardingSphereConnection().getSchema());
}
@Test
- public void assertGetSchema() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- assertNull(actual.getSchema());
- }
+ public void assertSetSchema() {
+ ShardingSphereConnection actual = mockShardingSphereConnection();
+ actual.setSchema("");
+ assertNull(actual.getSchema());
}
- @Test
- public void assertSetSchema() throws SQLException {
- try (ShardingSphereConnection actual = getShardingSphereDataSource().getConnection()) {
- actual.setSchema("");
- }
+ private ShardingSphereConnection mockShardingSphereConnection(final Connection... connections) {
+ ShardingSphereConnection result = new ShardingSphereConnection(
+ Collections.emptyMap(), mock(MetaDataContexts.class), mock(TransactionContexts.class, RETURNS_DEEP_STUBS), TransactionType.LOCAL);
+ result.getCachedConnections().putAll("", Arrays.asList(connections));
+ return result;
}
@SuppressWarnings("unchecked")