use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class RecordWithString method testEmptyValue.
// this tests that the datum reader treats empty fields correctly. It reproduces the issue in ENG-2404.
@Test
public void testEmptyValue() throws UnsupportedTypeException, IOException {
Schema schema = new ReflectionSchemaGenerator().generate(RecordWithString.class);
TypeRepresentation typeRep = new TypeRepresentation(RecordWithString.class);
DatumWriter<RecordWithString> datumWriter = new ReflectionDatumWriter<>(schema);
@SuppressWarnings("unchecked") ReflectionDatumReader<RecordWithString> datumReader = new ReflectionDatumReader<>(schema, (TypeToken<RecordWithString>) TypeToken.of(typeRep.toType()));
RecordWithString record = new RecordWithString();
record.setA(42);
record.setTheString("");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BinaryEncoder encoder = new BinaryEncoder(bos);
datumWriter.encode(record, encoder);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BinaryDecoder decoder = new BinaryDecoder(bis);
RecordWithString rec = datumReader.read(decoder, schema);
Assert.assertEquals(record.getA(), rec.getA());
Assert.assertEquals(record.getTheString(), rec.getTheString());
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class ASMDatumCodecTest method testMap.
@Test
public void testMap() throws IOException, UnsupportedTypeException {
TypeToken<Map<String, List<String>>> type = new TypeToken<Map<String, List<String>>>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
DatumWriter<Map<String, List<String>>> writer = getWriter(type);
ImmutableMap<String, List<String>> map = ImmutableMap.<String, List<String>>of("k1", Lists.newArrayList("v1"), "k2", Lists.newArrayList("v2", null));
writer.encode(map, new BinaryEncoder(os));
ReflectionDatumReader<Map<String, List<String>>> reader = new ReflectionDatumReader<>(getSchema(type), type);
Assert.assertEquals(map, reader.read(new BinaryDecoder(is), getSchema(type)));
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class ASMDatumCodecTest method testReferenceArray.
@Test
public void testReferenceArray() throws IOException, UnsupportedTypeException {
TypeToken<String[]> type = new TypeToken<String[]>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
String[] writeValue = new String[] { "1", "2", null, "3" };
DatumWriter<String[]> writer = getWriter(type);
writer.encode(writeValue, new BinaryEncoder(os));
ReflectionDatumReader<String[]> reader = new ReflectionDatumReader<>(getSchema(type), type);
String[] value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertArrayEquals(writeValue, value);
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class ASMDatumCodecTest method testRecordArray.
@Test
public void testRecordArray() throws IOException, UnsupportedTypeException {
TypeToken<Record[][]> type = new TypeToken<Record[][]>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
DatumWriter<Record[][]> writer = getWriter(type);
Record[][] writeValue = new Record[][] { { new Record(10, "testing", ImmutableList.of("a", "b", "c"), TestEnum.VALUE2) } };
writer.encode(writeValue, new BinaryEncoder(os));
ReflectionDatumReader<Record[][]> reader = new ReflectionDatumReader<>(getSchema(type), type);
Record[][] value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertArrayEquals(writeValue, value);
}
use of co.cask.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.
the class ASMDatumCodecTest method testString.
@Test
public void testString() throws UnsupportedTypeException, IOException {
TypeToken<String> type = new TypeToken<String>() {
};
PipedOutputStream os = new PipedOutputStream();
PipedInputStream is = new PipedInputStream(os);
DatumWriter<String> writer = getWriter(type);
writer.encode("Testing message", new BinaryEncoder(os));
ReflectionDatumReader<String> reader = new ReflectionDatumReader<>(getSchema(type), type);
String value = reader.read(new BinaryDecoder(is), getSchema(type));
Assert.assertEquals("Testing message", value);
}
Aggregations