Skip to content

Commit

Permalink
Add datum read/write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lawben committed Feb 8, 2019
1 parent fa35e43 commit 61eed9c
Showing 1 changed file with 181 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import com.google.common.reflect.TypeToken;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.avro.Schema;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.BinaryEncoder;
Expand All @@ -15,14 +20,26 @@
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.reflect.data.BeanClass;
import org.apache.avro.reflect.data.BuilderValueClass;
import org.apache.avro.reflect.data.GenericBaseClass;
import org.apache.avro.reflect.data.GenericClass;
import org.apache.avro.reflect.data.GenericSubClass;
import org.apache.avro.reflect.data.GenericValueMapClass;
import org.apache.avro.reflect.data.ListClass;
import org.apache.avro.reflect.data.MapClass;
import org.apache.avro.reflect.data.NestedGenericMapListValueClass;
import org.apache.avro.reflect.data.NestedGenericValueMapClass;
import org.apache.avro.reflect.data.NestedValueClass;
import org.apache.avro.reflect.data.ValueClass;
import org.junit.jupiter.api.Test;

class Reflect2DatumReaderTest {
static <T> void verify(T nestedValueClass, DatumWriter<T> datumWriter, DatumReader<T> datumReader, boolean reuse)
static <T> void verify(final T nestedValueClass, final DatumWriter<T> datumWriter, final DatumReader<T> datumReader)
throws IOException {
verify(nestedValueClass, datumWriter, datumReader, false);
}

static <T> void verify(final T nestedValueClass, final DatumWriter<T> datumWriter, final DatumReader<T> datumReader,
final boolean reuse) throws IOException {
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
datumWriter.write(nestedValueClass, encoder);
Expand Down Expand Up @@ -50,11 +67,11 @@ void testValueClass() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final Schema schema = reflectData.getSchema(ValueClass.class);

ValueClass valueClass = new ValueClass(42, "test");
final ValueClass valueClass = new ValueClass(42, "test");
final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(valueClass, datumWriter, datumReader, false);
verify(valueClass, datumWriter, datumReader);
}

@Test
Expand All @@ -63,19 +80,19 @@ void testGenericClass() throws IOException {
final Schema schema = reflectData.getSchema(new TypeToken<GenericClass<String>>() {
}.getType());

var valueClass = new GenericClass<>("test");
final var valueClass = new GenericClass<>("test");
final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(valueClass, datumWriter, datumReader, false);
verify(valueClass, datumWriter, datumReader);
}

@Test
void testBeanClass() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final Schema schema = reflectData.getSchema(BeanClass.class);

var bean = new BeanClass();
final var bean = new BeanClass();
bean.setText("test");
bean.setX(42);
final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
Expand All @@ -89,22 +106,176 @@ void testBuilderValueClass() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final Schema schema = reflectData.getSchema(BuilderValueClass.class);

var builderValueClass = BuilderValueClass.builder().x(42).text("test").build();
final var builderValueClass = BuilderValueClass.builder().x(42).text("test").build();
final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(builderValueClass, datumWriter, datumReader, false);
verify(builderValueClass, datumWriter, datumReader);
}

@Test
void testNestedValueClass() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final Schema schema = reflectData.getSchema(NestedValueClass.class);

var nestedValueClass = new NestedValueClass(13, new ValueClass(42, "test"));
final var nestedValueClass = new NestedValueClass(13, new ValueClass(42, "test"));
final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(nestedValueClass, datumWriter, datumReader, false);
verify(nestedValueClass, datumWriter, datumReader);
}

@Test
void testGenericSchemaWithBoundTypeFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final GenericClass<String> genericClass = new GenericClass<>("foo");
final Schema schema = reflectData.getSchema(genericClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(genericClass, datumWriter, datumReader);
}

@Test
void testValueSchemaFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final ValueClass valueClass = new ValueClass(10, "foo");
final Schema schema = reflectData.getSchema(valueClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(valueClass, datumWriter, datumReader);
}

@Test
void testNestedValueSchemaFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final NestedValueClass nestedClass = new NestedValueClass(10, new ValueClass(11, "foo"));
final Schema schema = reflectData.getSchema(nestedClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(nestedClass, datumWriter, datumReader);
}

@Test
void testListClassSchemaFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final ListClass listClass = new ListClass(new ArrayList<>(List.of(10)));
final Schema schema = reflectData.getSchema(listClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(listClass, datumWriter, datumReader);
}

@Test
void testMapClassSchemaFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final MapClass mapClass = new MapClass(new HashMap<>());
final Schema schema = reflectData.getSchema(mapClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(mapClass, datumWriter, datumReader);
}

@Test
void testNestedGenericValueMapClassSchemaBoundTypeFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final NestedGenericValueMapClass<Integer> nestedGenericValueMapClass = new NestedGenericValueMapClass<>(
new GenericValueMapClass<>(Map.of("foo", 10)));
final Schema schema = reflectData
.getSchema(nestedGenericValueMapClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(nestedGenericValueMapClass, datumWriter, datumReader);
}

@Test
void testNestedGenericValueMapClassSchemaValueClassListTypeFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final NestedGenericValueMapClass<List<ValueClass>> instance =
new NestedGenericValueMapClass<>(new GenericValueMapClass<>(Map.of("foo",
new ArrayList<>(List.of(new ValueClass(100, "nested"))))));

final Schema schema = reflectData.getSchema(instance);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(instance, datumWriter, datumReader);
}

@Test
void testListOfValueClassSchemaFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final List<ValueClass> list = new ArrayList<>(List.of(new ValueClass(100, "foo")));
final Schema schema = reflectData.getSchema(list);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(list, datumWriter, datumReader);
}

@Test
void testGenericSubClassSchemaOfSubClassFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final GenericBaseClass<String> baseClass = new GenericSubClass<>("base", "sub");
final Schema schema = reflectData.getSchema(baseClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(baseClass, datumWriter, datumReader);
}

@Test
void testGenericBaseClassSchemaOfSubClassFromInstance() throws IOException {
final Reflect2Data reflectData = Reflect2Data.get();
final GenericSubClass<String> baseClass = new GenericSubClass<>("base", "sub");
final Schema schema = reflectData.getSchema(baseClass);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(baseClass, datumWriter, datumReader);
}

@Test
void testNestedGenericMapListValueClassSchemaFromInstance() throws IOException {
final NestedGenericMapListValueClass<GenericClass<Float>> instance =
new NestedGenericMapListValueClass<>(new HashMap<>(Map.of("outer", new HashMap<>(Map.of("bla",
new ArrayList<>(List.of(new GenericClass<>(1.0f))))))), new GenericClass<>(2.0f),
new GenericClass<>(3.0f));

final Reflect2Data reflectData = Reflect2Data.get();
final Schema schema = reflectData.getSchema(instance);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(instance, datumWriter, datumReader);
}

@Test
void testNestedListSchemaFromInstance() throws IOException {
final List<GenericClass<Integer>> genericList = new LinkedList<>(List.of(new GenericClass<>(10)));
final Reflect2Data reflectData = Reflect2Data.get();
final Schema schema = reflectData.getSchema(genericList);

final DatumWriter datumWriter = reflectData.createDatumWriter(schema);
final DatumReader datumReader = reflectData.createDatumReader(schema);

verify(genericList, datumWriter, datumReader);
}

}

0 comments on commit 61eed9c

Please sign in to comment.