Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java?rev=1507862&r1=1507861&r2=1507862&view=diff ============================================================================== --- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java (original) +++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java Sun Jul 28 22:10:12 2013 @@ -26,18 +26,11 @@ public class TestSchema { @Test public void testSplitSchemaBuild() { Schema s = SchemaBuilder - .recordType("HandshakeRequest") - .namespace("org.apache.avro.ipc") - .unionType("clientProtocol", SchemaBuilder.unionType( - SchemaBuilder.NULL, - SchemaBuilder.STRING) - .build()) - .unionType("meta", SchemaBuilder.unionType( - SchemaBuilder.NULL, - SchemaBuilder.mapType(SchemaBuilder.BYTES) - .build()) - .build()) - .build(); + .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;
Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java?rev=1507862&r1=1507861&r2=1507862&view=diff ============================================================================== --- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java (original) +++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestSchemaBuilder.java Sun Jul 28 22:10:12 2013 @@ -17,26 +17,27 @@ */ package org.apache.avro; -import junit.framework.Assert; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.avro.Schema.Field.Order; import org.apache.avro.file.DataFileReader; import org.apache.avro.file.DataFileWriter; import org.apache.avro.generic.GenericData; import org.apache.avro.generic.GenericDatumReader; import org.apache.avro.generic.GenericDatumWriter; import org.apache.avro.generic.GenericRecordBuilder; -import org.apache.avro.reflect.ReflectDatumWriter; import org.codehaus.jackson.node.BooleanNode; import org.codehaus.jackson.node.NullNode; +import org.junit.Assert; import org.junit.Test; -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - public class TestSchemaBuilder { private static final File DIR = new File(System.getProperty("test.dir", "/tmp")); @@ -45,11 +46,11 @@ public class TestSchemaBuilder { @Test public void testRecord() { Schema schema = SchemaBuilder - .recordType("myrecord").namespace("org.example").aliases("oldrecord") - .requiredString("f0") - .requiredLong("f1").doc("This is f1") - .optionalBoolean("f2", true) - .build(); + .record("myrecord").namespace("org.example").aliases("oldrecord").fields() + .name("f0").aliases("f0alias").type().stringType().noDefault() + .name("f1").doc("This is f1").type().longType().noDefault() + .name("f2").type().nullable().booleanType().booleanDefault(true) + .endRecord(); Assert.assertEquals("myrecord", schema.getName()); Assert.assertEquals("org.example", schema.getNamespace()); @@ -60,6 +61,7 @@ public class TestSchemaBuilder { Assert.assertEquals( new Schema.Field("f0", Schema.create(Schema.Type.STRING), null, null), fields.get(0)); + Assert.assertTrue(fields.get(0).aliases().contains("f0alias")); Assert.assertEquals( new Schema.Field("f1", Schema.create(Schema.Type.LONG), "This is f1", null), fields.get(1)); @@ -71,177 +73,197 @@ public class TestSchemaBuilder { Assert.assertEquals(new Schema.Field("f2", optional, null, BooleanNode.getTrue()), fields.get(2)); } + + @Test + public void testDoc() { + Schema s = SchemaBuilder.fixed("myfixed").doc("mydoc").size(1); + Assert.assertEquals("mydoc", s.getDoc()); + } + + @Test + public void testProps() { + Schema s = SchemaBuilder.builder().intBuilder() + .prop("p1", "v1") + .prop("p2", "v2") + .prop("p2", "v2real") // overwrite + .endInt(); + @SuppressWarnings("deprecation") + int size = s.getProps().size(); + Assert.assertEquals(2, size); + Assert.assertEquals("v1", s.getProp("p1")); + Assert.assertEquals("v2real", s.getProp("p2")); + } @Test public void testNamespaces() { - Schema s1 = SchemaBuilder.recordType("myrecord").namespace("org.example") - .requiredInt("myint").build(); - Schema s2 = SchemaBuilder.recordType("org.example.myrecord") - .requiredInt("myint").build(); - Schema s3 = SchemaBuilder.recordType("org.example.myrecord").namespace("org.example2") - .requiredInt("myint").build(); - + Schema s1 = SchemaBuilder.record("myrecord") + .namespace("org.example") + .fields() + .name("myint").type().intType().noDefault() + .endRecord(); + Schema s2 = SchemaBuilder.record("org.example.myrecord") + .fields() + .name("myint").type().intType().noDefault() + .endRecord(); + Schema s3 = SchemaBuilder.record("org.example.myrecord") + .namespace("org.example2") + .fields() + .name("myint").type().intType().noDefault() + .endRecord(); + Schema s4 = SchemaBuilder.builder("org.example").record("myrecord") + .fields() + .name("myint").type().intType().noDefault() + .endRecord(); + Assert.assertEquals("myrecord", s1.getName()); Assert.assertEquals("myrecord", s2.getName()); Assert.assertEquals("myrecord", s3.getName()); + Assert.assertEquals("myrecord", s4.getName()); Assert.assertEquals("org.example", s1.getNamespace()); Assert.assertEquals("org.example", s2.getNamespace()); Assert.assertEquals("org.example", s3.getNamespace()); // namespace call is ignored + Assert.assertEquals("org.example", s4.getNamespace()); Assert.assertEquals("org.example.myrecord", s1.getFullName()); Assert.assertEquals("org.example.myrecord", s2.getFullName()); Assert.assertEquals("org.example.myrecord", s3.getFullName()); + Assert.assertEquals("org.example.myrecord", s4.getFullName()); } @Test(expected = NullPointerException.class) public void testMissingRecordName() { SchemaBuilder - .recordType(null) // null name - .requiredString("f0") - .build(); - } - - @Test - public void testError() { - Schema schema = SchemaBuilder - .errorType("myerror") - .requiredString("message") - .build(); - - Assert.assertEquals("myerror", schema.getName()); - Assert.assertTrue(schema.isError()); - } - - @Test - public void testRecordAll() throws IOException { - GenericData.Record defaultRecord = new GenericData.Record( - SchemaBuilder.recordType("nestedOptionalRecordWithDefault") - .requiredBoolean("nestedRequiredBoolean").build()); - defaultRecord.put("nestedRequiredBoolean", true); - - Schema schema = SchemaBuilder.recordType("recordAll") - .requiredBoolean("requiredBoolean") - .requiredBoolean("requiredBooleanWithDefault").defaultValue(true) // expert - .optionalBoolean("optionalBoolean") - .optionalBoolean("optionalBooleanWithDefault", true) - .requiredInt("requiredInt") - .optionalInt("optionalInt") - .optionalInt("optionalIntWithDefault", 1) - .requiredLong("requiredLong") - .optionalLong("optionalLong") - .optionalLong("optionalLongWithDefault", 1L) - .requiredFloat("requiredFloat") - .optionalFloat("optionalFloat") - .optionalFloat("optionalFloatWithDefault", 1.0f) - .requiredDouble("requiredDouble") - .optionalDouble("optionalDouble") - .optionalDouble("optionalDoubleWithDefault", 1.0) - .requiredBytes("requiredBytes") - .optionalBytes("optionalBytes") - .optionalBytes("optionalBytesWithDefault", new byte[]{(byte) 65}) - .requiredString("requiredString") - .optionalString("optionalString") - .optionalString("optionalStringWithDefault", "a") - .requiredRecord("requiredRecord", - SchemaBuilder.recordType("nestedRequiredRecord") - .requiredBoolean("nestedRequiredBoolean").build()) - .optionalRecord("optionalRecord", - SchemaBuilder.recordType("nestedOptionalRecord") - .requiredBoolean("nestedRequiredBoolean").build()) - .optionalRecord("optionalRecordWithDefault", - SchemaBuilder.recordType("nestedOptionalRecordWithDefault") - .requiredBoolean("nestedRequiredBoolean").build(), defaultRecord) - .requiredEnum("requiredEnum", - SchemaBuilder.enumType("requiredEnum", "a", "b").build()) - .optionalEnum("optionalEnum", - SchemaBuilder.enumType("optionalEnum", "a", "b").build()) - .optionalEnum("optionalEnumWithDefault", - SchemaBuilder.enumType("optionalEnumWithDefault", "a", "b").build(), "b") - .requiredArray("requiredArray", - SchemaBuilder.arrayType(SchemaBuilder.STRING).build()) - .optionalArray("optionalArray", - SchemaBuilder.arrayType(SchemaBuilder.STRING).build()) - .optionalArray("optionalArrayWithDefault", - SchemaBuilder.arrayType(SchemaBuilder.STRING).build(), - Collections.singletonList("a")) - .requiredMap("requiredMap", - SchemaBuilder.mapType(SchemaBuilder.STRING).build()) - .optionalMap("optionalMap", - SchemaBuilder.mapType(SchemaBuilder.STRING).build()) - .optionalMap("optionalMapWithDefault", - SchemaBuilder.mapType(SchemaBuilder.STRING).build(), - Collections.singletonMap("a", "b")) - .requiredFixed("requiredFixed", - SchemaBuilder.fixedType("requiredFixed", 1).build()) - .optionalFixed("optionalFixed", - SchemaBuilder.fixedType("optionalFixed", 1).build()) - .optionalFixed("optionalFixedWithDefault", - SchemaBuilder.fixedType("optionalFixedWithDefault", 1).build(), - new byte[]{(byte) 65}) - .unionType("unionType", SchemaBuilder.unionType(SchemaBuilder.LONG, - SchemaBuilder.NULL).build()) - .unionBoolean("unionBooleanWithDefault", true, SchemaBuilder.INT) - .unionInt("unionIntWithDefault", 1, SchemaBuilder.NULL) - .unionLong("unionLongWithDefault", 1L, SchemaBuilder.INT) - .unionFloat("unionFloatWithDefault", 1.0f, SchemaBuilder.INT) - .unionDouble("unionDoubleWithDefault", 1.0, SchemaBuilder.INT) - .unionBytes("unionBytesWithDefault", new byte[]{(byte) 65}, SchemaBuilder.INT) - .unionString("unionStringWithDefault", "a", SchemaBuilder.INT) - .unionRecord("unionRecordWithDefault", - SchemaBuilder.recordType("nestedUnionRecordWithDefault") - .requiredBoolean("nestedRequiredBoolean").build(), defaultRecord, - SchemaBuilder.INT) - .unionEnum("unionEnumWithDefault", - SchemaBuilder.enumType("nestedUnionEnumWithDefault", "a", "b").build(), "b", - SchemaBuilder.INT) - .unionArray("unionArrayWithDefault", - SchemaBuilder.arrayType(SchemaBuilder.STRING).build(), - Collections.singletonList("a"), - SchemaBuilder.INT) - .unionMap("unionMapWithDefault", - SchemaBuilder.mapType(SchemaBuilder.STRING).build(), - Collections.singletonMap("a", "b"), - SchemaBuilder.INT) - .unionFixed("unionFixedWithDefault", - SchemaBuilder.fixedType("nestedUnionFixedWithDefault", 1).build(), - new byte[]{(byte) 65}, - SchemaBuilder.INT) - .build(); - - Schema expected = new Schema.Parser().parse - (getClass().getResourceAsStream("/SchemaBuilder.avsc")); - - // To regenerate the expected schema, uncomment the following line - // and copy the test output to TestSchemaBuilder.avsc - //System.out.println(schema.toString(true)); - - Assert.assertEquals(expected.toString(true), schema.toString(true)); - - } - - private List<Schema.Field> fields(Schema.Field... fields) { - return Arrays.asList(fields); - } + .record(null).fields() // null name + .name("f0").type().stringType().noDefault() + .endRecord(); + } + + @Test + public void testBoolean() { + Schema.Type type = Schema.Type.BOOLEAN; + Schema simple = SchemaBuilder.builder().booleanType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .booleanBuilder().prop("p", "v").endBoolean(); + Assert.assertEquals(expected, built1); + } + + @Test + public void testInt() { + Schema.Type type = Schema.Type.INT; + Schema simple = SchemaBuilder.builder().intType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .intBuilder().prop("p", "v").endInt(); + Assert.assertEquals(expected, built1); + } + + @Test + public void testLong() { + Schema.Type type = Schema.Type.LONG; + Schema simple = SchemaBuilder.builder().longType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .longBuilder().prop("p", "v").endLong(); + Assert.assertEquals(expected, built1); + } + + @Test + public void testFloat() { + Schema.Type type = Schema.Type.FLOAT; + Schema simple = SchemaBuilder.builder().floatType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .floatBuilder().prop("p", "v").endFloat(); + Assert.assertEquals(expected, built1); + } + + @Test + public void testDuble() { + Schema.Type type = Schema.Type.DOUBLE; + Schema simple = SchemaBuilder.builder().doubleType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .doubleBuilder().prop("p", "v").endDouble(); + Assert.assertEquals(expected, built1); + } + + @Test + public void testString() { + Schema.Type type = Schema.Type.STRING; + Schema simple = SchemaBuilder.builder().stringType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .stringBuilder().prop("p", "v").endString(); + Assert.assertEquals(expected, built1); + } + + @Test + public void testBytes() { + Schema.Type type = Schema.Type.BYTES; + Schema simple = SchemaBuilder.builder().bytesType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .bytesBuilder().prop("p", "v").endBytes(); + Assert.assertEquals(expected, built1); + } + + @Test + public void testNull() { + Schema.Type type = Schema.Type.NULL; + Schema simple = SchemaBuilder.builder().nullType(); + Schema expected = primitive(type, simple); + Schema built1 = SchemaBuilder.builder() + .nullBuilder().prop("p", "v").endNull(); + Assert.assertEquals(expected, built1); + } + + + private Schema primitive(Schema.Type type, Schema bare) { + // test creation of bare schema by name + Schema bareByName = SchemaBuilder.builder().type(type.getName()); + Assert.assertEquals(Schema.create(type), bareByName); + Assert.assertEquals(bareByName, bare); + // return a schema with custom prop set + Schema p = Schema.create(type); + p.addProp("p", "v"); + return p; + } + + +// @Test +// public void testError() { +// Schema schema = SchemaBuilder +// .errorType("myerror") +// .requiredString("message") +// .build(); +// +// Assert.assertEquals("myerror", schema.getName()); +// Assert.assertTrue(schema.isError()); +// } @Test public void testRecursiveRecord() { - Schema schema = SchemaBuilder - .recordType("LongList") - .requiredLong("value") - .optionalRecord("next", SchemaBuilder.recordReference("LongList").build()) - .build(); + Schema schema = SchemaBuilder.record("LongList").fields() + .name("value").type().longType().noDefault() + .name("next").type().optional().type("LongList") + .endRecord(); Assert.assertEquals("LongList", schema.getName()); List<Schema.Field> fields = schema.getFields(); Assert.assertEquals(2, fields.size()); - Assert.assertEquals(new Schema.Field("value", Schema.create(Schema.Type.LONG), null, null), + Assert.assertEquals( + new Schema.Field("value", Schema.create(Schema.Type.LONG), null, null), fields.get(0)); - Assert.assertEquals(Schema.Type.UNION, fields.get(1).schema().getType()); + Assert.assertEquals( + Schema.Type.UNION, + fields.get(1).schema().getType()); - Assert.assertEquals(Schema.Type.NULL, fields.get(1).schema().getTypes().get(0) - .getType()); + Assert.assertEquals( + Schema.Type.NULL, + fields.get(1).schema().getTypes().get(0).getType()); Schema recordSchema = fields.get(1).schema().getTypes().get(1); Assert.assertEquals(Schema.Type.RECORD, recordSchema.getType()); Assert.assertEquals("LongList", recordSchema.getName()); @@ -250,47 +272,406 @@ public class TestSchemaBuilder { @Test public void testEnum() { - Schema schema = SchemaBuilder.enumType("myenum", "a", "b").build(); - Assert.assertEquals(Schema.createEnum("myenum", null, null, - Arrays.asList("a", "b")), schema); + List<String> symbols = Arrays.asList("a", "b"); + Schema expected = Schema.createEnum("myenum", null, null, symbols); + expected.addProp("p", "v"); + Schema schema = SchemaBuilder.enumeration("myenum") + .prop("p", "v").symbols("a", "b"); + Assert.assertEquals(expected, schema); } @Test - public void testArray() { - Schema schema = SchemaBuilder.arrayType(SchemaBuilder.LONG).build(); - Assert.assertEquals(Schema.createArray(Schema.create(Schema.Type.LONG)), schema); + public void testFixed() { + Schema expected = Schema.createFixed("myfixed", null, null, 16); + expected.addAlias("myOldFixed"); + Schema schema = SchemaBuilder.fixed("myfixed") + .aliases("myOldFixed").size(16); + Assert.assertEquals(expected, schema); } @Test - public void testMap() { - Schema schema = SchemaBuilder.mapType(SchemaBuilder.LONG).build(); - Assert.assertEquals(Schema.createMap(Schema.create(Schema.Type.LONG)), schema); - } + public void testArray() { + Schema longSchema = Schema.create(Schema.Type.LONG); + Schema expected = Schema.createArray(longSchema); + + Schema schema1 = SchemaBuilder.array().items().longType(); + Assert.assertEquals(expected, schema1); + + Schema schema2 = SchemaBuilder.array().items(longSchema); + Assert.assertEquals(expected, schema2); + + Schema schema3 = SchemaBuilder.array().prop("p", "v") + .items().type("long"); + expected.addProp("p", "v"); + Assert.assertEquals(expected, schema3); +} @Test - public void testFixed() { - Schema schema = SchemaBuilder.fixedType("myfixed", 16).build(); - Assert.assertEquals(Schema.createFixed("myfixed", null, null, 16), schema); + public void testMap() { + Schema intSchema = Schema.create(Schema.Type.INT); + Schema expected = Schema.createMap(intSchema); + + Schema schema1 = SchemaBuilder.map().values().intType(); + Assert.assertEquals(expected, schema1); + + Schema schema2 = SchemaBuilder.map().values(intSchema); + Assert.assertEquals(expected, schema2); + + Schema schema3 = SchemaBuilder.map().prop("p", "v") + .values().type("int"); + expected.addProp("p", "v"); + Assert.assertEquals(expected, schema3); } @Test - public void testUnion() { - Schema schema = SchemaBuilder - .unionType(SchemaBuilder.LONG, SchemaBuilder.NULL) - .build(); + public void testUnionAndNullable() { List<Schema> types = new ArrayList<Schema>(); types.add(Schema.create(Schema.Type.LONG)); types.add(Schema.create(Schema.Type.NULL)); - Assert.assertEquals(Schema.createUnion(types), schema); + Schema expected = Schema.createUnion(types); + + Schema schema = SchemaBuilder.unionOf() + .longType().and() + .nullType().endUnion(); + Assert.assertEquals(expected, schema); + + schema = SchemaBuilder.nullable().longType(); + Assert.assertEquals(expected, schema); + } + + @Test + public void testFields() { + Schema rec = SchemaBuilder.record("Rec").fields() + .name("documented").doc("documented").type().nullType().noDefault() + .name("ascending").orderAscending().type().booleanType().noDefault() + .name("descending").orderDescending().type().floatType().noDefault() + .name("ignored").orderIgnore().type().doubleType().noDefault() + .name("aliased").aliases("anAlias").type().stringType().noDefault() + .endRecord(); + Assert.assertEquals("documented", rec.getField("documented").doc()); + Assert.assertEquals(Order.ASCENDING, rec.getField("ascending").order()); + Assert.assertEquals(Order.DESCENDING, rec.getField("descending").order()); + Assert.assertEquals(Order.IGNORE, rec.getField("ignored").order()); + Assert.assertTrue(rec.getField("aliased").aliases().contains("anAlias")); + } + + @Test + public void testFieldShortcuts() { + Schema full = SchemaBuilder.record("Blah").fields() + .name("rbool").type().booleanType().noDefault() + .name("obool").type().optional().booleanType() + .name("nbool").type().nullable().booleanType().booleanDefault(true) + .name("rint").type().intType().noDefault() + .name("oint").type().optional().intType() + .name("nint").type().nullable().intType().intDefault(1) + .name("rlong").type().longType().noDefault() + .name("olong").type().optional().longType() + .name("nlong").type().nullable().longType().longDefault(2L) + .name("rfloat").type().floatType().noDefault() + .name("ofloat").type().optional().floatType() + .name("nfloat").type().nullable().floatType().floatDefault(-1.1f) + .name("rdouble").type().doubleType().noDefault() + .name("odouble").type().optional().doubleType() + .name("ndouble").type().nullable().doubleType().doubleDefault(99.9d) + .name("rstring").type().stringType().noDefault() + .name("ostring").type().optional().stringType() + .name("nstring").type().nullable().stringType().stringDefault("def") + .name("rbytes").type().bytesType().noDefault() + .name("obytes").type().optional().bytesType() + .name("nbytes").type().nullable().bytesType().bytesDefault(new byte[] {1,2,3}) + .endRecord(); + + Schema shortcut = SchemaBuilder.record("Blah").fields() + .requiredBoolean("rbool") + .optionalBoolean("obool") + .nullableBoolean("nbool", true) + .requiredInt("rint") + .optionalInt("oint") + .nullableInt("nint", 1) + .requiredLong("rlong") + .optionalLong("olong") + .nullableLong("nlong", 2L) + .requiredFloat("rfloat") + .optionalFloat("ofloat") + .nullableFloat("nfloat", -1.1f) + .requiredDouble("rdouble") + .optionalDouble("odouble") + .nullableDouble("ndouble", 99.9d) + .requiredString("rstring") + .optionalString("ostring") + .nullableString("nstring", "def") + .requiredBytes("rbytes") + .optionalBytes("obytes") + .nullableBytes("nbytes", new byte[] {1,2,3}) + .endRecord(); + + Assert.assertEquals(full, shortcut); + } + + @Test + public void testNames() { + // no contextual namespace + Schema r = SchemaBuilder.record("Rec").fields() + .name("f0").type().fixed("org.foo.MyFixed").size(1).noDefault() + .name("f1").type("org.foo.MyFixed").noDefault() + .name("f2").type("org.foo.MyFixed", "").noDefault() + .name("f3").type("org.foo.MyFixed", null).noDefault() + .name("f4").type("org.foo.MyFixed", "ignorethis").noDefault() + .name("f5").type("MyFixed", "org.foo").noDefault() + .endRecord(); + Schema expected = Schema.createFixed("org.foo.MyFixed", null, null, 1); + checkField(r, expected, "f0"); + checkField(r, expected, "f1"); + checkField(r, expected, "f2"); + checkField(r, expected, "f3"); + checkField(r, expected, "f4"); + checkField(r, expected, "f5"); + + // context namespace + Schema f = SchemaBuilder.builder("").fixed("Foo").size(1); + Assert.assertEquals(Schema.createFixed("Foo", null, null, 1), f); + + // context namespace from record matches + r = SchemaBuilder.record("Rec").namespace("org.foo").fields() + .name("f0").type().fixed("MyFixed").size(1).noDefault() + .name("f1").type("org.foo.MyFixed").noDefault() + .name("f2").type("org.foo.MyFixed", "").noDefault() + .name("f3").type("org.foo.MyFixed", null).noDefault() + .name("f4").type("org.foo.MyFixed", "ignorethis").noDefault() + .name("f5").type("MyFixed", "org.foo").noDefault() + .name("f6").type("MyFixed", null).noDefault() + .name("f7").type("MyFixed").noDefault() + .endRecord(); + checkField(r, expected, "f0"); + checkField(r, expected, "f1"); + checkField(r, expected, "f2"); + checkField(r, expected, "f3"); + checkField(r, expected, "f4"); + checkField(r, expected, "f5"); + checkField(r, expected, "f6"); + checkField(r, expected, "f7"); + + // context namespace from record does not match + r = SchemaBuilder.record("Rec").namespace("org.rec").fields() + .name("f0").type().fixed("MyFixed").namespace("org.foo").size(1).noDefault() + .name("f1").type("org.foo.MyFixed").noDefault() + .name("f2").type("org.foo.MyFixed", "").noDefault() + .name("f3").type("org.foo.MyFixed", null).noDefault() + .name("f4").type("org.foo.MyFixed", "ignorethis").noDefault() + .name("f5").type("MyFixed", "org.foo").noDefault() + .endRecord(); + checkField(r, expected, "f0"); + checkField(r, expected, "f1"); + checkField(r, expected, "f2"); + checkField(r, expected, "f3"); + checkField(r, expected, "f4"); + checkField(r, expected, "f5"); + + // context namespace from record, nested has no namespace + expected = Schema.createFixed("MyFixed", null, null, 1); + r = SchemaBuilder.record("Rec").namespace("org.rec").fields() + .name("f0").type().fixed("MyFixed").namespace("").size(1).noDefault() + .name("f1").type("MyFixed", "").noDefault() + .endRecord(); + checkField(r, expected, "f0"); + checkField(r, expected, "f1"); + + // mimic names of primitives, but with a namesapce. This is OK + SchemaBuilder.fixed("org.test.long").size(1); + SchemaBuilder.fixed("long").namespace("org.test").size(1); + SchemaBuilder.builder("org.test").fixed("long").size(1); + + } + + private void checkField(Schema r, Schema expected, String name) { + Assert.assertEquals(expected, r.getField(name).schema()); + } + + @Test(expected=SchemaParseException.class) + public void testNamesFailRedefined() { + SchemaBuilder.record("Rec").fields() + .name("f0").type().enumeration("MyEnum").symbols("A", "B").enumDefault("A") + .name("f1").type().enumeration("MyEnum").symbols("X", "Y").noDefault() + .endRecord(); + } + + @Test(expected=SchemaParseException.class) + public void testNamesFailAbsent() { + SchemaBuilder.builder().type("notdefined"); + } + + @Test(expected=AvroTypeException.class) + public void testNameReserved() { + SchemaBuilder.fixed("long").namespace("").size(1); + } + + @Test + public void testFieldTypesAndDefaultValues() { + byte[] bytedef = new byte[]{3}; + ByteBuffer bufdef = ByteBuffer.wrap(bytedef); + String strdef = "\u0003"; + HashMap<String, String> mapdef = new HashMap<String, String>(); + mapdef.put("a", "A"); + ArrayList<String> arrdef = new ArrayList<String>(); + arrdef.add("arr"); + + Schema rec = SchemaBuilder.record("inner").fields() + .name("f").type().intType().noDefault() + .endRecord(); + + Schema rec2 = SchemaBuilder.record("inner2").fields() + .name("f2").type().intType().noDefault() + .endRecord(); + + GenericData.Record recdef = + new GenericRecordBuilder(rec).set("f", 1).build(); + + GenericData.Record recdef2 = + new GenericRecordBuilder(rec2).set("f2", 2).build(); + + Schema r = SchemaBuilder.record("r").fields() + .name("boolF").type().booleanType().booleanDefault(false) + .name("intF").type().intType().intDefault(1) + .name("longF").type().longType().longDefault(2L) + .name("floatF").type().floatType().floatDefault(3.0f) + .name("doubleF").type().doubleType().doubleDefault(4.0d) + .name("stringF").type().stringType().stringDefault("def") + .name("bytesF1").type().bytesType().bytesDefault(bytedef) + .name("bytesF2").type().bytesType().bytesDefault(bufdef) + .name("bytesF3").type().bytesType().bytesDefault(strdef) + .name("nullF").type().nullType().nullDefault() + .name("fixedF1").type().fixed("F1").size(1).fixedDefault(bytedef) + .name("fixedF2").type().fixed("F2").size(1).fixedDefault(bufdef) + .name("fixedF3").type().fixed("F3").size(1).fixedDefault(strdef) + .name("enumF").type().enumeration("E1").symbols("S").enumDefault("S") + .name("mapF").type().map().values().stringType() + .mapDefault(mapdef) + .name("arrayF").type().array().items().stringType() + .arrayDefault(arrdef) + .name("recordF").type().record("inner").fields() + .name("f").type().intType().noDefault() + .endRecord().recordDefault(recdef) + .name("byName").type("E1").withDefault("S") + // union builders, one for each 'first type' in a union: + .name("boolU").type().unionOf().booleanType().and() + .intType().endUnion().booleanDefault(false) + .name("intU").type().unionOf().intType().and() + .longType().endUnion().intDefault(1) + .name("longU").type().unionOf().longType().and() + .intType().endUnion().longDefault(2L) + .name("floatU").type().unionOf().floatType().and() + .intType().endUnion().floatDefault(3.0f) + .name("doubleU").type().unionOf().doubleType().and() + .intType().endUnion().doubleDefault(4.0d) + .name("stringU").type().unionOf().stringType().and() + .intType().endUnion().stringDefault("def") + .name("bytesU").type().unionOf().bytesType().and() + .intType().endUnion().bytesDefault(bytedef) + .name("nullU").type().unionOf().nullType().and() + .intType().endUnion().nullDefault() + .name("fixedU").type().unionOf().fixed("F4").size(1).and() + .intType().endUnion().fixedDefault(bytedef) + .name("enumU").type().unionOf().enumeration("E2").symbols("SS").and() + .intType().endUnion().enumDefault("SS") + .name("mapU").type().unionOf().map().values().stringType().and() + .intType().endUnion().mapDefault(mapdef) + .name("arrayU").type().unionOf().array().items().stringType().and() + .intType().endUnion().arrayDefault(arrdef) + .name("recordU").type().unionOf().record("inner2").fields() + .name("f2").type().intType().noDefault() + .endRecord().and().intType().endUnion().recordDefault(recdef2) + .endRecord(); + + GenericData.Record newRec = + new GenericRecordBuilder(r).build(); + + Assert.assertEquals(false, newRec.get("boolF")); + Assert.assertEquals(false, newRec.get("boolU")); + Assert.assertEquals(1, newRec.get("intF")); + Assert.assertEquals(1, newRec.get("intU")); + Assert.assertEquals(2L, newRec.get("longF")); + Assert.assertEquals(2L, newRec.get("longU")); + Assert.assertEquals(3f, newRec.get("floatF")); + Assert.assertEquals(3f, newRec.get("floatU")); + Assert.assertEquals(4d, newRec.get("doubleF")); + Assert.assertEquals(4d, newRec.get("doubleU")); + Assert.assertEquals("def", newRec.get("stringF").toString()); + Assert.assertEquals("def", newRec.get("stringU").toString()); + Assert.assertEquals(bufdef, newRec.get("bytesF1")); + Assert.assertEquals(bufdef, newRec.get("bytesF2")); + Assert.assertEquals(bufdef, newRec.get("bytesF3")); + Assert.assertEquals(bufdef, newRec.get("bytesU")); + Assert.assertNull(newRec.get("nullF")); + Assert.assertNull(newRec.get("nullU")); + Assert.assertArrayEquals(bytedef, + ((GenericData.Fixed)newRec.get("fixedF1")).bytes()); + Assert.assertArrayEquals(bytedef, + ((GenericData.Fixed)newRec.get("fixedF2")).bytes()); + Assert.assertArrayEquals(bytedef, + ((GenericData.Fixed)newRec.get("fixedF3")).bytes()); + Assert.assertArrayEquals(bytedef, + ((GenericData.Fixed)newRec.get("fixedU")).bytes()); + Assert.assertEquals("S", newRec.get("enumF").toString()); + Assert.assertEquals("SS", newRec.get("enumU").toString()); + @SuppressWarnings("unchecked") + Map<CharSequence, CharSequence> map = + (Map<CharSequence, CharSequence>) newRec.get("mapF"); + Assert.assertEquals(mapdef.size(), map.size()); + for(Map.Entry<CharSequence, CharSequence> e : map.entrySet()) { + Assert.assertEquals( + mapdef.get(e.getKey().toString()), e.getValue().toString()); + } + Assert.assertEquals(newRec.get("mapF"), newRec.get("mapU")); + @SuppressWarnings("unchecked") + GenericData.Array<CharSequence> arr = + (GenericData.Array<CharSequence>) newRec.get("arrayF"); + Assert.assertEquals(arrdef.size(), arr.size()); + for(CharSequence c : arr) { + Assert.assertTrue(arrdef.contains(c.toString())); + } + Assert.assertEquals(newRec.get("arrF"), newRec.get("arrU")); + Assert.assertEquals(recdef, newRec.get("recordF")); + Assert.assertEquals(recdef2, newRec.get("recordU")); + Assert.assertEquals("S", newRec.get("byName").toString()); + } + + @Test(expected=SchemaBuilderException.class) + public void testBadDefault() { + SchemaBuilder.record("r").fields() + .name("f").type(Schema.create(Schema.Type.INT)).withDefault(new Object()) + .endRecord(); + } + + @Test + public void testUnionFieldBuild() { + SchemaBuilder.record("r").fields() + .name("allUnion").type().unionOf() + .booleanType().and() + .intType().and() + .longType().and() + .floatType().and() + .doubleType().and() + .stringType().and() + .bytesType().and() + .nullType().and() + .fixed("Fix").size(1).and() + .enumeration("Enu").symbols("Q").and() + .array().items().intType().and() + .map().values().longType().and() + .record("Rec").fields() + .name("one").type("Fix").noDefault() + .endRecord() + .endUnion().booleanDefault(false) + .endRecord(); } @Test public void testDefaults() throws IOException { - Schema writeSchema = SchemaBuilder.recordType("r") - .requiredInt("requiredInt") - .optionalInt("optionalInt") - .optionalInt("optionalIntWithDefault", 3) - .build(); + Schema writeSchema = SchemaBuilder.record("r").fields() + .name("requiredInt").type().intType().noDefault() + .name("optionalInt").type().optional().intType() + .name("nullableIntWithDefault").type().nullable().intType().intDefault(3) + .endRecord(); GenericData.Record rec1 = new GenericRecordBuilder(writeSchema) .set("requiredInt", 1) @@ -298,17 +679,17 @@ public class TestSchemaBuilder { Assert.assertEquals(1, rec1.get("requiredInt")); Assert.assertEquals(null, rec1.get("optionalInt")); - Assert.assertEquals(3, rec1.get("optionalIntWithDefault")); + Assert.assertEquals(3, rec1.get("nullableIntWithDefault")); GenericData.Record rec2 = new GenericRecordBuilder(writeSchema) .set("requiredInt", 1) .set("optionalInt", 2) - .set("optionalIntWithDefault", 13) + .set("nullableIntWithDefault", 13) .build(); Assert.assertEquals(1, rec2.get("requiredInt")); Assert.assertEquals(2, rec2.get("optionalInt")); - Assert.assertEquals(13, rec2.get("optionalIntWithDefault")); + Assert.assertEquals(13, rec2.get("nullableIntWithDefault")); // write to file DataFileWriter<Object> writer = @@ -318,13 +699,13 @@ public class TestSchemaBuilder { writer.append(rec2); writer.close(); - Schema readSchema = SchemaBuilder.recordType("r") - .requiredInt("requiredInt") - .optionalInt("optionalInt") - .optionalInt("optionalIntWithDefault", 3) - .optionalInt("newOptionalInt") - .optionalInt("newOptionalIntWithDefault", 5) - .build(); + Schema readSchema = SchemaBuilder.record("r").fields() + .name("requiredInt").type().intType().noDefault() + .name("optionalInt").type().optional().intType() + .name("nullableIntWithDefault").type().nullable().intType().intDefault(3) + .name("newOptionalInt").type().optional().intType() + .name("newNullableIntWithDefault").type().nullable().intType().intDefault(5) + .endRecord(); DataFileReader<GenericData.Record> reader = new DataFileReader<GenericData.Record>(FILE, @@ -333,16 +714,16 @@ public class TestSchemaBuilder { GenericData.Record rec1read = reader.iterator().next(); Assert.assertEquals(1, rec1read.get("requiredInt")); Assert.assertEquals(null, rec1read.get("optionalInt")); - Assert.assertEquals(3, rec1read.get("optionalIntWithDefault")); + Assert.assertEquals(3, rec1read.get("nullableIntWithDefault")); Assert.assertEquals(null, rec1read.get("newOptionalInt")); - Assert.assertEquals(5, rec1read.get("newOptionalIntWithDefault")); + Assert.assertEquals(5, rec1read.get("newNullableIntWithDefault")); GenericData.Record rec2read = reader.iterator().next(); Assert.assertEquals(1, rec2read.get("requiredInt")); Assert.assertEquals(2, rec2read.get("optionalInt")); - Assert.assertEquals(13, rec2read.get("optionalIntWithDefault")); + Assert.assertEquals(13, rec2read.get("nullableIntWithDefault")); Assert.assertEquals(null, rec2read.get("newOptionalInt")); - Assert.assertEquals(5, rec2read.get("newOptionalIntWithDefault")); + Assert.assertEquals(5, rec2read.get("newNullableIntWithDefault")); } } Modified: avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/idl/TestIdl.java URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/idl/TestIdl.java?rev=1507862&r1=1507861&r2=1507862&view=diff ============================================================================== --- avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/idl/TestIdl.java (original) +++ avro/trunk/lang/java/compiler/src/test/java/org/apache/avro/compiler/idl/TestIdl.java Sun Jul 28 22:10:12 2013 @@ -18,12 +18,10 @@ package org.apache.avro.compiler.idl; -import org.junit.Before; -import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -import java.util.ArrayList; -import java.util.List; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -32,8 +30,14 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; import org.apache.avro.Protocol; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Before; +import org.junit.Test; /** * Simple test harness for Idl. @@ -137,7 +141,8 @@ public class TestIdl { Idl parser = new Idl(in, ucl); Protocol p = parser.CompilationUnit(); - return p.toString(true); + parser.close(); + return p.toString(); } public String testName() { @@ -161,9 +166,12 @@ public class TestIdl { String line = null; StringBuilder builder = new StringBuilder(); while ((line = in.readLine()) != null) { - builder.append(line).append('\n'); + builder.append(line); } - return builder.toString(); + in.close(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode json = mapper.readTree(builder.toString()); + return mapper.writer().writeValueAsString(json); } private static void writeFile(File f, String s) throws IOException { Modified: avro/trunk/lang/java/pom.xml URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/pom.xml?rev=1507862&r1=1507861&r2=1507862&view=diff ============================================================================== --- avro/trunk/lang/java/pom.xml (original) +++ avro/trunk/lang/java/pom.xml Sun Jul 28 22:10:12 2013 @@ -44,7 +44,7 @@ --> <hadoop1.version>0.20.205.0</hadoop1.version> <hadoop2.version>2.0.1-alpha</hadoop2.version> - <jackson.version>1.8.8</jackson.version> + <jackson.version>1.9.13</jackson.version> <jetty.version>6.1.26</jetty.version> <jetty-servlet-api.version>2.5-20081211</jetty-servlet-api.version> <jopt-simple.version>4.1</jopt-simple.version> @@ -202,6 +202,7 @@ rather than the console. --> <redirectTestOutputToFile>true</redirectTestOutputToFile> <failIfNoTests>false</failIfNoTests> + <argLine>-Xmx1000m -XX:MaxPermSize=200m</argLine> <systemPropertyVariables> <test.dir>${project.basedir}/target/</test.dir> </systemPropertyVariables> @@ -307,6 +308,30 @@ </plugins> </build> </profile> + <profile> + <id>mac</id> + <activation> + <os> + <family>mac</family> + </os> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <systemPropertyVariables> + <test.dir>${project.basedir}/target/</test.dir> + <!-- avro-mapred will fail in tests on mac without this --> + <java.security.krb5.realm>OX.AC.UK</java.security.krb5.realm> + <java.security.krb5.kdc>kdc0.ox.ac.uk:kdc1.ox.ac.uk</java.security.krb5.kdc> + </systemPropertyVariables> + </configuration> + </plugin> + </plugins> + </build> + </profile> </profiles> <!-- dependencyManagement can be used to define dependency versions, scopes, and
