use of net.morimekta.providence.serializer.Serializer 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.Serializer in project providence by morimekta.
the class NonblockingSocketClientHandlerTest method testSimpleRequest_cannotConnect.
@Test
public void testSimpleRequest_cannotConnect() throws IOException, Failure {
Serializer serializer = new BinarySerializer();
InetSocketAddress address = new InetSocketAddress("localhost", port - 10);
try (NonblockingSocketClientHandler handler = new NonblockingSocketClientHandler(serializer, address)) {
MyService.Iface client = new MyService.Client(handler);
try {
client.test(Request.builder().setText("test").build());
fail("no exception");
} catch (ConnectException e) {
// The exception message is entirely localized, so it's impossible to reliably match against.
// assertThat(e.getMessage(), startsWith("Connection refused"));
}
verifyZeroInteractions(impl);
}
}
use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.
the class SocketClientHandlerTest method testSimpleRequest_cannotConnect.
@Test
public void testSimpleRequest_cannotConnect() throws IOException, Failure, InterruptedException {
Serializer serializer = new BinarySerializer();
InetSocketAddress address = new InetSocketAddress("localhost", port - 10);
MyService.Iface client = new MyService.Client(new SocketClientHandler(serializer, address));
try {
client.test(new Request(null));
fail("no exception");
} catch (ConnectException e) {
assertThat(e.getMessage(), containsString("Connection refused"));
}
Thread.sleep(10L);
verifyZeroInteractions(impl);
}
use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.
the class FormatUtils method getInput.
public static <Message extends PMessage<Message, Field>, Field extends PField> Stream<Message> getInput(@Nonnull PMessageDescriptor<Message, Field> descriptor, @Nonnull ConvertStream in, boolean strict) throws IOException {
Serializer serializer = in.format.createSerializer(strict);
InputStream is = getInputStream(in);
// Ensures it's closed at the end.
return StreamSupport.stream(new MessageSpliterator<>(is, serializer, descriptor, is), false);
}
use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.
the class MessageStreamsTest method testSpliterator.
@Test
public void testSpliterator() throws IOException {
InputStream in = mock(InputStream.class);
Serializer serializer = mock(Serializer.class);
Consumer<CompactFields> consumer = mock(Consumer.class);
MessageSpliterator<CompactFields, CompactFields._Field> spliterator = new MessageSpliterator<>(in, serializer, CompactFields.kDescriptor);
assertThat(spliterator.trySplit(), is(nullValue()));
assertThat(spliterator.estimateSize(), is(Long.MAX_VALUE));
assertThat(spliterator.getExactSizeIfKnown(), is(-1L));
assertThat(spliterator.characteristics(), is(Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.IMMUTABLE));
assertThat(spliterator.getComparator(), is(notNullValue()));
CompactFields cf = CompactFields.builder().build();
// -- read
doReturn(cf).when(serializer).deserialize(in, CompactFields.kDescriptor);
assertThat(spliterator.tryAdvance(consumer), is(true));
verify(serializer).deserialize(in, CompactFields.kDescriptor);
verify(in).markSupported();
verify(consumer).accept(same(cf));
reset(in, serializer, consumer);
doReturn(false).when(serializer).binaryProtocol();
doReturn(-1).when(in).read();
assertThat(spliterator.tryAdvance(consumer), is(false));
verify(in).markSupported();
verifyNoMoreInteractions(in);
verifyZeroInteractions(consumer);
// fails.
reset(in, serializer, consumer);
spliterator = new MessageSpliterator<>(in, serializer, CompactFields.kDescriptor);
doReturn(true).when(serializer).binaryProtocol();
doReturn(true).when(in).markSupported();
doThrow(new IOException("read")).when(in).read();
doThrow(new IOException("close")).when(in).close();
try {
spliterator.tryAdvance(consumer);
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("read"));
assertThat(e.getCause(), is(notNullValue()));
assertThat(e.getCause().getSuppressed().length, is(1));
assertThat(e.getCause().getSuppressed()[0].getMessage(), is("close"));
}
verify(in).markSupported();
verify(in).mark(2);
verify(in).read();
verify(in).close();
verifyNoMoreInteractions(in);
verifyZeroInteractions(serializer, consumer);
}
Aggregations