Search in sources :

Example 16 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class CompatibilityTest method testSetAndGetExtensions.

public void testSetAndGetExtensions() throws Exception {
    TypicalDataMessage extensionMessage = TypicalDataMessage.newBuilder().setMyMessageInt(321).build();
    List<Integer> repeatedInts = new ArrayList<Integer>();
    repeatedInts.add(1);
    repeatedInts.add(2);
    List<TypicalDataMessage> repeatedData = new ArrayList<TypicalDataMessage>();
    repeatedData.add(TypicalDataMessage.newBuilder().setMyMessageInt(432).build());
    repeatedData.add(TypicalDataMessage.newBuilder().setMyMessageInt(543).build());
    TypicalData.Builder dataBuilder = TypicalData.newBuilder().setExtension(Typical.myPrimitiveExtension, 123).setExtension(Typical.myExtension, extensionMessage).setExtension(Typical.myRepeatedPrimitiveExtension, repeatedInts).addExtension(Typical.myRepeatedPrimitiveExtension, 3).setExtension(Typical.myRepeatedExtension, repeatedData).setExtension(Typical.myEnumExtension, TypicalData.EnumType.VALUE1).setExtension(Typical.myBytesExtension, ByteString.copyFrom("abc".getBytes())).setExtension(Typical.myBoolExtension, Boolean.TRUE).setExtension(MsgWithNestedExtensions.intExt, 456);
    checkGetExtensions(dataBuilder);
    checkGetExtensions(dataBuilder.build());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    dataBuilder.build().writeTo(out);
    byte[] msgBytes = asBytes(new int[] { 0xC2, 0x3E, 0x03, 0x08, 0xC1, 0x02, 0xC8, 0x3E, 0x7B, 0xD0, 0x3E, 0x01, 0xD0, 0x3E, 0x02, 0xD0, 0x3E, 0x03, 0xDA, 0x3E, 0x03, 0x08, 0xB0, 0x03, 0xDA, 0x3E, 0x03, 0x08, 0x9F, 0x04, 0xE0, 0x3E, 0x01, 0xEA, 0x3E, 0x03, 0x61, 0x62, 0x63, 0xF0, 0x3E, 0x01, 0x80, 0x7D, 0xC8, 0x03 });
    checkBytes(msgBytes, out.toByteArray());
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    Typical.registerAllExtensions(registry);
    ByteArrayInputStream in = new ByteArrayInputStream(msgBytes);
    TypicalData data = TypicalData.newBuilder().mergeFrom(in, registry).build();
    checkGetExtensions(data);
}
Also used : TypicalDataMessage(protos.TypicalDataMessage) TypicalData(protos.TypicalData) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 17 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class PrimitivesTest method testParseFromByteArray.

public void testParseFromByteArray() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    Primitives.registerAllExtensions(registry);
    PrimitiveFields msg = PrimitiveFields.parseFrom(ALL_PRIMITIVES_BYTES, registry);
    checkFields(msg);
}
Also used : PrimitiveFields(protos.PrimitiveFields) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 18 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class StringsTest method testMergeFromInputStream.

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

Example 19 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class CompatibilityTest method testExtensionRegistryGetUnmodifiable.

public void testExtensionRegistryGetUnmodifiable() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    ExtensionRegistry registry2 = registry.getUnmodifiable();
    registry.add(Typical.myPrimitiveExtension);
    // Extension added to registry should be visible in registry2.
    Descriptor descriptor = TypicalData.getDescriptor();
    ExtensionRegistry.ExtensionInfo extensionInfo = registry2.findExtensionByNumber(descriptor, 1001);
    assertNotNull(extensionInfo);
    ExtensionRegistryLite registryLite = ExtensionRegistryLite.newInstance();
    ExtensionRegistryLite registryLite2 = registryLite.getUnmodifiable();
    assertNotNull(registryLite2);
}
Also used : ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) 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)

Example 20 with ExtensionRegistry

use of com.google.protobuf.ExtensionRegistry in project j2objc by google.

the class EnumsTest method testBadEnumValue.

// TODO(kstanger): This fails with native ObjC because it doesn't sign-extend
// when writing the negative enum value.
/*public void testSerialization() throws Exception {
    EnumMsg msg = getFilledMessage();

    assertEquals(71, msg.getSerializedSize());
    byte[] bytes1 = msg.toByteArray();
    checkBytes(ALL_ENUMS_BYTES, bytes1);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.writeTo(out);
    byte[] bytes2 = out.toByteArray();
    checkBytes(ALL_ENUMS_BYTES, bytes2);
  }*/
// Tests that unknown enum values are skipped and don't cause a
// InvalidProtocolBufferException.
public void testBadEnumValue() throws Exception {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    EnumFields.registerAllExtensions(registry);
    byte[] bytes = asBytes(new int[] { 0x08, 0x09, 0xA8, 0x01, 0x09, 0xCA, 0x02, 0x01, 0x09, 0xC8, 0x3E, 0x09, 0xE8, 0x3F, 0x09, 0x8A, 0x41, 0x01, 0x09 });
    EnumMsg msg = EnumMsg.parseFrom(bytes, registry);
    assertFalse(msg.hasEnumF());
    assertEquals(0, msg.getEnumRCount());
    assertEquals(0, msg.getEnumPCount());
// TODO(kstanger): Native ObjC behavior differs from Java behavior here.
//assertFalse(msg.hasExtension(EnumFields.enumFe));
//assertEquals(0, msg.getExtensionCount(EnumFields.enumRe));
//assertEquals(0, msg.getExtensionCount(EnumFields.enumPe));
}
Also used : EnumMsg(protos.EnumMsg) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Aggregations

ExtensionRegistry (com.google.protobuf.ExtensionRegistry)29 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Descriptor (com.google.protobuf.Descriptors.Descriptor)4 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)4 MessageData (protos.MessageData)4 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)3 EnumValueDescriptor (com.google.protobuf.Descriptors.EnumValueDescriptor)3 EnumMsg (protos.EnumMsg)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 PrimitiveFields (protos.PrimitiveFields)2 StringMsg (protos.StringMsg)2 TypicalData (protos.TypicalData)2 TypicalDataMessage (protos.TypicalDataMessage)2 ByteString (com.google.protobuf.ByteString)1 CodedInputStream (com.google.protobuf.CodedInputStream)1 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 GeneratedMessage (com.google.protobuf.GeneratedMessage)1 Message (com.google.protobuf.Message)1