This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 021c01c Squashed commit of the following:
021c01c is described below
commit 021c01c4c2756f793a4eee3a0f914c2c131823ac
Author: Daniel Kulp <[email protected]>
AuthorDate: Tue Nov 20 14:44:26 2018 -0500
Squashed commit of the following:
commit a3f86c2874ad6d86b11fc2edc908065adcefdeda
Author: Daniel Kulp <[email protected]>
Date: Tue Nov 20 14:32:48 2018 -0500
Grab some more tests from other PR
commit 7d7822b5960c157ddf6db7fa15a3d797d0b286ee
Author: Joseph Pachod <[email protected]>
Date: Sun Dec 11 22:50:45 2016 +0100
new javadoc with same formatting as other javadoc
commit 82e8f7af6736034f4e85a4e6e1bcaa803019082e
Author: Joseph Pachod <[email protected]>
Date: Sun Dec 11 22:38:33 2016 +0100
restore initial imports
commit 0ca9815c8c884bc30c73b5c8a3c5d4c6e5188cee
Author: Joseph Pachod <[email protected]>
Date: Sun Dec 11 22:36:03 2016 +0100
javadoc
commit 551e1eb4a6bdedf00260ef90576490648f9b4658
Author: Joseph Pachod <[email protected]>
Date: Wed Dec 7 23:14:03 2016 +0100
format and clarify pre existing tests
commit 857da0c70a2db321d5bf521b901fe0c035f32edc
Author: Joseph Pachod <[email protected]>
Date: Wed Dec 7 22:53:13 2016 +0100
AVRO-1961: Java: add isUnion and isNullable on Schema class
Closes #169
---
.../avro/src/main/java/org/apache/avro/Schema.java | 20 ++++
.../src/test/java/org/apache/avro/TestSchema.java | 110 ++++++++++++++++++---
.../compiler/specific/TestSpecificCompiler.java | 70 +++++++++++++
.../compiler/src/test/resources/simple_record.avsc | 3 +-
4 files changed, 187 insertions(+), 16 deletions(-)
diff --git a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
index e99143d..a87c7a2 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
@@ -391,6 +391,26 @@ public abstract class Schema extends JsonProperties {
"default","doc","name","order","type","aliases");
}
+ /** Returns true if this record is an union type. */
+ public boolean isUnion(){
+ return this instanceof UnionSchema;
+ }
+
+ /** Returns true if this record is an union type containing null. */
+ public boolean isNullable() {
+ if (!isUnion()) {
+ return getType().equals(Schema.Type.NULL);
+ }
+
+ for (Schema schema : getTypes()) {
+ if (schema.isNullable()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
/** A field within a record. */
public static class Field extends JsonProperties {
diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
index 0ca1437..6a74438 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
@@ -20,6 +20,7 @@ package org.apache.avro;
import static org.junit.Assert.*;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import org.apache.avro.Schema.Field;
@@ -30,35 +31,36 @@ public class TestSchema {
@Test
public void testSplitSchemaBuild() {
Schema s = SchemaBuilder
- .record("HandshakeRequest")
- .namespace("org.apache.avro.ipc").fields()
- .name("clientProtocol").type().optional().stringType()
- .name("meta").type().optional().map().values().bytesType()
- .endRecord();
+ .record("HandshakeRequest")
+ .namespace("org.apache.avro.ipc").fields()
+ .name("clientProtocol").type().optional().stringType()
+ .name("meta").type().optional().map().values().bytesType()
+ .endRecord();
String schemaString = s.toString();
- final int mid = schemaString.length() / 2;
+ int mid = schemaString.length() / 2;
Schema parsedStringSchema = new
org.apache.avro.Schema.Parser().parse(s.toString());
Schema parsedArrayOfStringSchema =
- new org.apache.avro.Schema.Parser().parse
- (schemaString.substring(0, mid), schemaString.substring(mid));
+ new org.apache.avro.Schema.Parser().parse
+ (schemaString.substring(0, mid), schemaString.substring(mid));
assertNotNull(parsedStringSchema);
assertNotNull(parsedArrayOfStringSchema);
assertEquals(parsedStringSchema.toString(),
parsedArrayOfStringSchema.toString());
}
@Test
- public void testDuplicateRecordFieldName() {
- final Schema schema = Schema.createRecord("RecordName", null, null, false);
- final List<Field> fields = new ArrayList<>();
+ public void testDefaultRecordWithDuplicateFieldName() {
+ String recordName = "name";
+ Schema schema = Schema.createRecord(recordName, "doc", "namespace", false);
+ List<Field> fields = new ArrayList<>();
fields.add(new Field("field_name", Schema.create(Type.NULL), null, null));
fields.add(new Field("field_name", Schema.create(Type.INT), null, null));
try {
schema.setFields(fields);
fail("Should not be able to create a record with duplicate field name.");
} catch (AvroRuntimeException are) {
- assertTrue(are.getMessage().contains("Duplicate field field_name in
record RecordName"));
+ assertTrue(are.getMessage().contains("Duplicate field field_name in
record " + recordName));
}
}
@@ -74,8 +76,22 @@ public class TestSchema {
}
@Test
+ public void testRecordWithNullDoc() {
+ Schema schema = Schema.createRecord("name", null, "namespace", false);
+ String schemaString = schema.toString();
+ assertNotNull(schemaString);
+ }
+
+ @Test
+ public void testRecordWithNullNamespace() {
+ Schema schema = Schema.createRecord("name", "doc", null, false);
+ String schemaString = schema.toString();
+ assertNotNull(schemaString);
+ }
+
+ @Test
public void testEmptyRecordSchema() {
- Schema schema = Schema.createRecord("foobar", null, null, false);
+ Schema schema = createDefaultRecord();
String schemaString = schema.toString();
assertNotNull(schemaString);
}
@@ -90,7 +106,8 @@ public class TestSchema {
List<Field> fields = new ArrayList<>();
fields.add(new Field("field_name1", Schema.create(Type.NULL), null, null));
fields.add(new Field("field_name2", Schema.create(Type.INT), null, null));
- Schema schema = Schema.createRecord("foobar", null, null, false, fields);
+ Schema schema = createDefaultRecord();
+ schema.setFields(fields);
String schemaString = schema.toString();
assertNotNull(schemaString);
assertEquals(2, schema.getFields().size());
@@ -98,8 +115,71 @@ public class TestSchema {
@Test(expected = NullPointerException.class)
public void testSchemaWithNullFields() {
- Schema.createRecord("foobar", null, null, false, null);
+ Schema.createRecord("name", "doc", "namespace", false, null);
+ }
+
+ @Test
+ public void testIsUnionOnUnionWithMultipleElements() {
+ Schema schema = Schema.createUnion(Schema.create(Type.NULL),
Schema.create(Type.LONG));
+ assertTrue(schema.isUnion());
+ }
+
+ @Test
+ public void testIsUnionOnUnionWithOneElement() {
+ Schema schema = Schema.createUnion(Schema.create(Type.LONG));
+ assertTrue(schema.isUnion());
+ }
+
+ @Test
+ public void testIsUnionOnRecord() {
+ Schema schema = createDefaultRecord();
+ assertFalse(schema.isUnion());
+ }
+
+ @Test
+ public void testIsUnionOnArray() {
+ Schema schema = Schema.createArray(Schema.create(Type.LONG));
+ assertFalse(schema.isUnion());
+ }
+
+ @Test
+ public void testIsUnionOnEnum() {
+ Schema schema = Schema.createEnum("name", "doc", "namespace",
Collections.singletonList("value"));
+ assertFalse(schema.isUnion());
+ }
+
+ @Test
+ public void testIsUnionOnFixed() {
+ Schema schema = Schema.createFixed("name", "doc", "space", 10);
+ assertFalse(schema.isUnion());
+ }
+
+ @Test
+ public void testIsUnionOnMap() {
+ Schema schema = Schema.createMap(Schema.create(Type.LONG));
+ assertFalse(schema.isUnion());
}
+ @Test
+ public void testIsNullableOnUnionWithNull() {
+ Schema schema = Schema.createUnion(Schema.create(Type.NULL),
Schema.create(Type.LONG));
+ assertTrue(schema.isNullable());
+ }
+
+ @Test
+ public void testIsNullableOnUnionWithoutNull() {
+ Schema schema = Schema.createUnion(Schema.create(Type.LONG));
+ assertFalse(schema.isNullable());
+ }
+
+ @Test
+ public void testIsNullableOnRecord() {
+ Schema schema = createDefaultRecord();
+ assertFalse(schema.isNullable());
+ }
+
+ private Schema createDefaultRecord() {
+ return Schema.createRecord("name", "doc", "namespace", false);
+ }
}
diff --git
a/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
b/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
index d993672..e1210ac 100644
---
a/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
+++
b/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
@@ -606,4 +606,74 @@ public class TestSpecificCompiler {
Assert.assertEquals("Should use null for decimal if the flag is off",
"null", compiler.conversionInstance(uuidSchema));
}
+
+ @Test
+ public void testPojoWithOptionalTurnedOffByDefault() throws IOException {
+ SpecificCompiler compiler = createCompiler();
+ compiler.compileToDestination(this.src, OUTPUT_DIR.getRoot());
+ assertTrue(this.outputFile.exists());
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader(this.outputFile));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ line = line.trim();
+ assertFalse(line.contains("Optional"));
+ }
+ } finally {
+ if (reader != null)
+ reader.close();
+ }
+ }
+
+ @Test
+ public void testPojoWithOptionalCreatedWhenOptionTurnedOn() throws
IOException {
+ SpecificCompiler compiler = createCompiler();
+ compiler.setGettersReturnOptional(true);
+ //compiler.setCreateOptionalGetters(true);
+ compiler.compileToDestination(this.src, OUTPUT_DIR.getRoot());
+ assertTrue(this.outputFile.exists());
+ int optionalFound = 0;
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader(this.outputFile));
+
+ String line;
+ while ((line = reader.readLine()) != null) {
+ line = line.trim();
+ if (line.contains("Optional")) {
+ optionalFound++;
+ }
+ }
+ } finally {
+ if (reader != null)
+ reader.close();
+ }
+ assertEquals(9, optionalFound);
+ }
+ @Test
+ public void testPojoWithOptionalCreatedWhenOptionalForEverythingTurnedOn()
throws IOException {
+ SpecificCompiler compiler = createCompiler();
+ //compiler.setGettersReturnOptional(true);
+ compiler.setCreateOptionalGetters(true);
+ compiler.compileToDestination(this.src, OUTPUT_DIR.getRoot());
+ assertTrue(this.outputFile.exists());
+ int optionalFound = 0;
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader(this.outputFile));
+
+ String line;
+ while ((line = reader.readLine()) != null) {
+ line = line.trim();
+ if (line.contains("Optional")) {
+ optionalFound++;
+ }
+ }
+ } finally {
+ if (reader != null)
+ reader.close();
+ }
+ assertEquals(17, optionalFound);
+ }
}
diff --git a/lang/java/compiler/src/test/resources/simple_record.avsc
b/lang/java/compiler/src/test/resources/simple_record.avsc
index 85781c5..d78fd17 100644
--- a/lang/java/compiler/src/test/resources/simple_record.avsc
+++ b/lang/java/compiler/src/test/resources/simple_record.avsc
@@ -2,6 +2,7 @@
"type": "record",
"name": "SimpleRecord",
"fields" : [
- {"name": "value", "type": "int"}
+ {"name": "value", "type": "int"},
+ {"name": "nullableValue", "type": ["null","int"], "doc" : "doc"}
]
}
\ No newline at end of file