public void testRepeatedArraysAreImmutable() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); builder.addRepeatedInt32(100); builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()); assertIsUnmodifiable(builder.getRepeatedInt32List()); assertIsUnmodifiable(builder.getRepeatedImportEnumList()); assertIsUnmodifiable(builder.getRepeatedForeignMessageList()); assertIsUnmodifiable(builder.getRepeatedFloatList()); TestAllTypes value = builder.build(); assertIsUnmodifiable(value.getRepeatedInt32List()); assertIsUnmodifiable(value.getRepeatedImportEnumList()); assertIsUnmodifiable(value.getRepeatedForeignMessageList()); assertIsUnmodifiable(value.getRepeatedFloatList()); }
@Test public void testRepeatedArraysAreImmutable() throws Exception { builder.addRepeatedField(fdInt32, 100); builder.addRepeatedField(fdImportEnum, UnittestImport.ImportEnum.IMPORT_BAR); builder.addRepeatedField(fdForeignMessage, ForeignMessage.getDefaultInstance()); assertIsUnmodifiable(builder.getField(fdInt32)); assertIsUnmodifiable(builder.getField(fdImportEnum)); assertIsUnmodifiable(builder.getField(fdForeignMessage)); assertIsUnmodifiable(builder.getField(fdFloat)); final Message value = builder.build(); assertIsUnmodifiable(value.getField(fdInt32)); assertIsUnmodifiable(value.getField(fdImportEnum)); assertIsUnmodifiable(value.getField(fdForeignMessage)); assertIsUnmodifiable(value.getField(fdFloat)); }
public void testProtosShareRepeatedArraysIfDidntChange() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); builder.addRepeatedInt32(100); builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()); TestAllTypes value1 = builder.build(); TestAllTypes value2 = value1.toBuilder().build(); assertSame(value1.getRepeatedInt32List(), value2.getRepeatedInt32List()); assertSame(value1.getRepeatedImportEnumList(), value2.getRepeatedImportEnumList()); assertSame(value1.getRepeatedForeignMessageList(), value2.getRepeatedForeignMessageList()); }
@Test public void testProtosShareRepeatedArraysIfDidntChange() throws Exception { builder.addRepeatedField(fdInt32, 100); builder.addRepeatedField(fdImportEnum, UnittestImport.ImportEnum.IMPORT_BAR); builder.addRepeatedField(fdForeignMessage, ForeignMessage.getDefaultInstance()); final Message value1 = builder.build(); final Message value2 = value1.toBuilder().build(); assertEquals(value1.getField(fdInt32), value2.getField(fdInt32)); assertEquals(value1.getField(fdImportEnum), value2.getField(fdImportEnum)); assertEquals(value1.getField(fdForeignMessage), value2.getField(fdForeignMessage)); }
public void testFileDescriptor() throws Exception { FileDescriptor file = UnittestProto.getDescriptor(); assertEquals("google/protobuf/unittest.proto", file.getName()); assertEquals("protobuf_unittest", file.getPackage()); assertEquals("UnittestProto", file.getOptions().getJavaOuterClassname()); assertEquals("google/protobuf/unittest.proto", file.toProto().getName()); assertEquals(Arrays.asList(UnittestImport.getDescriptor()), file.getDependencies()); Descriptor messageType = TestAllTypes.getDescriptor(); assertEquals(messageType, file.getMessageTypes().get(0)); assertEquals(messageType, file.findMessageTypeByName("TestAllTypes")); assertNull(file.findMessageTypeByName("NoSuchType")); assertNull(file.findMessageTypeByName("protobuf_unittest.TestAllTypes")); for (int i = 0; i < file.getMessageTypes().size(); i++) { assertEquals(i, file.getMessageTypes().get(i).getIndex()); } EnumDescriptor enumType = ForeignEnum.getDescriptor(); assertEquals(enumType, file.getEnumTypes().get(0)); assertEquals(enumType, file.findEnumTypeByName("ForeignEnum")); assertNull(file.findEnumTypeByName("NoSuchType")); assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum")); assertEquals(Arrays.asList(ImportEnum.getDescriptor()), UnittestImport.getDescriptor().getEnumTypes()); for (int i = 0; i < file.getEnumTypes().size(); i++) { assertEquals(i, file.getEnumTypes().get(i).getIndex()); } ServiceDescriptor service = TestService.getDescriptor(); assertEquals(service, file.getServices().get(0)); assertEquals(service, file.findServiceByName("TestService")); assertNull(file.findServiceByName("NoSuchType")); assertNull(file.findServiceByName("protobuf_unittest.TestService")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getServices()); for (int i = 0; i < file.getServices().size(); i++) { assertEquals(i, file.getServices().get(i).getIndex()); } FieldDescriptor extension = UnittestProto.optionalInt32Extension.getDescriptor(); assertEquals(extension, file.getExtensions().get(0)); assertEquals(extension, file.findExtensionByName("optional_int32_extension")); assertNull(file.findExtensionByName("no_such_ext")); assertNull(file.findExtensionByName( "protobuf_unittest.optional_int32_extension")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getExtensions()); for (int i = 0; i < file.getExtensions().size(); i++) { assertEquals(i, file.getExtensions().get(i).getIndex()); } }
public void testUsingBuilderMultipleTimes() throws Exception { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); // primitive field scalar and repeated builder.setOptionalSfixed64(100); builder.addRepeatedInt32(100); // enum field scalar and repeated builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); // proto field scalar and repeated builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(1)); builder.addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(1)); TestAllTypes value1 = builder.build(); assertEquals(100, value1.getOptionalSfixed64()); assertEquals(100, value1.getRepeatedInt32(0)); assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getOptionalImportEnum()); assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getRepeatedImportEnum(0)); assertEquals(1, value1.getOptionalForeignMessage().getC()); assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); // Make sure that builder didn't update previously created values builder.setOptionalSfixed64(200); builder.setRepeatedInt32(0, 200); builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_FOO); builder.setRepeatedImportEnum(0, UnittestImport.ImportEnum.IMPORT_FOO); builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(2)); builder.setRepeatedForeignMessage(0, ForeignMessage.newBuilder().setC(2)); TestAllTypes value2 = builder.build(); // Make sure value1 didn't change. assertEquals(100, value1.getOptionalSfixed64()); assertEquals(100, value1.getRepeatedInt32(0)); assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getOptionalImportEnum()); assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, value1.getRepeatedImportEnum(0)); assertEquals(1, value1.getOptionalForeignMessage().getC()); assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); // Make sure value2 is correct assertEquals(200, value2.getOptionalSfixed64()); assertEquals(200, value2.getRepeatedInt32(0)); assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, value2.getOptionalImportEnum()); assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, value2.getRepeatedImportEnum(0)); assertEquals(2, value2.getOptionalForeignMessage().getC()); assertEquals(2, value2.getRepeatedForeignMessage(0).getC()); }
public void testFileDescriptor() throws Exception { FileDescriptor file = UnittestProto.getDescriptor(); assertEquals("google/protobuf/unittest.proto", file.getName()); assertEquals("protobuf_unittest", file.getPackage()); assertEquals("UnittestProto", file.getOptions().getJavaOuterClassname()); assertEquals("google/protobuf/unittest.proto", file.toProto().getName()); assertEquals(Arrays.asList(UnittestImport.getDescriptor()), file.getDependencies()); Descriptor messageType = TestAllTypes.getDescriptor(); assertEquals(messageType, file.getMessageTypes().get(0)); assertEquals(messageType, file.findMessageTypeByName("TestAllTypes")); assertNull(file.findMessageTypeByName("NoSuchType")); assertNull(file.findMessageTypeByName("protobuf_unittest.TestAllTypes")); for (int i = 0; i < file.getMessageTypes().size(); i++) { assertEquals(i, file.getMessageTypes().get(i).getIndex()); } EnumDescriptor enumType = ForeignEnum.getDescriptor(); assertEquals(enumType, file.getEnumTypes().get(0)); assertEquals(enumType, file.findEnumTypeByName("ForeignEnum")); assertNull(file.findEnumTypeByName("NoSuchType")); assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum")); assertEquals(Arrays.asList(ImportEnum.getDescriptor(), ImportEnumForMap.getDescriptor()), UnittestImport.getDescriptor().getEnumTypes()); for (int i = 0; i < file.getEnumTypes().size(); i++) { assertEquals(i, file.getEnumTypes().get(i).getIndex()); } ServiceDescriptor service = TestService.getDescriptor(); assertEquals(service, file.getServices().get(0)); assertEquals(service, file.findServiceByName("TestService")); assertNull(file.findServiceByName("NoSuchType")); assertNull(file.findServiceByName("protobuf_unittest.TestService")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getServices()); for (int i = 0; i < file.getServices().size(); i++) { assertEquals(i, file.getServices().get(i).getIndex()); } FieldDescriptor extension = UnittestProto.optionalInt32Extension.getDescriptor(); assertEquals(extension, file.getExtensions().get(0)); assertEquals(extension, file.findExtensionByName("optional_int32_extension")); assertNull(file.findExtensionByName("no_such_ext")); assertNull(file.findExtensionByName( "protobuf_unittest.optional_int32_extension")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getExtensions()); for (int i = 0; i < file.getExtensions().size(); i++) { assertEquals(i, file.getExtensions().get(i).getIndex()); } }