diff options
author | Adam Cozzette <acozzette@google.com> | 2016-11-17 16:48:38 -0800 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2016-11-17 16:59:59 -0800 |
commit | 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 (patch) | |
tree | 0276f81f8848a05d84cd7e287b43d665e30f04e3 /java/core/src/test | |
parent | e28286fa05d8327fd6c5aa70cfb3be558f0932b8 (diff) | |
download | protobuf-5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74.tar.gz protobuf-5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74.tar.bz2 protobuf-5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74.zip |
Integrated internal changes from Google
Diffstat (limited to 'java/core/src/test')
7 files changed, 211 insertions, 155 deletions
diff --git a/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java b/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java index efdfdfbe..813fe6bc 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java @@ -31,7 +31,6 @@ package com.google.protobuf; import static protobuf_unittest.UnittestProto.optionalInt32Extension; -import static protobuf_unittest.UnittestProto.optionalInt64Extension; import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; @@ -219,29 +218,6 @@ public class LazyFieldLiteTest extends TestCase { assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance())); } - public void testMergeMightLoseExtensions() throws Exception { - // Test that we don't know about the extensions when parsing. - TestAllExtensions message1 = - TestAllExtensions.newBuilder().setExtension(optionalInt32Extension, 1).build(); - TestAllExtensions message2 = - TestAllExtensions.newBuilder().setExtension(optionalInt64Extension, 2L).build(); - - LazyFieldLite field = LazyFieldLite.fromValue(message1); - field.merge(LazyFieldLite.fromValue(message2)); - - // We lose the extensions from message 2 because we have to serialize it and then parse it - // again, using the empty registry this time. - TestAllExtensions value = - (TestAllExtensions) field.getValue(TestAllExtensions.getDefaultInstance()); - assertTrue(value.hasExtension(optionalInt32Extension)); - assertEquals(Integer.valueOf(1), value.getExtension(optionalInt32Extension)); - assertFalse(value.hasExtension(optionalInt64Extension)); - - // The field is still there, it is just unknown. - assertTrue(value.getUnknownFields() - .hasField(optionalInt64Extension.getDescriptor().getNumber())); - } - // Help methods. diff --git a/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java b/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java index c8ca9b6a..968ca206 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java @@ -101,6 +101,19 @@ public class LazyMessageLiteTest extends TestCase { assertEquals(119, outer.getRepeatedInner(0).getNum()); assertEquals(122, outer.getRepeatedInner(1).getNum()); } + + public void testRepeatedMutability() throws Exception { + LazyMessageLite outer = LazyMessageLite.newBuilder() + .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119)) + .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122)) + .build(); + + outer = LazyMessageLite.parseFrom(outer.toByteArray()); + try { + outer.getRepeatedInnerList().set(1, null); + fail(); + } catch (UnsupportedOperationException expected) {} + } public void testAddAll() { ArrayList<LazyInnerMessageLite> inners = new ArrayList<LazyInnerMessageLite>(); diff --git a/java/core/src/test/java/com/google/protobuf/LiteTest.java b/java/core/src/test/java/com/google/protobuf/LiteTest.java index 254beba1..538432f7 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteTest.java @@ -45,6 +45,7 @@ import com.google.protobuf.UnittestLite.TestAllTypesLite.OneofFieldCase; import com.google.protobuf.UnittestLite.TestAllTypesLite.OptionalGroup; import com.google.protobuf.UnittestLite.TestAllTypesLite.RepeatedGroup; import com.google.protobuf.UnittestLite.TestAllTypesLiteOrBuilder; +import com.google.protobuf.UnittestLite.TestHugeFieldNumbersLite; import com.google.protobuf.UnittestLite.TestNestedExtensionLite; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; @@ -1424,6 +1425,12 @@ public class LiteTest extends TestCase { assertToStringEquals("", TestAllTypesLite.getDefaultInstance()); } + public void testToStringScalarFieldsSuffixedWithList() throws Exception { + assertToStringEquals("deceptively_named_list: 7", TestAllTypesLite.newBuilder() + .setDeceptivelyNamedList(7) + .build()); + } + public void testToStringPrimitives() throws Exception { TestAllTypesLite proto = TestAllTypesLite.newBuilder() .setOptionalInt32(1) @@ -2235,6 +2242,25 @@ public class LiteTest extends TestCase { assertFalse(other.equals(mine)); } + public void testHugeFieldNumbers() throws InvalidProtocolBufferException { + TestHugeFieldNumbersLite message = + TestHugeFieldNumbersLite.newBuilder() + .setOptionalInt32(1) + .addRepeatedInt32(2) + .setOptionalEnum(ForeignEnumLite.FOREIGN_LITE_FOO) + .setOptionalString("xyz") + .setOptionalMessage(ForeignMessageLite.newBuilder().setC(3).build()) + .build(); + + TestHugeFieldNumbersLite parsedMessage = + TestHugeFieldNumbersLite.parseFrom(message.toByteArray()); + assertEquals(1, parsedMessage.getOptionalInt32()); + assertEquals(2, parsedMessage.getRepeatedInt32(0)); + assertEquals(ForeignEnumLite.FOREIGN_LITE_FOO, parsedMessage.getOptionalEnum()); + assertEquals("xyz", parsedMessage.getOptionalString()); + assertEquals(3, parsedMessage.getOptionalMessage().getC()); + } + private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) { assertFalse(o1.equals(o2)); assertFalse(o1.hashCode() == o2.hashCode()); diff --git a/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java b/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java index 8b64fa27..0a14f584 100644 --- a/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java @@ -501,7 +501,6 @@ public final class MapForProto2LiteTest extends TestCase { assertEquals(54321, messageWithUnknownEnums.getInt32ToInt32Field().get(2).intValue()); } - public void testIterationOrder() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); setMapValues(builder); diff --git a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java index 76bd7bd9..cfe4c453 100644 --- a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java +++ b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java @@ -58,26 +58,26 @@ public class MapForProto2Test extends TestCase { builder.getMutableInt32ToInt32Field().put(1, 11); builder.getMutableInt32ToInt32Field().put(2, 22); builder.getMutableInt32ToInt32Field().put(3, 33); - + // builder.getMutableInt32ToStringField().put(1, "11"); builder.getMutableInt32ToStringField().put(2, "22"); builder.getMutableInt32ToStringField().put(3, "33"); - + // builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11")); builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22")); builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33")); - + // builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO); builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR); builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ); - + // builder.getMutableInt32ToMessageField().put( 1, MessageValue.newBuilder().setValue(11).build()); builder.getMutableInt32ToMessageField().put( 2, MessageValue.newBuilder().setValue(22).build()); builder.getMutableInt32ToMessageField().put( 3, MessageValue.newBuilder().setValue(33).build()); - + // builder.getMutableStringToInt32Field().put("1", 11); builder.getMutableStringToInt32Field().put("2", 22); builder.getMutableStringToInt32Field().put("3", 33); @@ -120,6 +120,7 @@ public class MapForProto2Test extends TestCase { setMapValuesUsingAccessors(usingAccessorsBuilder); TestMap usingAccessors = usingAccessorsBuilder.build(); assertMapValuesSet(usingAccessors); + assertEquals(usingAccessors, usingMutableMap); } @@ -169,25 +170,25 @@ public class MapForProto2Test extends TestCase { builder.getMutableInt32ToInt32Field().put(1, 111); builder.getMutableInt32ToInt32Field().remove(2); builder.getMutableInt32ToInt32Field().put(4, 44); - + // builder.getMutableInt32ToStringField().put(1, "111"); builder.getMutableInt32ToStringField().remove(2); builder.getMutableInt32ToStringField().put(4, "44"); - + // builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111")); builder.getMutableInt32ToBytesField().remove(2); builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44")); - + // builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR); builder.getMutableInt32ToEnumField().remove(2); builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX); - + // builder.getMutableInt32ToMessageField().put( 1, MessageValue.newBuilder().setValue(111).build()); builder.getMutableInt32ToMessageField().remove(2); builder.getMutableInt32ToMessageField().put( 4, MessageValue.newBuilder().setValue(44).build()); - + // builder.getMutableStringToInt32Field().put("1", 111); builder.getMutableStringToInt32Field().remove("2"); builder.getMutableStringToInt32Field().put("4", 44); @@ -230,8 +231,9 @@ public class MapForProto2Test extends TestCase { setMapValuesUsingAccessors(usingAccessorsBuilder); TestMap usingAccessors = usingAccessorsBuilder.build(); assertMapValuesSet(usingAccessors); - assertEquals(usingAccessors, usingMutableMap); + assertEquals(usingAccessors, usingMutableMap); + // usingMutableMapBuilder = usingMutableMap.toBuilder(); updateMapValuesUsingMutableMap(usingMutableMapBuilder); usingMutableMap = usingMutableMapBuilder.build(); @@ -335,7 +337,7 @@ public class MapForProto2Test extends TestCase { assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); builder.getMutableInt32ToInt32Field().put(2, 3); assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); - + // Map<Integer, TestMap.EnumValue> enumMap = builder.getMutableInt32ToEnumField(); enumMap.put(1, TestMap.EnumValue.BAR); assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnumField()); @@ -350,7 +352,7 @@ public class MapForProto2Test extends TestCase { assertEquals( newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), builder.getInt32ToEnumField()); - + // Map<Integer, String> stringMap = builder.getMutableInt32ToStringField(); stringMap.put(1, "1"); assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); @@ -365,7 +367,7 @@ public class MapForProto2Test extends TestCase { assertEquals( newMap(1, "1", 2, "2"), builder.getInt32ToStringField()); - + // Map<Integer, TestMap.MessageValue> messageMap = builder.getMutableInt32ToMessageField(); messageMap.put(1, TestMap.MessageValue.getDefaultInstance()); assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), @@ -384,7 +386,7 @@ public class MapForProto2Test extends TestCase { 2, TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField()); } - + // public void testMutableMapLifecycle_collections() { TestMap.Builder builder = TestMap.newBuilder(); Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); @@ -431,18 +433,19 @@ public class MapForProto2Test extends TestCase { assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); } + public void testGettersAndSetters() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); TestMap message = builder.build(); assertMapValuesCleared(message); builder = message.toBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); message = builder.build(); assertMapValuesSet(message); builder = message.toBuilder(); - updateMapValuesUsingMutableMap(builder); + updateMapValuesUsingAccessors(builder); message = builder.build(); assertMapValuesUpdated(message); @@ -455,7 +458,7 @@ public class MapForProto2Test extends TestCase { public void testPutAll() throws Exception { TestMap.Builder sourceBuilder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(sourceBuilder); + setMapValuesUsingAccessors(sourceBuilder); TestMap source = sourceBuilder.build(); assertMapValuesSet(source); @@ -507,14 +510,14 @@ public class MapForProto2Test extends TestCase { public void testSerializeAndParse() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); assertEquals(message.getSerializedSize(), message.toByteString().size()); message = TestMap.parser().parseFrom(message.toByteString()); assertMapValuesSet(message); builder = message.toBuilder(); - updateMapValuesUsingMutableMap(builder); + updateMapValuesUsingAccessors(builder); message = builder.build(); assertEquals(message.getSerializedSize(), message.toByteString().size()); message = TestMap.parser().parseFrom(message.toByteString()); @@ -579,7 +582,7 @@ public class MapForProto2Test extends TestCase { public void testMergeFrom() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); TestMap.Builder other = TestMap.newBuilder(); @@ -594,22 +597,22 @@ public class MapForProto2Test extends TestCase { // We can't control the order of elements in a HashMap. The best we can do // here is to add elements in different order. TestMap.Builder b1 = TestMap.newBuilder(); - b1.getMutableInt32ToInt32Field().put(1, 2); - b1.getMutableInt32ToInt32Field().put(3, 4); - b1.getMutableInt32ToInt32Field().put(5, 6); + b1.putInt32ToInt32Field(1, 2); + b1.putInt32ToInt32Field(3, 4); + b1.putInt32ToInt32Field(5, 6); TestMap m1 = b1.build(); TestMap.Builder b2 = TestMap.newBuilder(); - b2.getMutableInt32ToInt32Field().put(5, 6); - b2.getMutableInt32ToInt32Field().put(1, 2); - b2.getMutableInt32ToInt32Field().put(3, 4); + b2.putInt32ToInt32Field(5, 6); + b2.putInt32ToInt32Field(1, 2); + b2.putInt32ToInt32Field(3, 4); TestMap m2 = b2.build(); assertEquals(m1, m2); assertEquals(m1.hashCode(), m2.hashCode()); // Make sure we did compare map fields. - b2.getMutableInt32ToInt32Field().put(1, 0); + b2.putInt32ToInt32Field(1, 0); m2 = b2.build(); assertFalse(m1.equals(m2)); // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed @@ -684,13 +687,11 @@ public class MapForProto2Test extends TestCase { public void testReflectionApi() throws Exception { // In reflection API, map fields are just repeated message fields. - TestMap.Builder builder = TestMap.newBuilder(); - builder.getMutableInt32ToInt32Field().put(1, 2); - builder.getMutableInt32ToInt32Field().put(3, 4); - builder.getMutableInt32ToMessageField().put( - 11, MessageValue.newBuilder().setValue(22).build()); - builder.getMutableInt32ToMessageField().put( - 33, MessageValue.newBuilder().setValue(44).build()); + TestMap.Builder builder = TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build()) + .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build()); TestMap message = builder.build(); // Test getField(), getRepeatedFieldCount(), getRepeatedField(). @@ -760,7 +761,7 @@ public class MapForProto2Test extends TestCase { public void testTextFormat() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); String textData = TextFormat.printToString(message); @@ -774,7 +775,7 @@ public class MapForProto2Test extends TestCase { public void testDynamicMessage() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); Message dynamicDefaultInstance = @@ -819,10 +820,9 @@ public class MapForProto2Test extends TestCase { } public void testUnknownEnumValues() throws Exception { - TestUnknownEnumValue.Builder builder = - TestUnknownEnumValue.newBuilder(); - builder.getMutableInt32ToInt32Field().put(1, 1); - builder.getMutableInt32ToInt32Field().put(2, 54321); + TestUnknownEnumValue.Builder builder = TestUnknownEnumValue.newBuilder() + .putInt32ToInt32Field(1, 1) + .putInt32ToInt32Field(2, 54321); ByteString data = builder.build().toByteString(); TestMap message = TestMap.parseFrom(data); @@ -841,26 +841,21 @@ public class MapForProto2Test extends TestCase { assertEquals(54321, messageWithUnknownEnums.getInt32ToInt32Field().get(2).intValue()); } - public void testRequiredMessage() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - builder.getMutableRequiredMessageMap().put(0, - MessageWithRequiredFields.newBuilder().buildPartial()); + builder.putRequiredMessageMap(0, MessageWithRequiredFields.newBuilder().buildPartial()); TestMap message = builder.buildPartial(); assertFalse(message.isInitialized()); - builder.getMutableRequiredMessageMap().put(0, - MessageWithRequiredFields.newBuilder().setValue(1).build()); + builder.putRequiredMessageMap(0, MessageWithRequiredFields.newBuilder().setValue(1).build()); message = builder.build(); assertTrue(message.isInitialized()); } public void testRecursiveMap() throws Exception { TestRecursiveMap.Builder builder = TestRecursiveMap.newBuilder(); - builder.getMutableRecursiveMapField().put( - 1, TestRecursiveMap.newBuilder().setValue(2).build()); - builder.getMutableRecursiveMapField().put( - 3, TestRecursiveMap.newBuilder().setValue(4).build()); + builder.putRecursiveMapField(1, TestRecursiveMap.newBuilder().setValue(2).build()); + builder.putRecursiveMapField(3, TestRecursiveMap.newBuilder().setValue(4).build()); ByteString data = builder.build().toByteString(); TestRecursiveMap message = TestRecursiveMap.parseFrom(data); @@ -870,7 +865,7 @@ public class MapForProto2Test extends TestCase { public void testIterationOrder() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); assertEquals(Arrays.asList("1", "2", "3"), @@ -879,7 +874,7 @@ public class MapForProto2Test extends TestCase { public void testContains() { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); assertMapContainsSetValues(builder); assertMapContainsSetValues(builder.build()); } @@ -920,7 +915,7 @@ public class MapForProto2Test extends TestCase { TestMap.Builder builder = TestMap.newBuilder(); assertMapCounts(0, builder); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); assertMapCounts(3, builder); TestMap message = builder.build(); @@ -1091,7 +1086,7 @@ public class MapForProto2Test extends TestCase { public void testRemove() { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); for (int times = 0; times < 2; times++) { builder.removeInt32ToInt32Field(1); @@ -1137,7 +1132,7 @@ public class MapForProto2Test extends TestCase { map_test.Message1.Builder builder = map_test.Message1.newBuilder(); - builder.getMutableMapField().put("key", true); + builder.putMapField("key", true); map_test.Message1 message = builder.build(); Message mapEntry = (Message) message.getRepeatedField( message.getDescriptorForType().findFieldByName("map_field"), 0); diff --git a/java/core/src/test/java/com/google/protobuf/MapTest.java b/java/core/src/test/java/com/google/protobuf/MapTest.java index 035a8335..81e951cc 100644 --- a/java/core/src/test/java/com/google/protobuf/MapTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapTest.java @@ -60,26 +60,26 @@ public class MapTest extends TestCase { builder.getMutableInt32ToInt32Field().put(1, 11); builder.getMutableInt32ToInt32Field().put(2, 22); builder.getMutableInt32ToInt32Field().put(3, 33); - + // builder.getMutableInt32ToStringField().put(1, "11"); builder.getMutableInt32ToStringField().put(2, "22"); builder.getMutableInt32ToStringField().put(3, "33"); - + // builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11")); builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22")); builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33")); - + // builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO); builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR); builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ); - + // builder.getMutableInt32ToMessageField().put( 1, MessageValue.newBuilder().setValue(11).build()); builder.getMutableInt32ToMessageField().put( 2, MessageValue.newBuilder().setValue(22).build()); builder.getMutableInt32ToMessageField().put( 3, MessageValue.newBuilder().setValue(33).build()); - + // builder.getMutableStringToInt32Field().put("1", 11); builder.getMutableStringToInt32Field().put("2", 22); builder.getMutableStringToInt32Field().put("3", 33); @@ -122,6 +122,7 @@ public class MapTest extends TestCase { setMapValuesUsingAccessors(usingAccessorsBuilder); TestMap usingAccessors = usingAccessorsBuilder.build(); assertMapValuesSet(usingAccessors); + assertEquals(usingAccessors, usingMutableMap); } @@ -171,25 +172,25 @@ public class MapTest extends TestCase { builder.getMutableInt32ToInt32Field().put(1, 111); builder.getMutableInt32ToInt32Field().remove(2); builder.getMutableInt32ToInt32Field().put(4, 44); - + // builder.getMutableInt32ToStringField().put(1, "111"); builder.getMutableInt32ToStringField().remove(2); builder.getMutableInt32ToStringField().put(4, "44"); - + // builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111")); builder.getMutableInt32ToBytesField().remove(2); builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44")); - + // builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR); builder.getMutableInt32ToEnumField().remove(2); builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX); - + // builder.getMutableInt32ToMessageField().put( 1, MessageValue.newBuilder().setValue(111).build()); builder.getMutableInt32ToMessageField().remove(2); builder.getMutableInt32ToMessageField().put( 4, MessageValue.newBuilder().setValue(44).build()); - + // builder.getMutableStringToInt32Field().put("1", 111); builder.getMutableStringToInt32Field().remove("2"); builder.getMutableStringToInt32Field().put("4", 44); @@ -232,8 +233,9 @@ public class MapTest extends TestCase { setMapValuesUsingAccessors(usingAccessorsBuilder); TestMap usingAccessors = usingAccessorsBuilder.build(); assertMapValuesSet(usingAccessors); - assertEquals(usingAccessors, usingMutableMap); + assertEquals(usingAccessors, usingMutableMap); + // usingMutableMapBuilder = usingMutableMap.toBuilder(); updateMapValuesUsingMutableMap(usingMutableMapBuilder); usingMutableMap = usingMutableMapBuilder.build(); @@ -337,7 +339,7 @@ public class MapTest extends TestCase { assertEquals(newMap(1, 2), builder.getInt32ToInt32Field()); builder.getMutableInt32ToInt32Field().put(2, 3); assertEquals(newMap(1, 2, 2, 3), builder.getInt32ToInt32Field()); - + // Map<Integer, TestMap.EnumValue> enumMap = builder.getMutableInt32ToEnumField(); enumMap.put(1, TestMap.EnumValue.BAR); assertEquals(newMap(1, TestMap.EnumValue.BAR), builder.build().getInt32ToEnumField()); @@ -352,7 +354,7 @@ public class MapTest extends TestCase { assertEquals( newMap(1, TestMap.EnumValue.BAR, 2, TestMap.EnumValue.FOO), builder.getInt32ToEnumField()); - + // Map<Integer, String> stringMap = builder.getMutableInt32ToStringField(); stringMap.put(1, "1"); assertEquals(newMap(1, "1"), builder.build().getInt32ToStringField()); @@ -363,11 +365,11 @@ public class MapTest extends TestCase { // expected } assertEquals(newMap(1, "1"), builder.getInt32ToStringField()); - builder.getMutableInt32ToStringField().put(2, "2"); + builder.putInt32ToStringField(2, "2"); assertEquals( newMap(1, "1", 2, "2"), builder.getInt32ToStringField()); - + // Map<Integer, TestMap.MessageValue> messageMap = builder.getMutableInt32ToMessageField(); messageMap.put(1, TestMap.MessageValue.getDefaultInstance()); assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), @@ -380,13 +382,13 @@ public class MapTest extends TestCase { } assertEquals(newMap(1, TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField()); - builder.getMutableInt32ToMessageField().put(2, TestMap.MessageValue.getDefaultInstance()); + builder.putInt32ToMessageField(2, TestMap.MessageValue.getDefaultInstance()); assertEquals( newMap(1, TestMap.MessageValue.getDefaultInstance(), 2, TestMap.MessageValue.getDefaultInstance()), builder.getInt32ToMessageField()); } - + // public void testMutableMapLifecycle_collections() { TestMap.Builder builder = TestMap.newBuilder(); Map<Integer, Integer> intMap = builder.getMutableInt32ToInt32Field(); @@ -433,18 +435,19 @@ public class MapTest extends TestCase { assertEquals(newMap(1, 2), builder.build().getInt32ToInt32Field()); } + public void testGettersAndSetters() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); TestMap message = builder.build(); assertMapValuesCleared(message); builder = message.toBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); message = builder.build(); assertMapValuesSet(message); builder = message.toBuilder(); - updateMapValuesUsingMutableMap(builder); + updateMapValuesUsingAccessors(builder); message = builder.build(); assertMapValuesUpdated(message); @@ -457,7 +460,7 @@ public class MapTest extends TestCase { public void testPutAll() throws Exception { TestMap.Builder sourceBuilder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(sourceBuilder); + setMapValuesUsingAccessors(sourceBuilder); TestMap source = sourceBuilder.build(); assertMapValuesSet(source); @@ -467,15 +470,16 @@ public class MapTest extends TestCase { } public void testPutAllForUnknownEnumValues() throws Exception { - TestMap.Builder sourceBuilder = TestMap.newBuilder(); - sourceBuilder.getMutableInt32ToEnumFieldValue().put(0, 0); - sourceBuilder.getMutableInt32ToEnumFieldValue().put(1, 1); - sourceBuilder.getMutableInt32ToEnumFieldValue().put(2, 1000); // unknown value. - TestMap source = sourceBuilder.build(); + TestMap source = TestMap.newBuilder() + .putAllInt32ToEnumFieldValue(newMap( + 0, 0, + 1, 1, + 2, 1000)) // unknown value. + .build(); - TestMap.Builder destinationBuilder = TestMap.newBuilder(); - destinationBuilder.putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()); - TestMap destination = destinationBuilder.build(); + TestMap destination = TestMap.newBuilder() + .putAllInt32ToEnumFieldValue(source.getInt32ToEnumFieldValue()) + .build(); assertEquals(0, destination.getInt32ToEnumFieldValue().get(0).intValue()); assertEquals(1, destination.getInt32ToEnumFieldValue().get(1).intValue()); @@ -542,14 +546,14 @@ public class MapTest extends TestCase { public void testSerializeAndParse() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); assertEquals(message.getSerializedSize(), message.toByteString().size()); message = TestMap.parser().parseFrom(message.toByteString()); assertMapValuesSet(message); builder = message.toBuilder(); - updateMapValuesUsingMutableMap(builder); + updateMapValuesUsingAccessors(builder); message = builder.build(); assertEquals(message.getSerializedSize(), message.toByteString().size()); message = TestMap.parser().parseFrom(message.toByteString()); @@ -614,7 +618,7 @@ public class MapTest extends TestCase { public void testMergeFrom() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); TestMap.Builder other = TestMap.newBuilder(); @@ -628,23 +632,23 @@ public class MapTest extends TestCase { // We can't control the order of elements in a HashMap. The best we can do // here is to add elements in different order. - TestMap.Builder b1 = TestMap.newBuilder(); - b1.getMutableInt32ToInt32Field().put(1, 2); - b1.getMutableInt32ToInt32Field().put(3, 4); - b1.getMutableInt32ToInt32Field().put(5, 6); + TestMap.Builder b1 = TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToInt32Field(5, 6); TestMap m1 = b1.build(); - TestMap.Builder b2 = TestMap.newBuilder(); - b2.getMutableInt32ToInt32Field().put(5, 6); - b2.getMutableInt32ToInt32Field().put(1, 2); - b2.getMutableInt32ToInt32Field().put(3, 4); + TestMap.Builder b2 = TestMap.newBuilder() + .putInt32ToInt32Field(5, 6) + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4); TestMap m2 = b2.build(); assertEquals(m1, m2); assertEquals(m1.hashCode(), m2.hashCode()); // Make sure we did compare map fields. - b2.getMutableInt32ToInt32Field().put(1, 0); + b2.putInt32ToInt32Field(1, 0); m2 = b2.build(); assertFalse(m1.equals(m2)); // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed @@ -652,7 +656,7 @@ public class MapTest extends TestCase { // Regression test for b/18549190: if a map is a subset of the other map, // equals() should return false. - b2.getMutableInt32ToInt32Field().remove(1); + b2.removeInt32ToInt32Field(1); m2 = b2.build(); assertFalse(m1.equals(m2)); assertFalse(m2.equals(m1)); @@ -661,20 +665,19 @@ public class MapTest extends TestCase { public void testNestedBuilderOnChangeEventPropagation() { TestOnChangeEventPropagation.Builder parent = TestOnChangeEventPropagation.newBuilder(); - parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 2); + parent.getOptionalMessageBuilder().putInt32ToInt32Field(1, 2); TestOnChangeEventPropagation message = parent.build(); assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); // Make a change using nested builder. - parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 3); + parent.getOptionalMessageBuilder().putInt32ToInt32Field(1, 3); // Should be able to observe the change. message = parent.build(); assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue()); // Make another change using mergeFrom() - TestMap.Builder other = TestMap.newBuilder(); - other.getMutableInt32ToInt32Field().put(1, 4); + TestMap.Builder other = TestMap.newBuilder().putInt32ToInt32Field(1, 4); parent.getOptionalMessageBuilder().mergeFrom(other.build()); // Should be able to observe the change. @@ -697,8 +700,7 @@ public class MapTest extends TestCase { TestMap.Builder testMapBuilder = parentBuilder.getOptionalMessageBuilder(); // Create a map entry message. - TestMap.Builder entryBuilder = TestMap.newBuilder(); - entryBuilder.getMutableInt32ToInt32Field().put(1, 1); + TestMap.Builder entryBuilder = TestMap.newBuilder().putInt32ToInt32Field(1, 1); // Put the entry into the nested builder. testMapBuilder.addRepeatedField( @@ -709,7 +711,7 @@ public class MapTest extends TestCase { assertEquals(1, message.getOptionalMessage().getInt32ToInt32Field().size()); // Change the entry value. - entryBuilder.getMutableInt32ToInt32Field().put(1, 4); + entryBuilder.putInt32ToInt32Field(1, 4); testMapBuilder = parentBuilder.getOptionalMessageBuilder(); testMapBuilder.setRepeatedField( intMapField, 0, entryBuilder.getRepeatedField(intMapField, 0)); @@ -796,13 +798,11 @@ public class MapTest extends TestCase { public void testReflectionApi() throws Exception { // In reflection API, map fields are just repeated message fields. - TestMap.Builder builder = TestMap.newBuilder(); - builder.getMutableInt32ToInt32Field().put(1, 2); - builder.getMutableInt32ToInt32Field().put(3, 4); - builder.getMutableInt32ToMessageField().put( - 11, MessageValue.newBuilder().setValue(22).build()); - builder.getMutableInt32ToMessageField().put( - 33, MessageValue.newBuilder().setValue(44).build()); + TestMap.Builder builder = TestMap.newBuilder() + .putInt32ToInt32Field(1, 2) + .putInt32ToInt32Field(3, 4) + .putInt32ToMessageField(11, MessageValue.newBuilder().setValue(22).build()) + .putInt32ToMessageField(33, MessageValue.newBuilder().setValue(44).build()); TestMap message = builder.build(); // Test getField(), getRepeatedFieldCount(), getRepeatedField(). @@ -872,7 +872,7 @@ public class MapTest extends TestCase { public void testTextFormat() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); String textData = TextFormat.printToString(message); @@ -886,7 +886,7 @@ public class MapTest extends TestCase { public void testDynamicMessage() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); Message dynamicDefaultInstance = @@ -931,10 +931,11 @@ public class MapTest extends TestCase { } public void testUnknownEnumValues() throws Exception { - TestMap.Builder builder = TestMap.newBuilder(); - builder.getMutableInt32ToEnumFieldValue().put(0, 0); - builder.getMutableInt32ToEnumFieldValue().put(1, 1); - builder.getMutableInt32ToEnumFieldValue().put(2, 1000); // unknown value. + TestMap.Builder builder = TestMap.newBuilder() + .putAllInt32ToEnumFieldValue(newMap( + 0, 0, + 1, 1, + 2, 1000)); // unknown value. TestMap message = builder.build(); assertEquals(TestMap.EnumValue.FOO, @@ -957,7 +958,7 @@ public class MapTest extends TestCase { assertEquals(1000, builder.getInt32ToEnumFieldValue().get(2).intValue()); // hashCode()/equals() should take unknown enum values into account. - builder.getMutableInt32ToEnumFieldValue().put(2, 1001); + builder.putAllInt32ToEnumFieldValue(newMap(2, 1001)); TestMap message2 = builder.build(); assertFalse(message.hashCode() == message2.hashCode()); assertFalse(message.equals(message2)); @@ -971,15 +972,13 @@ public class MapTest extends TestCase { EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor(); FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field"); - Map<Integer, Integer> data = new HashMap<Integer, Integer>(); - data.put(0, 0); - data.put(1, 1); - data.put(2, 1000); // unknown value. + Map<Integer, Integer> data = newMap( + 0, 0, + 1, 1, + 2, 1000); // unknown value - TestMap.Builder builder = TestMap.newBuilder(); - for (Map.Entry<Integer, Integer> entry : data.entrySet()) { - builder.getMutableInt32ToEnumFieldValue().put(entry.getKey(), entry.getValue()); - } + TestMap.Builder builder = TestMap.newBuilder() + .putAllInt32ToEnumFieldValue(data); // Try to read unknown enum values using reflection API. for (int i = 0; i < builder.getRepeatedFieldCount(field); i++) { @@ -1003,7 +1002,7 @@ public class MapTest extends TestCase { public void testIterationOrder() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); assertEquals(Arrays.asList("1", "2", "3"), @@ -1012,7 +1011,7 @@ public class MapTest extends TestCase { public void testGetMap() { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); TestMap message = builder.build(); assertEquals( message.getStringToInt32Field(), @@ -1033,7 +1032,7 @@ public class MapTest extends TestCase { public void testContains() { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); assertMapContainsSetValues(builder); assertMapContainsSetValues(builder.build()); } @@ -1074,7 +1073,7 @@ public class MapTest extends TestCase { TestMap.Builder builder = TestMap.newBuilder(); assertMapCounts(0, builder); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); assertMapCounts(3, builder); TestMap message = builder.build(); @@ -1269,7 +1268,7 @@ public class MapTest extends TestCase { public void testRemove() { TestMap.Builder builder = TestMap.newBuilder(); - setMapValuesUsingMutableMap(builder); + setMapValuesUsingAccessors(builder); assertEquals(11, builder.getInt32ToInt32FieldOrThrow(1)); for (int times = 0; times < 2; times++) { builder.removeInt32ToInt32Field(1); @@ -1485,4 +1484,12 @@ public class MapTest extends TestCase { map.put(key2, value2); return map; } + + private static <K, V> Map<K, V> newMap(K key1, V value1, K key2, V value2, K key3, V value3) { + Map<K, V> map = new HashMap<K, V>(); + map.put(key1, value1); + map.put(key2, value2); + map.put(key3, value3); + return map; + } } diff --git a/java/core/src/test/java/com/google/protobuf/ParserTest.java b/java/core/src/test/java/com/google/protobuf/ParserTest.java index 7086912b..8c2e4c26 100644 --- a/java/core/src/test/java/com/google/protobuf/ParserTest.java +++ b/java/core/src/test/java/com/google/protobuf/ParserTest.java @@ -46,6 +46,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InterruptedIOException; import junit.framework.TestCase; /** @@ -372,4 +373,43 @@ public class ParserTest extends TestCase { assertEquals(3, parsingMerge.getExtensionCount( TestParsingMergeLite.repeatedExt)); } + + public void testParseDelimitedFrom_firstByteInterrupted_preservesCause() { + try { + TestUtil.getAllSet().parseDelimitedFrom( + new InputStream() { + @Override + public int read() throws IOException { + throw new InterruptedIOException(); + } + }); + fail("Expected InterruptedIOException"); + } catch (Exception e) { + assertEquals(InterruptedIOException.class, e.getClass()); + } + } + + public void testParseDelimitedFrom_secondByteInterrupted_preservesCause() { + try { + TestUtil.getAllSet().parseDelimitedFrom( + new InputStream() { + private int i; + + @Override + public int read() throws IOException { + switch (i++) { + case 0: + return 1; + case 1: + throw new InterruptedIOException(); + default: + throw new AssertionError(); + } + } + }); + fail("Expected InterruptedIOException"); + } catch (Exception e) { + assertEquals(InterruptedIOException.class, e.getClass()); + } + } } |