public class ReflectionPacketSerializerTest {
@Test
public void serializesCorrectly() throws IOException, Exception {
// create a new serializer
ReflectionPacketSerializer packetSerializer = new ReflectionPacketSerializer(8, P008_TestPacket.class);
// create a new empty TestIncomingPacket
P008_TestPacket testIncomingPacket = new P008_TestPacket();
testIncomingPacket.setUnsignedInteger1(1);
testIncomingPacket.setUnsignedInteger2(2);
testIncomingPacket.setUnsignedShort1(3);
testIncomingPacket.setConstantUnsignedByteArray1(new short[]{20});
testIncomingPacket.setConstantUnsignedByteArray2(new short[]{21, 22});
testIncomingPacket.setConstantUnsignedByteArray3(new short[]{23, 24, 25});
testIncomingPacket.setString1("toto");
testIncomingPacket.setUnsignedShort2(4);
testIncomingPacket.setConstantUnsignedByteArray4(new short[]{26, 27, 28, 29});
testIncomingPacket.setUnsignedInteger3(5);
testIncomingPacket.setVariableUnsignedByteArray1(new short[]{30, 31, 32, 33, 34});
testIncomingPacket.setUnsignedByte1((short) 6);
testIncomingPacket.setUnsignedInteger4(7);
testIncomingPacket.setString2("titi");
// build the buffer that will contain all the correct output data inserted manually
ByteBuf neededOutputBytes = Unpooled.buffer(65);
neededOutputBytes = neededOutputBytes.order(ByteOrder.LITTLE_ENDIAN);
String output = "8 0 1 0 0 0 2 0 0 0 3 0 20 21 22 23 24 25 4 0 116 0 111 0 116 0 111 0 4 0 26 27 28 29 5 0 0 0 5 0 30 31 32 33 34 6 7 0 0 0 4 0 116 0 105 0 116 0 105 0";
String[] bytes = output.split("\\s");
for (String byteString : bytes) {
neededOutputBytes.writeByte(Byte.parseByte(byteString));
}
// build the buffer that will contain all the generated output data
ByteBuf generatedOutputBytes = Unpooled.buffer(65);
generatedOutputBytes = generatedOutputBytes.order(ByteOrder.LITTLE_ENDIAN);
// serialize the given packet
packetSerializer.serialize(testIncomingPacket, generatedOutputBytes);
// compare the size of each bufffer
assertEquals(neededOutputBytes.writerIndex(), generatedOutputBytes.writerIndex());
int writerIndex = generatedOutputBytes.writerIndex();