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 2020/06/15 15:43:01 UTC

[shardingsphere] branch master updated: #5868, refine get unsigned value (#6048)

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 edbd534  #5868, refine get unsigned value (#6048)
edbd534 is described below

commit edbd53479d60b2e75fe44a1d30d64d40c98bd334
Author: Zhang Yonglun <zh...@apache.org>
AuthorDate: Mon Jun 15 23:42:31 2020 +0800

    #5868, refine get unsigned value (#6048)
    
    * #5868, refine get unsigned value
    
    * #5868, for test cases
---
 .../protocol/mysql/payload/MySQLPacketPayload.java | 10 ++++-----
 .../mysql/payload/MySQLPacketPayloadTest.java      | 24 +++++++++++-----------
 .../payload/PostgreSQLPacketPayload.java           |  4 ++--
 .../netty/MySQLBinlogEventPacketDecoderTest.java   | 22 ++++++++++----------
 .../netty/MySQLCommandPacketDecoderTest.java       | 12 +++++------
 5 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java
index 40c3c97..aff3997 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayload.java
@@ -42,7 +42,7 @@ public final class MySQLPacketPayload implements PacketPayload {
      * @return 1 byte fixed length integer
      */
     public int readInt1() {
-        return byteBuf.readByte() & 0xff;
+        return byteBuf.readUnsignedByte();
     }
     
     /**
@@ -64,7 +64,7 @@ public final class MySQLPacketPayload implements PacketPayload {
      * @return 2 byte fixed length integer
      */
     public int readInt2() {
-        return byteBuf.readShortLE() & 0xffff;
+        return byteBuf.readUnsignedShortLE();
     }
     
     /**
@@ -86,7 +86,7 @@ public final class MySQLPacketPayload implements PacketPayload {
      * @return 3 byte fixed length integer
      */
     public int readInt3() {
-        return byteBuf.readMediumLE() & 0xffffff;
+        return byteBuf.readUnsignedMediumLE();
     }
     
     /**
@@ -188,10 +188,10 @@ public final class MySQLPacketPayload implements PacketPayload {
             return 0;
         }
         if (0xfc == firstByte) {
-            return byteBuf.readShortLE();
+            return readInt2();
         }
         if (0xfd == firstByte) {
-            return byteBuf.readMediumLE();
+            return readInt3();
         }
         return byteBuf.readLongLE();
     }
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java
index fb48b17..58237df 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/payload/MySQLPacketPayloadTest.java
@@ -37,7 +37,7 @@ public final class MySQLPacketPayloadTest {
     
     @Test
     public void assertReadInt1() {
-        when(byteBuf.readByte()).thenReturn((byte) 1);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 1);
         assertThat(new MySQLPacketPayload(byteBuf).readInt1(), is(1));
     }
     
@@ -49,7 +49,7 @@ public final class MySQLPacketPayloadTest {
     
     @Test
     public void assertReadInt2() {
-        when(byteBuf.readShortLE()).thenReturn((short) 1);
+        when(byteBuf.readUnsignedShortLE()).thenReturn(1);
         assertThat(new MySQLPacketPayload(byteBuf).readInt2(), is(1));
     }
     
@@ -61,7 +61,7 @@ public final class MySQLPacketPayloadTest {
     
     @Test
     public void assertReadInt3() {
-        when(byteBuf.readMediumLE()).thenReturn(1);
+        when(byteBuf.readUnsignedMediumLE()).thenReturn(1);
         assertThat(new MySQLPacketPayload(byteBuf).readInt3(), is(1));
     }
     
@@ -110,33 +110,33 @@ public final class MySQLPacketPayloadTest {
     
     @Test
     public void assertReadIntLenencWithOneByte() {
-        when(byteBuf.readByte()).thenReturn((byte) 1);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 1);
         assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(1L));
     }
     
     @Test
     public void assertReadIntLenencWithZero() {
-        when(byteBuf.readByte()).thenReturn((byte) 0xfb);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0xfb);
         assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(0L));
     }
     
     @Test
     public void assertReadIntLenencWithTwoBytes() {
-        when(byteBuf.readByte()).thenReturn((byte) 0xfc);
-        when(byteBuf.readShortLE()).thenReturn((short) 100);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0xfc);
+        when(byteBuf.readUnsignedShortLE()).thenReturn(100);
         assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(100L));
     }
     
     @Test
     public void assertReadIntLenencWithThreeBytes() {
-        when(byteBuf.readByte()).thenReturn((byte) 0xfd);
-        when(byteBuf.readMediumLE()).thenReturn(99999);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0xfd);
+        when(byteBuf.readUnsignedMediumLE()).thenReturn(99999);
         assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(99999L));
     }
     
     @Test
     public void assertReadIntLenencWithFourBytes() {
-        when(byteBuf.readByte()).thenReturn((byte) 0xff);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0xff);
         when(byteBuf.readLongLE()).thenReturn(Long.MAX_VALUE);
         assertThat(new MySQLPacketPayload(byteBuf).readIntLenenc(), is(Long.MAX_VALUE));
     }
@@ -170,13 +170,13 @@ public final class MySQLPacketPayloadTest {
     
     @Test
     public void assertReadStringLenenc() {
-        when(byteBuf.readByte()).thenReturn((byte) 0);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0);
         assertThat(new MySQLPacketPayload(byteBuf).readStringLenenc(), is(""));
     }
     
     @Test
     public void assertReadStringLenencByBytes() {
-        when(byteBuf.readByte()).thenReturn((byte) 0);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0);
         assertThat(new MySQLPacketPayload(byteBuf).readStringLenencByBytes(), is(new byte[] {}));
     }
     
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java
index 6406aa0..54115de 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/payload/PostgreSQLPacketPayload.java
@@ -39,7 +39,7 @@ public final class PostgreSQLPacketPayload implements PacketPayload {
      * @return 1 byte fixed length integer
      */
     public int readInt1() {
-        return byteBuf.readByte() & 0xff;
+        return byteBuf.readUnsignedByte();
     }
     
     /**
@@ -57,7 +57,7 @@ public final class PostgreSQLPacketPayload implements PacketPayload {
      * @return 2 byte fixed length integer
      */
     public int readInt2() {
-        return byteBuf.readShort() & 0xffff;
+        return byteBuf.readUnsignedShort();
     }
     
     /**
diff --git a/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java b/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java
index 9b68d4f..d810af6 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLBinlogEventPacketDecoderTest.java
@@ -62,7 +62,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
     
     @Test(expected = RuntimeException.class)
     public void assertDecodeWithPacketError() {
-        when(byteBuf.readByte()).thenReturn((byte) 255);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 255);
         binlogEventPacketDecoder.decode(null, byteBuf, null);
     }
     
@@ -74,7 +74,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
     
     @Test
     public void assertDecodeRotateEvent() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) 0, (byte) MySQLBinlogEventType.ROTATE_EVENT.getValue());
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.ROTATE_EVENT.getValue());
         List<Object> decodedEvents = new ArrayList<>();
         binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
         assertThat(decodedEvents.size(), is(0));
@@ -83,8 +83,8 @@ public final class MySQLBinlogEventPacketDecoderTest {
     
     @Test
     public void assertDecodeFormatDescriptionEvent() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) 0, (byte) MySQLBinlogEventType.FORMAT_DESCRIPTION_EVENT.getValue(), (byte) 19);
-        when(byteBuf.readShortLE()).thenReturn((short) 4);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.FORMAT_DESCRIPTION_EVENT.getValue(), (short) 19);
+        when(byteBuf.readUnsignedShortLE()).thenReturn(4);
         List<Object> decodedEvents = new ArrayList<>();
         binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
         assertThat(decodedEvents.size(), is(0));
@@ -93,7 +93,7 @@ public final class MySQLBinlogEventPacketDecoderTest {
     
     @Test
     public void assertDecodeTableMapEvent() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) 0, (byte) MySQLBinlogEventType.TABLE_MAP_EVENT.getValue(), (byte) 0);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.TABLE_MAP_EVENT.getValue(), (short) 0);
         List<Object> decodedEvents = new ArrayList<>();
         binlogEventPacketDecoder.decode(null, byteBuf, decodedEvents);
         assertThat(decodedEvents.size(), is(0));
@@ -103,8 +103,8 @@ public final class MySQLBinlogEventPacketDecoderTest {
     
     @Test
     public void assertDecodeWriteRowEvent() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) 0, (byte) MySQLBinlogEventType.WRITE_ROWS_EVENTv2.getValue(), (byte) 0);
-        when(byteBuf.readShortLE()).thenReturn((short) 2);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.WRITE_ROWS_EVENTv2.getValue(), (short) 0);
+        when(byteBuf.readUnsignedShortLE()).thenReturn(2);
         binlogContext.getTableMap().put(0L, tableMapEventPacket);
         when(tableMapEventPacket.getColumnDefs()).thenReturn(Collections.emptyList());
         List<Object> decodedEvents = new ArrayList<>();
@@ -115,8 +115,8 @@ public final class MySQLBinlogEventPacketDecoderTest {
     
     @Test
     public void assertDecodeUpdateRowEvent() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) 0, (byte) MySQLBinlogEventType.UPDATE_ROWS_EVENTv2.getValue(), (byte) 0);
-        when(byteBuf.readShortLE()).thenReturn((short) 2);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.UPDATE_ROWS_EVENTv2.getValue(), (short) 0);
+        when(byteBuf.readUnsignedShortLE()).thenReturn(2);
         binlogContext.getTableMap().put(0L, tableMapEventPacket);
         when(tableMapEventPacket.getColumnDefs()).thenReturn(Collections.emptyList());
         List<Object> decodedEvents = new ArrayList<>();
@@ -127,8 +127,8 @@ public final class MySQLBinlogEventPacketDecoderTest {
     
     @Test
     public void assertDecodeDeleteRowEvent() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) 0, (byte) MySQLBinlogEventType.DELETE_ROWS_EVENTv2.getValue(), (byte) 0);
-        when(byteBuf.readShortLE()).thenReturn((short) 2);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 0, (short) MySQLBinlogEventType.DELETE_ROWS_EVENTv2.getValue(), (short) 0);
+        when(byteBuf.readUnsignedShortLE()).thenReturn(2);
         binlogContext.getTableMap().put(0L, tableMapEventPacket);
         when(tableMapEventPacket.getColumnDefs()).thenReturn(Collections.emptyList());
         List<Object> decodedEvents = new ArrayList<>();
diff --git a/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java b/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java
index 9d29dc4..90de8d4 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLCommandPacketDecoderTest.java
@@ -56,8 +56,8 @@ public final class MySQLCommandPacketDecoderTest {
     
     @Test(expected = UnsupportedOperationException.class)
     public void assertDecodeUnsupportedAuthenticationMethod() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) MySQLServerInfo.PROTOCOL_VERSION);
-        when(byteBuf.readShortLE()).thenReturn((short) MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) MySQLServerInfo.PROTOCOL_VERSION);
+        when(byteBuf.readUnsignedShortLE()).thenReturn(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
         MySQLCommandPacketDecoder commandPacketDecoder = new MySQLCommandPacketDecoder();
         commandPacketDecoder.decode(null, byteBuf, null);
     }
@@ -103,7 +103,7 @@ public final class MySQLCommandPacketDecoderTest {
     }
     
     private ByteBuf mockOkPacket() {
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) MySQLOKPacket.HEADER);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) MySQLOKPacket.HEADER);
         when(byteBuf.getByte(1)).thenReturn((byte) MySQLOKPacket.HEADER);
         return byteBuf;
     }
@@ -119,7 +119,7 @@ public final class MySQLCommandPacketDecoderTest {
     
     private ByteBuf mockErrPacket() {
         when(byteBuf.getByte(1)).thenReturn((byte) MySQLErrPacket.HEADER);
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) MySQLErrPacket.HEADER);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) MySQLErrPacket.HEADER);
         return byteBuf;
     }
     
@@ -143,7 +143,7 @@ public final class MySQLCommandPacketDecoderTest {
     
     private ByteBuf mockFieldDefinition41Packet() {
         when(byteBuf.getByte(1)).thenReturn((byte) 3);
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) 3, (byte) 0x0c);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) 3, (short) 0x0c);
         when(byteBuf.readBytes(new byte[3])).then(invocationOnMock -> {
             byte[] input = invocationOnMock.getArgument(0);
             System.arraycopy("def".getBytes(), 0, input, 0, input.length);
@@ -154,7 +154,7 @@ public final class MySQLCommandPacketDecoderTest {
     
     private ByteBuf mockEofPacket() {
         when(byteBuf.getByte(1)).thenReturn((byte) MySQLEofPacket.HEADER);
-        when(byteBuf.readByte()).thenReturn((byte) 0, (byte) MySQLEofPacket.HEADER);
+        when(byteBuf.readUnsignedByte()).thenReturn((short) 0, (short) MySQLEofPacket.HEADER);
         return byteBuf;
     }