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 0169785d243 Add more test cases on PostgreSQLDataRowPacketTest and
PostgreSQLEmptyQueryResponsePacketTest (#38205)
0169785d243 is described below
commit 0169785d24301f9433d088e247d8ed2b5df9c238
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 26 12:11:55 2026 +0800
Add more test cases on PostgreSQLDataRowPacketTest and
PostgreSQLEmptyQueryResponsePacketTest (#38205)
---
.../command/query/PostgreSQLDataRowPacketTest.java | 102 +++++++++++----------
.../PostgreSQLEmptyQueryResponsePacketTest.java | 19 +++-
2 files changed, 68 insertions(+), 53 deletions(-)
diff --git
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
index 894a844e55e..965d01fe66c 100644
---
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
+++
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLDataRowPacketTest.java
@@ -18,32 +18,31 @@
package
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query;
import org.apache.shardingsphere.database.protocol.binary.BinaryCell;
+import org.apache.shardingsphere.database.protocol.payload.PacketPayload;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.identifier.PostgreSQLMessagePacketType;
import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
-import org.junit.jupiter.api.BeforeEach;
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.junit.jupiter.MockitoExtension;
-import org.mockito.junit.jupiter.MockitoSettings;
-import org.mockito.quality.Strictness;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.util.Collections;
+import java.util.stream.Stream;
-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.assertThrows;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-@MockitoSettings(strictness = Strictness.LENIENT)
class PostgreSQLDataRowPacketTest {
@Mock
@@ -52,68 +51,81 @@ class PostgreSQLDataRowPacketTest {
@Mock
private SQLXML sqlxml;
- @BeforeEach
- void setup() {
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("textValueCases")
+ void assertWriteWithTextValue(final String name, final Object value, final
byte[] expectedBytes) {
when(payload.getCharset()).thenReturn(StandardCharsets.UTF_8);
+ PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(value));
+ actual.write((PacketPayload) payload);
+ verify(payload).writeInt2(1);
+ verify(payload).writeInt4(expectedBytes.length);
+ verify(payload).writeBytes(expectedBytes);
}
@Test
- void assertWriteWithNull() {
+ void assertWriteWithNullValue() {
PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(null));
- actual.write(payload);
+ actual.write((PacketPayload) payload);
+ verify(payload).writeInt2(1);
verify(payload).writeInt4(0xFFFFFFFF);
}
@Test
- void assertWriteWithBytes() {
- PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(new byte[]{'a'}));
- actual.write(payload);
- byte[] expectedBytes = buildExpectedByteaText(new byte[]{'a'});
+ void assertWriteWithByteArrayValue() {
+ byte[] value = new byte[]{'a'};
+ PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singletonList(value));
+ actual.write((PacketPayload) payload);
+ byte[] expectedBytes = buildExpectedByteaText(value);
+ verify(payload).writeInt2(1);
verify(payload).writeInt4(expectedBytes.length);
verify(payload).writeBytes(expectedBytes);
}
- @Test
- void assertWriteWithSQLXML() throws SQLException {
- when(sqlxml.getString()).thenReturn("value");
- PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(sqlxml));
- actual.write(payload);
- byte[] valueBytes = "value".getBytes(StandardCharsets.UTF_8);
- verify(payload).writeInt4(valueBytes.length);
- verify(payload).writeBytes(valueBytes);
+ private byte[] buildExpectedByteaText(final byte[] value) {
+ byte[] result = new byte[value.length * 2 + 2];
+ result[0] = '\\';
+ result[1] = 'x';
+ byte[] hexDigits =
"0123456789abcdef".getBytes(StandardCharsets.US_ASCII);
+ for (int i = 0; i < value.length; i++) {
+ int unsignedByte = value[i] & 0xFF;
+ result[2 + i * 2] = hexDigits[unsignedByte >>> 4];
+ result[3 + i * 2] = hexDigits[unsignedByte & 0x0F];
+ }
+ return result;
}
@Test
- void assertWriteWithString() {
- PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton("value"));
- assertThat(actual.getData(), is(Collections.singleton("value")));
- actual.write(payload);
+ void assertWriteWithSQLXMLValue() throws SQLException {
+ when(payload.getCharset()).thenReturn(StandardCharsets.UTF_8);
+ when(sqlxml.getString()).thenReturn("value");
+ PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(sqlxml));
+ actual.write((PacketPayload) payload);
byte[] valueBytes = "value".getBytes(StandardCharsets.UTF_8);
+ verify(payload).writeInt2(1);
verify(payload).writeInt4(valueBytes.length);
verify(payload).writeBytes(valueBytes);
}
@Test
- void assertWriteWithSQLXML4Error() throws SQLException {
+ void assertWriteWithSQLXMLError() throws SQLException {
when(sqlxml.getString()).thenThrow(new SQLException("mock"));
- PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(sqlxml));
- assertThrows(RuntimeException.class, () -> actual.write(payload));
- verify(payload, never()).writeStringEOF(any());
+ PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singletonList(sqlxml));
+ assertThrows(IllegalStateException.class, () ->
actual.write((PacketPayload) payload));
}
@Test
- void assertWriteBinaryNull() {
- PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(new
BinaryCell(PostgreSQLColumnType.INT4, null)));
- actual.write(payload);
+ void assertWriteWithBinaryNullValue() {
+ PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.nCopies(1, new
BinaryCell(PostgreSQLColumnType.INT4, null)));
+ actual.write((PacketPayload) payload);
verify(payload).writeInt2(1);
verify(payload).writeInt4(0xFFFFFFFF);
}
@Test
- void assertWriteBinaryInt4() {
- final int value = 12345678;
+ void assertWriteWithBinaryInt4Value() {
+ int value = 12345678;
PostgreSQLDataRowPacket actual = new
PostgreSQLDataRowPacket(Collections.singleton(new
BinaryCell(PostgreSQLColumnType.INT4, value)));
- actual.write(payload);
+ actual.write((PacketPayload) payload);
verify(payload).writeInt2(1);
verify(payload).writeInt4(4);
verify(payload).writeInt4(value);
@@ -124,16 +136,10 @@ class PostgreSQLDataRowPacketTest {
assertThat(new
PostgreSQLDataRowPacket(Collections.emptyList()).getIdentifier(),
is(PostgreSQLMessagePacketType.DATA_ROW));
}
- private byte[] buildExpectedByteaText(final byte[] value) {
- byte[] result = new byte[value.length * 2 + 2];
- result[0] = '\\';
- result[1] = 'x';
- byte[] hexDigits =
"0123456789abcdef".getBytes(StandardCharsets.US_ASCII);
- for (int i = 0; i < value.length; i++) {
- int unsignedByte = value[i] & 0xFF;
- result[2 + i * 2] = hexDigits[unsignedByte >>> 4];
- result[3 + i * 2] = hexDigits[unsignedByte & 0x0F];
- }
- return result;
+ private static Stream<Arguments> textValueCases() {
+ return Stream.of(
+ Arguments.of("boolean_true", true,
"t".getBytes(StandardCharsets.UTF_8)),
+ Arguments.of("boolean_false", false,
"f".getBytes(StandardCharsets.UTF_8)),
+ Arguments.of("string_value", "value",
"value".getBytes(StandardCharsets.UTF_8)));
}
}
diff --git
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLEmptyQueryResponsePacketTest.java
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLEmptyQueryResponsePacketTest.java
index 2223c161b96..cb55af5e757 100644
---
a/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLEmptyQueryResponsePacketTest.java
+++
b/database/protocol/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/protocol/postgresql/packet/command/query/PostgreSQLEmptyQueryResponsePacketTest.java
@@ -17,18 +17,27 @@
package
org.apache.shardingsphere.database.protocol.postgresql.packet.command.query;
-import
org.apache.shardingsphere.database.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
+import org.apache.shardingsphere.database.protocol.payload.PacketPayload;
import
org.apache.shardingsphere.database.protocol.postgresql.packet.identifier.PostgreSQLMessagePacketType;
+import
org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.junit.jupiter.api.Test;
-import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoInteractions;
class PostgreSQLEmptyQueryResponsePacketTest {
@Test
- void assertIdentifier() {
- PostgreSQLIdentifierTag actual = new
PostgreSQLEmptyQueryResponsePacket().getIdentifier();
- assertThat(actual,
is(PostgreSQLMessagePacketType.EMPTY_QUERY_RESPONSE));
+ void assertWrite() {
+ PostgreSQLPacketPayload payload = mock(PostgreSQLPacketPayload.class);
+ new PostgreSQLEmptyQueryResponsePacket().write((PacketPayload)
payload);
+ verifyNoInteractions(payload);
+ }
+
+ @Test
+ void assertGetIdentifier() {
+ assertThat(new PostgreSQLEmptyQueryResponsePacket().getIdentifier(),
is(PostgreSQLMessagePacketType.EMPTY_QUERY_RESPONSE));
}
}