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 0620d9e157d Add more test cases on PostgreSQLComBindPacketTest,
PostgreSQLComClosePacketTest, PostgreSQLComDescribePacketTest (#38210)
0620d9e157d is described below
commit 0620d9e157dfde85e8cb931a82e4f44e931d1e47
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 26 15:51:06 2026 +0800
Add more test cases on PostgreSQLComBindPacketTest,
PostgreSQLComClosePacketTest, PostgreSQLComDescribePacketTest (#38210)
---
.../extended/bind/PostgreSQLComBindPacketTest.java | 113 +++++++++++++++++++--
.../close/PostgreSQLComClosePacketTest.java | 33 +++---
.../describe/PostgreSQLComDescribePacketTest.java | 15 +--
3 files changed, 128 insertions(+), 33 deletions(-)
diff --git
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacketTest.java
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacketTest.java
index 04309a670bb..d30b91ba3b5 100644
---
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacketTest.java
+++
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacketTest.java
@@ -18,32 +18,123 @@
package
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind;
import io.netty.buffer.Unpooled;
+import
org.apache.shardingsphere.database.protocol.postgresql.constant.PostgreSQLValueFormat;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.PostgreSQLBinaryColumnType;
import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.List;
+import java.util.stream.Stream;
-import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class PostgreSQLComBindPacketTest {
- private static final byte[] BIND_MESSAGE_BYTES = {
- 0x00, 0x00, 0x00, 0x19, 0x00, 0x53, 0x5f,
- 0x31, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01,
- 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0a,
- 0x00, 0x00
+ private static final byte[] NEW_INSTANCE_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00
+ };
+
+ private static final byte[] READ_PARAMETERS_NULL_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00,
+ 0x00, 0x00, 0x00, 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF
+ };
+
+ private static final byte[] READ_PARAMETERS_EMPTY_TEXT_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00,
+ 0x00, 0x00,
+ 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x02, 0x31, 0x31
+ };
+
+ private static final byte[] READ_PARAMETERS_BINARY_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00,
+ 0x00, 0x01, 0x00, 0x01,
+ 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x04,
+ 0x00, 0x00, 0x00, 0x0A
+ };
+
+ private static final byte[] READ_PARAMETERS_MIXED_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00,
+ 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
+ 0x00, 0x02,
+ 0x00, 0x00, 0x00, 0x02, 0x31, 0x32,
+ 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03
+ };
+
+ private static final byte[] READ_RESULT_FORMATS_EMPTY_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00, 0x00, 0x00
+ };
+
+ private static final byte[] READ_RESULT_FORMATS_SINGLE_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00,
+ 0x00, 0x01, 0x00, 0x01
+ };
+
+ private static final byte[] READ_RESULT_FORMATS_MULTI_PACKET_BYTES = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x31, 0x00,
+ 0x00, 0x02, 0x00, 0x00, 0x00, 0x01
};
@Test
- void assertConstructPostgreSQLComBindPacket() {
- PostgreSQLComBindPacket actual = new PostgreSQLComBindPacket(new
PostgreSQLPacketPayload(Unpooled.wrappedBuffer(BIND_MESSAGE_BYTES),
StandardCharsets.UTF_8));
+ void assertNewInstance() {
+ PostgreSQLComBindPacket actual = new PostgreSQLComBindPacket(new
PostgreSQLPacketPayload(Unpooled.wrappedBuffer(NEW_INSTANCE_PACKET_BYTES),
StandardCharsets.UTF_8));
assertThat(actual.getPortal(), is(""));
assertThat(actual.getStatementId(), is("S_1"));
-
assertThat(actual.readParameters(Collections.singletonList(PostgreSQLBinaryColumnType.INT4)),
is(Collections.singletonList(10)));
- assertTrue(actual.readResultFormats().isEmpty());
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("assertReadParametersArguments")
+ void assertReadParameters(final String name, final byte[] packetBytes,
final List<PostgreSQLBinaryColumnType> paramTypes, final List<Object> expected)
{
+ PostgreSQLComBindPacket packet = new PostgreSQLComBindPacket(new
PostgreSQLPacketPayload(Unpooled.wrappedBuffer(packetBytes),
StandardCharsets.UTF_8));
+ assertThat(packet.readParameters(paramTypes), is(expected));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("assertReadResultFormatsArguments")
+ void assertReadResultFormats(final String name, final byte[] packetBytes,
final List<PostgreSQLValueFormat> expected) {
+ PostgreSQLComBindPacket packet = new PostgreSQLComBindPacket(new
PostgreSQLPacketPayload(Unpooled.wrappedBuffer(packetBytes),
StandardCharsets.UTF_8));
+ assertThat(packet.readResultFormats(), is(expected));
+ }
+
+ @Test
+ void assertWrite() {
+ PostgreSQLPacketPayload payload = new
PostgreSQLPacketPayload(Unpooled.wrappedBuffer(NEW_INSTANCE_PACKET_BYTES),
StandardCharsets.UTF_8);
+ PostgreSQLComBindPacket packet = new PostgreSQLComBindPacket(payload);
+ assertDoesNotThrow(() -> packet.write(payload));
+ }
+
+ @Test
+ void assertGetIdentifier() {
+ PostgreSQLComBindPacket actual = new PostgreSQLComBindPacket(new
PostgreSQLPacketPayload(Unpooled.wrappedBuffer(NEW_INSTANCE_PACKET_BYTES),
StandardCharsets.UTF_8));
+ assertThat(actual.getIdentifier(),
is(PostgreSQLCommandPacketType.BIND_COMMAND));
+ }
+
+ private static Stream<Arguments> assertReadParametersArguments() {
+ return Stream.of(
+ Arguments.of("null parameter with empty format list",
READ_PARAMETERS_NULL_PACKET_BYTES,
+
Collections.singletonList(PostgreSQLBinaryColumnType.INT4),
Collections.singletonList(null)),
+ Arguments.of("text parameter with empty format list",
READ_PARAMETERS_EMPTY_TEXT_PACKET_BYTES,
+
Collections.singletonList(PostgreSQLBinaryColumnType.INT4),
Collections.singletonList(11)),
+ Arguments.of("single binary format parameter",
READ_PARAMETERS_BINARY_PACKET_BYTES,
+
Collections.singletonList(PostgreSQLBinaryColumnType.INT4),
Collections.singletonList(10)),
+ Arguments.of("multi format text and binary parameters",
READ_PARAMETERS_MIXED_PACKET_BYTES,
+ Arrays.asList(PostgreSQLBinaryColumnType.INT4,
PostgreSQLBinaryColumnType.INT4), Arrays.asList(12, 3)));
+ }
+
+ private static Stream<Arguments> assertReadResultFormatsArguments() {
+ return Stream.of(
+ Arguments.of("empty result formats",
READ_RESULT_FORMATS_EMPTY_PACKET_BYTES, Collections.emptyList()),
+ Arguments.of("single result format",
READ_RESULT_FORMATS_SINGLE_PACKET_BYTES,
Collections.singletonList(PostgreSQLValueFormat.BINARY)),
+ Arguments.of("multiple result formats",
READ_RESULT_FORMATS_MULTI_PACKET_BYTES,
Arrays.asList(PostgreSQLValueFormat.TEXT, PostgreSQLValueFormat.BINARY)));
}
}
diff --git
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/close/PostgreSQLComClosePacketTest.java
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/close/PostgreSQLComClosePacketTest.java
index 14175ec226f..e9646e75663 100644
---
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/close/PostgreSQLComClosePacketTest.java
+++
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/close/PostgreSQLComClosePacketTest.java
@@ -19,15 +19,15 @@ package
org.apache.shardingsphere.database.protocol.postgresql.packet.command.qu
import
org.apache.shardingsphere.database.protocol.postgresql.exception.PostgreSQLProtocolException;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
-import
org.apache.shardingsphere.database.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
@@ -39,31 +39,32 @@ class PostgreSQLComClosePacketTest {
@Test
void assertClosePreparedStatement() {
- when(payload.readInt1()).thenReturn((int) 'S');
- when(payload.readStringNul()).thenReturn("S_1");
- PostgreSQLComClosePacket actual = new
PostgreSQLComClosePacket(payload);
- assertThat(actual.getType(),
is(PostgreSQLComClosePacket.Type.PREPARED_STATEMENT));
- assertThat(actual.getName(), is("S_1"));
+ assertThat(newPacket('S', "S_1").getName(), is("S_1"));
}
@Test
void assertClosePortal() {
- when(payload.readInt1()).thenReturn((int) 'P');
- when(payload.readStringNul()).thenReturn("P_1");
- PostgreSQLComClosePacket actual = new
PostgreSQLComClosePacket(payload);
- assertThat(actual.getType(), is(PostgreSQLComClosePacket.Type.PORTAL));
- assertThat(actual.getName(), is("P_1"));
+ assertThat(newPacket('P', "P_1").getName(), is("P_1"));
}
@Test
- void assertIdentifier() {
- when(payload.readInt1()).thenReturn((int) 'S');
- PostgreSQLIdentifierTag actual = new
PostgreSQLComClosePacket(payload).getIdentifier();
- assertThat(actual, is(PostgreSQLCommandPacketType.CLOSE_COMMAND));
+ void assertWrite() {
+ assertDoesNotThrow(() -> newPacket('S', "S_1").write(payload));
+ }
+
+ @Test
+ void assertGetIdentifier() {
+ assertThat(newPacket('S', "S_1").getIdentifier(),
is(PostgreSQLCommandPacketType.CLOSE_COMMAND));
}
@Test
void assertInvalidType() {
assertThrows(PostgreSQLProtocolException.class, () -> new
PostgreSQLComClosePacket(payload));
}
+
+ private PostgreSQLComClosePacket newPacket(final char type, final String
name) {
+ when(payload.readInt1()).thenReturn((int) type);
+ when(payload.readStringNul()).thenReturn(name);
+ return new PostgreSQLComClosePacket(payload);
+ }
}
diff --git
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/describe/PostgreSQLComDescribePacketTest.java
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/describe/PostgreSQLComDescribePacketTest.java
index 93b4568409e..7b30373ef63 100644
---
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/describe/PostgreSQLComDescribePacketTest.java
+++
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/extended/describe/PostgreSQLComDescribePacketTest.java
@@ -26,6 +26,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -38,14 +39,16 @@ class PostgreSQLComDescribePacketTest {
void assertNewInstance() {
when(payload.readInt1()).thenReturn((int) 'P');
when(payload.readStringNul()).thenReturn("P_1");
- PostgreSQLComDescribePacket actual = new
PostgreSQLComDescribePacket(payload);
- assertThat(actual.getType(), is('P'));
- assertThat(actual.getName(), is("P_1"));
+ assertThat(new PostgreSQLComDescribePacket(payload).getName(),
is("P_1"));
}
@Test
- void assertIdentifier() {
- PostgreSQLComDescribePacket actual = new
PostgreSQLComDescribePacket(payload);
- assertThat(actual.getIdentifier(),
is(PostgreSQLCommandPacketType.DESCRIBE_COMMAND));
+ void assertWrite() {
+ assertDoesNotThrow(() -> new
PostgreSQLComDescribePacket(payload).write(payload));
+ }
+
+ @Test
+ void assertGetIdentifier() {
+ assertThat(new PostgreSQLComDescribePacket(payload).getIdentifier(),
is(PostgreSQLCommandPacketType.DESCRIBE_COMMAND));
}
}