@Test public void testNestedNonNullNamespace2() throws Exception { Schema inner1 = Schema.createFixed("InnerFixed", null, "space", 1); Schema inner2 = Schema.parse( "{\"type\":\"record\",\"namespace\":\"space\",\"name\":" + "\"InnerRecord\",\"fields\":[]}"); Schema nullOuter = Schema.createRecord("Outer", null, null, false); nullOuter.setFields( Arrays.asList(new Field("f1", inner1, null, null), new Field("f2", inner2, null, null))); assertEquals(nullOuter, Schema.parse(nullOuter.toString())); }
@Test public void testComplexUnions() throws Exception { // one of each unnamed type and two of named types String partial = "[\"int\", \"long\", \"float\", \"double\", \"boolean\", \"bytes\"," + " \"string\", {\"type\":\"array\", \"items\": \"long\"}," + " {\"type\":\"map\", \"values\":\"long\"}"; String namedTypes = ", {\"type\":\"record\",\"name\":\"Foo\",\"fields\":[]}," + " {\"type\":\"fixed\",\"name\":\"Bar\",\"size\": 1}," + " {\"type\":\"enum\",\"name\":\"Baz\",\"symbols\": [\"X\"]}"; String namedTypes2 = ", {\"type\":\"record\",\"name\":\"Foo2\",\"fields\":[]}," + " {\"type\":\"fixed\",\"name\":\"Bar2\",\"size\": 1}," + " {\"type\":\"enum\",\"name\":\"Baz2\",\"symbols\": [\"X\"]}"; check(partial + namedTypes + "]", false); check(partial + namedTypes + namedTypes2 + "]", false); checkParseError(partial + namedTypes + namedTypes + "]"); // fail with two branches of the same unnamed type checkUnionError(new Schema[] {Schema.create(Type.INT), Schema.create(Type.INT)}); checkUnionError(new Schema[] {Schema.create(Type.LONG), Schema.create(Type.LONG)}); checkUnionError(new Schema[] {Schema.create(Type.FLOAT), Schema.create(Type.FLOAT)}); checkUnionError(new Schema[] {Schema.create(Type.DOUBLE), Schema.create(Type.DOUBLE)}); checkUnionError(new Schema[] {Schema.create(Type.BOOLEAN), Schema.create(Type.BOOLEAN)}); checkUnionError(new Schema[] {Schema.create(Type.BYTES), Schema.create(Type.BYTES)}); checkUnionError(new Schema[] {Schema.create(Type.STRING), Schema.create(Type.STRING)}); checkUnionError( new Schema[] { Schema.createArray(Schema.create(Type.INT)), Schema.createArray(Schema.create(Type.INT)) }); checkUnionError( new Schema[] { Schema.createMap(Schema.create(Type.INT)), Schema.createMap(Schema.create(Type.INT)) }); List<String> symbols = new ArrayList<String>(); symbols.add("NOTHING"); // succeed with two branches of the same named type, if different names Schema u; u = buildUnion( new Schema[] { Schema.parse("{\"type\":\"record\",\"name\":\"x.A\",\"fields\":[]}"), Schema.parse("{\"type\":\"record\",\"name\":\"y.A\",\"fields\":[]}") }); check(u.toString(), false); u = buildUnion( new Schema[] { Schema.parse("{\"type\":\"enum\",\"name\":\"x.A\",\"symbols\":[\"X\"]}"), Schema.parse("{\"type\":\"enum\",\"name\":\"y.A\",\"symbols\":[\"Y\"]}") }); check(u.toString(), false); u = buildUnion( new Schema[] { Schema.parse("{\"type\":\"fixed\",\"name\":\"x.A\",\"size\":4}"), Schema.parse("{\"type\":\"fixed\",\"name\":\"y.A\",\"size\":8}") }); check(u.toString(), false); // fail with two branches of the same named type, but same names checkUnionError( new Schema[] { Schema.createRecord("Foo", null, "org.test", false), Schema.createRecord("Foo", null, "org.test", false) }); checkUnionError( new Schema[] { Schema.createEnum("Bar", null, "org.test", symbols), Schema.createEnum("Bar", null, "org.test", symbols) }); checkUnionError( new Schema[] { Schema.createFixed("Baz", null, "org.test", 2), Schema.createFixed("Baz", null, "org.test", 1) }); Schema union = buildUnion(new Schema[] {Schema.create(Type.INT)}); // fail if creating a union of a union checkUnionError(new Schema[] {union}); }