|
| 1 | +package javasabr.rlib.network; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.util.Arrays; |
| 5 | +import javasabr.rlib.network.packet.impl.AbstractReadableNetworkPacket; |
| 6 | +import javasabr.rlib.network.packet.impl.AbstractWritableNetworkPacket; |
| 7 | +import lombok.Builder; |
| 8 | +import lombok.EqualsAndHashCode; |
| 9 | +import lombok.ToString; |
| 10 | +import org.junit.jupiter.api.Assertions; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +class NetworkPacketTest { |
| 15 | + |
| 16 | + @Builder |
| 17 | + @ToString |
| 18 | + @EqualsAndHashCode(exclude = {"byteArrayField", "bytesField1", "bytesField2"}) |
| 19 | + private static class TestReadablePacket extends AbstractReadableNetworkPacket { |
| 20 | + private int byteField; |
| 21 | + private int byteUnsignedField; |
| 22 | + private int shortField; |
| 23 | + private int shortUnsignedField; |
| 24 | + private int intField; |
| 25 | + private long intUnsignedField; |
| 26 | + private long longField; |
| 27 | + private float floatField; |
| 28 | + private double doubleField; |
| 29 | + private byte[] byteArrayField; |
| 30 | + private byte[] bytesField1; |
| 31 | + private byte[] bytesField2; |
| 32 | + private String stringField; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void readImpl(ByteBuffer buffer) { |
| 36 | + super.readImpl(buffer); |
| 37 | + byteField = readByte(buffer); |
| 38 | + byteUnsignedField = readByteUnsigned(buffer); |
| 39 | + shortField = readShort(buffer); |
| 40 | + shortUnsignedField = readShortUnsigned(buffer); |
| 41 | + intField = readInt(buffer); |
| 42 | + intUnsignedField = readIntUnsigned(buffer); |
| 43 | + longField = readLong(buffer); |
| 44 | + floatField = readFloat(buffer); |
| 45 | + doubleField = readDouble(buffer); |
| 46 | + byteArrayField = readByteArray(buffer); |
| 47 | + int bytesCount = readInt(buffer); |
| 48 | + bytesField1 = new byte[bytesCount]; |
| 49 | + readBytes(buffer, bytesField1); |
| 50 | + bytesCount = readInt(buffer); |
| 51 | + bytesField2 = new byte[bytesCount]; |
| 52 | + readBytes(buffer, bytesField2, 0, bytesCount); |
| 53 | + stringField = readString(buffer, Integer.MAX_VALUE); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Builder |
| 58 | + private static class TestWritablePacket extends AbstractWritableNetworkPacket { |
| 59 | + private int byteField; |
| 60 | + private int byteUnsignedField; |
| 61 | + private int shortField; |
| 62 | + private int shortUnsignedField; |
| 63 | + private int intField; |
| 64 | + private long intUnsignedField; |
| 65 | + private long longField; |
| 66 | + private float floatField; |
| 67 | + private double doubleField; |
| 68 | + private byte[] byteArrayField; |
| 69 | + private byte[] bytesField1; |
| 70 | + private byte[] bytesField2; |
| 71 | + private String stringField; |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void writeImpl(ByteBuffer buffer) { |
| 75 | + super.writeImpl(buffer); |
| 76 | + writeByte(buffer, byteField); |
| 77 | + writeByte(buffer, byteUnsignedField); |
| 78 | + writeShort(buffer, shortField); |
| 79 | + writeShort(buffer, shortUnsignedField); |
| 80 | + writeInt(buffer, intField); |
| 81 | + writeInt(buffer, (int) intUnsignedField); |
| 82 | + writeLong(buffer, longField); |
| 83 | + writeFloat(buffer, floatField); |
| 84 | + writeDouble(buffer, doubleField); |
| 85 | + writeByteArray(buffer, byteArrayField); |
| 86 | + writeInt(buffer, bytesField1.length); |
| 87 | + writeBytes(buffer, bytesField1); |
| 88 | + writeInt(buffer, bytesField2.length); |
| 89 | + writeBytes(buffer, bytesField2, 0, bytesField2.length); |
| 90 | + writeString(buffer, stringField); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @DisplayName("should write packet correctly") |
| 96 | + void shouldWritePacketCorrectly() { |
| 97 | + |
| 98 | + // given: |
| 99 | + TestWritablePacket packet = TestWritablePacket |
| 100 | + .builder() |
| 101 | + .byteField(100) |
| 102 | + .byteUnsignedField(200) |
| 103 | + .shortField(25600) |
| 104 | + .shortUnsignedField(44000) |
| 105 | + .intField(500_400) |
| 106 | + .intUnsignedField(3_345_123_436L) |
| 107 | + .longField(4_564_869_376L) |
| 108 | + .floatField(1.55F) |
| 109 | + .doubleField(5.23) |
| 110 | + .byteArrayField(new byte[] {4, 6, 9}) |
| 111 | + .bytesField1(new byte[] {9, 1, 5, 8, 9}) |
| 112 | + .bytesField2(new byte[] {3, 1, 1, 8, 9, 9, 5}) |
| 113 | + .stringField("test string") |
| 114 | + .build(); |
| 115 | + |
| 116 | + var buffer = ByteBuffer.allocate(256); |
| 117 | + byte[] expected = {100, -56, 100, 0, -85, -32, 0, 7, -94, -80, -57, 98, -120, 108, |
| 118 | + 0, 0, 0, 1, 16, 22, 97, 0, 63, -58, 102, 102, 64, 20, -21, -123, |
| 119 | + 30, -72, 81, -20, 0, 0, 0, 3, 4, 6, 9, 0, 0, 0, 5, 9, 1, 5, |
| 120 | + 8, 9, 0, 0, 0, 7, 3, 1, 1, 8, 9, 9, 5, 0, 0, 0, 11, 0, 116, |
| 121 | + 0, 101, 0, 115, 0, 116, 0, 32, 0, 115, 0, 116, 0, 114, 0, |
| 122 | + 105, 0, 110, 0, 103}; |
| 123 | + |
| 124 | + // when: |
| 125 | + packet.write(buffer); |
| 126 | + buffer.flip(); |
| 127 | + |
| 128 | + // then: |
| 129 | + byte[] wroteBytes = Arrays.copyOf(buffer.array(), buffer.limit()); |
| 130 | + Assertions.assertArrayEquals(expected, wroteBytes); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @DisplayName("should read packet correctly") |
| 135 | + void shouldReadPacketCorrectly() { |
| 136 | + |
| 137 | + // given: |
| 138 | + byte[] data = {100, -56, 100, 0, -85, -32, 0, 7, -94, -80, -57, 98, -120, 108, |
| 139 | + 0, 0, 0, 1, 16, 22, 97, 0, 63, -58, 102, 102, 64, 20, -21, -123, |
| 140 | + 30, -72, 81, -20, 0, 0, 0, 3, 4, 6, 9, 0, 0, 0, 5, 9, 1, 5, |
| 141 | + 8, 9, 0, 0, 0, 7, 3, 1, 1, 8, 9, 9, 5, 0, 0, 0, 11, 0, 116, |
| 142 | + 0, 101, 0, 115, 0, 116, 0, 32, 0, 115, 0, 116, 0, 114, 0, |
| 143 | + 105, 0, 110, 0, 103}; |
| 144 | + |
| 145 | + TestReadablePacket packet = TestReadablePacket.builder().build(); |
| 146 | + |
| 147 | + TestReadablePacket expected = TestReadablePacket |
| 148 | + .builder() |
| 149 | + .byteField(100) |
| 150 | + .byteUnsignedField(200) |
| 151 | + .shortField(25600) |
| 152 | + .shortUnsignedField(44000) |
| 153 | + .intField(500_400) |
| 154 | + .intUnsignedField(3_345_123_436L) |
| 155 | + .longField(4_564_869_376L) |
| 156 | + .floatField(1.55F) |
| 157 | + .doubleField(5.23) |
| 158 | + .byteArrayField(new byte[] {4, 6, 9}) |
| 159 | + .bytesField1(new byte[] {9, 1, 5, 8, 9}) |
| 160 | + .bytesField2(new byte[] {3, 1, 1, 8, 9, 9, 5}) |
| 161 | + .stringField("test string") |
| 162 | + .build(); |
| 163 | + |
| 164 | + // when: |
| 165 | + packet.read(ByteBuffer.wrap(data), data.length); |
| 166 | + |
| 167 | + // then: |
| 168 | + Assertions.assertEquals(expected, packet); |
| 169 | + Assertions.assertArrayEquals(expected.byteArrayField, packet.byteArrayField); |
| 170 | + Assertions.assertArrayEquals(expected.bytesField1, packet.bytesField1); |
| 171 | + Assertions.assertArrayEquals(expected.bytesField2, packet.bytesField2); |
| 172 | + } |
| 173 | +} |
0 commit comments