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 4f3e6ad035a Add more test cases on statement.execute.protocol package
(#38173)
4f3e6ad035a is described below
commit 4f3e6ad035a447b7611199274deb94e6e92aa126
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Feb 24 01:02:15 2026 +0800
Add more test cases on statement.execute.protocol package (#38173)
* Add more test cases on statement.execute.protocol package
* Add more test cases on statement.execute.protocol package
---
.../FirebirdBlobBinaryProtocolValueTest.java | 185 +++++++++++++++++++++
.../FirebirdByteBinaryProtocolValueTest.java | 14 ++
.../FirebirdDateBinaryProtocolValueTest.java | 16 +-
.../FirebirdDoubleBinaryProtocolValueTest.java | 5 +
.../FirebirdFloatBinaryProtocolValueTest.java | 5 +
.../FirebirdInt16BinaryProtocolValueTest.java | 41 +++--
.../FirebirdInt1BinaryProtocolValueTest.java | 5 +
.../FirebirdInt2BinaryProtocolValueTest.java | 5 +
.../FirebirdInt4BinaryProtocolValueTest.java | 32 ++--
.../FirebirdInt8BinaryProtocolValueTest.java | 39 ++---
.../FirebirdNullBinaryProtocolValueTest.java | 7 +
.../FirebirdStringBinaryProtocolValueTest.java | 46 +++--
.../FirebirdTimeBinaryProtocolValueTest.java | 5 +
.../FirebirdTimestampBinaryProtocolValueTest.java | 17 +-
...FirebirdTimestampTZBinaryProtocolValueTest.java | 5 +
15 files changed, 364 insertions(+), 63 deletions(-)
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdBlobBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdBlobBinaryProtocolValueTest.java
new file mode 100644
index 00000000000..fc355df77c8
--- /dev/null
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdBlobBinaryProtocolValueTest.java
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.database.protocol.firebird.packet.command.query.statement.execute.protocol;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import lombok.SneakyThrows;
+import
org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
+import org.junit.jupiter.api.AfterEach;
+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 org.mockito.internal.configuration.plugins.Plugins;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class FirebirdBlobBinaryProtocolValueTest {
+
+ @AfterEach
+ void clearStore() {
+ getStore().clear();
+ }
+
+ @Test
+ void assertGetBlobContent() {
+ getStore().put(1L, new byte[]{1, 2});
+ assertArrayEquals(new byte[]{1, 2},
FirebirdBlobBinaryProtocolValue.getBlobContent(1L));
+ }
+
+ @Test
+ void assertRemoveBlobContent() {
+ getStore().put(2L, new byte[]{3});
+ FirebirdBlobBinaryProtocolValue.removeBlobContent(2L);
+ assertNull(getStore().get(2L));
+ }
+
+ @Test
+ void assertRead() {
+ ByteBuf byteBuf = Unpooled.buffer();
+ byteBuf.writeInt(3);
+ byteBuf.writeBytes(new byte[]{65, 66, 67});
+ byteBuf.writeByte(0);
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ assertThat(new FirebirdBlobBinaryProtocolValue().read(payload),
is("ABC"));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("writeRegisterArguments")
+ void assertWriteWithRegister(final String name, final Object value, final
byte[] expected) {
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ new FirebirdBlobBinaryProtocolValue().write(payload, value);
+ long blobId = byteBuf.getLong(0);
+ assertTrue(blobId > 0);
+ assertArrayEquals(expected, getStore().get(blobId));
+ }
+
+ @Test
+ void assertWriteWithNull() {
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ new FirebirdBlobBinaryProtocolValue().write(payload, null);
+ assertThat(byteBuf.getLong(0), is(0L));
+ }
+
+ @Test
+ void assertWriteWithBlobId() {
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ new FirebirdBlobBinaryProtocolValue().write(payload, 7L);
+ assertThat(byteBuf.getLong(0), is(7L));
+ }
+
+ @Test
+ void assertWriteWithBlob() throws SQLException, IOException {
+ Blob blob = mock(Blob.class);
+ InputStream inputStream = mock(InputStream.class);
+ when(inputStream.read(any(byte[].class))).thenReturn(0, 2, -1);
+ when(blob.getBinaryStream()).thenReturn(inputStream);
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ new FirebirdBlobBinaryProtocolValue().write(payload, blob);
+ long blobId = byteBuf.getLong(0);
+ assertTrue(blobId > 0);
+ assertArrayEquals(new byte[]{0, 0}, getStore().get(blobId));
+ }
+
+ @Test
+ void assertWriteWithBlobSQLException() throws SQLException {
+ Blob blob = mock(Blob.class);
+ when(blob.getBinaryStream()).thenThrow(new SQLException("failed"));
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ IllegalStateException actual =
assertThrows(IllegalStateException.class, () -> new
FirebirdBlobBinaryProtocolValue().write(payload, blob));
+ assertThat(actual.getMessage(), is("Failed to read java.sql.Blob
stream"));
+ }
+
+ @Test
+ void assertWriteWithBlobIOException() throws SQLException, IOException {
+ Blob blob = mock(Blob.class);
+ InputStream inputStream = mock(InputStream.class);
+ when(inputStream.read(any(byte[].class))).thenThrow(new
IOException("failed"));
+ when(blob.getBinaryStream()).thenReturn(inputStream);
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ IllegalStateException actual =
assertThrows(IllegalStateException.class, () -> new
FirebirdBlobBinaryProtocolValue().write(payload, blob));
+ assertThat(actual.getMessage(), is("Failed to read java.sql.Blob
content"));
+ }
+
+ @Test
+ void assertWriteWithClob() throws SQLException {
+ Clob clob = mock(Clob.class);
+ when(clob.length()).thenReturn(3L);
+ when(clob.getSubString(1L, 3)).thenReturn("xyz");
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ new FirebirdBlobBinaryProtocolValue().write(payload, clob);
+ long blobId = byteBuf.getLong(0);
+ assertTrue(blobId > 0);
+ assertArrayEquals(new byte[]{120, 121, 122}, getStore().get(blobId));
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ @SuppressWarnings("unchecked")
+ private static Map<Long, byte[]> getStore() {
+ return (Map<Long, byte[]>)
Plugins.getMemberAccessor().get(FirebirdBlobBinaryProtocolValue.class.getDeclaredField("STORE"),
FirebirdBlobBinaryProtocolValue.class);
+ }
+
+ @Test
+ void assertWriteWithClobSQLException() throws SQLException {
+ Clob clob = mock(Clob.class);
+ when(clob.length()).thenThrow(new SQLException("failed"));
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ IllegalStateException actual =
assertThrows(IllegalStateException.class, () -> new
FirebirdBlobBinaryProtocolValue().write(payload, clob));
+ assertThat(actual.getMessage(), is("Failed to read java.sql.Clob"));
+ }
+
+ @Test
+ void assertGetLength() {
+ ByteBuf byteBuf = Unpooled.buffer();
+ FirebirdPacketPayload payload = new FirebirdPacketPayload(byteBuf,
StandardCharsets.UTF_8);
+ assertThat(new FirebirdBlobBinaryProtocolValue().getLength(payload),
is(8));
+ }
+
+ private static Stream<Arguments> writeRegisterArguments() {
+ return Stream.of(
+ Arguments.of("byte array", new byte[]{1, 2}, new byte[]{1, 2}),
+ Arguments.of("string", "bar", new byte[]{98, 97, 114}),
+ Arguments.of("string builder", new StringBuilder("baz"), new
byte[]{98, 97, 122}));
+ }
+}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdByteBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdByteBinaryProtocolValueTest.java
index 0019866830f..312df00a5e1 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdByteBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdByteBinaryProtocolValueTest.java
@@ -17,6 +17,7 @@
package
org.apache.shardingsphere.database.protocol.firebird.packet.command.query.statement.execute.protocol;
+import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import
org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
import org.junit.jupiter.api.Test;
@@ -24,6 +25,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -34,6 +37,9 @@ class FirebirdByteBinaryProtocolValueTest {
@Mock
private FirebirdPacketPayload payload;
+ @Mock
+ private ByteBuf byteBuf;
+
@Test
void assertRead() {
byte[] expected = {1, 2};
@@ -53,4 +59,12 @@ class FirebirdByteBinaryProtocolValueTest {
new FirebirdByteBinaryProtocolValue().write(payload, bytes);
verify(payload).writeBuffer(bytes);
}
+
+ @Test
+ void assertGetLength() {
+ when(payload.getByteBuf()).thenReturn(byteBuf);
+ when(byteBuf.readerIndex()).thenReturn(2);
+ when(payload.getBufferLength(2)).thenReturn(8);
+ assertThat(new FirebirdByteBinaryProtocolValue().getLength(payload),
is(8));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDateBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDateBinaryProtocolValueTest.java
index 9592c1fa73f..674fe0b4c78 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDateBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDateBinaryProtocolValueTest.java
@@ -40,16 +40,28 @@ class FirebirdDateBinaryProtocolValueTest {
@Test
void assertRead() {
- LocalDateTime date = LocalDateTime.of(2024, 1, 1, 0, 0);
+ LocalDateTime date = LocalDateTime.parse("2024-01-01T00:00:00");
int encoded = FirebirdDateTimeUtils.getEncodedDate(date);
when(payload.readInt4()).thenReturn(encoded);
assertThat(new FirebirdDateBinaryProtocolValue().read(payload),
is(FirebirdDateTimeUtils.getDate(encoded)));
}
@Test
- void assertWrite() {
+ void assertWriteWithDate() {
LocalDateTime date = LocalDateTime.of(2024, 1, 1, 0, 0);
new FirebirdDateBinaryProtocolValue().write(payload,
Timestamp.valueOf(date));
verify(payload).writeInt4(FirebirdDateTimeUtils.getEncodedDate(date));
}
+
+ @Test
+ void assertWriteWithLocalDateTime() {
+ LocalDateTime date = LocalDateTime.parse("2024-01-02T00:00:00");
+ new FirebirdDateBinaryProtocolValue().write(payload, date);
+ verify(payload).writeInt4(FirebirdDateTimeUtils.getEncodedDate(date));
+ }
+
+ @Test
+ void assertGetLength() {
+ assertThat(new FirebirdDateBinaryProtocolValue().getLength(payload),
is(4));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDoubleBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDoubleBinaryProtocolValueTest.java
index 72c9c52eed9..5b7b12b0e41 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDoubleBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdDoubleBinaryProtocolValueTest.java
@@ -48,4 +48,9 @@ class FirebirdDoubleBinaryProtocolValueTest {
new FirebirdDoubleBinaryProtocolValue().write(new
FirebirdPacketPayload(byteBuf, StandardCharsets.UTF_8), 1.0D);
verify(byteBuf).writeDouble(1.0D);
}
+
+ @Test
+ void assertGetLength() {
+ assertThat(new FirebirdDoubleBinaryProtocolValue().getLength(new
FirebirdPacketPayload(byteBuf, StandardCharsets.UTF_8)), is(8));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdFloatBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdFloatBinaryProtocolValueTest.java
index ff0394aa399..bf00ec7dc94 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdFloatBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdFloatBinaryProtocolValueTest.java
@@ -48,4 +48,9 @@ class FirebirdFloatBinaryProtocolValueTest {
new FirebirdFloatBinaryProtocolValue().write(new
FirebirdPacketPayload(byteBuf, StandardCharsets.UTF_8), 1.0F);
verify(byteBuf).writeFloat(1.0F);
}
+
+ @Test
+ void assertGetLength() {
+ assertThat(new FirebirdFloatBinaryProtocolValue().getLength(new
FirebirdPacketPayload(byteBuf, StandardCharsets.UTF_8)), is(4));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt16BinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt16BinaryProtocolValueTest.java
index 005e55b0ebd..647a928a525 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt16BinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt16BinaryProtocolValueTest.java
@@ -18,17 +18,24 @@
package
org.apache.shardingsphere.database.protocol.firebird.packet.command.query.statement.execute.protocol;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import
org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
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 java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Stream;
-import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -50,19 +57,27 @@ class FirebirdInt16BinaryProtocolValueTest {
assertThat(new FirebirdInt16BinaryProtocolValue().read(payload),
is(result));
}
- @Test
- void assertWriteWithInteger() {
- when(payload.getByteBuf()).thenReturn(byteBuf);
- new FirebirdInt16BinaryProtocolValue().write(payload, 1);
- verify(byteBuf).writeZero(12);
- verify(payload).writeInt4(1);
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("writeArguments")
+ void assertWrite(final String name, final Object value, final byte[]
expected) {
+ ByteBuf actualBuffer = Unpooled.buffer();
+ FirebirdPacketPayload actualPayload = new
FirebirdPacketPayload(actualBuffer, StandardCharsets.UTF_8);
+ new FirebirdInt16BinaryProtocolValue().write(actualPayload, value);
+ byte[] actual = new byte[16];
+ actualBuffer.getBytes(0, actual);
+ assertArrayEquals(expected, actual);
}
@Test
- void assertWriteWithBigDecimal() {
- when(payload.getByteBuf()).thenReturn(byteBuf);
- new FirebirdInt16BinaryProtocolValue().write(payload, BigDecimal.ONE);
- verify(byteBuf).writeZero(15);
- verify(byteBuf).writeBytes(new byte[]{1});
+ void assertGetLength() {
+ assertThat(new FirebirdInt16BinaryProtocolValue().getLength(payload),
is(16));
+ }
+
+ private static Stream<Arguments> writeArguments() {
+ return Stream.of(
+ Arguments.of("big decimal", BigDecimal.valueOf(10L), new
byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10}),
+ Arguments.of("integer", 513, new byte[]{0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2, 1}),
+ Arguments.of("big integer", BigInteger.valueOf(66051L), new
byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3}),
+ Arguments.of("long", 4294967298L, new byte[]{0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 2}));
}
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt1BinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt1BinaryProtocolValueTest.java
index b1b45ab67c2..08842f17a2b 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt1BinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt1BinaryProtocolValueTest.java
@@ -45,4 +45,9 @@ class FirebirdInt1BinaryProtocolValueTest {
new FirebirdInt1BinaryProtocolValue().write(payload, 1);
verify(payload).writeInt2(1);
}
+
+ @Test
+ void assertGetLength() {
+ assertThat(new FirebirdInt1BinaryProtocolValue().getLength(payload),
is(1));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt2BinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt2BinaryProtocolValueTest.java
index 89c235b639f..5b7a14553c0 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt2BinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt2BinaryProtocolValueTest.java
@@ -45,4 +45,9 @@ class FirebirdInt2BinaryProtocolValueTest {
new FirebirdInt2BinaryProtocolValue().write(payload, 1);
verify(payload).writeInt4(1);
}
+
+ @Test
+ void assertGetLength() {
+ assertThat(new FirebirdInt2BinaryProtocolValue().getLength(payload),
is(4));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt4BinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt4BinaryProtocolValueTest.java
index c52b842b6c3..ddb8005ca7c 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt4BinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt4BinaryProtocolValueTest.java
@@ -20,11 +20,17 @@ package
org.apache.shardingsphere.database.protocol.firebird.packet.command.quer
import
org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
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 java.math.BigDecimal;
+import java.util.stream.Stream;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -37,25 +43,25 @@ class FirebirdInt4BinaryProtocolValueTest {
@Test
void assertRead() {
when(payload.readInt4()).thenReturn(1);
- new FirebirdInt4BinaryProtocolValue().read(payload);
- verify(payload).readInt4();
+ assertThat(new FirebirdInt4BinaryProtocolValue().read(payload), is(1));
}
- @Test
- void assertWriteWithBigDecimal() {
- new FirebirdInt4BinaryProtocolValue().write(payload, BigDecimal.ONE);
- verify(payload).writeInt4(1);
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("writeArguments")
+ void assertWrite(final String name, final Object value, final int
expected) {
+ new FirebirdInt4BinaryProtocolValue().write(payload, value);
+ verify(payload).writeInt4(expected);
}
- @Test
- void assertWriteWithInteger() {
- new FirebirdInt4BinaryProtocolValue().write(payload, 1);
- verify(payload).writeInt4(1);
+ private static Stream<Arguments> writeArguments() {
+ return Stream.of(
+ Arguments.of("big decimal", BigDecimal.ONE, 1),
+ Arguments.of("integer", 1, 1),
+ Arguments.of("long", 1L, 1));
}
@Test
- void assertWriteWithLong() {
- new FirebirdInt4BinaryProtocolValue().write(payload, 1L);
- verify(payload).writeInt4(1);
+ void assertGetLength() {
+ assertThat(new FirebirdInt4BinaryProtocolValue().getLength(payload),
is(4));
}
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt8BinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt8BinaryProtocolValueTest.java
index cf4bae80cc9..b0a32c574d0 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt8BinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdInt8BinaryProtocolValueTest.java
@@ -20,12 +20,18 @@ package
org.apache.shardingsphere.database.protocol.firebird.packet.command.quer
import
org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
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 java.math.BigDecimal;
import java.math.BigInteger;
+import java.util.stream.Stream;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -38,31 +44,26 @@ class FirebirdInt8BinaryProtocolValueTest {
@Test
void assertRead() {
when(payload.readInt8()).thenReturn(1L);
- new FirebirdInt8BinaryProtocolValue().read(payload);
- verify(payload).readInt8();
+ assertThat(new FirebirdInt8BinaryProtocolValue().read(payload),
is(1L));
}
- @Test
- void assertWriteWithBigDecimal() {
- new FirebirdInt8BinaryProtocolValue().write(payload, BigDecimal.ONE);
- verify(payload).writeInt8(1L);
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("writeArguments")
+ void assertWrite(final String name, final Object value, final long
expected) {
+ new FirebirdInt8BinaryProtocolValue().write(payload, value);
+ verify(payload).writeInt8(expected);
}
- @Test
- void assertWriteWithInteger() {
- new FirebirdInt8BinaryProtocolValue().write(payload, 1);
- verify(payload).writeInt8(1L);
- }
-
- @Test
- void assertWriteWithBigInteger() {
- new FirebirdInt8BinaryProtocolValue().write(payload, BigInteger.ONE);
- verify(payload).writeInt8(1L);
+ private static Stream<Arguments> writeArguments() {
+ return Stream.of(
+ Arguments.of("big decimal", BigDecimal.ONE, 1L),
+ Arguments.of("integer", 1, 1L),
+ Arguments.of("big integer", BigInteger.ONE, 1L),
+ Arguments.of("long", 1L, 1L));
}
@Test
- void assertWriteWithLong() {
- new FirebirdInt8BinaryProtocolValue().write(payload, 1L);
- verify(payload).writeInt8(1L);
+ void assertGetLength() {
+ assertThat(new FirebirdInt8BinaryProtocolValue().getLength(payload),
is(8));
}
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdNullBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdNullBinaryProtocolValueTest.java
index f97c944519b..00dca27f457 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdNullBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdNullBinaryProtocolValueTest.java
@@ -23,6 +23,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.verify;
@@ -42,4 +44,9 @@ class FirebirdNullBinaryProtocolValueTest {
new FirebirdNullBinaryProtocolValue().write(payload, null);
verify(payload).writeBuffer(new byte[0]);
}
+
+ @Test
+ void assertGetLength() {
+ assertThat(new FirebirdNullBinaryProtocolValue().getLength(payload),
is(0));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdStringBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdStringBinaryProtocolValueTest.java
index 830b61bbf4a..03bbbe99bd2 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdStringBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdStringBinaryProtocolValueTest.java
@@ -17,15 +17,23 @@
package
org.apache.shardingsphere.database.protocol.firebird.packet.command.query.statement.execute.protocol;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import
org.apache.shardingsphere.database.protocol.firebird.payload.FirebirdPacketPayload;
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 static org.hamcrest.Matchers.is;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Stream;
+
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -34,28 +42,38 @@ class FirebirdStringBinaryProtocolValueTest {
@Mock
private FirebirdPacketPayload payload;
+ @Mock
+ private ByteBuf byteBuf;
+
@Test
void assertRead() {
when(payload.readString()).thenReturn("foo");
assertThat(new FirebirdStringBinaryProtocolValue().read(payload),
is("foo"));
}
- @Test
- void assertWriteWithBytes() {
- byte[] bytes = {1};
- new FirebirdStringBinaryProtocolValue().write(payload, bytes);
- verify(payload).writeBuffer(bytes);
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("writeArguments")
+ void assertWrite(final String name, final Object value, final byte[]
expected) {
+ ByteBuf actualBuffer = Unpooled.buffer();
+ FirebirdPacketPayload actualPayload = new
FirebirdPacketPayload(actualBuffer, StandardCharsets.UTF_8);
+ new FirebirdStringBinaryProtocolValue().write(actualPayload, value);
+ byte[] actual = new byte[actualBuffer.writerIndex()];
+ actualBuffer.getBytes(0, actual);
+ assertArrayEquals(expected, actual);
}
@Test
- void assertWriteWithString() {
- new FirebirdStringBinaryProtocolValue().write(payload, "bar");
- verify(payload).writeString("bar");
+ void assertGetLength() {
+ when(payload.getByteBuf()).thenReturn(byteBuf);
+ when(byteBuf.readerIndex()).thenReturn(2);
+ when(payload.getBufferLength(2)).thenReturn(6);
+ assertThat(new FirebirdStringBinaryProtocolValue().getLength(payload),
is(6));
}
- @Test
- void assertWriteWithNull() {
- new FirebirdStringBinaryProtocolValue().write(payload, null);
- verify(payload).writeString("");
+ private static Stream<Arguments> writeArguments() {
+ return Stream.of(
+ Arguments.of("byte array", new byte[]{1}, new byte[]{0, 0, 0,
1, 1, 0, 0, 0}),
+ Arguments.of("string", "bar", new byte[]{0, 0, 0, 3, 98, 97,
114, 0}),
+ Arguments.of("null", null, new byte[]{0, 0, 0, 0}));
}
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimeBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimeBinaryProtocolValueTest.java
index e061958e085..5870c3828c3 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimeBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimeBinaryProtocolValueTest.java
@@ -57,4 +57,9 @@ class FirebirdTimeBinaryProtocolValueTest {
new FirebirdTimeBinaryProtocolValue().write(payload, time);
verify(payload).writeInt4(encoded);
}
+
+ @Test
+ void assertGetLength() {
+ assertThat(new FirebirdTimeBinaryProtocolValue().getLength(payload),
is(4));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampBinaryProtocolValueTest.java
index 5183983e83e..9a686501cfd 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampBinaryProtocolValueTest.java
@@ -40,7 +40,7 @@ class FirebirdTimestampBinaryProtocolValueTest {
@Test
void assertRead() {
- LocalDateTime dateTime = LocalDateTime.of(2024, 1, 1, 12, 0);
+ LocalDateTime dateTime = LocalDateTime.parse("2024-01-01T12:00:00");
int encodedDate = FirebirdDateTimeUtils.getEncodedDate(dateTime);
int encodedTime = new FirebirdDateTimeUtils(dateTime).getEncodedTime();
when(payload.readInt4()).thenReturn(encodedDate, encodedTime);
@@ -48,10 +48,23 @@ class FirebirdTimestampBinaryProtocolValueTest {
}
@Test
- void assertWrite() {
+ void assertWriteWithDate() {
LocalDateTime dateTime = LocalDateTime.of(2024, 1, 1, 12, 0);
new FirebirdTimestampBinaryProtocolValue().write(payload,
Timestamp.valueOf(dateTime));
verify(payload).writeInt4(FirebirdDateTimeUtils.getEncodedDate(dateTime));
verify(payload).writeInt4(new
FirebirdDateTimeUtils(dateTime).getEncodedTime());
}
+
+ @Test
+ void assertWriteWithLocalDateTime() {
+ LocalDateTime dateTime = LocalDateTime.parse("2024-01-02T13:00:00");
+ new FirebirdTimestampBinaryProtocolValue().write(payload, dateTime);
+
verify(payload).writeInt4(FirebirdDateTimeUtils.getEncodedDate(dateTime));
+ verify(payload).writeInt4(new
FirebirdDateTimeUtils(dateTime).getEncodedTime());
+ }
+
+ @Test
+ void assertGetLength() {
+ assertThat(new
FirebirdTimestampBinaryProtocolValue().getLength(payload), is(8));
+ }
}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampTZBinaryProtocolValueTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampTZBinaryProtocolValueTest.java
index 7f431cfa3e4..40bf378e1c6 100644
---
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampTZBinaryProtocolValueTest.java
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/FirebirdTimestampTZBinaryProtocolValueTest.java
@@ -59,4 +59,9 @@ class FirebirdTimestampTZBinaryProtocolValueTest {
verify(payload).writeInt4(encodedTime);
verify(payload).writeInt4(120);
}
+
+ @Test
+ void assertGetLength() {
+ assertThat(new
FirebirdTimestampTZBinaryProtocolValue().getLength(payload), is(12));
+ }
}