ProtoCoder extends CustomCoder<T> { public static <T extends Message> ProtoCoder<T> of(Class<T> protoMessageClass) { return new ProtoCoder<>(protoMessageClass, ImmutableSet.of()); } protected  ProtoCoder(Class<T> protoMessageClass, Set<Class<?>> extensionHostClasses); static ProtoCoder<T> of(Class<T> protoMessageClass); static ProtoCoder<T> of(TypeDescriptor<T> protoMessageType); ProtoCoder<T> withExtensionsFrom(Iterable<Class<?>> moreExtensionHosts); ProtoCoder<T> withExtensionsFrom(Class<?>... moreExtensionHosts); @Override void encode(T value, OutputStream outStream); @Override void encode(T value, OutputStream outStream, Context context); @Override T decode(InputStream inStream); @Override T decode(InputStream inStream, Context context); @Override boolean equals(@Nullable Object other); @Override int hashCode(); @Override void verifyDeterministic(); Class<T> getMessageType(); Set<Class<?>> getExtensionHosts(); ExtensionRegistry getExtensionRegistry(); static CoderProvider getCoderProvider(); static final long serialVersionUID; }
@Test public void testCoderEncodeDecodeEqual() throws Exception { MessageA value = MessageA.newBuilder() .setField1("hello") .addField2(MessageB.newBuilder().setField1(true).build()) .addField2(MessageB.newBuilder().setField1(false).build()) .build(); CoderProperties.coderDecodeEncodeEqual(ProtoCoder.of(MessageA.class), value); }
@Test public void testCoderEncodeDecodeEqualNestedContext() throws Exception { MessageA value1 = MessageA.newBuilder() .setField1("hello") .addField2(MessageB.newBuilder().setField1(true).build()) .addField2(MessageB.newBuilder().setField1(false).build()) .build(); MessageA value2 = MessageA.newBuilder() .setField1("world") .addField2(MessageB.newBuilder().setField1(false).build()) .addField2(MessageB.newBuilder().setField1(true).build()) .build(); CoderProperties.coderDecodeEncodeEqual( ListCoder.of(ProtoCoder.of(MessageA.class)), ImmutableList.of(value1, value2)); }
@Test public void testCoderSerialization() throws Exception { ProtoCoder<MessageA> coder = ProtoCoder.of(MessageA.class); CoderProperties.coderSerializable(coder); }
@Test public void encodeNullThrowsCoderException() throws Exception { thrown.expect(CoderException.class); thrown.expectMessage("cannot encode a null MessageA"); CoderUtils.encodeToBase64(ProtoCoder.of(MessageA.class), null); }
@Test public void testNonDeterministicProperty() throws CoderException { MessageWithMap.Builder msg1B = MessageWithMap.newBuilder(); MessageWithMap.Builder msg2B = MessageWithMap.newBuilder(); for (int i = 0; i < 10; ++i) { msg1B.getMutableField1().put("key" + i, MessageA.getDefaultInstance()); msg2B.getMutableField1().put("key" + (9 - i), MessageA.getDefaultInstance()); } MessageWithMap msg1 = msg1B.build(); MessageWithMap msg2 = msg2B.build(); assertEquals(msg2, msg1); Coder<MessageWithMap> coder = ProtoCoder.of(MessageWithMap.class); assertNotEquals(CoderUtils.encodeToBase64(coder, msg2), CoderUtils.encodeToBase64(coder, msg1)); }