|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package info.avalon566.shardingscaling.sync.mysql.binlog.codec; |
| 19 | + |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.mockito.Mock; |
| 23 | +import org.mockito.invocation.InvocationOnMock; |
| 24 | +import org.mockito.junit.MockitoJUnitRunner; |
| 25 | +import org.mockito.stubbing.Answer; |
| 26 | +import io.netty.buffer.ByteBuf; |
| 27 | +import java.util.BitSet; |
| 28 | + |
| 29 | +import static org.hamcrest.CoreMatchers.is; |
| 30 | +import static org.junit.Assert.assertThat; |
| 31 | +import static org.mockito.ArgumentMatchers.any; |
| 32 | +import static org.mockito.ArgumentMatchers.eq; |
| 33 | +import static org.mockito.Mockito.verify; |
| 34 | +import static org.mockito.Mockito.when; |
| 35 | + |
| 36 | +@RunWith(MockitoJUnitRunner.class) |
| 37 | +public class DataTypesCodecTest { |
| 38 | + |
| 39 | + private static final String EXPECTED_STRING = "0123456789"; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private ByteBuf byteBuf; |
| 43 | + |
| 44 | + @Test |
| 45 | + public void assertReadNul() { |
| 46 | + DataTypesCodec.readNul(byteBuf); |
| 47 | + verify(byteBuf).readByte(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void assertReadFloatLE() { |
| 52 | + when(byteBuf.readFloatLE()).thenReturn(1.1f); |
| 53 | + assertThat(DataTypesCodec.readFloatLE(byteBuf), is(1.1f)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void assertReadDoubleLE() { |
| 58 | + when(byteBuf.readDoubleLE()).thenReturn(1.1d); |
| 59 | + assertThat(DataTypesCodec.readDoubleLE(byteBuf), is(1.1d)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void assertReadBitmap() { |
| 64 | + when(byteBuf.readByte()).thenReturn((byte) 0x01, (byte) 0x02, (byte) 0x04, (byte) 0x08, (byte) 0x10, (byte) 0x20, (byte) 0x40, (byte) 0x80); |
| 65 | + BitSet expected = new BitSet(); |
| 66 | + for (int i = 0; i <= 7; i++) { |
| 67 | + expected.set(i * 8 + i); |
| 68 | + } |
| 69 | + assertThat(DataTypesCodec.readBitmap(64, byteBuf), is(expected)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void assertReadInt8LE() { |
| 74 | + when(byteBuf.readLongLE()).thenReturn(1L); |
| 75 | + assertThat(DataTypesCodec.readInt8LE(byteBuf), is(1L)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void assertReadUnsignedInt1() { |
| 80 | + when(byteBuf.readUnsignedByte()).thenReturn((short) 0xff); |
| 81 | + assertThat(DataTypesCodec.readUnsignedInt1(byteBuf), is((short) 0xff)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void assertReadUnsignedInt2LE() { |
| 86 | + when(byteBuf.readUnsignedShortLE()).thenReturn(0xff); |
| 87 | + assertThat(DataTypesCodec.readUnsignedInt2LE(byteBuf), is(0xff)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void assertReadUnsignedInt3BE() { |
| 92 | + when(byteBuf.readUnsignedMedium()).thenReturn(1); |
| 93 | + assertThat(DataTypesCodec.readUnsignedInt3BE(byteBuf), is(1)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void assertReadUnsignedInt3LE() { |
| 98 | + when(byteBuf.readUnsignedMediumLE()).thenReturn(1); |
| 99 | + assertThat(DataTypesCodec.readUnsignedInt3LE(byteBuf), is(1)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void assertReadUnsignedInt4BE() { |
| 104 | + when(byteBuf.readUnsignedInt()).thenReturn(1L + Integer.MAX_VALUE); |
| 105 | + assertThat(DataTypesCodec.readUnsignedInt4BE(byteBuf), is(1L + Integer.MAX_VALUE)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void assertReadUnsignedInt4LE() { |
| 110 | + when(byteBuf.readUnsignedIntLE()).thenReturn(1L + Integer.MAX_VALUE); |
| 111 | + assertThat(DataTypesCodec.readUnsignedInt4LE(byteBuf), is(1L + Integer.MAX_VALUE)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void assertReadUnsignedInt5BE() { |
| 116 | + when(byteBuf.readByte()).thenReturn((byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x02, (byte) 0x01); |
| 117 | + assertThat(DataTypesCodec.readUnsignedInt5BE(byteBuf), is(4328718849L)); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void assertReadUnsignedInt6LE() { |
| 122 | + when(byteBuf.readByte()).thenReturn((byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x03, (byte) 0x02, (byte) 0x01); |
| 123 | + assertThat(DataTypesCodec.readUnsignedInt6LE(byteBuf), is(1108152091137L)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void assertReadLengthCodedIntLE() { |
| 128 | + when(byteBuf.readByte()).thenReturn((byte) 251); |
| 129 | + assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(-1L)); |
| 130 | + when(byteBuf.readByte()).thenReturn((byte) 252); |
| 131 | + when(byteBuf.readUnsignedShortLE()).thenReturn(0x00010000); |
| 132 | + assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(65536L)); |
| 133 | + when(byteBuf.readByte()).thenReturn((byte) 253); |
| 134 | + when(byteBuf.readUnsignedMediumLE()).thenReturn(0x01000000); |
| 135 | + assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(16777216L)); |
| 136 | + when(byteBuf.readByte()).thenReturn((byte) 254); |
| 137 | + when(byteBuf.readLongLE()).thenReturn(1L + Integer.MAX_VALUE); |
| 138 | + assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(1L + Integer.MAX_VALUE)); |
| 139 | + when(byteBuf.readByte()).thenReturn((byte) 10); |
| 140 | + assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(10L)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void assertReadBytes() { |
| 145 | + byte[] actual = DataTypesCodec.readBytes(10, byteBuf); |
| 146 | + assertThat(actual.length, is(10)); |
| 147 | + verify(byteBuf).readBytes(actual, 0, 10); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void assertReadFixedLengthString() { |
| 152 | + when(byteBuf.readBytes(any(byte[].class), eq(0), eq(10))).then(mockReadBytesAnswer()); |
| 153 | + assertThat(DataTypesCodec.readFixedLengthString(10, byteBuf), is(EXPECTED_STRING)); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void assertReadLengthCodedString() { |
| 158 | + when(byteBuf.readByte()).thenReturn((byte) 10); |
| 159 | + when(byteBuf.readBytes(any(byte[].class), eq(0), eq(10))).then(mockReadBytesAnswer()); |
| 160 | + assertThat(DataTypesCodec.readLengthCodedString(byteBuf), is(EXPECTED_STRING)); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void assertReadNulTerminatedString() { |
| 165 | + when(byteBuf.bytesBefore((byte) 0x00)).thenReturn(10); |
| 166 | + when(byteBuf.readBytes(any(byte[].class), eq(0), eq(10))).then(mockReadBytesAnswer()); |
| 167 | + assertThat(DataTypesCodec.readNulTerminatedString(byteBuf), is(EXPECTED_STRING)); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void assertWriteByte() { |
| 172 | + final byte data = 0x00; |
| 173 | + DataTypesCodec.writeByte(data, byteBuf); |
| 174 | + verify(byteBuf).writeByte(data); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void assertWriteInt2LE() { |
| 179 | + final short data = 0x00; |
| 180 | + DataTypesCodec.writeInt2LE(data, byteBuf); |
| 181 | + verify(byteBuf).writeShortLE(data); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void assertWriteInt4LE() { |
| 186 | + final int data = 0x00; |
| 187 | + DataTypesCodec.writeInt4LE(data, byteBuf); |
| 188 | + verify(byteBuf).writeIntLE(data); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void assertWriteLengthCodedInt() { |
| 193 | + DataTypesCodec.writeLengthCodedInt(1, byteBuf); |
| 194 | + verify(byteBuf).writeByte(1); |
| 195 | + DataTypesCodec.writeLengthCodedInt(1 << 15L, byteBuf); |
| 196 | + verify(byteBuf).writeByte((byte) 252); |
| 197 | + verify(byteBuf).writeShortLE(1 << 15L); |
| 198 | + DataTypesCodec.writeLengthCodedInt(1 << 23L, byteBuf); |
| 199 | + verify(byteBuf).writeByte((byte) 253); |
| 200 | + verify(byteBuf).writeMediumLE(1 << 23L); |
| 201 | + DataTypesCodec.writeLengthCodedInt(1 << 24L, byteBuf); |
| 202 | + verify(byteBuf).writeByte((byte) 254); |
| 203 | + verify(byteBuf).writeIntLE(1 << 24L); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void assertWriteBytes() { |
| 208 | + final byte[] data = new byte[10]; |
| 209 | + DataTypesCodec.writeBytes(data, byteBuf); |
| 210 | + verify(byteBuf).writeBytes(data); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void assertWriteNulTerminatedString() { |
| 215 | + DataTypesCodec.writeNulTerminatedString(EXPECTED_STRING, byteBuf); |
| 216 | + verify(byteBuf).writeBytes(EXPECTED_STRING.getBytes()); |
| 217 | + verify(byteBuf).writeByte((byte) 0x00); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void assertWriteLengthCodedBinary() { |
| 222 | + DataTypesCodec.writeLengthCodedBinary(EXPECTED_STRING.getBytes(), byteBuf); |
| 223 | + verify(byteBuf).writeByte((byte) 10); |
| 224 | + verify(byteBuf).writeBytes(EXPECTED_STRING.getBytes()); |
| 225 | + } |
| 226 | + |
| 227 | + private Answer mockReadBytesAnswer() { |
| 228 | + return new Answer<ByteBuf>() { |
| 229 | + |
| 230 | + @Override |
| 231 | + public ByteBuf answer(final InvocationOnMock invocationOnMock) throws Throwable { |
| 232 | + byte[] args = invocationOnMock.getArgument(0); |
| 233 | + byte[] expectedBytes = DataTypesCodecTest.EXPECTED_STRING.getBytes(); |
| 234 | + System.arraycopy(expectedBytes, 0, args, 0, expectedBytes.length); |
| 235 | + return null; |
| 236 | + } |
| 237 | + }; |
| 238 | + } |
| 239 | +} |
0 commit comments