-
Notifications
You must be signed in to change notification settings - Fork 359
feat(java): add xlang tests for Union types #3076
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
Open
zhan7236
wants to merge
3
commits into
apache:main
Choose a base branch
from
zhan7236:feat/java-union-xlang-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1575,6 +1575,109 @@ public void testEnumSchemaEvolutionCompatible() throws java.io.IOException { | |
| Assert.assertNull(result2.f2); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Union type tests | ||
| // ============================================================================ | ||
|
|
||
| /** Struct containing Union fields of different arities (Union through Union6). */ | ||
| @Data | ||
| public static class StructWithUnionFields { | ||
| public org.apache.fory.type.union.Union union; | ||
|
||
| public org.apache.fory.type.union.Union2<String, Long> union2; | ||
| public org.apache.fory.type.union.Union3<Integer, String, Double> union3; | ||
| public org.apache.fory.type.union.Union4<Integer, String, Double, Boolean> union4; | ||
| public org.apache.fory.type.union.Union5<Integer, String, Double, Boolean, Long> union5; | ||
| public org.apache.fory.type.union.Union6<Integer, String, Double, Boolean, Long, Float> union6; | ||
| } | ||
|
|
||
| @Test | ||
| public void testStructWithUnionFields() throws java.io.IOException { | ||
| String caseName = "test_struct_with_union_fields"; | ||
| Fory fory = | ||
| Fory.builder() | ||
| .withLanguage(Language.XLANG) | ||
| .withCompatibleMode(CompatibleMode.SCHEMA_CONSISTENT) | ||
| .withCodegen(false) | ||
| .build(); | ||
| fory.register(StructWithUnionFields.class, 200); | ||
|
|
||
| StructWithUnionFields obj = new StructWithUnionFields(); | ||
| obj.union = new org.apache.fory.type.union.Union(0, 42); | ||
| obj.union2 = org.apache.fory.type.union.Union2.ofT1("hello"); | ||
| obj.union3 = org.apache.fory.type.union.Union3.ofT2("world"); | ||
| obj.union4 = org.apache.fory.type.union.Union4.ofT3(3.14); | ||
| obj.union5 = org.apache.fory.type.union.Union5.ofT4(true); | ||
| obj.union6 = org.apache.fory.type.union.Union6.ofT5(999L); | ||
|
|
||
| MemoryBuffer buffer = MemoryUtils.buffer(128); | ||
| fory.serialize(buffer, obj); | ||
|
|
||
| ExecutionContext ctx = prepareExecution(caseName, buffer.getBytes(0, buffer.writerIndex())); | ||
| runPeer(ctx); | ||
|
|
||
| MemoryBuffer buffer2 = readBuffer(ctx.dataFile()); | ||
| StructWithUnionFields result = (StructWithUnionFields) fory.deserialize(buffer2); | ||
|
|
||
| Assert.assertEquals(result.union.getIndex(), 0); | ||
| Assert.assertEquals(result.union.getValue(), 42); | ||
| Assert.assertEquals(result.union2.getIndex(), 0); | ||
| Assert.assertEquals(result.union2.getValue(), "hello"); | ||
| Assert.assertTrue(result.union2.isT1()); | ||
| Assert.assertEquals(result.union3.getIndex(), 1); | ||
| Assert.assertEquals(result.union3.getValue(), "world"); | ||
| Assert.assertTrue(result.union3.isT2()); | ||
| Assert.assertEquals(result.union4.getIndex(), 2); | ||
| Assert.assertEquals((Double) result.union4.getValue(), 3.14, 0.001); | ||
| Assert.assertTrue(result.union4.isT3()); | ||
| Assert.assertEquals(result.union5.getIndex(), 3); | ||
| Assert.assertEquals(result.union5.getValue(), true); | ||
| Assert.assertTrue(result.union5.isT4()); | ||
| Assert.assertEquals(result.union6.getIndex(), 4); | ||
| Assert.assertEquals(result.union6.getValue(), 999L); | ||
| Assert.assertTrue(result.union6.isT5()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTopLevelUnion() throws java.io.IOException { | ||
| String caseName = "test_top_level_union"; | ||
| Fory fory = | ||
| Fory.builder() | ||
| .withLanguage(Language.XLANG) | ||
| .withCompatibleMode(CompatibleMode.SCHEMA_CONSISTENT) | ||
| .withCodegen(false) | ||
| .build(); | ||
|
|
||
| // Test Union with integer value | ||
| org.apache.fory.type.union.Union union1 = new org.apache.fory.type.union.Union(0, 42); | ||
| MemoryBuffer buffer = MemoryUtils.buffer(64); | ||
| fory.serialize(buffer, union1); | ||
|
|
||
| ExecutionContext ctx1 = | ||
| prepareExecution(caseName + "_int", buffer.getBytes(0, buffer.writerIndex())); | ||
| runPeer(ctx1); | ||
|
|
||
| MemoryBuffer buffer2 = readBuffer(ctx1.dataFile()); | ||
| org.apache.fory.type.union.Union result1 = | ||
| (org.apache.fory.type.union.Union) fory.deserialize(buffer2); | ||
| Assert.assertEquals(result1.getIndex(), 0); | ||
| Assert.assertEquals(result1.getValue(), 42); | ||
|
|
||
| // Test Union with string value | ||
| org.apache.fory.type.union.Union union2 = new org.apache.fory.type.union.Union(1, "hello"); | ||
| buffer = MemoryUtils.buffer(64); | ||
| fory.serialize(buffer, union2); | ||
|
|
||
| ExecutionContext ctx2 = | ||
| prepareExecution(caseName + "_str", buffer.getBytes(0, buffer.writerIndex())); | ||
| runPeer(ctx2); | ||
|
|
||
| MemoryBuffer buffer3 = readBuffer(ctx2.dataFile()); | ||
| org.apache.fory.type.union.Union result2 = | ||
| (org.apache.fory.type.union.Union) fory.deserialize(buffer3); | ||
| Assert.assertEquals(result2.getIndex(), 1); | ||
| Assert.assertEquals(result2.getValue(), "hello"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private void assertStringEquals(Object actual, Object expected, boolean useToString) { | ||
| if (useToString) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add cpp tests in
cpp/fory/serialization/xlang_test_main.cc?Only added cpp tests, and make it pass, we can ensure union xlang serialization is OK