Implemented possibility to read parent object while writing custom binary field
This commit is contained in:
parent
3514a58c40
commit
fcbb2ef227
8 changed files with 372 additions and 29 deletions
39
test/zutil/ArrayUtilTest.java
Normal file
39
test/zutil/ArrayUtilTest.java
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package zutil;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ArrayUtilTest {
|
||||
|
||||
@Test
|
||||
public void toIntArray() {
|
||||
assertArrayEquals(new int[]{}, ArrayUtil.toIntArray(Collections.emptyList()));
|
||||
assertArrayEquals(new int[]{1, 2, 3}, ArrayUtil.toIntArray(Arrays.asList(1, 2, 3)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contains() {
|
||||
assertFalse(ArrayUtil.contains(new Integer[]{}, 1));
|
||||
assertTrue(ArrayUtil.contains(new Integer[]{1}, 1));
|
||||
assertTrue(ArrayUtil.contains(new Integer[]{2, 1, 3}, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combine() {
|
||||
assertArrayEquals(new Integer[]{}, ArrayUtil.combine(new Integer[]{}, new Integer[]{}));
|
||||
assertArrayEquals(new Integer[]{1, 2}, ArrayUtil.combine(new Integer[]{1, 2}, new Integer[]{}));
|
||||
assertArrayEquals(new Integer[]{1, 2, 3, 4}, ArrayUtil.combine(new Integer[]{1, 2}, new Integer[]{3, 4}));
|
||||
|
||||
assertArrayEquals(new int[]{}, ArrayUtil.combine(new int[]{}, new int[]{}));
|
||||
assertArrayEquals(new int[]{1, 2}, ArrayUtil.combine(new int[]{1, 2}, new int[]{}));
|
||||
assertArrayEquals(new int[]{1, 2, 3, 4}, ArrayUtil.combine(new int[]{1, 2}, new int[]{3, 4}));
|
||||
|
||||
assertArrayEquals(new byte[]{}, ArrayUtil.combine(new byte[]{}, new byte[]{}));
|
||||
assertArrayEquals(new byte[]{1, 2}, ArrayUtil.combine(new byte[]{1, 2}, new byte[]{}));
|
||||
assertArrayEquals(new byte[]{1, 2, 3, 4}, ArrayUtil.combine(new byte[]{1, 2}, new byte[]{3, 4}));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
package zutil.parser.binary.serializer;
|
||||
|
||||
import org.junit.Test;
|
||||
import zutil.ArrayUtil;
|
||||
import zutil.ByteUtil;
|
||||
import zutil.parser.binary.BinaryFieldData;
|
||||
import zutil.parser.binary.BinaryStruct;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StreamCorruptedException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TwoByteLengthPrefixedDataSerializerTest implements BinaryStruct {
|
||||
|
||||
@BinaryField(index = 10, length = 1)
|
||||
private String tmpStringField;
|
||||
@BinaryField(index = 20, length = 1)
|
||||
private byte[] tmpByteField;
|
||||
|
||||
@Test(expected = StreamCorruptedException.class)
|
||||
public void readPrematureEnd0() throws IOException {
|
||||
TwoByteLengthPrefixedDataSerializer serializer = new TwoByteLengthPrefixedDataSerializer();
|
||||
|
||||
// 0 length stream
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);
|
||||
serializer.read(inputStream, null, this);
|
||||
}
|
||||
|
||||
@Test(expected = StreamCorruptedException.class)
|
||||
public void readPrematureEnd1() throws IOException {
|
||||
TwoByteLengthPrefixedDataSerializer serializer = new TwoByteLengthPrefixedDataSerializer();
|
||||
|
||||
// 1 length stream
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[]{0x00});
|
||||
serializer.read(inputStream, null, this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException, NoSuchFieldException {
|
||||
TwoByteLengthPrefixedDataSerializer serializer = new TwoByteLengthPrefixedDataSerializer();
|
||||
List<BinaryFieldData> fieldDataList = BinaryFieldData.getStructFieldList(this.getClass());
|
||||
BinaryFieldData stringFieldData = fieldDataList.get(0);
|
||||
BinaryFieldData byteFieldData = fieldDataList.get(1);
|
||||
|
||||
// 0 Length
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[]{0x00, 0x00});
|
||||
assertEquals("", serializer.read(inputStream, stringFieldData, this));
|
||||
inputStream.reset();
|
||||
assertArrayEquals(new byte[0], (byte[])serializer.read(inputStream, byteFieldData, this));
|
||||
|
||||
// String "1234"
|
||||
inputStream = new ByteArrayInputStream(ArrayUtil.combine(new byte[]{0x00, 0x04}, "1234".getBytes(StandardCharsets.UTF_8)));
|
||||
assertEquals("1234", serializer.read(inputStream, stringFieldData, this));
|
||||
inputStream.reset();
|
||||
assertArrayEquals("1234".getBytes(StandardCharsets.UTF_8), (byte[])serializer.read(inputStream, byteFieldData, this));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
TwoByteLengthPrefixedDataSerializer serializer = new TwoByteLengthPrefixedDataSerializer();
|
||||
List<BinaryFieldData> fieldDataList = BinaryFieldData.getStructFieldList(this.getClass());
|
||||
BinaryFieldData stringFieldData = fieldDataList.get(0);
|
||||
BinaryFieldData byteFieldData = fieldDataList.get(1);
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
// 0 Length
|
||||
outputStream.reset();outputStream.reset();
|
||||
serializer.write(outputStream, null, stringFieldData, this);
|
||||
assertArrayEquals(new byte[]{}, outputStream.toByteArray());
|
||||
outputStream.reset();
|
||||
serializer.write(outputStream, null, byteFieldData, this);
|
||||
assertArrayEquals(new byte[]{}, outputStream.toByteArray());
|
||||
|
||||
// 0 Length
|
||||
outputStream.reset();
|
||||
serializer.write(outputStream, "", stringFieldData, this);
|
||||
assertArrayEquals(new byte[]{0, 0}, outputStream.toByteArray());
|
||||
outputStream.reset();
|
||||
serializer.write(outputStream, new byte[0], byteFieldData, this);
|
||||
assertArrayEquals(new byte[]{0, 0}, outputStream.toByteArray());
|
||||
|
||||
// String "1234"
|
||||
outputStream.reset();
|
||||
serializer.write(outputStream, "1234", stringFieldData, this);
|
||||
assertArrayEquals(new byte[]{0, 4, 49, 50, 51, 52}, outputStream.toByteArray());
|
||||
outputStream.reset();
|
||||
serializer.write(outputStream, "1234".getBytes(StandardCharsets.UTF_8), byteFieldData, this);
|
||||
assertArrayEquals(new byte[]{0, 4, 49, 50, 51, 52}, outputStream.toByteArray());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue