Search in sources :

Example 1 with MapMsg

use of protos.MapMsg in project j2objc by google.

the class MapsTest method testMapFieldDescriptor.

@SuppressWarnings("unchecked")
public void testMapFieldDescriptor() throws Exception {
    Descriptor descriptor = MapMsg.Builder.getDescriptor();
    FieldDescriptor stringStringField = descriptor.findFieldByNumber(1);
    FieldDescriptor boolEnumField = descriptor.findFieldByNumber(7);
    assertEquals(Type.MESSAGE, stringStringField.getType());
    assertTrue(stringStringField.isRepeated());
    Descriptor entryDescriptor = stringStringField.getMessageType();
    assertNotNull(entryDescriptor);
    assertEquals(2, entryDescriptor.getFields().size());
    FieldDescriptor keyFieldDescriptor = entryDescriptor.findFieldByNumber(1);
    FieldDescriptor valueFieldDescriptor = entryDescriptor.findFieldByNumber(2);
    assertEquals("key", keyFieldDescriptor.getName());
    assertEquals(Type.STRING, keyFieldDescriptor.getType());
    assertEquals("value", valueFieldDescriptor.getName());
    assertEquals(Type.STRING, valueFieldDescriptor.getType());
    MapMsg msg = getFilledMessage();
    Object rawValue = msg.getField(stringStringField);
    assertTrue(rawValue instanceof List);
    List<?> list = (List<?>) rawValue;
    assertEquals(2, list.size());
    Object rawEntry = list.get(0);
    assertTrue(rawEntry instanceof MapEntry);
    MapEntry<?, ?> entry = (MapEntry<?, ?>) rawEntry;
    assertEquals("duck", entry.getKey());
    assertEquals("quack", entry.getValue());
    rawEntry = msg.getRepeatedField(stringStringField, 1);
    assertTrue(rawEntry instanceof MapEntry);
    entry = (MapEntry<?, ?>) rawEntry;
    assertEquals("cat", entry.getKey());
    assertEquals("meow", entry.getValue());
    list = (List<?>) msg.getField(boolEnumField);
    entry = (MapEntry<?, ?>) list.get(0);
    assertTrue(entry.getKey() instanceof Boolean);
    assertTrue(entry.getValue() instanceof Integer);
    assertEquals(Boolean.TRUE, entry.getKey());
    assertEquals(Integer.valueOf(0), entry.getValue());
    entry = (MapEntry<?, ?>) msg.getRepeatedField(boolEnumField, 1);
    assertTrue(entry.getKey() instanceof Boolean);
    assertTrue(entry.getValue() instanceof Integer);
    assertEquals(Boolean.FALSE, entry.getKey());
    assertEquals(Integer.valueOf(2), entry.getValue());
    MapMsg.Builder builder = msg.toBuilder();
    MapEntry<String, String> stringStringEntry = ((List<MapEntry<String, String>>) msg.getField(stringStringField)).get(0);
    stringStringEntry = stringStringEntry.toBuilder().setValue("neigh").build();
    builder.setRepeatedField(stringStringField, 0, stringStringEntry);
    stringStringEntry = stringStringEntry.toBuilder().setKey("cow").setValue("moo").build();
    builder.addRepeatedField(stringStringField, stringStringEntry);
    assertEquals(3, builder.getStringStringCount());
    assertEquals(3, builder.getRepeatedFieldCount(stringStringField));
    assertEquals("moo", builder.getStringStringOrThrow("cow"));
    builder.clearField(stringStringField);
    assertEquals("default", builder.getStringStringOrDefault("cow", "default"));
    assertEquals(0, builder.getStringStringCount());
    List<MapEntry<String, String>> newStringStringList = new ArrayList<>();
    newStringStringList.add(stringStringEntry.toBuilder().setKey("parrot").setValue("squawk").build());
    newStringStringList.add(stringStringEntry.toBuilder().setKey("pig").setValue("oink").build());
    builder.setField(stringStringField, newStringStringList);
    assertEquals("squawk", builder.getStringStringOrThrow("parrot"));
    assertEquals("oink", builder.getStringStringOrThrow("pig"));
}
Also used : MapEntry(com.google.protobuf.MapEntry) ArrayList(java.util.ArrayList) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) MapMsg(protos.MapMsg) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with MapMsg

use of protos.MapMsg in project j2objc by google.

the class MapsTest method testMergeFromInputStream.

public void testMergeFromInputStream() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    ByteArrayInputStream in = new ByteArrayInputStream(ALL_MAPS_BYTES);
    MapMsg.Builder builder = MapMsg.newBuilder().mergeFrom(in, registry);
    MapMsg msg = builder.build();
    checkFields(builder);
    checkFields(msg);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MapMsg(protos.MapMsg) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 3 with MapMsg

use of protos.MapMsg in project j2objc by google.

the class MapsTest method testSerialization.

public void testSerialization() throws Exception {
    MapMsg msg = getFilledMessage();
    assertEquals(150, msg.getSerializedSize());
    byte[] bytes1 = msg.toByteArray();
    checkBytes(ALL_MAPS_BYTES, bytes1);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.writeTo(out);
    byte[] bytes2 = out.toByteArray();
    checkBytes(ALL_MAPS_BYTES, bytes2);
}
Also used : MapMsg(protos.MapMsg) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with MapMsg

use of protos.MapMsg in project j2objc by google.

the class MapsTest method testMergeFromOtherMessage.

public void testMergeFromOtherMessage() throws Exception {
    MapMsg filledMsg = getFilledMessage();
    MapMsg.Builder builder = MapMsg.newBuilder();
    builder.mergeFrom(filledMsg);
    MapMsg msg = builder.build();
    checkFields(builder);
    checkFields(msg);
}
Also used : MapMsg(protos.MapMsg)

Example 5 with MapMsg

use of protos.MapMsg in project j2objc by google.

the class MapsTest method testParseFromByteArray.

@AutoreleasePool
public void testParseFromByteArray() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    MapMsg msg = MapMsg.parseFrom(ALL_MAPS_BYTES, registry);
    checkFields(msg);
}
Also used : MapMsg(protos.MapMsg) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) AutoreleasePool(com.google.j2objc.annotations.AutoreleasePool)

Aggregations

MapMsg (protos.MapMsg)9 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)2 AutoreleasePool (com.google.j2objc.annotations.AutoreleasePool)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 MapEntry (com.google.protobuf.MapEntry)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1