Skip to content

Commit

Permalink
Adds read-write API generation support for Java classes generation (#100
Browse files Browse the repository at this point in the history
)

* Adds class template changes for read API generation
* Modifies current `is_built_in_type` filter for `JavaLanguage` to
change float to double
* Adds write API generation support
* Adds roundtrip tests in CodeGenTest.java and Rust project
* Add setter support for Java classes
* Renamed `value` property of field to `value_type`
* Changes Rust and Java demo project tests to run throughbad/invalid
input Ion files and assert Exception is thrown
* Changes Rust and java dmeo projects tests to run through each
good/valid input Ion files and assert equivelnce for roundtrip test
* Adds valid and invalid input Ion files for tests
  • Loading branch information
desaikd authored Apr 25, 2024
1 parent e8c44a7 commit 515bd90
Show file tree
Hide file tree
Showing 20 changed files with 447 additions and 69 deletions.
8 changes: 8 additions & 0 deletions code-gen-projects/input/bad/nested_struct/mismatched_type.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// nested struct with type mismatched fields
{
A: "hello",
B: 12,
C: {
D: 1e0, // expected type: bool
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// struct with mismatched sequence element
{
A: "hello",
B: 12,
C: [1, 2, 3],
D: 10e2
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// simple struct with type mismatched fields
{
A: "hello",
B: false, // expected field type: int
C: ["foo", "bar", "baz"],
D: 10e2
}

8 changes: 8 additions & 0 deletions code-gen-projects/input/good/nested_struct/empty_values.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// nested struct with empty string and zeros
{
C: {
D: false,
},
A: "",
B: 0,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// nested struct with all valid fields
{
A: "hello",
B: 12,
C: {
D: false,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// nested struct with unordered fields
{
B: 12,
A: "hello",
C: {
D: false,
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// struct with empty list, empty string and zeros
{
C: [],
A: "",
B: 0,
D: 0e0,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// simple struct with all valid fields
{
A: "hello",
B: 12,
C: ["foo", "bar", "baz"],
D: 10e2
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

// struct with unordered fields
{
C: ["foo", "bar", "baz"],
A: "hello",
B: 12,
C: ["foo", "bar", "baz"],
}
D: 10e2,
}
4 changes: 4 additions & 0 deletions code-gen-projects/java/code-gen-demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ tasks {
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
testLogging {
showStandardStreams = true
events("skipped", "failed")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,133 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import com.amazon.ion.system.IonReaderBuilder;
import com.amazon.ion.IonReader;
import com.amazon.ion.system.IonTextWriterBuilder;
import com.amazon.ion.IonWriter;
import com.amazon.ion.IonSystem;
import com.amazon.ion.system.IonSystemBuilder;
import com.amazon.ion.IonLoader;
import com.amazon.ion.IonException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;

class CodeGenTest {
// TODO: Add roundtrip tests after read-write APIs are supported for Java code generation
private static final IonSystem ionSystem = IonSystemBuilder.standard().build();
private static final IonLoader ionLoader = ionSystem.getLoader();

@Test void getterAndSetterTestForStructWithFields() {
ArrayList<String> a = new ArrayList<String>();
a.add("foo");
a.add("bar");
a.add("baz");
StructWithFields s = new StructWithFields("hello", 12, new AnonymousType2(a));
StructWithFields s = new StructWithFields("hello", 12, new AnonymousType2(a), 10e2);

// getter tests for `StructWithFields`
assertEquals("hello", s.getA(), "s.getA() should return \"hello\"");
assertEquals(12, s.getB(), "s.getB() should return `12`");
assertEquals(3, s.getC().getValue().size(), "s.getC().getValue() should return ArrayList fo size 3");
assertEquals(10e2, s.getD(), "s.getD() should return `10e2`");

// setter tests for `StructWithFields`
s.setA("hi");
assertEquals("hi", s.getA(), "s.getA() should return \"hi\"");
s.setB(6);
assertEquals(6, s.getB(), "s.getB() should return `6`");
s.setC(new AnonymousType2(new ArrayList<String>()));
assertEquals(true, s.getC().getValue().isEmpty(), "s.getC().isEmpty() should return `true`");
s.setD(11e3);
assertEquals(11e3 ,s.getD(), "s.getD() should return `11e3`");
}

@Test void getterAndSetterTestForNestedStruct() {
NestedStruct n = new NestedStruct("hello", 12, new AnonymousType1(false));
assertEquals("hello", n.getA(), "n.getA() should return \"hello\"");
assertEquals(12, n.getB(), "n.getB() should return `12`");
assertEquals(false, n.getC().getD(), "n.getC().getD() should return `false`");
// getter tests for `NestedStruct`
NestedStruct n = new NestedStruct("hello", 12, new AnonymousType1(false));
assertEquals("hello", n.getA(), "n.getA() should return \"hello\"");
assertEquals(12, n.getB(), "n.getB() should return `12`");
assertEquals(false, n.getC().getD(), "n.getC().getD() should return `false`");

// setter tests for `NestedStruct`
n.setA("hi");
assertEquals("hi", n.getA(), "s.getA() should return \"hi\"");
n.setB(6);
assertEquals(6, n.getB(), "s.getB() should return `6`");
n.getC().setD(true);
assertEquals(true, n.getC().getD(), "s.getC().getD() should return `true`");
}

@Test void roundtripGoodTestForStructWithFields() throws IOException {
File dir = new File("./../../input/good/struct_with_fields");
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
reader.next();
StructWithFields s = StructWithFields.readFrom(reader);
IonWriter writer = b.build(out);
s.writeTo(writer);
writer.close();
assertEquals(ionLoader.load(f), ionLoader.load(out.toByteArray()));
}
}
}

@Test void roundtripBadTestForStructWithFields() throws IOException {
File dir = new File("./../../input/bad/struct_with_fields");
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
reader.next();
assertThrows(Throwable.class, () -> { StructWithFields s = StructWithFields.readFrom(reader); });
}
}
}

@Test void roundtripGoodTestForNestedStruct() throws IOException {
File dir = new File("./../../input/good/nested_struct");
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
reader.next();
NestedStruct n = NestedStruct.readFrom(reader);
IonWriter writer = b.build(out);
n.writeTo(writer);
writer.close();
assertEquals(ionLoader.load(f), ionLoader.load(out.toByteArray()));
}
}
}

@Test void roundtripBadTestForNestedStruct() throws IOException {
File dir = new File("./../../input/bad/nested_struct");
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
reader.next();
assertThrows(Throwable.class, () -> { NestedStruct n = NestedStruct.readFrom(reader); });
}
}
}
}
Loading

0 comments on commit 515bd90

Please sign in to comment.