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 0d9997e5f44 Cover firebird protocol/date utils and parameter buffer
branches (#37597)
0d9997e5f44 is described below
commit 0d9997e5f44fef33e9a54751edf4eb036eeab77d
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Jan 1 11:51:16 2026 +0800
Cover firebird protocol/date utils and parameter buffer branches (#37597)
---
.../buffer/FirebirdParameterBufferTest.java | 117 ++++++++++++++++++++
.../protocol/util/FirebirdDateTimeUtilsTest.java | 118 +++++++++++++++++++++
2 files changed, 235 insertions(+)
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/constant/buffer/FirebirdParameterBufferTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/constant/buffer/FirebirdParameterBufferTest.java
new file mode 100644
index 00000000000..f8444a3e4e5
--- /dev/null
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/constant/buffer/FirebirdParameterBufferTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.constant.buffer;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import
org.apache.shardingsphere.database.protocol.firebird.constant.FirebirdValueFormat;
+import
org.apache.shardingsphere.database.protocol.firebird.exception.FirebirdProtocolException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class FirebirdParameterBufferTest {
+
+ @Mock
+ private FirebirdParameterBufferType intType;
+
+ @Mock
+ private FirebirdParameterBufferType booleanType;
+
+ @Mock
+ private FirebirdParameterBufferType stringType;
+
+ @Mock
+ private FirebirdParameterBufferType unsupportedType;
+
+ @Test
+ void assertParseTraditionalBuffer() {
+ when(intType.getFormat()).thenReturn(FirebirdValueFormat.INT);
+ when(booleanType.getFormat()).thenReturn(FirebirdValueFormat.BOOLEAN);
+ when(stringType.getFormat()).thenReturn(FirebirdValueFormat.STRING);
+ ByteBuf buffer = Unpooled.buffer();
+ buffer.writeByte(1);
+ buffer.writeByte(1);
+ buffer.writeByte(0);
+ buffer.writeIntLE(123);
+ buffer.writeByte(2);
+ buffer.writeByte(3);
+ String expectedString = "sharding";
+ buffer.writeByte(expectedString.length());
+ buffer.writeCharSequence(expectedString, StandardCharsets.UTF_8);
+ FirebirdParameterBuffer parameterBuffer = new
FirebirdParameterBuffer(createValueOf(), version -> version == 1);
+ parameterBuffer.parseBuffer(buffer);
+ assertThat(parameterBuffer.getVersion(), is(1));
+ assertThat(parameterBuffer.<Integer>getValue(intType), is(123));
+ boolean actualBoolean = parameterBuffer.getValue(booleanType);
+ assertTrue(actualBoolean);
+ assertThat(parameterBuffer.getValue(stringType), is(expectedString));
+ }
+
+ @Test
+ void assertParseExtendedBuffer() {
+ when(intType.getFormat()).thenReturn(FirebirdValueFormat.INT);
+ when(stringType.getFormat()).thenReturn(FirebirdValueFormat.STRING);
+ ByteBuf buffer = Unpooled.buffer();
+ buffer.writeByte(2);
+ buffer.writeByte(1);
+ buffer.writeIntLE(0);
+ buffer.writeIntLE(789);
+ buffer.writeByte(3);
+ String expectedString = "firebird";
+ buffer.writeIntLE(expectedString.length());
+ buffer.writeCharSequence(expectedString, StandardCharsets.UTF_8);
+ FirebirdParameterBuffer parameterBuffer = new
FirebirdParameterBuffer(createValueOf(), version -> version == 1);
+ parameterBuffer.parseBuffer(buffer);
+ assertThat(parameterBuffer.getVersion(), is(2));
+ assertThat(parameterBuffer.<Integer>getValue(intType), is(789));
+ assertThat(parameterBuffer.getValue(stringType), is(expectedString));
+ }
+
+ @Test
+ void assertParseUnsupportedFormat() {
+
when(unsupportedType.getFormat()).thenReturn(FirebirdValueFormat.BINARY);
+ ByteBuf buffer = Unpooled.buffer();
+ buffer.writeByte(1);
+ buffer.writeByte(4);
+ FirebirdParameterBuffer parameterBuffer = new
FirebirdParameterBuffer(createValueOf(), version -> version == 1);
+ assertThrows(FirebirdProtocolException.class, () ->
parameterBuffer.parseBuffer(buffer));
+ }
+
+ private Function<Integer, FirebirdParameterBufferType> createValueOf() {
+ Map<Integer, FirebirdParameterBufferType> mapping = new HashMap<>(4,
1F);
+ mapping.put(1, intType);
+ mapping.put(2, booleanType);
+ mapping.put(3, stringType);
+ mapping.put(4, unsupportedType);
+ return mapping::get;
+ }
+}
diff --git
a/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/util/FirebirdDateTimeUtilsTest.java
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/util/FirebirdDateTimeUtilsTest.java
new file mode 100644
index 00000000000..8c1befea22c
--- /dev/null
+++
b/database/protocol/dialect/firebird/src/test/java/org/apache/shardingsphere/database/protocol/firebird/packet/command/query/statement/execute/protocol/util/FirebirdDateTimeUtilsTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class FirebirdDateTimeUtilsTest {
+
+ @Test
+ void assertEncodeAndDecodeDateAfterFebruary() {
+ LocalDateTime sourceDateTime = LocalDateTime.of(2024, 4, 15, 10, 20,
30);
+ FirebirdDateTimeUtils utils = new
FirebirdDateTimeUtils(sourceDateTime);
+ int actualEncodedDate = utils.getEncodedDate();
+ assertThat(actualEncodedDate, is(60415));
+ FirebirdDateTimeUtils decoded = new
FirebirdDateTimeUtils().setDate(actualEncodedDate);
+ assertThat(decoded.getYear(), is(2024));
+ assertThat(decoded.getMonth(), is(4));
+ assertThat(decoded.getDay(), is(6));
+ }
+
+ @Test
+ void assertEncodeAndDecodeDateBeforeMarch() {
+ LocalDateTime sourceDateTime = LocalDateTime.of(2024, 2, 1, 8, 0);
+ int actualEncodedDate =
FirebirdDateTimeUtils.getEncodedDate(sourceDateTime);
+ assertThat(actualEncodedDate, is(60341));
+ FirebirdDateTimeUtils decoded = new
FirebirdDateTimeUtils().setDate(actualEncodedDate);
+ assertThat(decoded.getYear(), is(2023));
+ assertThat(decoded.getMonth(), is(11));
+ assertThat(decoded.getDay(), is(25));
+ }
+
+ @Test
+ void assertGetEncodedTimeAndSetTime() {
+ LocalDateTime sourceDateTime = LocalDateTime.of(2024, 6, 30, 1, 2, 3,
500_000_000);
+ FirebirdDateTimeUtils utils = new
FirebirdDateTimeUtils(sourceDateTime);
+ int expectedFractions = (sourceDateTime.getNano() /
FirebirdDateTimeUtils.NANOSECONDS_PER_FRACTION)
+ % FirebirdDateTimeUtils.FRACTIONS_PER_SECOND;
+ int expectedEncodedTime = sourceDateTime.getHour() *
FirebirdDateTimeUtils.FRACTIONS_PER_HOUR
+ + sourceDateTime.getMinute() *
FirebirdDateTimeUtils.FRACTIONS_PER_MINUTE
+ + sourceDateTime.getSecond() *
FirebirdDateTimeUtils.FRACTIONS_PER_SECOND
+ + expectedFractions;
+ int actualEncodedTime = utils.getEncodedTime();
+ assertThat(actualEncodedTime, is(expectedEncodedTime));
+ FirebirdDateTimeUtils decoded = new
FirebirdDateTimeUtils().setTime(actualEncodedTime);
+ assertThat(decoded.getHour(), is(sourceDateTime.getHour()));
+ assertThat(decoded.getMinute(), is(sourceDateTime.getMinute()));
+ assertThat(decoded.getSecond(), is(sourceDateTime.getSecond()));
+ assertThat(decoded.getFractions(), is(expectedFractions));
+ }
+
+ @Test
+ void assertGetDate() {
+ LocalDateTime sourceDateTime = LocalDateTime.of(2023, 7, 1, 0, 0);
+ int encodedDate = FirebirdDateTimeUtils.getEncodedDate(sourceDateTime);
+ Timestamp actualDate = FirebirdDateTimeUtils.getDate(encodedDate);
+ LocalDateTime expectedDateTime = LocalDateTime.of(2023, 6, 6, 0, 0);
+ assertThat(actualDate.toLocalDateTime(), is(expectedDateTime));
+ }
+
+ @Test
+ void assertGetTime() {
+ LocalDateTime sourceDateTime = LocalDateTime.of(2024, 1, 1, 5, 6, 7,
100_000);
+ int encodedTime = new
FirebirdDateTimeUtils(sourceDateTime).getEncodedTime();
+ Timestamp actualTime = FirebirdDateTimeUtils.getTime(encodedTime);
+ LocalDateTime expectedDateTime = LocalDateTime.of(1, 1, 1,
sourceDateTime.getHour(), sourceDateTime.getMinute(),
sourceDateTime.getSecond(),
+ (sourceDateTime.getNano() /
FirebirdDateTimeUtils.NANOSECONDS_PER_FRACTION) %
FirebirdDateTimeUtils.FRACTIONS_PER_SECOND);
+ assertThat(actualTime.toLocalDateTime(), is(expectedDateTime));
+ }
+
+ @Test
+ void assertGetDateTime() {
+ LocalDateTime sourceDateTime = LocalDateTime.of(2024, 12, 31, 23, 59,
58);
+ int encodedDate = FirebirdDateTimeUtils.getEncodedDate(sourceDateTime);
+ int encodedTime = new
FirebirdDateTimeUtils(sourceDateTime).getEncodedTime();
+ Timestamp actualDateTime =
FirebirdDateTimeUtils.getDateTime(encodedDate, encodedTime);
+ LocalDateTime expectedDateTime = LocalDateTime.of(2024, 10, 31,
sourceDateTime.getHour(), sourceDateTime.getMinute(),
sourceDateTime.getSecond(), 0);
+ assertThat(actualDateTime.toLocalDateTime(), is(expectedDateTime));
+ }
+
+ @Test
+ void assertGetDateTimeWithOffset() {
+ LocalDateTime sourceDateTime = LocalDateTime.of(2025, 1, 1, 9, 30, 0);
+ int encodedDate = FirebirdDateTimeUtils.getEncodedDate(sourceDateTime);
+ int encodedTime = new
FirebirdDateTimeUtils(sourceDateTime).getEncodedTime();
+ Timestamp actualDateTime =
FirebirdDateTimeUtils.getDateTimeWithOffset(encodedDate, encodedTime, 60);
+ LocalDateTime expectedDateTime = LocalDateTime.of(2024, 11, 1,
sourceDateTime.getHour(), sourceDateTime.getMinute(),
sourceDateTime.getSecond(), 0);
+ assertThat(actualDateTime.toLocalDateTime(), is(expectedDateTime));
+ }
+
+ @Test
+ void assertDecodeDateForJanFebBranch() {
+ FirebirdDateTimeUtils decoded = new
FirebirdDateTimeUtils().setDate(-1999715);
+ assertThat(decoded.getMonth(), is(1));
+ assertThat(decoded.getYear(), is(-3616));
+ assertThat(decoded.getDay(), is(-29));
+ }
+}