public void testAnnotationAvroStringableFields() throws Exception { check(RAvroStringableField.class, "{\"type\":\"record\",\"name\":\"RAvroNameCollide\",\"namespace\":" +"\"org.apache.avro.reflect.TestReflect\",\"fields\":[" +"{\"name\":\"a\",\"type\":\"String\"}]}"); }
public static void checkBinary(ReflectData reflectData, Schema schema, Object datum, boolean equals) throws IOException { checkBinary(reflectData, schema, datum, equals, false); }
/** Test stringable classes. */ @Test public void testStringables() throws Exception { checkStringable(java.math.BigDecimal.class, "10"); checkStringable(java.math.BigInteger.class, "20"); checkStringable(java.net.URI.class, "foo://bar:9000/baz"); checkStringable(java.net.URL.class, "http://bar:9000/baz"); checkStringable(java.io.File.class, "foo.bar"); }
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 public void testR2() throws Exception { R2 r2 = new R2(); r2.arrayField = new String[] {"foo"}; r2.collectionField = new ArrayList<>(); r2.collectionField.add("foo"); checkReadWrite(r2); }
@Test public void testDisableUnsafe() throws Exception { String saved = System.getProperty("avro.disable.unsafe"); try { System.setProperty("avro.disable.unsafe", "true"); ReflectData.ACCESSOR_CACHE.remove(multipleAnnotationRecord.class); ReflectData.ACCESSOR_CACHE.remove(AnotherSampleRecord.class); ReflectionUtil.resetFieldAccess(); testMultipleAnnotations(); testRecordWithNullIO(); } finally { if (saved == null) System.clearProperty("avro.disable.unsafe"); else System.setProperty("avro.disable.unsafe", saved); ReflectData.ACCESSOR_CACHE.remove(multipleAnnotationRecord.class); ReflectData.ACCESSOR_CACHE.remove(AnotherSampleRecord.class); ReflectionUtil.resetFieldAccess(); } }
@Test public void testPrimitiveArrayBlocking() throws Exception { testPrimitiveArrays(true); }
private void testPrimitiveArrays(boolean blocking) throws Exception { testPrimitiveArray(boolean.class, blocking); testPrimitiveArray(byte.class, blocking); testPrimitiveArray(short.class, blocking); testPrimitiveArray(char.class, blocking); testPrimitiveArray(int.class, blocking); testPrimitiveArray(long.class, blocking); testPrimitiveArray(float.class, blocking); testPrimitiveArray(double.class, blocking); }
@Test public void testNullableByteArrayNotNullValue() throws Exception { checkReadWrite(new NullableBytesTest("foo".getBytes())); }
@Test public void testPrimitiveArray() throws Exception { testPrimitiveArrays(false); }
@Test public void testAvroAliasOnClass() { check(AliasA.class, "{\"type\":\"record\",\"name\":\"AliasA\",\"namespace\":\"org.apache.avro.reflect.TestReflect\",\"fields\":[],\"aliases\":[\"b.a\"]}"); check(AliasB.class, "{\"type\":\"record\",\"name\":\"AliasB\",\"namespace\":\"org.apache.avro.reflect.TestReflect\",\"fields\":[],\"aliases\":[\"a\"]}"); check(AliasC.class, "{\"type\":\"record\",\"name\":\"AliasC\",\"namespace\":\"org.apache.avro.reflect.TestReflect\",\"fields\":[],\"aliases\":[\"a\"]}"); }
void checkReadWrite(Object object) throws Exception { checkReadWrite(object, ReflectData.get().getSchema(object.getClass())); } void checkReadWrite(Object object, Schema s) throws Exception {
/** 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); }
private void check(Object o, String schemaJson) { check(o.getClass(), schemaJson); }
@Test public void testR3() throws Exception { R3 r3 = new R3(); r3.intArray = new int[] {1}; checkReadWrite(r3); }
public static void checkBinary(Schema schema, Object datum) throws IOException { checkBinary(ReflectData.get(), schema, datum, false); }
@Test public void testR5() throws Exception { R5 r5 = new R5(); r5.value = 1; r5.shorts = new short[] {3,255,256,Short.MAX_VALUE,Short.MIN_VALUE}; r5.b = 99; r5.c = 'a'; checkReadWrite(r5); }
@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)); }