Search in sources :

Example 1 with SimpleMessage

use of com.squareup.wire.protos.simple.SimpleMessage 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 SimpleMessage

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

the class WireTest method testEmptyList.

@Test
public void testEmptyList() throws IOException {
    SimpleMessage noListMessage = new SimpleMessage.Builder().required_int32(1).build();
    SimpleMessage emptyListMessage = new SimpleMessage.Builder().required_int32(1).repeated_double(new ArrayList<Double>()).build();
    assertThat(noListMessage.repeated_double).isNotNull();
    assertThat(noListMessage.repeated_double).hasSize(0);
    assertThat(emptyListMessage.repeated_double).isNotNull();
    assertThat(emptyListMessage.repeated_double).hasSize(0);
    // Empty lists and null lists compare as equals()
    assertThat(noListMessage).isEqualTo(emptyListMessage);
    // Empty lists and null lists have the same hashCode()
    int noListHashCode = noListMessage.hashCode();
    int emptyListHashCode = emptyListMessage.hashCode();
    assertThat(noListHashCode).isEqualTo(emptyListHashCode);
    ProtoAdapter<SimpleMessage> adapter = SimpleMessage.ADAPTER;
    // Empty lists and null lists occupy 0 bytes in the wire encoding
    byte[] noListBytes = adapter.encode(noListMessage);
    byte[] emptyListBytes = adapter.encode(emptyListMessage);
    assertThat(noListBytes.length).isEqualTo(2);
    assertThat(emptyListBytes.length).isEqualTo(2);
    assertThat(emptyListBytes[0]).isEqualTo(noListBytes[0]);
    assertThat(emptyListBytes[1]).isEqualTo(noListBytes[1]);
    // Parsed results have a null list
    SimpleMessage noListParsed = adapter.decode(noListBytes);
    SimpleMessage emptyListParsed = adapter.decode(emptyListBytes);
    assertThat(noListParsed.repeated_double).isNotNull();
    assertThat(noListParsed.repeated_double).hasSize(0);
    assertThat(emptyListParsed.repeated_double).isNotNull();
    assertThat(emptyListParsed.repeated_double).hasSize(0);
}
Also used : SimpleMessage(com.squareup.wire.protos.simple.SimpleMessage) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with SimpleMessage

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

the class SerializableTest method simple.

@Test
public void simple() throws Exception {
    SimpleMessage message = new SimpleMessage.Builder().required_int32(42).build();
    assertThat(deserialize(serialize(message))).isEqualTo(message);
}
Also used : SimpleMessage(com.squareup.wire.protos.simple.SimpleMessage) Test(org.junit.Test)

Example 4 with SimpleMessage

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

the class SerializableTest method decodeGolden.

@Test
public void decodeGolden() throws Exception {
    SimpleMessage goldenValue = new SimpleMessage.Builder().required_int32(99).result("tacos").build();
    ByteString goldenSerialized = ByteString.decodeBase64("rO0ABXNyACdjb20uc3F1YXJldXAud2lyZS5NZXNz" + "YWdlU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAlsABWJ5dGVzdAACW0JMAAxtZXNzYWdlQ2xhc3N0ABFMamF2YS9s" + "YW5nL0NsYXNzO3hwdXIAAltCrPMX+AYIVOACAAB4cAAAAAkoY1IFdGFjb3N2cgAtY29tLnNxdWFyZXVwLndpcmUu" + "cHJvdG9zLnNpbXBsZS5TaW1wbGVNZXNzYWdlAAAAAAAAAAACAAxMABRkZWZhdWx0X2ZvcmVpZ25fZW51bXQALkxj" + "b20vc3F1YXJldXAvd2lyZS9wcm90b3MvZm9yZWlnbi9Gb3JlaWduRW51bTtMABNkZWZhdWx0X25lc3RlZF9lbnVt" + "dAA6TGNvbS9zcXVhcmV1cC93aXJlL3Byb3Rvcy9zaW1wbGUvU2ltcGxlTWVzc2FnZSROZXN0ZWRFbnVtO0wAF25v" + "X2RlZmF1bHRfZm9yZWlnbl9lbnVtcQB+AAdMAAFvdAASTGphdmEvbGFuZy9TdHJpbmc7TAAVb3B0aW9uYWxfZXh0" + "ZXJuYWxfbXNndAAxTGNvbS9zcXVhcmV1cC93aXJlL3Byb3Rvcy9zaW1wbGUvRXh0ZXJuYWxNZXNzYWdlO0wADm9w" + "dGlvbmFsX2ludDMydAATTGphdmEvbGFuZy9JbnRlZ2VyO0wAE29wdGlvbmFsX25lc3RlZF9tc2d0AD1MY29tL3Nx" + "dWFyZXVwL3dpcmUvcHJvdG9zL3NpbXBsZS9TaW1wbGVNZXNzYWdlJE5lc3RlZE1lc3NhZ2U7TAAFb3RoZXJxAH4A" + "CUwACHBhY2thZ2VfcQB+AAlMAA9yZXBlYXRlZF9kb3VibGV0ABBMamF2YS91dGlsL0xpc3Q7TAAOcmVxdWlyZWRf" + "aW50MzJxAH4AC0wABnJlc3VsdHEAfgAJeHIAGWNvbS5zcXVhcmV1cC53aXJlLk1lc3NhZ2UAAAAAAAAAAAIAAHhw");
    assertThat(deserialize(goldenSerialized)).isEqualTo(goldenValue);
    assertThat(serialize(goldenValue)).isEqualTo(goldenSerialized);
}
Also used : ByteString(okio.ByteString) SimpleMessage(com.squareup.wire.protos.simple.SimpleMessage) Test(org.junit.Test)

