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 7ab56248ac6 Add more test cases on PostgreSQLCommandPacketFactoryTest
(#38203)
7ab56248ac6 is described below
commit 7ab56248ac628a4fc3dfef3608ac3bea8c1bfa81
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 26 11:20:54 2026 +0800
Add more test cases on PostgreSQLCommandPacketFactoryTest (#38203)
---
.../PostgreSQLCommandPacketFactoryTest.java | 107 +++++++++++++--------
1 file changed, 65 insertions(+), 42 deletions(-)
diff --git
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java
index 281e9e1c2ec..f5d1fbc2f00 100644
---
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java
+++
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/PostgreSQLCommandPacketFactoryTest.java
@@ -18,71 +18,94 @@
package org.apache.shardingsphere.database.protocol.postgresql.packet.command;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.admin.PostgreSQLUnsupportedCommandPacket;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.PostgreSQLAggregatedCommandPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLComBindPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.close.PostgreSQLComClosePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.describe.PostgreSQLComDescribePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.execute.PostgreSQLComExecutePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.flush.PostgreSQLComFlushPacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.parse.PostgreSQLComParsePacket;
+import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.sync.PostgreSQLComSyncPacket;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.simple.PostgreSQLComQueryPacket;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.generic.PostgreSQLComTerminationPacket;
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 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.List;
+import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-@ExtendWith(MockitoExtension.class)
class PostgreSQLCommandPacketFactoryTest {
- @Mock
- private PostgreSQLPacketPayload payload;
-
- @Test
- void assertNewInstanceWithQueryComPacket() {
- when(payload.getByteBuf()).thenReturn(mock(ByteBuf.class));
- when(payload.readStringNul()).thenReturn("");
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.SIMPLE_QUERY,
payload), isA(PostgreSQLComQueryPacket.class));
- }
-
- @Test
- void assertNewInstanceWithParseComPacket() {
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.PARSE_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
- }
-
- @Test
- void assertNewInstanceWithBindComPacket() {
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.BIND_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("nonExtendedProtocolCases")
+ void assertNewInstanceWithNonExtendedProtocol(final String name, final
PostgreSQLCommandPacketType commandPacketType,
+ final int packetTypeValue,
final byte[] packetBody, final Class<? extends PostgreSQLCommandPacket>
expectedPacketClass) {
+
assertThat(PostgreSQLCommandPacketFactory.newInstance(commandPacketType,
createPayload(packetTypeValue, packetBody)), isA(expectedPacketClass));
}
@Test
- void assertNewInstanceWithDescribeComPacket() {
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.DESCRIBE_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
+ void assertNewInstanceWithExtendedProtocolWithoutPackets() {
+ assertTrue(((PostgreSQLAggregatedCommandPacket)
PostgreSQLCommandPacketFactory.newInstance(
+ PostgreSQLCommandPacketType.PARSE_COMMAND, new
PostgreSQLPacketPayload(Unpooled.buffer(),
StandardCharsets.UTF_8))).getPackets().isEmpty());
}
- @Test
- void assertNewInstanceWithExecuteComPacket() {
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.EXECUTE_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("extendedProtocolCases")
+ void assertNewInstanceWithExtendedProtocol(final String name, final int
packetTypeValue, final byte[] packetBody,
+ final Class<? extends
PostgreSQLCommandPacket> expectedPacketClass) {
+ PostgreSQLCommandPacket actual =
PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.PARSE_COMMAND,
createPayload(packetTypeValue, packetBody));
+ List<PostgreSQLCommandPacket> actualPackets =
((PostgreSQLAggregatedCommandPacket) actual).getPackets();
+ assertThat(actualPackets.size(), is(1));
+ assertThat(actualPackets.get(0), isA(expectedPacketClass));
}
- @Test
- void assertNewInstanceWithSyncComPacket() {
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.SYNC_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
+ private static PostgreSQLPacketPayload createPayload(final int
packetTypeValue, final byte[] packetBody) {
+ ByteBuf byteBuf = Unpooled.buffer(1 + Integer.BYTES +
packetBody.length);
+ byteBuf.writeByte(packetTypeValue);
+ byteBuf.writeInt(Integer.BYTES + packetBody.length);
+ byteBuf.writeBytes(packetBody);
+ return new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
}
- @Test
- void assertNewInstanceWithCloseComPacket() {
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.CLOSE_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
+ private static Stream<Arguments> nonExtendedProtocolCases() {
+ return Stream.of(
+ Arguments.of("simple_query",
PostgreSQLCommandPacketType.SIMPLE_QUERY, (int)
PostgreSQLCommandPacketType.SIMPLE_QUERY.getValue(),
+ createQueryBody(), PostgreSQLComQueryPacket.class),
+ Arguments.of("termination",
PostgreSQLCommandPacketType.TERMINATE, (int)
PostgreSQLCommandPacketType.TERMINATE.getValue(),
+ new byte[0], PostgreSQLComTerminationPacket.class),
+ Arguments.of("unsupported_password",
PostgreSQLCommandPacketType.PASSWORD, (int)
PostgreSQLCommandPacketType.PASSWORD.getValue(),
+ new byte[0],
PostgreSQLUnsupportedCommandPacket.class));
}
- @Test
- void assertNewInstanceWithFlushComPacket() {
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.FLUSH_COMMAND,
payload), isA(PostgreSQLAggregatedCommandPacket.class));
+ private static byte[] createQueryBody() {
+ byte[] queryBytes = "SELECT 1".getBytes(StandardCharsets.UTF_8);
+ ByteBuf byteBuf = Unpooled.buffer(queryBytes.length + 1);
+ byteBuf.writeBytes(queryBytes);
+ byteBuf.writeByte(0);
+ byte[] result = new byte[byteBuf.readableBytes()];
+ byteBuf.getBytes(0, result);
+ return result;
}
- @Test
- void assertNewInstanceWithTerminationComPacket() {
- when(payload.getByteBuf()).thenReturn(mock(ByteBuf.class));
-
assertThat(PostgreSQLCommandPacketFactory.newInstance(PostgreSQLCommandPacketType.TERMINATE,
payload), isA(PostgreSQLComTerminationPacket.class));
+ private static Stream<Arguments> extendedProtocolCases() {
+ return Stream.of(
+ Arguments.of("parse", (int)
PostgreSQLCommandPacketType.PARSE_COMMAND.getValue(), new byte[]{0, 0},
PostgreSQLComParsePacket.class),
+ Arguments.of("bind", (int)
PostgreSQLCommandPacketType.BIND_COMMAND.getValue(), new byte[]{0, 0},
PostgreSQLComBindPacket.class),
+ Arguments.of("describe", (int)
PostgreSQLCommandPacketType.DESCRIBE_COMMAND.getValue(), new byte[]{'S', 0},
PostgreSQLComDescribePacket.class),
+ Arguments.of("execute", (int)
PostgreSQLCommandPacketType.EXECUTE_COMMAND.getValue(), new byte[]{0, 0, 0, 0,
0}, PostgreSQLComExecutePacket.class),
+ Arguments.of("sync", (int)
PostgreSQLCommandPacketType.SYNC_COMMAND.getValue(), new byte[0],
PostgreSQLComSyncPacket.class),
+ Arguments.of("close", (int)
PostgreSQLCommandPacketType.CLOSE_COMMAND.getValue(), new byte[]{'S', 0},
PostgreSQLComClosePacket.class),
+ Arguments.of("flush", (int)
PostgreSQLCommandPacketType.FLUSH_COMMAND.getValue(), new byte[0],
PostgreSQLComFlushPacket.class));
}
}