Search in sources :

Example 1 with ConsumeAll

use of net.morimekta.test.providence.core.ConsumeAll in project providence by morimekta.

the class SerializerTest method testSerializer.

/**
 * Test that the serializer can serialize and deserialize a test-set of
 * random data. This is not testing backward compatibility of the
 * serializer.
 *
 * @param serializer The serializer to test.
 */
private void testSerializer(Serializer serializer) throws IOException {
    try {
        // Just a sanity check.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais;
        int size;
        // simple message.
        {
            baos.reset();
            size = serializer.serialize(baos, operation);
            assertEquals(baos.size(), size);
            bais = new ByteArrayInputStream(baos.toByteArray());
            Operation actual = serializer.deserialize(bais, Operation.kDescriptor);
            assertEquals(actual, operation);
        }
        // complex message, one at a time.
        for (Containers expected : containers) {
            baos.reset();
            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());
                e.printStackTrace();
                fail("oops");
                return;
            }
            assertThat(actual, new EqualToMessage<>(expected));
        }
        // complex message in stream.
        {
            baos.reset();
            boolean first = true;
            size = 0;
            for (Containers c : containers) {
                if (first) {
                    first = false;
                } else {
                    baos.write('\n');
                    size += 1;
                }
                size += serializer.serialize(baos, c);
            }
            assertEquals(baos.size(), size);
            bais = new ByteArrayInputStream(baos.toByteArray());
            first = true;
            for (Containers expected : containers) {
                if (first) {
                    first = false;
                } else {
                    assertThat(bais.read(), is((int) '\n'));
                }
                Containers actual = serializer.deserialize(bais, Containers.kDescriptor);
                assertThat(actual, new EqualToMessage<>(expected));
            }
            assertEquals(0, bais.available());
        }
        try {
            if (serializer instanceof PrettySerializer) {
                String tmp = new String(baos.toByteArray(), UTF_8);
                bais = new ByteArrayInputStream(tmp.replaceFirst("providence[.]Containers", "providence.ConsumeAll").getBytes(UTF_8));
            } else {
                bais = new ByteArrayInputStream(baos.toByteArray());
            }
            boolean first = true;
            for (Containers ignore : containers) {
                if (first) {
                    first = false;
                } else {
                    assertThat(bais.read(), is((int) '\n'));
                }
                ConsumeAll actual = serializer.deserialize(bais, ConsumeAll.kDescriptor);
                assertThat(actual, new EqualToMessage<>(ConsumeAll.builder().build()));
            }
        } catch (TokenizerException e) {
            System.err.println(e.asString());
            throw e;
        }
        // service
        for (PServiceCall<?, ?> call : serviceCalls) {
            baos.reset();
            int i = serializer.serialize(baos, call);
            assertThat(i, is(baos.size()));
            bais = new ByteArrayInputStream(baos.toByteArray());
            PServiceCall<?, ?> re = serializer.deserialize(bais, ContainerService.kDescriptor);
            assertThat(re, is(call));
        }
    } catch (SerializerException e) {
        System.err.println(e.asString());
        throw e;
    }
}
Also used : Containers(net.morimekta.test.providence.core.Containers) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Operation(net.morimekta.test.providence.core.calculator.Operation) ConsumeAll(net.morimekta.test.providence.core.ConsumeAll) EqualToMessage(net.morimekta.providence.util_internal.EqualToMessage) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)1 EqualToMessage (net.morimekta.providence.util_internal.EqualToMessage)1 ConsumeAll (net.morimekta.test.providence.core.ConsumeAll)1 Containers (net.morimekta.test.providence.core.Containers)1 Operation (net.morimekta.test.providence.core.calculator.Operation)1