Search in sources :

Example 1 with TypicalDataMessage

use of protos.TypicalDataMessage in project j2objc by google.

the class CompatibilityTest method checkGetters.

private void checkGetters(TypicalDataOrBuilder data, FieldDescriptor[] fields) {
    assertEquals(42, data.getMyInt());
    assertEquals("foo", new String(data.getMyBytes().toByteArray()));
    assertEquals(TypicalData.EnumType.VALUE9, data.getMyEnumType());
    assertTrue(data.getMyBool());
    assertEquals(new Float(43.8), data.getMyFloat());
    assertEquals(new Double(44.5), data.getMyDouble());
    assertEquals("bar", data.getMyString());
    assertEquals(24, data.getMyUint());
    assertEquals(4422, data.getMyLong());
    assertEquals(2244, data.getMyUlong());
    assertEquals(1, data.getRepeatedInt32Count());
    assertEquals(72, data.getRepeatedInt32(0));
    assertEquals("aaa", data.getRepeatedString(0));
    assertEquals("ccc", data.getRepeatedString(1));
    assertEquals(1.2, data.getRepeatedDouble(0), 0.0001);
    Object result;
    result = data.getField(fields[1]);
    assertTrue(result instanceof Integer);
    assertEquals(42, result);
    result = data.getField(fields[2]);
    assertTrue(result instanceof ByteString);
    assertEquals("foo", new String(((ByteString) result).toByteArray()));
    result = data.getField(fields[3]);
    assertTrue(result instanceof EnumValueDescriptor);
    assertEquals(9, ((EnumValueDescriptor) result).getNumber());
    result = data.getField(fields[11]);
    assertTrue(result instanceof TypicalDataMessage);
    assertEquals(TypicalDataMessage.newBuilder().build(), result);
    result = data.getField(fields[12]);
    assertTrue(result instanceof Boolean);
    assertEquals(Boolean.TRUE, result);
    result = data.getField(fields[13]);
    assertTrue(result instanceof Float);
    assertEquals(43.8, ((Float) result).floatValue(), 0.0001);
    result = data.getField(fields[14]);
    assertTrue(result instanceof Double);
    assertEquals(44.5, ((Double) result).doubleValue(), 0.0001);
    result = data.getField(fields[15]);
    assertTrue(result instanceof String);
    assertEquals("bar", result);
    result = data.getField(fields[16]);
    assertTrue(result instanceof Integer);
    assertEquals(24, result);
    result = data.getField(fields[17]);
    assertTrue(result instanceof Long);
    assertEquals(4422L, result);
    result = data.getField(fields[18]);
    assertTrue(result instanceof Long);
    assertEquals(2244L, result);
    assertEquals(1, data.getRepeatedFieldCount(fields[4]));
    result = data.getRepeatedField(fields[4], 0);
    assertTrue(result instanceof Integer);
    assertEquals(72, result);
    assertEquals(2, data.getRepeatedFieldCount(fields[8]));
    result = data.getRepeatedField(fields[8], 1);
    assertEquals("ccc", result);
    assertEquals(2, data.getRepeatedFieldCount(fields[7]));
    result = data.getRepeatedField(fields[7], 1);
    assertEquals(3.4, ((Double) result).doubleValue(), 0.0001);
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor)

Example 2 with TypicalDataMessage

use of protos.TypicalDataMessage in project j2objc by google.

the class CompatibilityTest method checkGetExtensions.

private void checkGetExtensions(TypicalDataOrBuilder data) {
    assertEquals(123, ((Integer) data.getExtension(Typical.myPrimitiveExtension)).intValue());
    Object msg = data.getExtension(Typical.myExtension);
    assertTrue(msg instanceof TypicalDataMessage);
    assertEquals(321, ((TypicalDataMessage) msg).getMyMessageInt());
    Object result = data.getExtension(Typical.myRepeatedPrimitiveExtension);
    assertTrue(result instanceof List);
    assertTrue(((List) result).get(0) instanceof Integer);
    assertEquals(3, data.getExtensionCount(Typical.myRepeatedPrimitiveExtension));
    assertEquals(2, ((Integer) data.getExtension(Typical.myRepeatedPrimitiveExtension, 1)).intValue());
    assertEquals(3, ((Integer) data.getExtension(Typical.myRepeatedPrimitiveExtension, 2)).intValue());
    assertEquals(2, data.getExtensionCount(Typical.myRepeatedExtension));
    result = data.getExtension(Typical.myRepeatedExtension, 1);
    assertTrue(result instanceof TypicalDataMessage);
    assertEquals(543, ((TypicalDataMessage) result).getMyMessageInt());
    assertEquals(TypicalData.EnumType.VALUE1, data.getExtension(Typical.myEnumExtension));
    result = data.getExtension(Typical.myBytesExtension);
    assertTrue(result instanceof ByteString);
    assertEquals("abc", new String(((ByteString) result).toByteArray()));
    result = data.getExtension(Typical.myBoolExtension);
    assertEquals(Boolean.TRUE, result);
    assertEquals(456, ((Integer) data.getExtension(MsgWithNestedExtensions.intExt)).intValue());
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) List(java.util.List) ByteString(com.google.protobuf.ByteString)

