use of io.cdap.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class RecordWithString method testEnum.
@Test
public void testEnum() throws IOException, UnsupportedTypeException {
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
Schema schema = new ReflectionSchemaGenerator().generate(TestEnum.class);
ReflectionDatumWriter<TestEnum> writer = new ReflectionDatumWriter<>(schema);
BinaryEncoder encoder = new BinaryEncoder(output);
writer.encode(TestEnum.VALUE1, encoder);
writer.encode(TestEnum.VALUE3, encoder);
writer.encode(TestEnum.VALUE2, encoder);
BinaryDecoder decoder = new BinaryDecoder(input);
Schema readSchema = Schema.parseJson(schema.toString());
ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(readSchema, TypeToken.of(TestEnum.class));
Assert.assertEquals(TestEnum.VALUE1, reader.read(decoder, readSchema));
Assert.assertEquals(TestEnum.VALUE3, reader.read(decoder, readSchema));
Assert.assertEquals(TestEnum.VALUE2, reader.read(decoder, readSchema));
}
use of io.cdap.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class RecordWithString method testCollection.
@Test
public void testCollection() throws UnsupportedTypeException, IOException {
List<String> list = Lists.newArrayList("1", "2", "3");
Schema sourceSchema = new ReflectionSchemaGenerator().generate(new TypeToken<List<String>>() {
}.getType());
Schema targetSchema = new ReflectionSchemaGenerator().generate(new TypeToken<Set<String>>() {
}.getType());
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
Set<String> set = new ReflectionDatumReader<>(targetSchema, new TypeToken<Set<String>>() {
}).read(new BinaryDecoder(input), sourceSchema);
Assert.assertEquals(Sets.newHashSet("1", "2", "3"), set);
targetSchema = new ReflectionSchemaGenerator().generate(String[].class);
new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
String[] array = new ReflectionDatumReader<>(targetSchema, new TypeToken<String[]>() {
}).read(new BinaryDecoder(input), sourceSchema);
Assert.assertArrayEquals(new String[] { "1", "2", "3" }, array);
}
use of io.cdap.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class SchemaTest method testPrimitiveArray.
@Test
public void testPrimitiveArray() throws UnsupportedTypeException {
Schema schema = new ReflectionSchemaGenerator().generate(int[].class);
Assert.assertEquals(Schema.arrayOf(Schema.of(Schema.Type.INT)), schema);
}
use of io.cdap.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class SchemaTest method testSameRecordDifferentLevels.
@Test
public void testSameRecordDifferentLevels() throws UnsupportedTypeException, IOException {
Schema actual = new ReflectionSchemaGenerator().generate(Node6.class);
Assert.assertEquals(Node6.SCHEMA, actual);
// check serialization and deserialization.
Assert.assertEquals(Node6.SCHEMA, Schema.parseJson(actual.toString()));
}
use of io.cdap.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class SchemaTest method testCachedJavaSerialization.
@Test
public void testCachedJavaSerialization() throws UnsupportedTypeException {
Schema s1 = SchemaCache.intern(new ReflectionSchemaGenerator().generate(Node.class));
Assert.assertSame(s1, SerializationUtils.clone(s1));
}
Aggregations