You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/02/23 15:18:56 UTC
(shardingsphere) branch master updated: Add more test cases on FirebirdPrepareStatementPacketTest, FirebirdReturnColumnPacketTest (#38171)
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 801fabe8470 Add more test cases on FirebirdPrepareStatementPacketTest, FirebirdReturnColumnPacketTest (#38171)
801fabe8470 is described below
commit 801fabe84708660d3ed1f748c0ae0f53196f9709
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Feb 23 23:18:49 2026 +0800
Add more test cases on FirebirdPrepareStatementPacketTest, FirebirdReturnColumnPacketTest (#38171)
---
.../FirebirdPrepareStatementPacketTest.java | 83 +++++++++++++++-------
.../prepare/FirebirdReturnColumnPacketTest.java | 78 +++++++++++++++-----
2 files changed, 118 insertions(+), 43 deletions(-)
diff --git a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdPrepareStatementPacketTest.java b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdPrepareStatementPacketTest.java
index 316ea2635af..2d281c4e4e1 100644
--- a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdPrepareStatementPacketTest.java
+++ b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdPrepareStatementPacketTest.java
@@ -20,18 +20,24 @@ package org.apache.shardingsphere.database.protocol.firebird.packet.command.quer
import io.netty.buffer.ByteBuf;
import org.apache.shardingsphere.database.protocol.firebird.packet.command.query.info.type.sql.FirebirdSQLInfoPacketType;
import org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
+import org.apache.shardingsphere.database.protocol.payload.PacketPayload;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
+import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.jupiter.MockitoExtension;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -44,36 +50,53 @@ class FirebirdPrepareStatementPacketTest {
private ByteBuf byteBuf;
@Test
- void assertPrepareStatementPacket() {
- doNothing().when(payload).skipReserved(anyInt());
- when(payload.readInt4()).thenReturn(1, 2, 3, 10);
- when(payload.readString()).thenReturn("SELECT 1");
- when(payload.readBuffer()).thenReturn(byteBuf);
- when(byteBuf.isReadable()).thenReturn(true, true, false);
- when(byteBuf.readByte()).thenReturn((byte) FirebirdSQLInfoPacketType.STMT_TYPE.getCode(), (byte) FirebirdSQLInfoPacketType.DESCRIBE_VARS.getCode());
- FirebirdPrepareStatementPacket packet = new FirebirdPrepareStatementPacket(payload);
+ void assertConstructor() {
+ FirebirdPrepareStatementPacket packet = createPacket(2, Arrays.asList(FirebirdSQLInfoPacketType.STMT_TYPE, FirebirdSQLInfoPacketType.DESCRIBE_VARS));
verify(payload).skipReserved(4);
assertThat(packet.getTransactionId(), is(1));
assertThat(packet.getStatementId(), is(2));
assertThat(packet.getSqlDialect(), is(3));
- assertThat(packet.getSQL(), is("SELECT 1"));
- assertTrue(packet.isValidStatementHandle());
- assertTrue(packet.nextItem());
- assertThat(packet.getCurrentItem(), is(FirebirdSQLInfoPacketType.STMT_TYPE));
- assertTrue(packet.nextItem());
- assertThat(packet.getCurrentItem(), is(FirebirdSQLInfoPacketType.DESCRIBE_VARS));
- assertFalse(packet.nextItem());
+ assertThat(packet.getInfoItems(), is(Arrays.asList(FirebirdSQLInfoPacketType.STMT_TYPE, FirebirdSQLInfoPacketType.DESCRIBE_VARS)));
+ assertThat(packet.getMaxLength(), is(10));
+ }
+
+ @Test
+ void assertIsValidStatementHandle() {
+ assertTrue(createPacket(2, Collections.emptyList()).isValidStatementHandle());
}
@Test
void assertIsValidStatementHandleWhenInvalid() {
- doNothing().when(payload).skipReserved(anyInt());
- when(payload.readInt4()).thenReturn(1, 0xFFFF, 3, 10);
- when(payload.readString()).thenReturn("SELECT 1");
- when(payload.readBuffer()).thenReturn(byteBuf);
- when(byteBuf.isReadable()).thenReturn(false);
- FirebirdPrepareStatementPacket packet = new FirebirdPrepareStatementPacket(payload);
- assertFalse(packet.isValidStatementHandle());
+ assertFalse(createPacket(0xFFFF, Collections.emptyList()).isValidStatementHandle());
+ }
+
+ @Test
+ void assertNextItem() {
+ assertTrue(createPacket(2, Collections.singletonList(FirebirdSQLInfoPacketType.STMT_TYPE)).nextItem());
+ }
+
+ @Test
+ void assertNextItemWhenNoItem() {
+ assertFalse(createPacket(2, Collections.emptyList()).nextItem());
+ }
+
+ @Test
+ void assertGetCurrentItem() throws ReflectiveOperationException {
+ FirebirdPrepareStatementPacket packet = createPacket(2, Collections.singletonList(FirebirdSQLInfoPacketType.STMT_TYPE));
+ Plugins.getMemberAccessor().set(FirebirdPrepareStatementPacket.class.getDeclaredField("currentItemIdx"), packet, 0);
+ assertThat(packet.getCurrentItem(), is(FirebirdSQLInfoPacketType.STMT_TYPE));
+ }
+
+ @Test
+ void assertGetSQL() {
+ assertThat(createPacket(2, Collections.emptyList()).getSQL(), is("SELECT 1"));
+ }
+
+ @Test
+ void assertWrite() {
+ FirebirdPacketPayload writePayload = mock(FirebirdPacketPayload.class);
+ createPacket(2, Collections.emptyList()).write((PacketPayload) writePayload);
+ verifyNoInteractions(writePayload);
}
@Test
@@ -84,4 +107,16 @@ class FirebirdPrepareStatementPacketTest {
verify(payload).getBufferLength(16);
verify(payload).getBufferLength(28);
}
+
+ private FirebirdPrepareStatementPacket createPacket(final int statementId, final List<FirebirdSQLInfoPacketType> infoItems) {
+ when(payload.readInt4()).thenReturn(1, statementId, 3, 10);
+ when(payload.readString()).thenReturn("SELECT 1");
+ when(payload.readBuffer()).thenReturn(byteBuf);
+ int[] idx = new int[1];
+ when(byteBuf.isReadable()).thenAnswer(invocation -> idx[0] < infoItems.size());
+ if (!infoItems.isEmpty()) {
+ when(byteBuf.readByte()).thenAnswer(invocation -> (byte) infoItems.get(idx[0]++).getCode());
+ }
+ return new FirebirdPrepareStatementPacket(payload);
+ }
}
diff --git a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdReturnColumnPacketTest.java b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdReturnColumnPacketTest.java
index 2a705cfd557..85e95ccaa8d 100644
--- a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdReturnColumnPacketTest.java
+++ b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/prepare/FirebirdReturnColumnPacketTest.java
@@ -17,19 +17,32 @@
package org.apache.shardingsphere.database.protocol.firebird.packet.command.query.statement.prepare;
+import org.apache.shardingsphere.database.protocol.firebird.exception.FirebirdProtocolException;
+import org.apache.shardingsphere.database.protocol.firebird.packet.command.query.FirebirdBinaryColumnType;
import org.apache.shardingsphere.database.protocol.firebird.packet.command.query.info.type.sql.FirebirdSQLInfoPacketType;
import org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
+import org.mockito.MockedConstruction;
+import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
+import java.nio.charset.StandardCharsets;
import java.sql.Types;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
+import java.util.stream.Stream;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mockConstruction;
+import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -41,9 +54,7 @@ class FirebirdReturnColumnPacketTest {
@Test
void assertWrite() {
- ShardingSphereColumn column = new ShardingSphereColumn("col", Types.VARCHAR, false, false, false, true, false, true);
- ShardingSphereTable table = new ShardingSphereTable("tbl", Collections.singleton(column), Collections.emptyList(), Collections.emptyList());
- FirebirdReturnColumnPacket packet = new FirebirdReturnColumnPacket(Arrays.asList(
+ FirebirdReturnColumnPacket packet = createPacket(Arrays.asList(
FirebirdSQLInfoPacketType.SQLDA_SEQ,
FirebirdSQLInfoPacketType.TYPE,
FirebirdSQLInfoPacketType.SUB_TYPE,
@@ -54,31 +65,60 @@ class FirebirdReturnColumnPacketTest {
FirebirdSQLInfoPacketType.RELATION,
FirebirdSQLInfoPacketType.RELATION_ALIAS,
FirebirdSQLInfoPacketType.OWNER,
- FirebirdSQLInfoPacketType.DESCRIBE_END), 1, table, column, "t", "c", "o", 99, false, null);
- when(payload.getCharset()).thenReturn(java.nio.charset.StandardCharsets.UTF_8);
+ FirebirdSQLInfoPacketType.DESCRIBE_END), Types.VARCHAR, 99, false, null);
+ when(payload.getCharset()).thenReturn(StandardCharsets.UTF_8);
packet.write(payload);
verify(payload).writeInt1(FirebirdSQLInfoPacketType.SQLDA_SEQ.getCode());
verify(payload).writeInt1(FirebirdSQLInfoPacketType.DESCRIBE_END.getCode());
verify(payload).writeInt4LE(99);
}
- @Test
- void assertWriteUsesDefaultColumnLength() {
- ShardingSphereColumn column = new ShardingSphereColumn("col", Types.INTEGER, false, false, false, true, false, true);
- ShardingSphereTable table = new ShardingSphereTable("tbl", Collections.singleton(column), Collections.emptyList(), Collections.emptyList());
- FirebirdReturnColumnPacket packet = new FirebirdReturnColumnPacket(Collections.singletonList(FirebirdSQLInfoPacketType.LENGTH),
- 1, table, column, "t", "c", "o", null, false, null);
- packet.write(payload);
- verify(payload).writeInt4LE(4);
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("assertWriteLengthArguments")
+ void assertWriteLength(final String name, final FirebirdBinaryColumnType columnType, final int expectedLength) {
+ FirebirdReturnColumnPacket packet = createPacket(Collections.singletonList(FirebirdSQLInfoPacketType.LENGTH), Types.INTEGER, 99, false, null);
+ try (MockedStatic<FirebirdBinaryColumnType> mocked = mockStatic(FirebirdBinaryColumnType.class)) {
+ mocked.when(() -> FirebirdBinaryColumnType.valueOfJDBCType(Types.INTEGER)).thenReturn(columnType);
+ packet.write(payload);
+ }
+ verify(payload).writeInt4LE(expectedLength);
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("assertWriteSubTypeArguments")
+ void assertWriteSubType(final String name, final boolean blobColumn, final Integer blobSubType, final int expectedSubType) {
+ createPacket(Collections.singletonList(FirebirdSQLInfoPacketType.SUB_TYPE), Types.INTEGER, null, blobColumn, blobSubType).write(payload);
+ verify(payload).writeInt4LE(expectedSubType);
}
@Test
- void assertWriteUsesBlobSubtype() {
- ShardingSphereColumn column = new ShardingSphereColumn("col", Types.BLOB, false, false, false, true, false, true);
+ void assertWriteWithUnsupportedRequestedItem() {
+ FirebirdReturnColumnPacket packet = createPacket(Collections.singletonList(FirebirdSQLInfoPacketType.SELECT), Types.INTEGER, null, false, null);
+ try (MockedConstruction<FirebirdProtocolException> ignored = mockConstruction(FirebirdProtocolException.class)) {
+ assertThrows(FirebirdProtocolException.class, () -> packet.write(payload));
+ }
+ }
+
+ private FirebirdReturnColumnPacket createPacket(final Collection<FirebirdSQLInfoPacketType> requestedItems, final int dataType, final Integer columnLength,
+ final boolean blobColumn, final Integer blobSubType) {
+ ShardingSphereColumn column = new ShardingSphereColumn("col", dataType, false, false, false, true, false, true);
ShardingSphereTable table = new ShardingSphereTable("tbl", Collections.singleton(column), Collections.emptyList(), Collections.emptyList());
- FirebirdReturnColumnPacket packet = new FirebirdReturnColumnPacket(Collections.singletonList(FirebirdSQLInfoPacketType.SUB_TYPE),
- 1, table, column, "t", "c", "o", null, true, 7);
- packet.write(payload);
- verify(payload).writeInt4LE(7);
+ return new FirebirdReturnColumnPacket(requestedItems, 1, table, column, "t", "c", "o", columnLength, blobColumn, blobSubType);
+ }
+
+ private static Stream<Arguments> assertWriteLengthArguments() {
+ return Stream.of(
+ Arguments.of("length_varying", FirebirdBinaryColumnType.VARYING, 99),
+ Arguments.of("length_legacy_varying", FirebirdBinaryColumnType.LEGACY_VARYING, 99),
+ Arguments.of("length_text", FirebirdBinaryColumnType.TEXT, 99),
+ Arguments.of("length_legacy_text", FirebirdBinaryColumnType.LEGACY_TEXT, 99),
+ Arguments.of("length_long", FirebirdBinaryColumnType.LONG, FirebirdBinaryColumnType.LONG.getLength()));
+ }
+
+ private static Stream<Arguments> assertWriteSubTypeArguments() {
+ return Stream.of(
+ Arguments.of("blob_with_subtype", true, 7, 7),
+ Arguments.of("blob_without_subtype", true, null, FirebirdBinaryColumnType.BLOB.getSubtype()),
+ Arguments.of("long_default_subtype", false, null, FirebirdBinaryColumnType.LONG.getSubtype()));
}
}