@Test public void testDouble() throws Exception { assertEquals(Schema.create(Type.DOUBLE), Schema.parse("\"double\"")); assertEquals(Schema.create(Type.DOUBLE), Schema.parse("{\"type\":\"double\"}")); check("\"double\"", "1.2", new Double(1.2)); checkDefault("\"double\"", "\"NaN\"", Double.NaN); checkDefault("\"double\"", "\"Infinity\"", Double.POSITIVE_INFINITY); checkDefault("\"double\"", "\"-Infinity\"", Double.NEGATIVE_INFINITY); }
@Test public void testFloat() throws Exception { assertEquals(Schema.create(Type.FLOAT), Schema.parse("\"float\"")); assertEquals(Schema.create(Type.FLOAT), Schema.parse("{\"type\":\"float\"}")); check("\"float\"", "1.1", new Float(1.1)); checkDefault("\"float\"", "\"NaN\"", Float.NaN); checkDefault("\"float\"", "\"Infinity\"", Float.POSITIVE_INFINITY); checkDefault("\"float\"", "\"-Infinity\"", Float.NEGATIVE_INFINITY); }
@Test public void testNames_GetNotFound() { Schema.Names names = new Schema.Names("space"); names.put(new Schema.Name("Name", "otherspace"), Schema.create(Type.STRING)); assertNull(names.get("Name")); }
@Test public void testNames_GetWithNullNamespace() { Schema schema = Schema.create(Type.STRING); Schema.Names names = new Schema.Names("space"); names.put(new Schema.Name("Name", ""), schema); assertEquals(schema, names.get(new Schema.Name("Name", ""))); assertEquals(schema, names.get("Name")); }
@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 testBoolean() throws Exception { assertEquals(Schema.create(Type.BOOLEAN), Schema.parse("\"boolean\"")); assertEquals(Schema.create(Type.BOOLEAN), Schema.parse("{\"type\":\"boolean\"}")); check("\"boolean\"", "true", Boolean.TRUE); }
@Test public void testNull() throws Exception { assertEquals(Schema.create(Type.NULL), Schema.parse("\"null\"")); assertEquals(Schema.create(Type.NULL), Schema.parse("{\"type\":\"null\"}")); check("\"null\"", "null", null); }
@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}); }
@Test public void testLong() throws Exception { assertEquals(Schema.create(Type.LONG), Schema.parse("\"long\"")); assertEquals(Schema.create(Type.LONG), Schema.parse("{\"type\":\"long\"}")); check("\"long\"", "11", new Long(11)); }
@Test public void testInt() throws Exception { assertEquals(Schema.create(Type.INT), Schema.parse("\"int\"")); assertEquals(Schema.create(Type.INT), Schema.parse("{\"type\":\"int\"}")); check("\"int\"", "9", new Integer(9)); }
@Test public void testBytes() throws Exception { assertEquals(Schema.create(Type.BYTES), Schema.parse("\"bytes\"")); assertEquals(Schema.create(Type.BYTES), Schema.parse("{\"type\":\"bytes\"}")); check("\"bytes\"", "\"\\u0000ABC\\u00FF\"", ByteBuffer.wrap(new byte[] {0, 65, 66, 67, -1})); }
@Test public void testString() throws Exception { assertEquals(Schema.create(Type.STRING), Schema.parse("\"string\"")); assertEquals(Schema.create(Type.STRING), Schema.parse("{\"type\":\"string\"}")); check("\"string\"", "\"foo\"", new Utf8("foo")); }