You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/03/14 02:05:33 UTC
(shardingsphere) branch master updated: Add more test cases on MySQLTimeBinaryProtocolValueTest (#38417)
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 ef2e5314d14 Add more test cases on MySQLTimeBinaryProtocolValueTest (#38417)
ef2e5314d14 is described below
commit ef2e5314d14f0f8f74dfce98ea46c2539791ed1b
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Mar 14 10:05:26 2026 +0800
Add more test cases on MySQLTimeBinaryProtocolValueTest (#38417)
---
.../protocol/MySQLTimeBinaryProtocolValueTest.java | 113 ++++++++++++---------
1 file changed, 63 insertions(+), 50 deletions(-)
diff --git a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLTimeBinaryProtocolValueTest.java b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLTimeBinaryProtocolValueTest.java
index 41f9e93a8b2..8057e8f4d77 100644
--- a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLTimeBinaryProtocolValueTest.java
+++ b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/command/query/binary/execute/protocol/MySQLTimeBinaryProtocolValueTest.java
@@ -20,6 +20,10 @@ package org.apache.shardingsphere.database.protocol.mysql.packet.command.query.b
import org.apache.shardingsphere.database.protocol.mysql.payload.MySQLPacketPayload;
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.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@@ -27,82 +31,91 @@ import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Time;
import java.sql.Timestamp;
-import java.util.Calendar;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.ZoneId;
+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.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class MySQLTimeBinaryProtocolValueTest {
+ private final MySQLTimeBinaryProtocolValue binaryProtocolValue = new MySQLTimeBinaryProtocolValue();
+
@Mock
private MySQLPacketPayload payload;
- @Test
- void assertReadWithZeroByte() throws SQLException {
- assertThat(new MySQLTimeBinaryProtocolValue().read(payload, false), is(new Timestamp(0L)));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("readArguments")
+ void assertRead(final String name, final int[] int1Values, final int[] int4Values, final Timestamp expected) throws SQLException {
+ when(payload.readInt1()).thenReturn(int1Values[0], int1Values[1], int1Values[2], int1Values[3], int1Values[4]);
+ when(payload.readInt4()).thenReturn(int4Values[0], int4Values[1]);
+ assertThat(binaryProtocolValue.read(payload, false), is(expected));
}
@Test
- void assertReadWithEightBytes() throws SQLException {
- when(payload.readInt1()).thenReturn(8, 0, 10, 59, 0);
- Calendar actual = Calendar.getInstance();
- actual.setTimeInMillis(((Timestamp) new MySQLTimeBinaryProtocolValue().read(payload, false)).getTime());
- assertThat(actual.get(Calendar.HOUR_OF_DAY), is(10));
- assertThat(actual.get(Calendar.MINUTE), is(59));
- assertThat(actual.get(Calendar.SECOND), is(0));
+ void assertReadWithUnsupportedLength() {
+ when(payload.readInt1()).thenReturn(100, 0);
+ when(payload.readInt4()).thenReturn(0);
+ SQLFeatureNotSupportedException actual = assertThrows(SQLFeatureNotSupportedException.class, () -> binaryProtocolValue.read(payload, false));
+ assertThat(actual.getMessage(), is("Wrong length `100` of MYSQL_TYPE_DATE"));
}
- @Test
- void assertReadWithTwelveBytes() throws SQLException {
- when(payload.readInt1()).thenReturn(12, 0, 10, 59, 0);
- Calendar actual = Calendar.getInstance();
- actual.setTimeInMillis(((Timestamp) new MySQLTimeBinaryProtocolValue().read(payload, false)).getTime());
- assertThat(actual.get(Calendar.HOUR_OF_DAY), is(10));
- assertThat(actual.get(Calendar.MINUTE), is(59));
- assertThat(actual.get(Calendar.SECOND), is(0));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("writeArguments")
+ void assertWrite(final String name, final Time value, final List<Integer> expectedInt1Values, final List<Integer> expectedInt4Values) {
+ binaryProtocolValue.write(payload, value);
+ ArgumentCaptor<Integer> int1Captor = ArgumentCaptor.forClass(Integer.class);
+ verify(payload, times(expectedInt1Values.size())).writeInt1(int1Captor.capture());
+ List<Integer> actualInt1Values = int1Captor.getAllValues();
+ assertThat(actualInt1Values, is(expectedInt1Values));
+ if (expectedInt4Values.isEmpty()) {
+ verify(payload, never()).writeInt4(anyInt());
+ } else {
+ ArgumentCaptor<Integer> int4Captor = ArgumentCaptor.forClass(Integer.class);
+ verify(payload, times(expectedInt4Values.size())).writeInt4(int4Captor.capture());
+ List<Integer> actualInt4Values = int4Captor.getAllValues();
+ assertThat(actualInt4Values, is(expectedInt4Values));
+ }
+ verifyNoMoreInteractions(payload);
}
- @Test
- void assertReadWithIllegalArgument() {
- when(payload.readInt1()).thenReturn(100);
- assertThrows(SQLFeatureNotSupportedException.class, () -> new MySQLTimeBinaryProtocolValue().read(payload, false));
+ private static Stream<Arguments> readArguments() {
+ return Stream.of(
+ Arguments.of("zero_length", new int[]{0, 0, 0, 0, 0}, new int[]{0, 0}, new Timestamp(0L)),
+ Arguments.of("eight_bytes", new int[]{8, 0, 10, 59, 0}, new int[]{0, 0}, createTimestamp(0)),
+ Arguments.of("twelve_bytes", new int[]{12, 0, 10, 59, 0}, new int[]{0, 1000}, createTimestamp(1000)));
}
- @Test
- void assertWriteWithZeroByte() {
- MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();
- actual.write(payload, Time.valueOf("00:00:00"));
- verify(payload).writeInt1(0);
+ private static Stream<Arguments> writeArguments() {
+ return Stream.of(
+ Arguments.of("time_and_nanos_absent", Time.valueOf(LocalTime.of(0, 0)), Collections.singletonList(0), Collections.emptyList()),
+ Arguments.of("hour_present_without_nanos", Time.valueOf(LocalTime.of(1, 30, 10)), Arrays.asList(8, 0, 1, 30, 10), Collections.singletonList(0)),
+ Arguments.of("minutes_present_without_nanos", Time.valueOf(LocalTime.of(0, 30)), Arrays.asList(8, 0, 0, 30, 0), Collections.singletonList(0)),
+ Arguments.of("seconds_present_without_nanos", Time.valueOf(LocalTime.of(0, 0, 10)), Arrays.asList(8, 0, 0, 0, 10), Collections.singletonList(0)),
+ Arguments.of("nanos_only", createTimeAtLocalDateStart(), Arrays.asList(12, 0, 0, 0, 0), Arrays.asList(0, 1000)));
}
- @Test
- void assertWriteWithEightBytes() {
- MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();
- actual.write(payload, Time.valueOf("01:30:10"));
- verify(payload).writeInt1(8);
- verify(payload).writeInt1(0);
- verify(payload).writeInt4(0);
- payload.writeInt1(0);
- payload.writeInt4(0);
- verify(payload).writeInt1(1);
- verify(payload).writeInt1(30);
- verify(payload).writeInt1(10);
+ private static Timestamp createTimestamp(final int nanos) {
+ Timestamp result = Timestamp.valueOf(LocalDateTime.of(0, 1, 1, 10, 59, 0));
+ result.setNanos(nanos);
+ return result;
}
- @Test
- void assertWriteWithTwelveBytes() {
- MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();
- actual.write(payload, new Time(1L));
- verify(payload, atLeastOnce()).writeInt1(12);
- verify(payload, times(5)).writeInt1(anyInt());
- verify(payload).writeInt4(0);
- verify(payload).writeInt4(1000);
+ private static Time createTimeAtLocalDateStart() {
+ return new Time(LocalDate.of(1970, 1, 1).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() + 1);
}
}