From 55089a8348f26940f0dfd0e663494dcc7b38762f Mon Sep 17 00:00:00 2001 From: Nick Glorioso Date: Wed, 31 Aug 2022 14:14:51 -0500 Subject: [PATCH] Update the IO done in the test to read from resources / write to temp folders --- java/pom.xml | 2 -- java/src/test/java/JavaTest.java | 36 ++++++++----------- java/src/test/java/NamespaceA | 2 +- java/src/test/java/NamespaceC | 2 +- java/src/test/resources/monsterdata_test.mon | Bin 0 -> 600 bytes 5 files changed, 17 insertions(+), 25 deletions(-) create mode 100644 java/src/test/resources/monsterdata_test.mon diff --git a/java/pom.xml b/java/pom.xml index f5a96c40d0e3..cb03ea521c49 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -57,8 +57,6 @@ - src/main - src/test maven-compiler-plugin diff --git a/java/src/test/java/JavaTest.java b/java/src/test/java/JavaTest.java index dde51f586a45..368e55174c0e 100644 --- a/java/src/test/java/JavaTest.java +++ b/java/src/test/java/JavaTest.java @@ -4,6 +4,8 @@ import DictionaryLookup.*; import MyGame.Example.*; +import com.google.common.io.ByteStreams; +import java.nio.file.Files; import optional_scalars.ScalarStuff; import optional_scalars.OptionalByte; import MyGame.MonsterExtra; @@ -33,6 +35,8 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -56,31 +60,20 @@ @RunWith(JUnit4.class) public class JavaTest { + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + @org.junit.Test - public void mainTest() { + public void mainTest() throws IOException { // First, let's test reading a FlatBuffer generated by C++ code: // This file was generated from monsterdata_test.json - - byte[] data = null; - File file = new File("monsterdata_test.mon"); - RandomAccessFile f = null; - try { - f = new RandomAccessFile(file, "r"); - data = new byte[(int)f.length()]; - f.readFully(data); - f.close(); - } catch(java.io.IOException e) { - System.out.println("FlatBuffers test: couldn't read file"); - return; - } + byte[] data = ByteStreams.toByteArray( + JavaTest.class.getClassLoader().getResourceAsStream("monsterdata_test.mon")); // Now test it: - ByteBuffer bb = ByteBuffer.wrap(data); TestBuffer(bb); TestPackUnpack(bb); - - // Second, let's create a FlatBuffer from scratch in Java, and test it also. } @org.junit.Test @@ -279,13 +272,14 @@ static void TestExtendedBuffer(ByteBuffer bb) { TestEq(ByteBuffer.wrap(inventory), monsterObject.inventoryAsByteBuffer()); } - @org.junit.Test public void TestByteBufferFactory() { + @org.junit.Test public void TestByteBufferFactory() throws IOException { + File file = tempFolder.newFile("javatest.bin"); final class MappedByteBufferFactory extends FlatBufferBuilder.ByteBufferFactory { @Override public ByteBuffer newByteBuffer(int capacity) { ByteBuffer bb; try { - RandomAccessFile f = new RandomAccessFile("javatest.bin", "rw"); + RandomAccessFile f = new RandomAccessFile(file, "rw"); bb = f.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, capacity).order(ByteOrder.LITTLE_ENDIAN); f.close(); } catch(Throwable e) { @@ -325,7 +319,7 @@ public ByteBuffer newByteBuffer(int capacity) { TestEq(count, array.length); } - static void TestBuilderBasics(FlatBufferBuilder fbb, boolean sizePrefix) { + void TestBuilderBasics(FlatBufferBuilder fbb, boolean sizePrefix) { int[] names = {fbb.createString("Frodo"), fbb.createString("Barney"), fbb.createString("Wilma")}; int[] off = new int[3]; Monster.startMonster(fbb); @@ -388,7 +382,7 @@ static void TestBuilderBasics(FlatBufferBuilder fbb, boolean sizePrefix) { try { String filename = "monsterdata_java_wire" + (sizePrefix ? "_sp" : "") + ".mon"; - FileChannel fc = new FileOutputStream(filename).getChannel(); + FileChannel fc = new FileOutputStream(tempFolder.newFile(filename)).getChannel(); fc.write(fbb.dataBuffer().duplicate()); fc.close(); } catch(java.io.IOException e) { diff --git a/java/src/test/java/NamespaceA b/java/src/test/java/NamespaceA index 539eb47ad491..37a8d897e4c6 120000 --- a/java/src/test/java/NamespaceA +++ b/java/src/test/java/NamespaceA @@ -1 +1 @@ -../../../../tests/NamespaceA \ No newline at end of file +../../../../tests/namespace_test/NamespaceA \ No newline at end of file diff --git a/java/src/test/java/NamespaceC b/java/src/test/java/NamespaceC index 06c597534445..c1e44a8c94b1 120000 --- a/java/src/test/java/NamespaceC +++ b/java/src/test/java/NamespaceC @@ -1 +1 @@ -../../../../tests/NamespaceC \ No newline at end of file +../../../../tests/namespace_test/NamespaceC \ No newline at end of file diff --git a/java/src/test/resources/monsterdata_test.mon b/java/src/test/resources/monsterdata_test.mon new file mode 100644 index 0000000000000000000000000000000000000000..da0ed8698fcafc71115525dc6fe40fead48515d1 GIT binary patch literal 600 zcmZ{hy-EX75QWcfepo{wDPn7@MFf=;7J};!lEP>}3kwSi7ZZq4vs)xsm~^QGA&-#4 z(uWYl!Z%1jpFk8Wtk!dGb|nZ-^39w(Gr2RnX#=S2?;S9pfdL%GG>Y^~B#9DMu}0+* zAcbv|afvo|QAHPh9Gbk`5jn;QPJc81i5e$0ShTDveRrMC<(I_fxHe~{qvOy52Bb*x zWW@X<*?8`Z#nrHPEAE-=lUGuTdi5k0Z1nD~KVQ1JW*k4pXHTQ@_f{O)#xCAZ7Iffr zhtIDak`d>1-v=@%u##mZqc+~2rJraZL0zi{CaStfxC&-Til|pT7C#SR7{+(+v^?|T zv_pX?ul~UEgOw@0OsyVmF~?1(b=LfQEb`Zy^Y?De