use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.
the class CommonCoderTest method executeSingleTest.
@Test
public void executeSingleTest() throws IOException {
assertCoderIsKnown(testSpec.getCoder());
Coder coder = instantiateCoder(testSpec.getCoder());
Object testValue = convertValue(testSpec.getValue(), testSpec.getCoder(), coder);
Context context = testSpec.getNested() ? Context.NESTED : Context.OUTER;
byte[] encoded = CoderUtils.encodeToByteArray(coder, testValue, context);
Object decodedValue = CoderUtils.decodeFromByteArray(coder, testSpec.getSerialized(), context);
if (!testSpec.getCoder().getNonDeterministic()) {
assertThat(testSpec.toString(), encoded, equalTo(testSpec.getSerialized()));
}
verifyDecodedValue(testSpec.getCoder(), decodedValue, testValue);
}
use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.
the class CommonCoderTest method executeSingleTest.
@Test
public void executeSingleTest() throws IOException {
assertCoderIsKnown(testSpec.getCoder());
Coder coder = instantiateCoder(testSpec.getCoder());
Object testValue = convertValue(testSpec.getValue(), testSpec.getCoder(), coder);
Context context = testSpec.getNested() ? Context.NESTED : Context.OUTER;
byte[] encoded = CoderUtils.encodeToByteArray(coder, testValue, context);
Object decodedValue = CoderUtils.decodeFromByteArray(coder, testSpec.getSerialized(), context);
if (!testSpec.getCoder().getNonDeterministic()) {
assertThat(testSpec.toString(), encoded, equalTo(testSpec.getSerialized()));
}
verifyDecodedValue(testSpec.getCoder(), decodedValue, testValue);
}
use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.
the class CoderTest method testContextEqualsAndHashCode.
@Test
public void testContextEqualsAndHashCode() {
assertEquals(Context.NESTED, new Context(false));
assertEquals(Context.OUTER, new Context(true));
assertNotEquals(Context.NESTED, Context.OUTER);
assertEquals(Context.NESTED.hashCode(), new Context(false).hashCode());
assertEquals(Context.OUTER.hashCode(), new Context(true).hashCode());
// Even though this isn't strictly required by the hashCode contract,
// we still want this to be true.
assertNotEquals(Context.NESTED.hashCode(), Context.OUTER.hashCode());
}
use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.
the class AvroCoderTest method testAvroCoderTreeMapDeterminism.
@Test
public void testAvroCoderTreeMapDeterminism() throws Exception, NonDeterministicException {
TreeMapField size1 = new TreeMapField();
TreeMapField size2 = new TreeMapField();
// Different order for entries
size1.field.put("hello", "world");
size1.field.put("another", "entry");
size2.field.put("another", "entry");
size2.field.put("hello", "world");
AvroCoder<TreeMapField> coder = AvroCoder.of(TreeMapField.class);
coder.verifyDeterministic();
ByteArrayOutputStream outStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outStream2 = new ByteArrayOutputStream();
Context context = Context.NESTED;
coder.encode(size1, outStream1, context);
coder.encode(size2, outStream2, context);
assertArrayEquals(outStream1.toByteArray(), outStream2.toByteArray());
}
use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.
the class AvroCoderTest method testEncodingNotBuffered.
@Test
public void testEncodingNotBuffered() throws Exception {
// This test ensures that the coder doesn't read ahead and buffer data.
// Reading ahead causes a problem if the stream consists of records of different
// types.
Pojo before = new Pojo("Hello", 42, DATETIME_A);
AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class);
SerializableCoder<Integer> intCoder = SerializableCoder.of(Integer.class);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Context context = Context.NESTED;
coder.encode(before, outStream, context);
intCoder.encode(10, outStream, context);
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
Pojo after = coder.decode(inStream, context);
assertEquals(before, after);
Integer intAfter = intCoder.decode(inStream, context);
assertEquals(Integer.valueOf(10), intAfter);
}
Aggregations