Example 5 with SimpleMessage

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

the class WireTest method testSimpleMessage.

@Test
public void testSimpleMessage() throws Exception {
    SimpleMessage msg = new SimpleMessage.Builder().required_int32(456).build();
    assertThat(msg.optional_int32).isNull();
    assertThat(msg.optional_nested_msg).isNull();
    assertThat(msg.optional_external_msg).isNull();
    assertThat(msg.default_nested_enum).isNull();
    assertThat(msg.required_int32).isEqualTo(new Integer(456));
    assertThat(msg.repeated_double).isNotNull();
    assertThat(msg.repeated_double).hasSize(0);
    SimpleMessage.Builder builder = new SimpleMessage.Builder();
    builder.optional_int32(789);
    SimpleMessage.NestedMessage.Builder nested_msg_builder = new SimpleMessage.NestedMessage.Builder();
    nested_msg_builder.bb(2);
    builder.optional_nested_msg(nested_msg_builder.build());
    ExternalMessage.Builder external_msg_builder = new ExternalMessage.Builder();
    external_msg_builder.f(99.9f);
    builder.optional_external_msg(external_msg_builder.build());
    builder.default_nested_enum(SimpleMessage.NestedEnum.BAR);
    builder.required_int32(456);
    List<Double> doubles = Arrays.asList(1.0, 2.0, 3.0);
    builder.repeated_double(doubles);
    msg = builder.build();
    assertThat(msg.optional_int32).isEqualTo(new Integer(789));
    assertThat(msg.optional_nested_msg.bb).isEqualTo(new Integer(2));
    assertThat(msg.optional_external_msg.f).isEqualTo(new Float(99.9f));
    assertThat(msg.default_nested_enum).isEqualTo(SimpleMessage.NestedEnum.BAR);
    assertThat(msg.required_int32).isEqualTo(new Integer(456));
    assertThat(msg.repeated_double).isEqualTo(doubles);
    // Modifying the builder list does not affect an already-built message
    List<Double> savedData = new ArrayList<Double>(msg.repeated_double);
    doubles.set(1, 1.1);
    assertThat(msg.repeated_double).isNotSameAs(doubles);
    assertThat(msg.repeated_double).isEqualTo(savedData);
    // Rebuilding will use the new list
    msg = builder.build();
    assertThat(msg.repeated_double).isEqualTo(doubles);
    // Check for required fields
    builder.required_int32(null);
    try {
        builder.build();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessage("Required field not set:\n  required_int32");
    }
    // The message list is immutable
    try {
        msg.repeated_double.set(0, 0.0);
        fail();
    } catch (UnsupportedOperationException e) {
    // expected
    }
    ProtoAdapter<SimpleMessage> adapter = SimpleMessage.ADAPTER;
    byte[] result = adapter.encode(msg);
    assertThat(result.length).isEqualTo(46);
    SimpleMessage newMsg = adapter.decode(result);
    assertThat(newMsg.optional_int32).isEqualTo(new Integer(789));
    assertThat(newMsg.optional_nested_msg.bb).isEqualTo(new Integer(2));
    assertThat(newMsg.optional_external_msg.f).isEqualTo(new Float(99.9F));
    assertThat(newMsg.default_nested_enum).isEqualTo(SimpleMessage.NestedEnum.BAR);
    assertThat(newMsg.required_int32).isEqualTo(new Integer(456));
    assertThat(msg.repeated_double).isEqualTo(doubles);
}
Also used : SimpleMessage(com.squareup.wire.protos.simple.SimpleMessage) ArrayList(java.util.ArrayList) ExternalMessage(com.squareup.wire.protos.simple.ExternalMessage) Test(org.junit.Test)

Aggregations

SimpleMessage (com.squareup.wire.protos.simple.SimpleMessage)8 Test (org.junit.Test)8 ExternalMessage (com.squareup.wire.protos.simple.ExternalMessage)4 ArrayList (java.util.ArrayList)2 Buffer (okio.Buffer)1 ByteString (okio.ByteString)1