Search in sources :

Example 1 with ExternalMessage

use of com.squareup.wire.protos.simple.ExternalMessage in project wire by square.

the class WireTest method testExtensionEnum.

@Test
public void testExtensionEnum() throws Exception {
    ExternalMessage optional_external_msg = new ExternalMessage.Builder().nested_enum_ext(SimpleMessage.NestedEnum.BAZ).build();
    SimpleMessage msg = new SimpleMessage.Builder().optional_external_msg(optional_external_msg).required_int32(456).build();
    ProtoAdapter<SimpleMessage> adapter = SimpleMessage.ADAPTER;
    byte[] data = adapter.encode(msg);
    // Change BAZ enum to a value not known by this client.
    data[4] = 17;
    // Parse the altered message.
    SimpleMessage newMsg = adapter.decode(data);
    // Original value shows up as an extension.
    assertThat(msg.toString()).contains("nested_enum_ext=BAZ");
    // New value is unknown in the tag map.
    ProtoReader reader = new ProtoReader(new Buffer().write(newMsg.optional_external_msg.unknownFields()));
    reader.beginMessage();
    assertThat(reader.nextTag()).isEqualTo(129);
    assertThat(reader.peekFieldEncoding().rawProtoAdapter().decode(reader)).isEqualTo(17L);
    // Serialized outputs are the same.
    byte[] newData = adapter.encode(newMsg);
    assertThat(data).isEqualTo(newData);
}
Also used : Buffer(okio.Buffer) SimpleMessage(com.squareup.wire.protos.simple.SimpleMessage) ExternalMessage(com.squareup.wire.protos.simple.ExternalMessage) Test(org.junit.Test)

Example 2 with ExternalMessage

use of com.squareup.wire.protos.simple.ExternalMessage in project wire by square.

the class WireTest method testExtensionsNoRegistry.

@Test
public void testExtensionsNoRegistry() throws Exception {
    ExternalMessage optional_external_msg = new ExternalMessage.Builder().fooext(Arrays.asList(444, 555)).barext(333).bazext(222).build();
    SimpleMessage msg = new SimpleMessage.Builder().optional_external_msg(optional_external_msg).required_int32(456).build();
    assertThat(msg.optional_external_msg.fooext).containsExactly(444, 555);
    assertThat(msg.optional_external_msg.barext).isEqualTo(new Integer(333));
    assertThat(msg.optional_external_msg.bazext).isEqualTo(new Integer(222));
    ProtoAdapter<SimpleMessage> adapter = SimpleMessage.ADAPTER;
    byte[] result = adapter.encode(msg);
    assertThat(result.length).isEqualTo(21);
    SimpleMessage newMsg = adapter.decode(result);
    assertThat(newMsg.optional_external_msg.fooext).isEqualTo(Arrays.asList(444, 555));
    assertThat(newMsg.optional_external_msg.barext).isEqualTo(333);
    assertThat(newMsg.optional_external_msg.bazext).isEqualTo(222);
}
Also used : SimpleMessage(com.squareup.wire.protos.simple.SimpleMessage) ExternalMessage(com.squareup.wire.protos.simple.ExternalMessage) Test(org.junit.Test)

Example 3 with ExternalMessage

use of com.squareup.wire.protos.simple.ExternalMessage in project wire by square.

the class WireTest method testExtensions.

@Test
public void testExtensions() throws Exception {
    ExternalMessage optional_external_msg = new ExternalMessage.Builder().fooext(Arrays.asList(444, 555)).barext(333).bazext(222).nested_message_ext(new SimpleMessage.NestedMessage.Builder().bb(77).build()).nested_enum_ext(SimpleMessage.NestedEnum.BAZ).build();
    SimpleMessage msg = new SimpleMessage.Builder().optional_external_msg(optional_external_msg).required_int32(456).build();
    assertThat(msg.optional_external_msg.fooext).containsExactly(444, 555);
    assertThat(msg.optional_external_msg.barext).isEqualTo(new Integer(333));
    assertThat(msg.optional_external_msg.bazext).isEqualTo(new Integer(222));
    assertThat(msg.optional_external_msg.nested_message_ext.bb).isEqualTo(new Integer(77));
    assertThat(msg.optional_external_msg.nested_enum_ext).isEqualTo(SimpleMessage.NestedEnum.BAZ);
    ProtoAdapter<SimpleMessage> adapter = SimpleMessage.ADAPTER;
    byte[] result = adapter.encode(msg);
    assertThat(result.length).isEqualTo(29);
    SimpleMessage newMsg = adapter.decode(result);
    assertThat(newMsg.optional_external_msg.fooext).containsExactly(444, 555);
    assertThat(newMsg.optional_external_msg.barext).isEqualTo(new Integer(333));
    assertThat(newMsg.optional_external_msg.bazext).isEqualTo(new Integer(222));
}
Also used : SimpleMessage(com.squareup.wire.protos.simple.SimpleMessage) ExternalMessage(com.squareup.wire.protos.simple.ExternalMessage) Test(org.junit.Test)

Aggregations

ExternalMessage (com.squareup.wire.protos.simple.ExternalMessage)3 SimpleMessage (com.squareup.wire.protos.simple.SimpleMessage)3 Test (org.junit.Test)3 Buffer (okio.Buffer)1