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 1d56cccba78 Add more test cases on 
MySQLBinlogFormatDescriptionEventPacketTest (#38177)
1d56cccba78 is described below

commit 1d56cccba787780726067b40a97b7fa43fec4227
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Feb 24 18:56:14 2026 +0800

    Add more test cases on MySQLBinlogFormatDescriptionEventPacketTest (#38177)
---
 .../MySQLBinlogFormatDescriptionEventPacket.java   |   1 -
 ...ySQLBinlogFormatDescriptionEventPacketTest.java | 102 ++++++++++++++++++---
 2 files changed, 90 insertions(+), 13 deletions(-)

diff --git 
a/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacket.java
 
b/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacket.java
index 46e6bffe431..762fd6908fd 100644
--- 
a/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacket.java
+++ 
b/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacket.java
@@ -76,6 +76,5 @@ public final class MySQLBinlogFormatDescriptionEventPacket 
extends AbstractMySQL
     
     @Override
     protected void writeEvent(final MySQLPacketPayload payload) {
-        // TODO
     }
 }
diff --git 
a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacketTest.java
 
b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacketTest.java
index c24bb82d822..39904102bd1 100644
--- 
a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacketTest.java
+++ 
b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/management/MySQLBinlogFormatDescriptionEventPacketTest.java
@@ -20,18 +20,43 @@ package 
org.apache.shardingsphere.database.protocol.mysql.packet.binlog.manageme
 import 
org.apache.shardingsphere.database.protocol.mysql.packet.binlog.MySQLBinlogEventHeader;
 import 
org.apache.shardingsphere.database.protocol.mysql.payload.MySQLPacketPayload;
 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.junit.jupiter.api.extension.ExtendWith;
 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.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(MockitoExtension.class)
 class MySQLBinlogFormatDescriptionEventPacketTest {
     
-    private static final String MYSQL_SERVER_VERSION = "5.7.14-log" + new 
String(new byte[40]);
+    private static final int BINLOG_VERSION = 4;
+    
+    private static final int CREATE_TIMESTAMP = 1234567890;
+    
+    private static final int EVENT_HEADER_LENGTH = 19;
+    
+    private static final int FORMAT_DESCRIPTION_EVENT_LENGTH = 95;
+    
+    private static final int TYPE_HEADER_LENGTH = 14;
+    
+    private static final int TYPE_HEADER_REMAIN_LENGTH = 23;
+    
+    private static final byte[] MYSQL_SERVER_VERSION = 
"5.7.14-log0000000000000000000000000000000000000000".getBytes(StandardCharsets.UTF_8);
+    
+    private static final String BINLOG_VERSION_ERROR_MESSAGE = "Binlog version 
of FORMAT_DESCRIPTION_EVENT should always 4";
+    
+    private static final String EVENT_HEADER_LENGTH_ERROR_MESSAGE = "Length of 
the Binlog Event Header should always be 19.";
     
     @Mock
     private MySQLPacketPayload payload;
@@ -39,17 +64,70 @@ class MySQLBinlogFormatDescriptionEventPacketTest {
     @Mock
     private MySQLBinlogEventHeader binlogEventHeader;
     
-    @Test
-    void assertNew() {
-        when(payload.readInt2()).thenReturn(4);
-        
when(payload.readStringFixByBytes(50)).thenReturn(MYSQL_SERVER_VERSION.getBytes());
-        when(payload.readInt4()).thenReturn(1234567890);
-        when(payload.readInt1()).thenReturn(19);
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("assertNewParameters")
+    void assertNew(final String name, final int checksumAlgorithmFlag, final 
int expectedChecksumSkipReservedCount) {
+        mockPayloadForValidCase(checksumAlgorithmFlag);
         MySQLBinlogFormatDescriptionEventPacket actual = new 
MySQLBinlogFormatDescriptionEventPacket(binlogEventHeader, payload);
-        assertThat(actual.getBinlogVersion(), is(4));
-        assertThat(actual.getMysqlServerVersion(), 
is(MYSQL_SERVER_VERSION.getBytes()));
-        assertThat(actual.getCreateTimestamp(), is(1234567890));
-        assertThat(actual.getEventHeaderLength(), is(19));
+        assertThat(actual.getBinlogVersion(), is(BINLOG_VERSION));
+        assertThat(actual.getMysqlServerVersion(), is(MYSQL_SERVER_VERSION));
+        assertThat(actual.getCreateTimestamp(), is(CREATE_TIMESTAMP));
+        assertThat(actual.getEventHeaderLength(), is(EVENT_HEADER_LENGTH));
         assertThat(actual.getBinlogEventHeader(), is(binlogEventHeader));
+        verify(payload).skipReserved(TYPE_HEADER_LENGTH);
+        verify(payload).skipReserved(TYPE_HEADER_REMAIN_LENGTH);
+        verify(payload, 
times(expectedChecksumSkipReservedCount)).skipReserved(4);
+    }
+    
+    @Test
+    void assertNewWithInvalidBinlogVersion() {
+        when(payload.readInt2()).thenReturn(3);
+        assertThat(assertThrows(IllegalArgumentException.class, () -> new 
MySQLBinlogFormatDescriptionEventPacket(binlogEventHeader, 
payload)).getMessage(), is(BINLOG_VERSION_ERROR_MESSAGE));
+    }
+    
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("assertNewWithInvalidEventHeaderLengthParameters")
+    void assertNewWithInvalidEventHeaderLength(final String name, final int 
invalidEventHeaderLength) {
+        when(payload.readInt2()).thenReturn(BINLOG_VERSION);
+        
when(payload.readStringFixByBytes(50)).thenReturn(MYSQL_SERVER_VERSION);
+        when(payload.readInt4()).thenReturn(CREATE_TIMESTAMP);
+        when(payload.readInt1()).thenReturn(invalidEventHeaderLength);
+        IllegalArgumentException actual = 
assertThrows(IllegalArgumentException.class, () -> new 
MySQLBinlogFormatDescriptionEventPacket(binlogEventHeader, payload));
+        assertThat(actual.getMessage(), is(EVENT_HEADER_LENGTH_ERROR_MESSAGE));
+    }
+    
+    @Test
+    void assertWrite() {
+        mockPayloadForValidCase(0);
+        MySQLBinlogEventHeader eventHeader = new MySQLBinlogEventHeader(1, 2, 
3, 4, 5, 6, 0);
+        MySQLBinlogFormatDescriptionEventPacket packet = new 
MySQLBinlogFormatDescriptionEventPacket(eventHeader, payload);
+        packet.write(payload);
+        verify(payload).writeInt4(1);
+        verify(payload).writeInt1(2);
+        verify(payload).writeInt4(3);
+        verify(payload).writeInt4(4);
+        verify(payload).writeInt4(5);
+        verify(payload).writeInt2(6);
+    }
+    
+    private void mockPayloadForValidCase(final int checksumAlgorithmFlag) {
+        when(payload.readInt2()).thenReturn(BINLOG_VERSION);
+        
when(payload.readStringFixByBytes(50)).thenReturn(MYSQL_SERVER_VERSION);
+        when(payload.readInt4()).thenReturn(CREATE_TIMESTAMP);
+        when(payload.readInt1()).thenReturn(EVENT_HEADER_LENGTH, 
FORMAT_DESCRIPTION_EVENT_LENGTH, checksumAlgorithmFlag);
+    }
+    
+    private static Stream<Arguments> assertNewParameters() {
+        return Stream.of(
+                Arguments.of("checksum algorithm is crc32", 1, 1),
+                Arguments.of("checksum algorithm is off", 0, 0),
+                Arguments.of("checksum algorithm is unsupported", 2, 0));
+    }
+    
+    private static Stream<Arguments> 
assertNewWithInvalidEventHeaderLengthParameters() {
+        return Stream.of(
+                Arguments.of("event header length is less than 19", 18),
+                Arguments.of("event header length is greater than 19", 20),
+                Arguments.of("event header length is zero", 0));
     }
 }

Reply via email to