package mc.nbt.io; import mc.nbt.TypeTag; import mc.nbt.tag.*; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; class NbtOutputStreamTest { private ByteArrayOutputStream baos; private NbtOutputStream nbtOutputStream; @BeforeEach void before() throws IOException { baos = new ByteArrayOutputStream(); nbtOutputStream = new NbtOutputStream(baos); } @Test void testWriteTagEnd() throws IOException { nbtOutputStream.writeTag(new TagEnd()); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.END, tag.getType()); assertNull(tag.getName()); } private static Stream streamOfTagByte() { final byte value = RandomUtils.nextBytes(1)[0]; return Stream.of( Arguments.of(new TagByte(value)), Arguments.of(new TagByte(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagByte") void testWriteTagByte(TagByte tagByte) throws IOException { nbtOutputStream.writeTag(tagByte); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.BYTE, tag.getType()); assertEquals(tagByte.getValue(), tag.asTagByte().getValue()); assertName(tagByte, tag); } private static Stream streamOfTagShort() { final short value = (short) RandomUtils.nextInt(0, Short.MAX_VALUE); return Stream.of( Arguments.of(new TagShort(value)), Arguments.of(new TagShort(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagShort") void testWriteTagShort(TagShort tagShort) throws IOException { nbtOutputStream.writeTag(tagShort); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.SHORT, tag.getType()); assertEquals(tagShort.getValue(), tag.asTagShort().getValue()); assertName(tagShort, tag); } private static Stream streamOfTagInt() { final int value = RandomUtils.nextInt(); return Stream.of( Arguments.of(new TagInt(value)), Arguments.of(new TagInt(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagInt") void testWriteTagInt(TagInt tagInt) throws IOException { nbtOutputStream.writeTag(tagInt); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.INT, tag.getType()); assertEquals(tagInt.getValue(), tag.asTagInt().getValue()); assertName(tagInt, tag); } private static Stream streamOfTagLong() { final long value = RandomUtils.nextLong(); return Stream.of( Arguments.of(new TagLong(value)), Arguments.of(new TagLong(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagLong") void testWriteTagLong(TagLong tagLong) throws IOException { nbtOutputStream.writeTag(tagLong); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.LONG, tag.getType()); assertEquals(tagLong.getValue(), tag.asTagLong().getValue()); assertName(tagLong, tag); } private static Stream streamOfTagFloat() { final float value = RandomUtils.nextFloat(); return Stream.of( Arguments.of(new TagFloat(value)), Arguments.of(new TagFloat(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagFloat") void testWriteTagFloat(TagFloat tagFloat) throws IOException { nbtOutputStream.writeTag(tagFloat); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.FLOAT, tag.getType()); assertEquals(tagFloat.getValue(), tag.asTagFloat().getValue()); assertName(tagFloat, tag); } private static Stream streamOfTagDouble() { final double value = RandomUtils.nextDouble(); return Stream.of( Arguments.of(new TagDouble(value)), Arguments.of(new TagDouble(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagDouble") void testWriteTagDouble(TagDouble tagDouble) throws IOException { nbtOutputStream.writeTag(tagDouble); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.DOUBLE, tag.getType()); assertEquals(tagDouble.getValue(), tag.asTagDouble().getValue()); assertName(tagDouble, tag); } private static Stream streamOfTagByteArray() { final byte[] value = RandomUtils.nextBytes(RandomUtils.nextInt(1, 1000)); return Stream.of( Arguments.of(new TagByteArray(value)), Arguments.of(new TagByteArray(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagByteArray") void testWriteTagByteArray(TagByteArray tagByteArray) throws IOException { nbtOutputStream.writeTag(tagByteArray); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.BYTE_ARRAY, tag.getType()); assertArrayEquals(tagByteArray.getValue(), tag.asTagByteArray().getValue()); assertName(tagByteArray, tag); } private static Stream streamOfTagString() { final String value = randomString(); return Stream.of( Arguments.of(new TagString(value)), Arguments.of(new TagString(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagString") void testWriteTagString(TagString tagString) throws IOException { nbtOutputStream.writeTag(tagString); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.STRING, tag.getType()); assertEquals(tagString.getValue(), tag.asTagString().getValue()); assertName(tagString, tag); } private static Stream streamOfTagList() { final List value = new ArrayList<>(); value.add(new TagLong("TestName", RandomUtils.nextLong())); value.add(new TagLong(RandomUtils.nextLong())); value.add(new TagLong(RandomUtils.nextLong())); value.add(new TagLong(RandomUtils.nextLong())); return Stream.of( Arguments.of(new TagList(value)), Arguments.of(new TagList(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagList") void testWriteTagList(TagList tagList) throws IOException { nbtOutputStream.writeTag(tagList); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.LIST, tag.getType()); assertEquals(tagList.getValue().size(), tag.asTagList().getValue().size()); assertEquals(tagList.getTypeList(), tag.asTagList().getTypeList()); assertName(tagList, tag); for (int i = 0; i < tagList.getValue().size(); i++) { TagLong tagLong1 = tagList.getValue().get(i).asTagLong(); TagLong tagLong2 = tag.asTagList().getValue().get(i).asTagLong(); assertNull(tagLong2.getName()); assertEquals(tagLong1.getValue(), tagLong2.getValue()); } } private static Stream streamOfTagCompound() { final List value = new ArrayList<>(); value.add(new TagLong("TestName1", RandomUtils.nextLong())); value.add(new TagLong(RandomUtils.nextLong())); value.add(new TagLong("TestName3",RandomUtils.nextLong())); value.add(new TagLong(RandomUtils.nextLong())); return Stream.of( Arguments.of(new TagCompound(value)), Arguments.of(new TagCompound(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagCompound") void testWriteTagCompound(TagCompound tagCompound) throws IOException { nbtOutputStream.writeTag(tagCompound); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.COMPOUND, tag.getType()); assertEquals(tagCompound.getValue().size(), tag.asTagCompound().getValue().size()); assertName(tagCompound, tag); for (int i = 0; i < tagCompound.getValue().size(); i++) { TagLong tagLong1 = tagCompound.getValue().get(i).asTagLong(); TagLong tagLong2 = tag.asTagCompound().getValue().get(i).asTagLong(); assertNotNull(tagLong2.getName()); assertName(tagLong1, tagLong2); assertEquals(tagLong1.getValue(), tagLong2.getValue()); } } private static Stream streamOfTagIntArray() { final int[] value = new int[RandomUtils.nextInt(1, 1000)]; for (int i = 0; i < value.length; i++) { value[i] = RandomUtils.nextInt(); } return Stream.of( Arguments.of(new TagIntArray(value)), Arguments.of(new TagIntArray(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagIntArray") void testWriteTagIntArray(TagIntArray tagIntArray) throws IOException { nbtOutputStream.writeTag(tagIntArray); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.INT_ARRAY, tag.getType()); assertArrayEquals(tagIntArray.getValue(), tag.asTagIntArray().getValue()); assertName(tagIntArray, tag); } private static Stream streamOfTagLongArray() { final long[] value = new long[RandomUtils.nextInt(1, 1000)]; for (int i = 0; i < value.length; i++) { value[i] = RandomUtils.nextLong(); } return Stream.of( Arguments.of(new TagLongArray(value)), Arguments.of(new TagLongArray(randomString(), value)) ); } @ParameterizedTest @MethodSource("streamOfTagLongArray") void testWriteTagLongArray(TagLongArray tagLongArray) throws IOException { nbtOutputStream.writeTag(tagLongArray); Tag tag = createNbtInputStream().readTag(); assertEquals(TypeTag.LONG_ARRAY, tag.getType()); assertArrayEquals(tagLongArray.getValue(), tag.asTagLongArray().getValue()); assertName(tagLongArray, tag); } private void assertName(Tag expectedTag, Tag actualTag) { if (expectedTag.getName() == null) { assertTrue(actualTag.getName().isEmpty()); } else { assertEquals(expectedTag.getName(), actualTag.getName()); } } private static String randomString() { return RandomStringUtils.randomAscii(RandomUtils.nextInt(1, Short.MAX_VALUE)); } private NbtInputStream createNbtInputStream() throws IOException { return new NbtInputStream(new ByteArrayInputStream(baos.toByteArray())); } }