Skip to content

Commit

Permalink
Merge pull request apache#7 from KomachiSion/master
Browse files Browse the repository at this point in the history
Add unit test for mysql.binlog.codec
  • Loading branch information
avalon566 authored Sep 27, 2019
2 parents f936cc4 + 7030691 commit a7973c6
Show file tree
Hide file tree
Showing 7 changed files with 442 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@
import java.math.BigDecimal;

/**
* Decimal Value decoder.
*
* @author avalon566
*/
public class DecimalValueDecoder {

private static final int DIG_PER_DEC = 9;

private static final int[] DIG_TO_BYTES = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4};


/**
* decode new Decimal.
*
* @param meta meta
* @param in byte buffer
* @return decimal value
*/
public static Serializable decodeNewDecimal(final int meta, final ByteBuf in) {
var precision = meta >> 8;
var scale = meta & 0xFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
import info.avalon566.shardingscaling.sync.mysql.binlog.event.DeleteRowsEvent;
import info.avalon566.shardingscaling.sync.mysql.binlog.event.UpdateRowsEvent;
import info.avalon566.shardingscaling.sync.mysql.binlog.event.WriteRowsEvent;
import info.avalon566.shardingscaling.sync.mysql.binlog.packet.binlog.*;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import info.avalon566.shardingscaling.sync.mysql.binlog.packet.binlog.BinlogEventHeader;
import info.avalon566.shardingscaling.sync.mysql.binlog.packet.binlog.EventTypes;
import info.avalon566.shardingscaling.sync.mysql.binlog.packet.binlog.FormatDescriptionEvent;
import info.avalon566.shardingscaling.sync.mysql.binlog.packet.binlog.RotateEvent;
import info.avalon566.shardingscaling.sync.mysql.binlog.packet.binlog.RowsEvent;
import info.avalon566.shardingscaling.sync.mysql.binlog.packet.binlog.TableMapEvent;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public final class MySQLLengthFieldBasedFrameEncoder extends MessageToByteEncode

@Override
protected void encode(final ChannelHandlerContext ctx, final Object msg, final ByteBuf out) {
var bb = ((AbstractPacket) msg).toByteBuf();
var byteBuf = ((AbstractPacket) msg).toByteBuf();
HeaderPacket h = new HeaderPacket();
h.setPacketBodyLength(bb.readableBytes());
h.setPacketBodyLength(byteBuf.readableBytes());
h.setPacketSequenceNumber(((AbstractPacket) msg).getSequenceNumber());
out.writeBytes(h.toByteBuf());
out.writeBytes(bb);
out.writeBytes(byteBuf);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
* 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 info.avalon566.shardingscaling.sync.mysql.binlog.codec;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import io.netty.buffer.ByteBuf;
import java.util.BitSet;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class DataTypesCodecTest {

private static final String EXPECTED_STRING = "0123456789";

@Mock
private ByteBuf byteBuf;

@Test
public void assertReadNul() {
DataTypesCodec.readNul(byteBuf);
verify(byteBuf).readByte();
}

@Test
public void assertReadFloatLE() {
when(byteBuf.readFloatLE()).thenReturn(1.1f);
assertThat(DataTypesCodec.readFloatLE(byteBuf), is(1.1f));
}

@Test
public void assertReadDoubleLE() {
when(byteBuf.readDoubleLE()).thenReturn(1.1d);
assertThat(DataTypesCodec.readDoubleLE(byteBuf), is(1.1d));
}

@Test
public void assertReadBitmap() {
when(byteBuf.readByte()).thenReturn((byte) 0x01, (byte) 0x02, (byte) 0x04, (byte) 0x08, (byte) 0x10, (byte) 0x20, (byte) 0x40, (byte) 0x80);
BitSet expected = new BitSet();
for (int i = 0; i <= 7; i++) {
expected.set(i * 8 + i);
}
assertThat(DataTypesCodec.readBitmap(64, byteBuf), is(expected));
}

@Test
public void assertReadInt8LE() {
when(byteBuf.readLongLE()).thenReturn(1L);
assertThat(DataTypesCodec.readInt8LE(byteBuf), is(1L));
}

@Test
public void assertReadUnsignedInt1() {
when(byteBuf.readUnsignedByte()).thenReturn((short) 0xff);
assertThat(DataTypesCodec.readUnsignedInt1(byteBuf), is((short) 0xff));
}

@Test
public void assertReadUnsignedInt2LE() {
when(byteBuf.readUnsignedShortLE()).thenReturn(0xff);
assertThat(DataTypesCodec.readUnsignedInt2LE(byteBuf), is(0xff));
}

@Test
public void assertReadUnsignedInt3BE() {
when(byteBuf.readUnsignedMedium()).thenReturn(1);
assertThat(DataTypesCodec.readUnsignedInt3BE(byteBuf), is(1));
}

@Test
public void assertReadUnsignedInt3LE() {
when(byteBuf.readUnsignedMediumLE()).thenReturn(1);
assertThat(DataTypesCodec.readUnsignedInt3LE(byteBuf), is(1));
}

@Test
public void assertReadUnsignedInt4BE() {
when(byteBuf.readUnsignedInt()).thenReturn(1L + Integer.MAX_VALUE);
assertThat(DataTypesCodec.readUnsignedInt4BE(byteBuf), is(1L + Integer.MAX_VALUE));
}

@Test
public void assertReadUnsignedInt4LE() {
when(byteBuf.readUnsignedIntLE()).thenReturn(1L + Integer.MAX_VALUE);
assertThat(DataTypesCodec.readUnsignedInt4LE(byteBuf), is(1L + Integer.MAX_VALUE));
}

@Test
public void assertReadUnsignedInt5BE() {
when(byteBuf.readByte()).thenReturn((byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x02, (byte) 0x01);
assertThat(DataTypesCodec.readUnsignedInt5BE(byteBuf), is(4328718849L));
}

@Test
public void assertReadUnsignedInt6LE() {
when(byteBuf.readByte()).thenReturn((byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x03, (byte) 0x02, (byte) 0x01);
assertThat(DataTypesCodec.readUnsignedInt6LE(byteBuf), is(1108152091137L));
}

@Test
public void assertReadLengthCodedIntLE() {
when(byteBuf.readByte()).thenReturn((byte) 251);
assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(-1L));
when(byteBuf.readByte()).thenReturn((byte) 252);
when(byteBuf.readUnsignedShortLE()).thenReturn(0x00010000);
assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(65536L));
when(byteBuf.readByte()).thenReturn((byte) 253);
when(byteBuf.readUnsignedMediumLE()).thenReturn(0x01000000);
assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(16777216L));
when(byteBuf.readByte()).thenReturn((byte) 254);
when(byteBuf.readLongLE()).thenReturn(1L + Integer.MAX_VALUE);
assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(1L + Integer.MAX_VALUE));
when(byteBuf.readByte()).thenReturn((byte) 10);
assertThat(DataTypesCodec.readLengthCodedIntLE(byteBuf), is(10L));
}

@Test
public void assertReadBytes() {
byte[] actual = DataTypesCodec.readBytes(10, byteBuf);
assertThat(actual.length, is(10));
verify(byteBuf).readBytes(actual, 0, 10);
}

@Test
public void assertReadFixedLengthString() {
when(byteBuf.readBytes(any(byte[].class), eq(0), eq(10))).then(mockReadBytesAnswer());
assertThat(DataTypesCodec.readFixedLengthString(10, byteBuf), is(EXPECTED_STRING));
}

@Test
public void assertReadLengthCodedString() {
when(byteBuf.readByte()).thenReturn((byte) 10);
when(byteBuf.readBytes(any(byte[].class), eq(0), eq(10))).then(mockReadBytesAnswer());
assertThat(DataTypesCodec.readLengthCodedString(byteBuf), is(EXPECTED_STRING));
}

@Test
public void assertReadNulTerminatedString() {
when(byteBuf.bytesBefore((byte) 0x00)).thenReturn(10);
when(byteBuf.readBytes(any(byte[].class), eq(0), eq(10))).then(mockReadBytesAnswer());
assertThat(DataTypesCodec.readNulTerminatedString(byteBuf), is(EXPECTED_STRING));
}

@Test
public void assertWriteByte() {
final byte data = 0x00;
DataTypesCodec.writeByte(data, byteBuf);
verify(byteBuf).writeByte(data);
}

@Test
public void assertWriteInt2LE() {
final short data = 0x00;
DataTypesCodec.writeInt2LE(data, byteBuf);
verify(byteBuf).writeShortLE(data);
}

@Test
public void assertWriteInt4LE() {
final int data = 0x00;
DataTypesCodec.writeInt4LE(data, byteBuf);
verify(byteBuf).writeIntLE(data);
}

@Test
public void assertWriteLengthCodedInt() {
DataTypesCodec.writeLengthCodedInt(1, byteBuf);
verify(byteBuf).writeByte(1);
DataTypesCodec.writeLengthCodedInt(1 << 15L, byteBuf);
verify(byteBuf).writeByte((byte) 252);
verify(byteBuf).writeShortLE(1 << 15L);
DataTypesCodec.writeLengthCodedInt(1 << 23L, byteBuf);
verify(byteBuf).writeByte((byte) 253);
verify(byteBuf).writeMediumLE(1 << 23L);
DataTypesCodec.writeLengthCodedInt(1 << 24L, byteBuf);
verify(byteBuf).writeByte((byte) 254);
verify(byteBuf).writeIntLE(1 << 24L);
}

@Test
public void assertWriteBytes() {
final byte[] data = new byte[10];
DataTypesCodec.writeBytes(data, byteBuf);
verify(byteBuf).writeBytes(data);
}

@Test
public void assertWriteNulTerminatedString() {
DataTypesCodec.writeNulTerminatedString(EXPECTED_STRING, byteBuf);
verify(byteBuf).writeBytes(EXPECTED_STRING.getBytes());
verify(byteBuf).writeByte((byte) 0x00);
}

@Test
public void assertWriteLengthCodedBinary() {
DataTypesCodec.writeLengthCodedBinary(EXPECTED_STRING.getBytes(), byteBuf);
verify(byteBuf).writeByte((byte) 10);
verify(byteBuf).writeBytes(EXPECTED_STRING.getBytes());
}

private Answer mockReadBytesAnswer() {
return new Answer<ByteBuf>() {

@Override
public ByteBuf answer(final InvocationOnMock invocationOnMock) throws Throwable {
byte[] args = invocationOnMock.getArgument(0);
byte[] expectedBytes = DataTypesCodecTest.EXPECTED_STRING.getBytes();
System.arraycopy(expectedBytes, 0, args, 0, expectedBytes.length);
return null;
}
};
}
}
Loading

0 comments on commit a7973c6

Please # to comment.