Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds read-write API generation support for Java code generation #100

Merged
merged 8 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
desaikd marked this conversation as resolved.
Show resolved Hide resolved
{
A: "hello",
B: 12,
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() {
desaikd marked this conversation as resolved.
Show resolved Hide resolved
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
Loading