You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by ie...@apache.org on 2020/11/17 13:37:24 UTC
[avro] 03/03: AVRO-2702: Add unit test.
This is an automated email from the ASF dual-hosted git repository.
iemejia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
commit 75c4dd3436cbc8b86a04e9681ad83a2f2a22334e
Author: Ryan Skraba <ry...@skraba.com>
AuthorDate: Mon Nov 16 18:46:18 2020 +0100
AVRO-2702: Add unit test.
---
.../io/parsing/TestResolvingGrammarGenerator2.java | 48 ++++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator2.java b/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator2.java
index 940defe..fc69801 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator2.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/io/parsing/TestResolvingGrammarGenerator2.java
@@ -17,6 +17,8 @@
*/
package org.apache.avro.io.parsing;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
@@ -24,9 +26,19 @@ import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.SchemaValidationException;
import org.apache.avro.SchemaValidatorBuilder;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
import org.junit.Assert;
import org.junit.Test;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.not;
+
/** ResolvingGrammarGenerator tests that are not Parameterized. */
public class TestResolvingGrammarGenerator2 {
@Test
@@ -111,4 +123,40 @@ public class TestResolvingGrammarGenerator2 {
Symbol.UnionAdjustAction action = (Symbol.UnionAdjustAction) grammar.production[1];
Assert.assertEquals(4, action.rindex);
}
+
+ @Test
+ public void testAvro2702StringProperties() throws IOException {
+
+ // Create a nested record schema with string fields at two levels.
+ Schema inner = SchemaBuilder.builder().record("B").fields().requiredString("b1").endRecord();
+ Schema outer = SchemaBuilder.builder().record("A").fields().requiredString("a1").name("inner").type().unionOf()
+ .nullType().and().type(inner).endUnion().noDefault().endRecord();
+
+ // Make a copy with the two string fields annotated.
+ Schema outer2 = new Schema.Parser().parse(outer.toString());
+ outer2.getField("a1").schema().addProp(GenericData.STRING_PROP, "String");
+ Schema inner2 = outer2.getField("inner").schema().getTypes().get(1);
+ inner2.getField("b1").schema().addProp(GenericData.STRING_PROP, "String");
+
+ // The two schemas are not the same, but they serialize to the same.
+ assertThat(outer, not(outer2));
+
+ // This is a serialized record.
+ byte[] serialized = { 2, 'a', // a1 is a one character string
+ 2, // Pick the non-null UNION branch and
+ 2, 'b' // Another one character string
+ };
+
+ GenericRecord out = null;
+ try (ByteArrayInputStream bais = new ByteArrayInputStream(serialized)) {
+ Decoder decoder = DecoderFactory.get().binaryDecoder(bais, null);
+ DatumReader<GenericRecord> r = new GenericDatumReader<>(outer, outer2, GenericData.get());
+ out = r.read(null, decoder);
+ }
+
+ // Assert that the two fields were read and are of type String.
+ assertThat(out.get("a1"), instanceOf(String.class));
+ assertThat(((GenericRecord) out.get("inner")).get("b1"), instanceOf(String.class));
+ }
+
}