Example 3 with TypicalDataMessage

use of protos.TypicalDataMessage in project j2objc by google.

the class CompatibilityTest method testExtensionRegistry.

public void testExtensionRegistry() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    Typical.registerAllExtensions(registry);
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(1);
    assertFalse(fieldDescriptor.isExtension());
    ExtensionRegistry.ExtensionInfo extensionInfo = registry.findImmutableExtensionByNumber(descriptor, 1000);
    assertNotNull(extensionInfo);
    FieldDescriptor extensionFieldDescriptor = extensionInfo.descriptor;
    assertNotNull(extensionFieldDescriptor);
    assertEquals(1000, extensionFieldDescriptor.getNumber());
    assertTrue(extensionFieldDescriptor.isExtension());
    Message message = extensionInfo.defaultInstance;
    assertTrue(message instanceof TypicalDataMessage);
    TypicalDataMessage data = ((TypicalDataMessage.Builder) message.toBuilder()).setMyMessageInt(100).build();
    assertEquals(100, data.getMyMessageInt());
    // Primitive extension
    extensionInfo = registry.findImmutableExtensionByNumber(descriptor, 1001);
    assertNotNull(extensionInfo);
    extensionFieldDescriptor = extensionInfo.descriptor;
    assertNotNull(extensionFieldDescriptor);
    assertEquals(1001, extensionFieldDescriptor.getNumber());
    assertTrue(extensionFieldDescriptor.isExtension());
    assertNull(extensionInfo.defaultInstance);
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) TypicalDataMessage(protos.TypicalDataMessage) AbstractMessage(com.google.protobuf.AbstractMessage) Message(com.google.protobuf.Message) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 4 with TypicalDataMessage

use of protos.TypicalDataMessage in project j2objc by google.

the class CompatibilityTest method testNewBuilderForField.

public void testNewBuilderForField() throws Exception {
    Descriptor descriptor = TypicalData.Builder.getDescriptor();
    FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(11);
    TypicalData.Builder dataBuilder = TypicalData.newBuilder();
    TypicalDataMessage.Builder messageBuilder = (TypicalDataMessage.Builder) dataBuilder.newBuilderForField(fieldDescriptor);
    TypicalDataMessage message = messageBuilder.setMyMessageInt(10).build();
    assertEquals(10, message.getMyMessageInt());
    fieldDescriptor = descriptor.findFieldByNumber(1);
    try {
        dataBuilder.newBuilderForField(fieldDescriptor);
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    // Expected.
    }
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) TypicalData(protos.TypicalData) TypicalDataOrBuilder(protos.TypicalDataOrBuilder) MsgWithDefaultsOrBuilder(protos.MsgWithDefaultsOrBuilder) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) EnumValueDescriptor(com.google.protobuf.Descriptors.EnumValueDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 5 with TypicalDataMessage

use of protos.TypicalDataMessage in project j2objc by google.

the class CompatibilityTest method testIsEqualAndHashCode.

public void testIsEqualAndHashCode() throws Exception {
    TypicalDataMessage subMsg1 = TypicalDataMessage.newBuilder().setMyMessageInt(11).build();
    TypicalData.Builder builder1 = TypicalData.newBuilder().setMyMessage(subMsg1).setMyInt(22);
    TypicalData data1 = builder1.build();
    TypicalDataMessage subMsg2 = TypicalDataMessage.newBuilder().setMyMessageInt(11).build();
    TypicalData.Builder builder2 = TypicalData.newBuilder().setMyMessage(subMsg2).setMyInt(22);
    TypicalData data2 = builder2.build();
    TypicalDataMessage subMsg3 = TypicalDataMessage.newBuilder().setMyMessageInt(33).build();
    TypicalData.Builder builder3 = TypicalData.newBuilder().setMyMessage(subMsg3).setMyInt(22);
    TypicalData data3 = builder3.build();
    // Builders are not equal.
    assertFalse(builder1.equals(builder2));
    assertFalse(builder2.equals(builder1));
    assertTrue(data1.equals(data2));
    assertTrue(data2.equals(data1));
    assertEquals(data1.hashCode(), data2.hashCode());
    assertFalse(data1.equals(data3));
    assertFalse(data3.equals(data1));
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) TypicalData(protos.TypicalData)

Aggregations

TypicalDataMessage (protos.TypicalDataMessage)8 TypicalData (protos.TypicalData)5 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)4 Descriptor (com.google.protobuf.Descriptors.Descriptor)3 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)3 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)3 ByteString (com.google.protobuf.ByteString)2 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)2 ArrayList (java.util.ArrayList)2 AbstractMessage (com.google.protobuf.AbstractMessage)1 Message (com.google.protobuf.Message)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 List (java.util.List)1 MsgWithDefaultsOrBuilder (protos.MsgWithDefaultsOrBuilder)1 TypicalDataOrBuilder (protos.TypicalDataOrBuilder)1