Skip to content

Commit

Permalink
Update the IO done in the test to read from resources / write to temp…
Browse files Browse the repository at this point in the history
… folders
  • Loading branch information
nick-someone committed Aug 31, 2022
1 parent 8fee741 commit 55089a8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
2 changes: 0 additions & 2 deletions java/pom.xml
Expand Up @@ -57,8 +57,6 @@
</snapshotRepository>
</distributionManagement>
<build>
<sourceDirectory>src/main</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
36 changes: 15 additions & 21 deletions java/src/test/java/JavaTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion java/src/test/java/NamespaceA
2 changes: 1 addition & 1 deletion java/src/test/java/NamespaceC
Binary file added java/src/test/resources/monsterdata_test.mon
Binary file not shown.

0 comments on commit 55089a8

Please sign in to comment.