Search in sources :

Example 1 with PrettySerializer

use of net.morimekta.providence.serializer.PrettySerializer in project providence by morimekta.

the class MessageStreamsTest method testFileCollector.

@Test
public void testFileCollector() throws IOException {
    File file = tmp.newFile("tmp");
    Collector<CompactFields, OutputStream, Integer> collector = MessageCollectors.toFile(file, new PrettySerializer().config());
    OutputStream out = mock(OutputStream.class);
    doThrow(new IOException("oops")).when(out).write(MessageStreams.READABLE_ENTRY_SEP);
    doThrow(new IOException("close")).when(out).close();
    assertThat(collector.combiner().apply(out, out), is(sameInstance(out)));
    try {
        collector.accumulator().accept(out, list.get(0));
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Unable to write to tmp"));
        assertThat(e.getCause(), is(instanceOf(IOException.class)));
        assertThat(e.getCause().getMessage(), is("oops"));
    }
    try {
        collector.finisher().apply(out);
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Unable to close tmp"));
        assertThat(e.getCause(), is(instanceOf(IOException.class)));
        assertThat(e.getCause().getMessage(), is("close"));
    }
    Serializer ms = mock(Serializer.class);
    doThrow(new SerializerException("oops2")).when(ms).serialize(out, list.get(0));
    collector = MessageCollectors.toFile(file, ms);
    try {
        collector.accumulator().accept(out, list.get(0));
        fail("no exception");
    } catch (UncheckedIOException e) {
        assertThat(e.getMessage(), is("Bad data"));
        assertThat(e.getCause(), is(instanceOf(SerializerException.class)));
        assertThat(e.getCause().getMessage(), is("oops2"));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) CompactFields(net.morimekta.test.providence.core.CompactFields) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Test(org.junit.Test)

Example 2 with PrettySerializer

use of net.morimekta.providence.serializer.PrettySerializer in project providence by morimekta.

the class GeneratorWatcherTest method pretty.

private <M extends PMessage<M, F>, F extends PField> String pretty(M message) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new PrettySerializer().config().serialize(baos, message);
    return new String(baos.toByteArray(), StandardCharsets.UTF_8);
}
Also used : PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 3 with PrettySerializer

use of net.morimekta.providence.serializer.PrettySerializer in project providence by morimekta.

the class ResourceConfigSupplier method loadInternal.

private static <Message extends PMessage<Message, Field>, Field extends PField> Message loadInternal(String resourceName, PMessageDescriptor<Message, Field> descriptor) throws ProvidenceConfigException {
    int lastDot = resourceName.lastIndexOf(".");
    if (lastDot < 1) {
        throw new ProvidenceConfigException("No file ending, or no resource file name: " + resourceName);
    }
    int lastSlash = resourceName.lastIndexOf("/");
    String fileName = resourceName;
    if (lastSlash >= 0) {
        fileName = resourceName.substring(lastSlash + 1);
    }
    String suffix = resourceName.substring(lastDot).toLowerCase(Locale.US);
    Serializer serializer;
    switch(suffix) {
        case ".jsn":
        case ".json":
            serializer = new JsonSerializer();
            break;
        case ".cfg":
        case ".cnf":
        case ".config":
        case ".pvd":
        case ".providence":
            serializer = new PrettySerializer().config();
            break;
        // TODO: Add YAML serializer to the file options. Could be a wrapper around SnakeYAML.
        default:
            throw new ProvidenceConfigException(String.format(Locale.US, "Unrecognized resource config type: %s (%s)", suffix, resourceName));
    }
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    InputStream in = classLoader.getResourceAsStream(resourceName);
    if (in == null) {
        in = ResourceConfigSupplier.class.getResourceAsStream(resourceName);
        if (in == null) {
            throw new ProvidenceConfigException("No such config resource: " + resourceName);
        }
    }
    try {
        try (InputStream bin = new BufferedInputStream(in)) {
            return serializer.deserialize(bin, descriptor);
        } catch (TokenizerException te) {
            throw new ProvidenceConfigException(te);
        } catch (JsonSerializerException se) {
            throw new ProvidenceConfigException(se);
        } catch (IOException e) {
            throw new ProvidenceConfigException(e, "Unknown serializer exception: " + e.getMessage());
        }
    } catch (ProvidenceConfigException pce) {
        pce.setFile(fileName);
        throw pce;
    }
}
Also used : JsonSerializerException(net.morimekta.providence.serializer.JsonSerializerException) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer)

Example 4 with PrettySerializer

use of net.morimekta.providence.serializer.PrettySerializer in project providence by morimekta.

the class LogformatterTest method testFormat_EverythingDefault.

@Test
public void testFormat_EverythingDefault() throws IOException {
    Containers containers = MessageStreams.resource("/compat/binary.data", new BinarySerializer(), Containers.kDescriptor).findFirst().orElseThrow(() -> new AssertionError("resource"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new PrettySerializer().config().serialize(baos, containers);
    assertThat(new LogFormatter(true).format(containers), is(equalToLines(new String(baos.toByteArray(), UTF_8))));
}
Also used : PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Containers(net.morimekta.test.providence.core.Containers) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 5 with PrettySerializer

use of net.morimekta.providence.serializer.PrettySerializer in project providence by morimekta.

the class GeneratorWatcherTest method testRandom_withReader.

@Test
public void testRandom_withReader() {
    ByteArrayInputStream bais = new ByteArrayInputStream(("{\n" + "  name = \"villa\"\n" + "  id = 123\n" + "  label = \"Sjampanjebrus\"\n" + "}\n").getBytes(StandardCharsets.UTF_8));
    MessageReader reader = new IOMessageReader(bais, new PrettySerializer());
    CompactFields compact = CompactFields.builder().setId(123).setName("villa").setLabel("Sjampanjebrus").build();
    GeneratorWatcher<SimpleGeneratorBase, SimpleGeneratorContext> generator = GeneratorWatcher.create().dumpOnFailure().setMessageReader(reader);
    generator.starting(Description.EMPTY);
    CompactFields gen = generator.generate(CompactFields.kDescriptor);
    assertThat(gen, is(equalToMessage(compact)));
    assertThat(generator.allGenerated(), hasItem(compact));
    generator.failed(new Throwable(), Description.EMPTY);
    assertThat(console.output(), is(""));
    assertThat(console.error(), is("android.CompactFields {\n" + "  name = \"villa\"\n" + "  id = 123\n" + "  label = \"Sjampanjebrus\"\n" + "}\n"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) MessageReader(net.morimekta.providence.mio.MessageReader) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) CompactFields(net.morimekta.test.android.CompactFields) Test(org.junit.Test)

Aggregations

PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)7 Test (org.junit.Test)5 IOException (java.io.IOException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)2 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)2 Serializer (net.morimekta.providence.serializer.Serializer)2 ImmutableList (com.google.common.collect.ImmutableList)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1