This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 dc0ffea3eab Add more test cases for shardingsphere-database-connector
module (#37575)
dc0ffea3eab is described below
commit dc0ffea3eabbfdd9088b887b2bbf78906cf93567
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Dec 29 23:04:36 2025 +0800
Add more test cases for shardingsphere-database-connector module (#37575)
---
.../frontend/netty/ProxyFlowControlHandlerTest.java | 18 ++++++++++++++++++
...FirebirdCommitTransactionCommandExecutorTest.java | 15 ++++++++++++---
...rebirdRollbackTransactionCommandExecutorTest.java | 15 ++++++++++++---
.../mysql/err/MySQLErrorPacketFactoryTest.java | 20 ++++++++++++++++++++
.../extended/sync/PostgreSQLComSyncExecutorTest.java | 12 ++++++++++--
5 files changed, 72 insertions(+), 8 deletions(-)
diff --git
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ProxyFlowControlHandlerTest.java
b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ProxyFlowControlHandlerTest.java
index e2aec5f95b0..82248fcad1e 100644
---
a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ProxyFlowControlHandlerTest.java
+++
b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ProxyFlowControlHandlerTest.java
@@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicBoolean;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ProxyFlowControlHandlerTest {
@@ -44,4 +45,21 @@ class ProxyFlowControlHandlerTest {
assertTrue(channel.config().isAutoRead());
assertTrue(eventReceived.get());
}
+
+ @Test
+ void assertUserEventTriggeredWithoutWriteCompleteEvent() {
+ AtomicBoolean eventReceived = new AtomicBoolean(false);
+ Object otherEvent = new Object();
+ EmbeddedChannel channel = new EmbeddedChannel(new
ProxyFlowControlHandler(), new ChannelInboundHandlerAdapter() {
+
+ @Override
+ public void userEventTriggered(final ChannelHandlerContext ctx,
final Object event) {
+ eventReceived.set(otherEvent.equals(event));
+ }
+ });
+ channel.config().setAutoRead(false);
+ channel.pipeline().fireUserEventTriggered(otherEvent);
+ assertFalse(channel.config().isAutoRead());
+ assertTrue(eventReceived.get());
+ }
}
diff --git
a/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdCommitTransactionCommandExecutorTest.java
b/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdCommitTransactionCommandExecutorTest.java
index 5c9a536f9dc..a1008d3bde7 100644
---
a/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdCommitTransactionCommandExecutorTest.java
+++
b/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdCommitTransactionCommandExecutorTest.java
@@ -32,6 +32,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.sql.SQLException;
import java.util.Collection;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
import static org.mockito.Mockito.mockConstruction;
@@ -49,13 +50,21 @@ class FirebirdCommitTransactionCommandExecutorTest {
@Test
void assertExecute() throws SQLException {
- when(connectionSession.isAutoCommit()).thenReturn(false);
try (MockedConstruction<ProxyBackendTransactionManager> mocked =
mockConstruction(ProxyBackendTransactionManager.class, (mock, context) -> {
})) {
- FirebirdCommitTransactionCommandExecutor executor = new
FirebirdCommitTransactionCommandExecutor(packet, connectionSession);
- Collection<DatabasePacket> actual = executor.execute();
+ Collection<DatabasePacket> actual = new
FirebirdCommitTransactionCommandExecutor(packet, connectionSession).execute();
assertThat(actual.iterator().next(),
isA(FirebirdGenericResponsePacket.class));
verify(mocked.constructed().get(0)).commit();
}
}
+
+ @Test
+ void assertExecuteWithAutoCommit() throws SQLException {
+ when(connectionSession.isAutoCommit()).thenReturn(true);
+ try (MockedConstruction<ProxyBackendTransactionManager> mocked =
mockConstruction(ProxyBackendTransactionManager.class)) {
+ Collection<DatabasePacket> actual = new
FirebirdCommitTransactionCommandExecutor(packet, connectionSession).execute();
+ assertThat(actual.iterator().next(),
isA(FirebirdGenericResponsePacket.class));
+ assertTrue(mocked.constructed().isEmpty());
+ }
+ }
}
diff --git
a/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdRollbackTransactionCommandExecutorTest.java
b/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdRollbackTransactionCommandExecutorTest.java
index 09f5f79b4ba..95e106720f1 100644
---
a/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdRollbackTransactionCommandExecutorTest.java
+++
b/proxy/frontend/dialect/firebird/src/test/java/org/apache/shardingsphere/proxy/frontend/firebird/command/query/transaction/FirebirdRollbackTransactionCommandExecutorTest.java
@@ -32,6 +32,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.sql.SQLException;
import java.util.Collection;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
import static org.mockito.Mockito.mockConstruction;
@@ -49,13 +50,21 @@ class FirebirdRollbackTransactionCommandExecutorTest {
@Test
void assertExecute() throws SQLException {
- when(connectionSession.isAutoCommit()).thenReturn(false);
try (MockedConstruction<ProxyBackendTransactionManager> mocked =
mockConstruction(ProxyBackendTransactionManager.class, (mock, context) -> {
})) {
- FirebirdRollbackTransactionCommandExecutor executor = new
FirebirdRollbackTransactionCommandExecutor(packet, connectionSession);
- Collection<DatabasePacket> actual = executor.execute();
+ Collection<DatabasePacket> actual = new
FirebirdRollbackTransactionCommandExecutor(packet, connectionSession).execute();
assertThat(actual.iterator().next(),
isA(FirebirdGenericResponsePacket.class));
verify(mocked.constructed().get(0)).rollback();
}
}
+
+ @Test
+ void assertExecuteWithAutoCommit() throws SQLException {
+ when(connectionSession.isAutoCommit()).thenReturn(true);
+ try (MockedConstruction<ProxyBackendTransactionManager> mocked =
mockConstruction(ProxyBackendTransactionManager.class)) {
+ Collection<DatabasePacket> actual = new
FirebirdRollbackTransactionCommandExecutor(packet, connectionSession).execute();
+ assertThat(actual.iterator().next(),
isA(FirebirdGenericResponsePacket.class));
+ assertTrue(mocked.constructed().isEmpty());
+ }
+ }
}
diff --git
a/proxy/frontend/dialect/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
b/proxy/frontend/dialect/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
index bb428a9046d..e9a000398a3 100644
---
a/proxy/frontend/dialect/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
+++
b/proxy/frontend/dialect/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
@@ -43,4 +43,24 @@ class MySQLErrorPacketFactoryTest {
assertThat(actual.getSqlState(),
is(XOpenSQLState.GENERAL_ERROR.getValue()));
assertThat(actual.getErrorMessage(), is("Unknown exception." +
System.lineSeparator() + "More details: java.lang.RuntimeException: No
reason"));
}
+
+ @Test
+ void assertNewInstanceWithoutErrorMessageUsesNextExceptionMessage() {
+ SQLException cause = new SQLException("");
+ cause.setNextException(new SQLException("Next reason"));
+ MySQLErrPacket actual = MySQLErrorPacketFactory.newInstance(cause);
+ assertThat(actual.getErrorCode(), is(1815));
+ assertThat(actual.getSqlState(),
is(XOpenSQLState.GENERAL_ERROR.getValue()));
+ assertThat(actual.getErrorMessage(), is("Internal error: Next
reason"));
+ }
+
+ @Test
+ void assertNewInstanceWithNextExceptionButUsesCurrentMessage() {
+ SQLException cause = new SQLException("Primary reason");
+ cause.setNextException(new SQLException("Next reason"));
+ MySQLErrPacket actual = MySQLErrorPacketFactory.newInstance(cause);
+ assertThat(actual.getErrorCode(), is(1815));
+ assertThat(actual.getSqlState(),
is(XOpenSQLState.GENERAL_ERROR.getValue()));
+ assertThat(actual.getErrorMessage(), is("Internal error: Primary
reason"));
+ }
}
diff --git
a/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/sync/PostgreSQLComSyncExecutorTest.java
b/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/sync/PostgreSQLComSyncExecutorTest.java
index 9541e492172..64ab1457685 100644
---
a/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/sync/PostgreSQLComSyncExecutorTest.java
+++
b/proxy/frontend/dialect/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/sync/PostgreSQLComSyncExecutorTest.java
@@ -27,7 +27,6 @@ import org.mockito.junit.jupiter.MockitoExtension;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.isA;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -40,6 +39,15 @@ class PostgreSQLComSyncExecutorTest {
void assertNewInstance() {
when(connectionSession.getTransactionStatus()).thenReturn(new
TransactionStatus());
PostgreSQLComSyncExecutor actual = new
PostgreSQLComSyncExecutor(connectionSession);
- assertThat(actual.execute().iterator().next(),
is(isA(PostgreSQLReadyForQueryPacket.class)));
+ assertThat(actual.execute().iterator().next(),
is(PostgreSQLReadyForQueryPacket.NOT_IN_TRANSACTION));
+ }
+
+ @Test
+ void assertExecuteInTransaction() {
+ TransactionStatus transactionStatus = new TransactionStatus();
+ transactionStatus.setInTransaction(true);
+
when(connectionSession.getTransactionStatus()).thenReturn(transactionStatus);
+ PostgreSQLComSyncExecutor actual = new
PostgreSQLComSyncExecutor(connectionSession);
+ assertThat(actual.execute().iterator().next(),
is(PostgreSQLReadyForQueryPacket.IN_TRANSACTION));
}
}