@Test public void testNestedNullNamespace() throws Exception { Schema inner = Schema.parse("{\"type\":\"record\",\"name\":\"Inner\",\"fields\":[]}"); Schema outer = Schema.createRecord("Outer", null, "space", false); outer.setFields(Arrays.asList(new Field("f", inner, null, null))); assertEquals(outer, Schema.parse(outer.toString())); }
@Test /** * Test that equals() and hashCode() don't require exponential time on certain pathological * schemas. */ public void testSchemaExplosion() throws Exception { for (int i = 1; i < 15; i++) { // 15 is big enough to trigger // create a list of records, each with a single field whose type is a // union of all of the records. List<Schema> recs = new ArrayList<Schema>(); for (int j = 0; j < i; j++) recs.add(Schema.createRecord("" + (char) ('A' + j), null, null, false)); for (Schema s : recs) { Schema union = Schema.createUnion(recs); Field f = new Field("x", union, null, null); List<Field> fields = new ArrayList<Field>(); fields.add(f); s.setFields(fields); } // check that equals and hashcode are correct and complete in a // reasonable amount of time for (Schema s1 : recs) { Schema s2 = Schema.parse(s1.toString()); assertEquals(s1.hashCode(), s2.hashCode()); assertEquals(s1, s2); } } }
@Test public void testNestedNullNamespaceReferencingWithUnion() { Schema inner = Schema.parse("{\"type\":\"record\",\"name\":\"Inner\",\"fields\":[]}"); Schema innerUnion = Schema.createUnion(Arrays.asList(inner, Schema.create(Type.NULL))); Schema outer = Schema.createRecord("Outer", null, "space", false); outer.setFields( Arrays.asList( new Field("f1", innerUnion, null, null), new Field("f2", innerUnion, null, null))); assertEquals(outer, Schema.parse(outer.toString())); }
@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}); }