public static void checkBinary(ReflectData reflectData, Schema schema, Object datum, boolean equals) throws IOException { checkBinary(reflectData, schema, datum, equals, false); }
public static void checkBinary(Schema schema, Object datum) throws IOException { checkBinary(ReflectData.get(), schema, datum, false); }
/** Test union of null and an array. */ @Test public void testNullArray() throws Exception { String json = "[{\"type\":\"array\", \"items\": \"long\"}, \"null\"]"; Schema schema = new Schema.Parser().parse(json); checkBinary(schema, null); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public void checkStringable(Class c, String value) throws Exception { ReflectData data = new ReflectData(); Schema schema = data.getSchema(c); assertEquals ("{\"type\":\"string\",\"java-class\":\""+c.getName()+"\"}", schema.toString()); checkBinary(schema, c.getConstructor(String.class).newInstance(value)); }
private void testPrimitiveArray(Class<?> c, boolean blocking) throws Exception { ReflectData data = new ReflectData(); Random r = new Random(); int size = 200; Object array = Array.newInstance(c, size); Schema s = data.getSchema(array.getClass()); for(int i = 0; i < size; i++) { Array.set(array, i, randomFor(c, r)); } checkBinary(data, s, array, false, blocking); }
/** Test Map with stringable key classes. */ @Test public void testStringableMapKeys() throws Exception { M1 record = new M1(); record.integerKeyMap = new HashMap<>(1); record.integerKeyMap.put(10, "foo"); record.bigIntegerKeyMap = new HashMap<>(1); record.bigIntegerKeyMap.put(java.math.BigInteger.TEN, "bar"); record.bigDecimalKeyMap = new HashMap<>(1); record.bigDecimalKeyMap.put(java.math.BigDecimal.ONE, "bigDecimal"); record.fileKeyMap = new HashMap<>(1); record.fileKeyMap.put(new java.io.File("foo.bar"), "file"); ReflectData data = new ReflectData().addStringable(Integer.class); checkBinary(data, data.getSchema(M1.class), record, true); }
@Test public void testNullableStringableField() throws Exception { NullableStringable datum = new NullableStringable(); datum.number = java.math.BigDecimal.TEN; Schema schema = ReflectData.AllowNull.get().getSchema(NullableStringable.class); checkBinary(schema, datum); }
@Test /** Test nesting of reflect data within generic. */ public void testReflectWithinGeneric() throws Exception { ReflectData data = ReflectData.get(); // define a record with a field that's a specific Y Schema schema = Schema.createRecord("Foo", "", "x.y.z", false); List<Schema.Field> fields = new ArrayList<>(); fields.add(new Schema.Field("f", data.getSchema(Y.class), "", null)); schema.setFields(fields); // create a generic instance of this record Y y = new Y(); y.i = 1; GenericData.Record record = new GenericData.Record(schema); record.put("f", y); // test that this instance can be written & re-read checkBinary(schema, record); }