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"));
}
}
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);
}
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;
}
}
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))));
}
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"));
}
Aggregations