Search in sources :

Example 1 with Containers

use of net.morimekta.test.providence.core.no_rw_binary.Containers in project providence by morimekta.

the class BinarySerializerTest method testNonPrecompiled_lenient.

@Test
public void testNonPrecompiled_lenient() throws IOException {
    Serializer serializer = new BinarySerializer();
    // Just a sanity check.
    assertTrue(containers.size() == 10);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bais;
    int size;
    // complex message, one at a time.
    for (int i = 0; i < 10; ++i) {
        baos.reset();
        Containers expected = containers.get(i);
        size = serializer.serialize(baos, expected);
        assertEquals(baos.size(), size);
        bais = new ByteArrayInputStream(baos.toByteArray());
        Containers actual;
        try {
            actual = serializer.deserialize(bais, Containers.kDescriptor);
        } catch (TokenizerException e) {
            System.err.println(new String(baos.toByteArray(), UTF_8));
            System.err.println(e.asString());
            fail("oops");
            return;
        }
        assertEquals(actual, expected);
    }
    // complex message in stream.
    {
        baos.reset();
        size = 0;
        for (int i = 0; i < 10; ++i) {
            size += serializer.serialize(baos, containers.get(i));
        }
        assertEquals(baos.size(), size);
        bais = new ByteArrayInputStream(baos.toByteArray());
        for (int i = 0; i < 10; ++i) {
            Containers expected = containers.get(i);
            Containers actual = serializer.deserialize(bais, Containers.kDescriptor);
            assertEquals(actual, expected);
        }
        assertEquals(0, bais.available());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Containers(net.morimekta.test.providence.core.no_rw_binary.Containers) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)1 Containers (net.morimekta.test.providence.core.no_rw_binary.Containers)1 Test (org.junit.Test)1