Skip to content

Commit

Permalink
improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Dec 17, 2018
1 parent 152bccf commit f1f2c54
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/main/java/net/querz/nbt/ArrayTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.lang.reflect.Array;

/**
* ArrayTag is an abstract representation of any NBT array tag.
* For implementations see {@link ByteArrayTag}, {@link IntArrayTag}, {@link LongArrayTag}.
* @param <T> The array type.
* */
public abstract class ArrayTag<T> extends Tag<T> {

public ArrayTag() {}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/net/querz/nbt/mca/MCAFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public int serialize(RandomAccessFile raf, boolean changeLastUpdate) throws IOEx
int chunkXOffset = MCAUtil.regionToChunk(regionX);
int chunkZOffset = MCAUtil.regionToChunk(regionZ);

if (chunks == null) {
return 0;
}

for (int cx = 0; cx < 32; cx++) {
for (int cz = 0; cz < 32; cz++) {
int index = getChunkIndex(cx, cz);
Expand All @@ -104,15 +108,15 @@ public int serialize(RandomAccessFile raf, boolean changeLastUpdate) throws IOEx
raf.writeByte(globalOffset & 0xFF);
raf.writeByte(sectors);

//write timestamp to tmp file
// write timestamp
raf.seek(index * 4 + 4096);
raf.writeInt(changeLastUpdate ? timestamp : chunk.getLastMCAUpdate());

globalOffset += sectors;
}
}

//padding
// padding
if (lastWritten % 4096 != 0) {
raf.seek(globalOffset * 4096 - 1);
raf.write(0);
Expand Down
19 changes: 17 additions & 2 deletions src/test/java/net/querz/nbt/NBTTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void tearDown() throws Exception {
TagFactory.unregisterCustomTag(100);
TagFactory.unregisterCustomTag(110);
TagFactory.unregisterCustomTag(120);
cleanupTmpDir();
// cleanupTmpDir();
}

protected byte[] serialize(Tag<?> tag) {
Expand Down Expand Up @@ -151,10 +151,16 @@ protected <T, E extends Exception> void assertThrowsException(ExceptionSupplier<
}

protected <T, E extends Exception> T assertThrowsNoException(ExceptionSupplier<T, E> r) {
return assertThrowsNoException(r, false);
}

protected <T, E extends Exception> T assertThrowsNoException(ExceptionSupplier<T, E> r, boolean printStackTrace) {
try {
return r.run();
} catch (Exception ex) {
ex.printStackTrace();
if (printStackTrace) {
ex.printStackTrace();
}
TestCase.fail("Threw exception " + ex.getClass().getName() + " with message \"" + ex.getMessage() + "\"");
}
return null;
Expand Down Expand Up @@ -218,6 +224,15 @@ protected File getNewTmpFile(String name) {
return tmpFile;
}

protected File getTmpFile(String name) {
String workingDir = System.getProperty("user.dir");
File tmpDir = new File(workingDir, "tmp");
if (!tmpDir.exists()) {
tmpDir.mkdirs();
}
return new File(tmpDir, name);
}

protected File copyResourceToTmp(String resource) {
URL resFileURL = getClass().getClassLoader().getResource(resource);
TestCase.assertNotNull(resFileURL);
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/net/querz/nbt/TagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,14 @@ public void testMakeMyCoverageGreatAgain() {
assertThrowsException(() -> NBTUtil.writeTag(null, (File) null), NullPointerException.class);
assertThrowsException(() -> NBTUtil.writeTag(null, null, (File) null, false), NullPointerException.class);
assertThrowsException(() -> NBTUtil.writeTag(null, null, (String) null, false), NullPointerException.class);

CompoundTag dummy = new CompoundTag();
assertThrowsNoException(() -> NBTUtil.writeTag(dummy, getNewTmpFile("coverage.dat")));
assertThrowsNoException(() -> NBTUtil.writeTag(dummy, getNewTmpFile("coverage.dat").getAbsolutePath()));
assertThrowsNoException(() -> NBTUtil.writeTag(dummy, getNewTmpFile("coverage.dat"), true));
assertThrowsNoException(() -> NBTUtil.writeTag(dummy, getNewTmpFile("coverage.dat").getAbsolutePath(), true));
assertThrowsNoException(() -> NBTUtil.writeTag(dummy, "foo", getNewTmpFile("coverage.dat")));
assertThrowsNoException(() -> NBTUtil.writeTag(dummy, "foo", getNewTmpFile("coverage.dat").getAbsolutePath()));
assertThrowsNoException(() -> NBTUtil.writeTag(dummy, "foo", getNewTmpFile("coverage.dat").getAbsolutePath(), true));
}
}
8 changes: 7 additions & 1 deletion src/test/java/net/querz/nbt/mca/MCAUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public void testMakeMyCoverageGreatAgain() {
assertThrowsException(() -> MCAUtil.writeMCAFile(null, (File) null), NullPointerException.class);
assertThrowsException(() -> MCAUtil.writeMCAFile(null, (String) null, false), NullPointerException.class);
assertThrowsException(() -> MCAUtil.readMCAFile("r.a.b.mca"), IllegalArgumentException.class);
assertThrowsException(() -> new MCAFile(0, 0).serialize(null), NullPointerException.class);
assertThrowsNoException(() -> new MCAFile(0, 0).serialize(null)); // empty MCAFile will not even attempt to write to file

// test overwriting file
MCAFile m = new MCAFile(0, 0);
m.setChunk(0, Chunk.newChunk());
assertThrowsNoException(() -> MCAUtil.writeMCAFile(m, getTmpFile("r.0.0.mca"), false), true);
assertThrowsNoException(() -> MCAUtil.writeMCAFile(m, getTmpFile("r.0.0.mca"), false), true);
}
}

0 comments on commit f1f2c54

Please sign in to comment.