diff options
author | Adam Cozzette <acozzette@google.com> | 2016-06-29 15:23:27 -0700 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2016-06-29 15:38:03 -0700 |
commit | d64a2d9941c36a7bc2a7959ea10ab8363192ac14 (patch) | |
tree | 52330d146ad63d3d70f3baade00d5d1fea8f5e0c /java/util/src/test | |
parent | c18aa7795a2e02ef700ff8b039d94ecdcc33432f (diff) | |
download | protobuf-d64a2d9941c36a7bc2a7959ea10ab8363192ac14.tar.gz protobuf-d64a2d9941c36a7bc2a7959ea10ab8363192ac14.tar.bz2 protobuf-d64a2d9941c36a7bc2a7959ea10ab8363192ac14.zip |
Integrated internal changes from Google
This includes all internal changes from around May 20 to now.
Diffstat (limited to 'java/util/src/test')
4 files changed, 472 insertions, 452 deletions
diff --git a/java/util/src/test/java/com/google/protobuf/util/FieldMaskUtilTest.java b/java/util/src/test/java/com/google/protobuf/util/FieldMaskUtilTest.java index 194f7b9c..1a998570 100644 --- a/java/util/src/test/java/com/google/protobuf/util/FieldMaskUtilTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/FieldMaskUtilTest.java @@ -41,52 +41,55 @@ public class FieldMaskUtilTest extends TestCase { public void testIsValid() throws Exception { assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload")); assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "nonexist")); - assertTrue(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.optional_int32")); - assertTrue(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.repeated_int32")); - assertTrue(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.optional_nested_message")); - assertTrue(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.repeated_nested_message")); - assertFalse(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.nonexist")); - - assertTrue(FieldMaskUtil.isValid( - NestedTestAllTypes.class, FieldMaskUtil.fromString("payload"))); - assertFalse(FieldMaskUtil.isValid( - NestedTestAllTypes.class, FieldMaskUtil.fromString("nonexist"))); - assertFalse(FieldMaskUtil.isValid( - NestedTestAllTypes.class, FieldMaskUtil.fromString("payload,nonexist"))); - + assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional_int32")); + assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated_int32")); + assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional_nested_message")); + assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated_nested_message")); + assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.nonexist")); + + assertTrue( + FieldMaskUtil.isValid(NestedTestAllTypes.class, FieldMaskUtil.fromString("payload"))); + assertFalse( + FieldMaskUtil.isValid(NestedTestAllTypes.class, FieldMaskUtil.fromString("nonexist"))); + assertFalse( + FieldMaskUtil.isValid( + NestedTestAllTypes.class, FieldMaskUtil.fromString("payload,nonexist"))); + assertTrue(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "payload")); assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.getDescriptor(), "nonexist")); - - assertTrue(FieldMaskUtil.isValid( - NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("payload"))); - assertFalse(FieldMaskUtil.isValid( - NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("nonexist"))); - - assertTrue(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.optional_nested_message.bb")); + + assertTrue( + FieldMaskUtil.isValid( + NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("payload"))); + assertFalse( + FieldMaskUtil.isValid( + NestedTestAllTypes.getDescriptor(), FieldMaskUtil.fromString("nonexist"))); + + assertTrue( + FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional_nested_message.bb")); // Repeated fields cannot have sub-paths. - assertFalse(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.repeated_nested_message.bb")); + assertFalse( + FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.repeated_nested_message.bb")); // Non-message fields cannot have sub-paths. - assertFalse(FieldMaskUtil.isValid( - NestedTestAllTypes.class, "payload.optional_int32.bb")); + assertFalse(FieldMaskUtil.isValid(NestedTestAllTypes.class, "payload.optional_int32.bb")); } - + public void testToString() throws Exception { assertEquals("", FieldMaskUtil.toString(FieldMask.getDefaultInstance())); FieldMask mask = FieldMask.newBuilder().addPaths("foo").build(); assertEquals("foo", FieldMaskUtil.toString(mask)); mask = FieldMask.newBuilder().addPaths("foo").addPaths("bar").build(); assertEquals("foo,bar", FieldMaskUtil.toString(mask)); - + // Empty field paths are ignored. - mask = FieldMask.newBuilder().addPaths("").addPaths("foo").addPaths(""). - addPaths("bar").addPaths("").build(); + mask = + FieldMask.newBuilder() + .addPaths("") + .addPaths("foo") + .addPaths("") + .addPaths("bar") + .addPaths("") + .build(); assertEquals("foo,bar", FieldMaskUtil.toString(mask)); } @@ -111,8 +114,7 @@ public class FieldMaskUtilTest extends TestCase { mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, ",payload"); try { - mask = FieldMaskUtil.fromString( - NestedTestAllTypes.class, "payload,nonexist"); + mask = FieldMaskUtil.fromString(NestedTestAllTypes.class, "payload,nonexist"); fail("Exception is expected."); } catch (IllegalArgumentException e) { // Expected. @@ -143,7 +145,33 @@ public class FieldMaskUtilTest extends TestCase { } catch (IllegalArgumentException expected) { } } - + + public void testToJsonString() throws Exception { + FieldMask mask = FieldMask.getDefaultInstance(); + assertEquals("", FieldMaskUtil.toJsonString(mask)); + mask = FieldMask.newBuilder().addPaths("foo").build(); + assertEquals("foo", FieldMaskUtil.toJsonString(mask)); + mask = FieldMask.newBuilder().addPaths("foo.bar_baz").addPaths("").build(); + assertEquals("foo.barBaz", FieldMaskUtil.toJsonString(mask)); + mask = FieldMask.newBuilder().addPaths("foo").addPaths("bar_baz").build(); + assertEquals("foo,barBaz", FieldMaskUtil.toJsonString(mask)); + } + + public void testFromJsonString() throws Exception { + FieldMask mask = FieldMaskUtil.fromJsonString(""); + assertEquals(0, mask.getPathsCount()); + mask = FieldMaskUtil.fromJsonString("foo"); + assertEquals(1, mask.getPathsCount()); + assertEquals("foo", mask.getPaths(0)); + mask = FieldMaskUtil.fromJsonString("foo.barBaz"); + assertEquals(1, mask.getPathsCount()); + assertEquals("foo.bar_baz", mask.getPaths(0)); + mask = FieldMaskUtil.fromJsonString("foo,barBaz"); + assertEquals(2, mask.getPathsCount()); + assertEquals("foo", mask.getPaths(0)); + assertEquals("bar_baz", mask.getPaths(1)); + } + public void testUnion() throws Exception { // Only test a simple case here and expect // {@link FieldMaskTreeTest#testAddFieldPath} to cover all scenarios. @@ -161,7 +189,7 @@ public class FieldMaskUtilTest extends TestCase { FieldMask result = FieldMaskUtil.union(mask1, mask2, mask3, mask4); assertEquals("bar,foo", FieldMaskUtil.toString(result)); } - + public void testIntersection() throws Exception { // Only test a simple case here and expect // {@link FieldMaskTreeTest#testIntersectFieldPath} to cover all scenarios. @@ -170,13 +198,14 @@ public class FieldMaskUtilTest extends TestCase { FieldMask result = FieldMaskUtil.intersection(mask1, mask2); assertEquals("bar.baz,bar.quz,foo.bar", FieldMaskUtil.toString(result)); } - + public void testMerge() throws Exception { // Only test a simple case here and expect // {@link FieldMaskTreeTest#testMerge} to cover all scenarios. - NestedTestAllTypes source = NestedTestAllTypes.newBuilder() - .setPayload(TestAllTypes.newBuilder().setOptionalInt32(1234)) - .build(); + NestedTestAllTypes source = + NestedTestAllTypes.newBuilder() + .setPayload(TestAllTypes.newBuilder().setOptionalInt32(1234)) + .build(); NestedTestAllTypes.Builder builder = NestedTestAllTypes.newBuilder(); FieldMaskUtil.merge(FieldMaskUtil.fromString("payload"), source, builder); assertEquals(1234, builder.getPayload().getOptionalInt32()); diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java index d95b626c..4d9a417d 100644 --- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java @@ -41,6 +41,7 @@ import com.google.protobuf.Int64Value; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.ListValue; import com.google.protobuf.Message; +import com.google.protobuf.NullValue; import com.google.protobuf.StringValue; import com.google.protobuf.Struct; import com.google.protobuf.UInt32Value; @@ -82,7 +83,7 @@ public class JsonFormatTest extends TestCase { builder.setOptionalDouble(1.25); builder.setOptionalBool(true); builder.setOptionalString("Hello world!"); - builder.setOptionalBytes(ByteString.copyFrom(new byte[]{0, 1, 2})); + builder.setOptionalBytes(ByteString.copyFrom(new byte[] {0, 1, 2})); builder.setOptionalNestedEnum(NestedEnum.BAR); builder.getOptionalNestedMessageBuilder().setValue(100); @@ -100,7 +101,7 @@ public class JsonFormatTest extends TestCase { builder.addRepeatedDouble(1.25); builder.addRepeatedBool(true); builder.addRepeatedString("Hello world!"); - builder.addRepeatedBytes(ByteString.copyFrom(new byte[]{0, 1, 2})); + builder.addRepeatedBytes(ByteString.copyFrom(new byte[] {0, 1, 2})); builder.addRepeatedNestedEnum(NestedEnum.BAR); builder.addRepeatedNestedMessageBuilder().setValue(100); @@ -118,7 +119,7 @@ public class JsonFormatTest extends TestCase { builder.addRepeatedDouble(11.25); builder.addRepeatedBool(true); builder.addRepeatedString("ello world!"); - builder.addRepeatedBytes(ByteString.copyFrom(new byte[]{1, 2})); + builder.addRepeatedBytes(ByteString.copyFrom(new byte[] {1, 2})); builder.addRepeatedNestedEnum(NestedEnum.BAZ); builder.addRepeatedNestedMessageBuilder().setValue(200); } @@ -198,20 +199,22 @@ public class JsonFormatTest extends TestCase { } public void testUnknownEnumValues() throws Exception { - TestAllTypes message = TestAllTypes.newBuilder() - .setOptionalNestedEnumValue(12345) - .addRepeatedNestedEnumValue(12345) - .addRepeatedNestedEnumValue(0) - .build(); + TestAllTypes message = + TestAllTypes.newBuilder() + .setOptionalNestedEnumValue(12345) + .addRepeatedNestedEnumValue(12345) + .addRepeatedNestedEnumValue(0) + .build(); assertEquals( "{\n" - + " \"optionalNestedEnum\": 12345,\n" - + " \"repeatedNestedEnum\": [12345, \"FOO\"]\n" - + "}", toJsonString(message)); + + " \"optionalNestedEnum\": 12345,\n" + + " \"repeatedNestedEnum\": [12345, \"FOO\"]\n" + + "}", + toJsonString(message)); assertRoundTripEquals(message); TestMap.Builder mapBuilder = TestMap.newBuilder(); - mapBuilder.getMutableInt32ToEnumMapValue().put(1, 0); + mapBuilder.putInt32ToEnumMapValue(1, 0); mapBuilder.getMutableInt32ToEnumMapValue().put(2, 12345); TestMap mapMessage = mapBuilder.build(); assertEquals( @@ -226,19 +229,21 @@ public class JsonFormatTest extends TestCase { } public void testSpecialFloatValues() throws Exception { - TestAllTypes message = TestAllTypes.newBuilder() - .addRepeatedFloat(Float.NaN) - .addRepeatedFloat(Float.POSITIVE_INFINITY) - .addRepeatedFloat(Float.NEGATIVE_INFINITY) - .addRepeatedDouble(Double.NaN) - .addRepeatedDouble(Double.POSITIVE_INFINITY) - .addRepeatedDouble(Double.NEGATIVE_INFINITY) - .build(); + TestAllTypes message = + TestAllTypes.newBuilder() + .addRepeatedFloat(Float.NaN) + .addRepeatedFloat(Float.POSITIVE_INFINITY) + .addRepeatedFloat(Float.NEGATIVE_INFINITY) + .addRepeatedDouble(Double.NaN) + .addRepeatedDouble(Double.POSITIVE_INFINITY) + .addRepeatedDouble(Double.NEGATIVE_INFINITY) + .build(); assertEquals( "{\n" - + " \"repeatedFloat\": [\"NaN\", \"Infinity\", \"-Infinity\"],\n" - + " \"repeatedDouble\": [\"NaN\", \"Infinity\", \"-Infinity\"]\n" - + "}", toJsonString(message)); + + " \"repeatedFloat\": [\"NaN\", \"Infinity\", \"-Infinity\"],\n" + + " \"repeatedDouble\": [\"NaN\", \"Infinity\", \"-Infinity\"]\n" + + "}", + toJsonString(message)); assertRoundTripEquals(message); } @@ -247,15 +252,16 @@ public class JsonFormatTest extends TestCase { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); mergeFromJson( "{\n" - + " \"optionalInt32\": \"1234\",\n" - + " \"optionalUint32\": \"5678\",\n" - + " \"optionalSint32\": \"9012\",\n" - + " \"optionalFixed32\": \"3456\",\n" - + " \"optionalSfixed32\": \"7890\",\n" - + " \"optionalFloat\": \"1.5\",\n" - + " \"optionalDouble\": \"1.25\",\n" - + " \"optionalBool\": \"true\"\n" - + "}", builder); + + " \"optionalInt32\": \"1234\",\n" + + " \"optionalUint32\": \"5678\",\n" + + " \"optionalSint32\": \"9012\",\n" + + " \"optionalFixed32\": \"3456\",\n" + + " \"optionalSfixed32\": \"7890\",\n" + + " \"optionalFloat\": \"1.5\",\n" + + " \"optionalDouble\": \"1.25\",\n" + + " \"optionalBool\": \"true\"\n" + + "}", + builder); TestAllTypes message = builder.build(); assertEquals(1234, message.getOptionalInt32()); assertEquals(5678, message.getOptionalUint32()); @@ -276,8 +282,9 @@ public class JsonFormatTest extends TestCase { + " \"repeatedUint32\": [1.000, 1e5, \"1.000\", \"1e5\"],\n" + " \"repeatedInt64\": [1.000, 1e5, \"1.000\", \"1e5\"],\n" + " \"repeatedUint64\": [1.000, 1e5, \"1.000\", \"1e5\"]\n" - + "}", builder); - int[] expectedValues = new int[]{1, 100000, 1, 100000}; + + "}", + builder); + int[] expectedValues = new int[] {1, 100000, 1, 100000}; assertEquals(4, builder.getRepeatedInt32Count()); assertEquals(4, builder.getRepeatedUint32Count()); assertEquals(4, builder.getRepeatedInt64Count()); @@ -366,51 +373,49 @@ public class JsonFormatTest extends TestCase { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); mergeFromJson( "{\n" - + " \"optionalInt32\": null,\n" - + " \"optionalInt64\": null,\n" - + " \"optionalUint32\": null,\n" - + " \"optionalUint64\": null,\n" - + " \"optionalSint32\": null,\n" - + " \"optionalSint64\": null,\n" - + " \"optionalFixed32\": null,\n" - + " \"optionalFixed64\": null,\n" - + " \"optionalSfixed32\": null,\n" - + " \"optionalSfixed64\": null,\n" - + " \"optionalFloat\": null,\n" - + " \"optionalDouble\": null,\n" - + " \"optionalBool\": null,\n" - + " \"optionalString\": null,\n" - + " \"optionalBytes\": null,\n" - + " \"optionalNestedMessage\": null,\n" - + " \"optionalNestedEnum\": null,\n" - + " \"repeatedInt32\": null,\n" - + " \"repeatedInt64\": null,\n" - + " \"repeatedUint32\": null,\n" - + " \"repeatedUint64\": null,\n" - + " \"repeatedSint32\": null,\n" - + " \"repeatedSint64\": null,\n" - + " \"repeatedFixed32\": null,\n" - + " \"repeatedFixed64\": null,\n" - + " \"repeatedSfixed32\": null,\n" - + " \"repeatedSfixed64\": null,\n" - + " \"repeatedFloat\": null,\n" - + " \"repeatedDouble\": null,\n" - + " \"repeatedBool\": null,\n" - + " \"repeatedString\": null,\n" - + " \"repeatedBytes\": null,\n" - + " \"repeatedNestedMessage\": null,\n" - + " \"repeatedNestedEnum\": null\n" - + "}", builder); + + " \"optionalInt32\": null,\n" + + " \"optionalInt64\": null,\n" + + " \"optionalUint32\": null,\n" + + " \"optionalUint64\": null,\n" + + " \"optionalSint32\": null,\n" + + " \"optionalSint64\": null,\n" + + " \"optionalFixed32\": null,\n" + + " \"optionalFixed64\": null,\n" + + " \"optionalSfixed32\": null,\n" + + " \"optionalSfixed64\": null,\n" + + " \"optionalFloat\": null,\n" + + " \"optionalDouble\": null,\n" + + " \"optionalBool\": null,\n" + + " \"optionalString\": null,\n" + + " \"optionalBytes\": null,\n" + + " \"optionalNestedMessage\": null,\n" + + " \"optionalNestedEnum\": null,\n" + + " \"repeatedInt32\": null,\n" + + " \"repeatedInt64\": null,\n" + + " \"repeatedUint32\": null,\n" + + " \"repeatedUint64\": null,\n" + + " \"repeatedSint32\": null,\n" + + " \"repeatedSint64\": null,\n" + + " \"repeatedFixed32\": null,\n" + + " \"repeatedFixed64\": null,\n" + + " \"repeatedSfixed32\": null,\n" + + " \"repeatedSfixed64\": null,\n" + + " \"repeatedFloat\": null,\n" + + " \"repeatedDouble\": null,\n" + + " \"repeatedBool\": null,\n" + + " \"repeatedString\": null,\n" + + " \"repeatedBytes\": null,\n" + + " \"repeatedNestedMessage\": null,\n" + + " \"repeatedNestedEnum\": null\n" + + "}", + builder); TestAllTypes message = builder.build(); assertEquals(TestAllTypes.getDefaultInstance(), message); // Repeated field elements cannot be null. try { builder = TestAllTypes.newBuilder(); - mergeFromJson( - "{\n" - + " \"repeatedInt32\": [null, null],\n" - + "}", builder); + mergeFromJson("{\n" + " \"repeatedInt32\": [null, null],\n" + "}", builder); fail(); } catch (InvalidProtocolBufferException e) { // Exception expected. @@ -418,16 +423,21 @@ public class JsonFormatTest extends TestCase { try { builder = TestAllTypes.newBuilder(); - mergeFromJson( - "{\n" - + " \"repeatedNestedMessage\": [null, null],\n" - + "}", builder); + mergeFromJson("{\n" + " \"repeatedNestedMessage\": [null, null],\n" + "}", builder); fail(); } catch (InvalidProtocolBufferException e) { // Exception expected. } } + public void testNullInOneof() throws Exception { + TestOneof.Builder builder = TestOneof.newBuilder(); + mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", builder); + TestOneof message = builder.build(); + assertEquals(TestOneof.OneofFieldCase.ONEOF_NULL_VALUE, message.getOneofFieldCase()); + assertEquals(NullValue.NULL_VALUE, message.getOneofNullValue()); + } + public void testParserRejectDuplicatedFields() throws Exception { // TODO(xiaofeng): The parser we are currently using (GSON) will accept and keep the last // one if multiple entries have the same name. This is not the desired behavior but it can @@ -441,7 +451,8 @@ public class JsonFormatTest extends TestCase { "{\n" + " \"optionalNestedMessage\": {},\n" + " \"optional_nested_message\": {}\n" - + "}", builder); + + "}", + builder); fail(); } catch (InvalidProtocolBufferException e) { // Exception expected. @@ -452,9 +463,10 @@ public class JsonFormatTest extends TestCase { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); mergeFromJson( "{\n" - + " \"repeatedNestedMessage\": [null, null],\n" - + " \"repeated_nested_message\": [null, null]\n" - + "}", builder); + + " \"repeatedNestedMessage\": [null, null],\n" + + " \"repeated_nested_message\": [null, null]\n" + + "}", + builder); fail(); } catch (InvalidProtocolBufferException e) { // Exception expected. @@ -463,11 +475,7 @@ public class JsonFormatTest extends TestCase { // Duplicated oneof fields. try { TestOneof.Builder builder = TestOneof.newBuilder(); - mergeFromJson( - "{\n" - + " \"oneofInt32\": 1,\n" - + " \"oneof_int32\": 2\n" - + "}", builder); + mergeFromJson("{\n" + " \"oneofInt32\": 1,\n" + " \"oneof_int32\": 2\n" + "}", builder); fail(); } catch (InvalidProtocolBufferException e) { // Exception expected. @@ -476,143 +484,138 @@ public class JsonFormatTest extends TestCase { public void testMapFields() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); - builder.getMutableInt32ToInt32Map().put(1, 10); - builder.getMutableInt64ToInt32Map().put(1234567890123456789L, 10); - builder.getMutableUint32ToInt32Map().put(2, 20); - builder.getMutableUint64ToInt32Map().put(2234567890123456789L, 20); - builder.getMutableSint32ToInt32Map().put(3, 30); - builder.getMutableSint64ToInt32Map().put(3234567890123456789L, 30); - builder.getMutableFixed32ToInt32Map().put(4, 40); - builder.getMutableFixed64ToInt32Map().put(4234567890123456789L, 40); - builder.getMutableSfixed32ToInt32Map().put(5, 50); - builder.getMutableSfixed64ToInt32Map().put(5234567890123456789L, 50); - builder.getMutableBoolToInt32Map().put(false, 6); - builder.getMutableStringToInt32Map().put("Hello", 10); - - builder.getMutableInt32ToInt64Map().put(1, 1234567890123456789L); - builder.getMutableInt32ToUint32Map().put(2, 20); - builder.getMutableInt32ToUint64Map().put(2, 2234567890123456789L); - builder.getMutableInt32ToSint32Map().put(3, 30); - builder.getMutableInt32ToSint64Map().put(3, 3234567890123456789L); - builder.getMutableInt32ToFixed32Map().put(4, 40); - builder.getMutableInt32ToFixed64Map().put(4, 4234567890123456789L); - builder.getMutableInt32ToSfixed32Map().put(5, 50); - builder.getMutableInt32ToSfixed64Map().put(5, 5234567890123456789L); - builder.getMutableInt32ToFloatMap().put(6, 1.5f); - builder.getMutableInt32ToDoubleMap().put(6, 1.25); - builder.getMutableInt32ToBoolMap().put(7, false); - builder.getMutableInt32ToStringMap().put(7, "World"); - builder.getMutableInt32ToBytesMap().put( - 8, ByteString.copyFrom(new byte[]{1, 2, 3})); - builder.getMutableInt32ToMessageMap().put( - 8, NestedMessage.newBuilder().setValue(1234).build()); - builder.getMutableInt32ToEnumMap().put(9, NestedEnum.BAR); + builder.putInt32ToInt32Map(1, 10); + builder.putInt64ToInt32Map(1234567890123456789L, 10); + builder.putUint32ToInt32Map(2, 20); + builder.putUint64ToInt32Map(2234567890123456789L, 20); + builder.putSint32ToInt32Map(3, 30); + builder.putSint64ToInt32Map(3234567890123456789L, 30); + builder.putFixed32ToInt32Map(4, 40); + builder.putFixed64ToInt32Map(4234567890123456789L, 40); + builder.putSfixed32ToInt32Map(5, 50); + builder.putSfixed64ToInt32Map(5234567890123456789L, 50); + builder.putBoolToInt32Map(false, 6); + builder.putStringToInt32Map("Hello", 10); + + builder.putInt32ToInt64Map(1, 1234567890123456789L); + builder.putInt32ToUint32Map(2, 20); + builder.putInt32ToUint64Map(2, 2234567890123456789L); + builder.putInt32ToSint32Map(3, 30); + builder.putInt32ToSint64Map(3, 3234567890123456789L); + builder.putInt32ToFixed32Map(4, 40); + builder.putInt32ToFixed64Map(4, 4234567890123456789L); + builder.putInt32ToSfixed32Map(5, 50); + builder.putInt32ToSfixed64Map(5, 5234567890123456789L); + builder.putInt32ToFloatMap(6, 1.5f); + builder.putInt32ToDoubleMap(6, 1.25); + builder.putInt32ToBoolMap(7, false); + builder.putInt32ToStringMap(7, "World"); + builder.putInt32ToBytesMap(8, ByteString.copyFrom(new byte[] {1, 2, 3})); + builder.putInt32ToMessageMap(8, NestedMessage.newBuilder().setValue(1234).build()); + builder.putInt32ToEnumMap(9, NestedEnum.BAR); TestMap message = builder.build(); assertEquals( "{\n" - + " \"int32ToInt32Map\": {\n" - + " \"1\": 10\n" - + " },\n" - + " \"int64ToInt32Map\": {\n" - + " \"1234567890123456789\": 10\n" - + " },\n" - + " \"uint32ToInt32Map\": {\n" - + " \"2\": 20\n" - + " },\n" - + " \"uint64ToInt32Map\": {\n" - + " \"2234567890123456789\": 20\n" - + " },\n" - + " \"sint32ToInt32Map\": {\n" - + " \"3\": 30\n" - + " },\n" - + " \"sint64ToInt32Map\": {\n" - + " \"3234567890123456789\": 30\n" - + " },\n" - + " \"fixed32ToInt32Map\": {\n" - + " \"4\": 40\n" - + " },\n" - + " \"fixed64ToInt32Map\": {\n" - + " \"4234567890123456789\": 40\n" - + " },\n" - + " \"sfixed32ToInt32Map\": {\n" - + " \"5\": 50\n" - + " },\n" - + " \"sfixed64ToInt32Map\": {\n" - + " \"5234567890123456789\": 50\n" - + " },\n" - + " \"boolToInt32Map\": {\n" - + " \"false\": 6\n" - + " },\n" - + " \"stringToInt32Map\": {\n" - + " \"Hello\": 10\n" - + " },\n" - + " \"int32ToInt64Map\": {\n" - + " \"1\": \"1234567890123456789\"\n" - + " },\n" - + " \"int32ToUint32Map\": {\n" - + " \"2\": 20\n" - + " },\n" - + " \"int32ToUint64Map\": {\n" - + " \"2\": \"2234567890123456789\"\n" - + " },\n" - + " \"int32ToSint32Map\": {\n" - + " \"3\": 30\n" - + " },\n" - + " \"int32ToSint64Map\": {\n" - + " \"3\": \"3234567890123456789\"\n" - + " },\n" - + " \"int32ToFixed32Map\": {\n" - + " \"4\": 40\n" - + " },\n" - + " \"int32ToFixed64Map\": {\n" - + " \"4\": \"4234567890123456789\"\n" - + " },\n" - + " \"int32ToSfixed32Map\": {\n" - + " \"5\": 50\n" - + " },\n" - + " \"int32ToSfixed64Map\": {\n" - + " \"5\": \"5234567890123456789\"\n" - + " },\n" - + " \"int32ToFloatMap\": {\n" - + " \"6\": 1.5\n" - + " },\n" - + " \"int32ToDoubleMap\": {\n" - + " \"6\": 1.25\n" - + " },\n" - + " \"int32ToBoolMap\": {\n" - + " \"7\": false\n" - + " },\n" - + " \"int32ToStringMap\": {\n" - + " \"7\": \"World\"\n" - + " },\n" - + " \"int32ToBytesMap\": {\n" - + " \"8\": \"AQID\"\n" - + " },\n" - + " \"int32ToMessageMap\": {\n" - + " \"8\": {\n" - + " \"value\": 1234\n" - + " }\n" - + " },\n" - + " \"int32ToEnumMap\": {\n" - + " \"9\": \"BAR\"\n" - + " }\n" - + "}", toJsonString(message)); + + " \"int32ToInt32Map\": {\n" + + " \"1\": 10\n" + + " },\n" + + " \"int64ToInt32Map\": {\n" + + " \"1234567890123456789\": 10\n" + + " },\n" + + " \"uint32ToInt32Map\": {\n" + + " \"2\": 20\n" + + " },\n" + + " \"uint64ToInt32Map\": {\n" + + " \"2234567890123456789\": 20\n" + + " },\n" + + " \"sint32ToInt32Map\": {\n" + + " \"3\": 30\n" + + " },\n" + + " \"sint64ToInt32Map\": {\n" + + " \"3234567890123456789\": 30\n" + + " },\n" + + " \"fixed32ToInt32Map\": {\n" + + " \"4\": 40\n" + + " },\n" + + " \"fixed64ToInt32Map\": {\n" + + " \"4234567890123456789\": 40\n" + + " },\n" + + " \"sfixed32ToInt32Map\": {\n" + + " \"5\": 50\n" + + " },\n" + + " \"sfixed64ToInt32Map\": {\n" + + " \"5234567890123456789\": 50\n" + + " },\n" + + " \"boolToInt32Map\": {\n" + + " \"false\": 6\n" + + " },\n" + + " \"stringToInt32Map\": {\n" + + " \"Hello\": 10\n" + + " },\n" + + " \"int32ToInt64Map\": {\n" + + " \"1\": \"1234567890123456789\"\n" + + " },\n" + + " \"int32ToUint32Map\": {\n" + + " \"2\": 20\n" + + " },\n" + + " \"int32ToUint64Map\": {\n" + + " \"2\": \"2234567890123456789\"\n" + + " },\n" + + " \"int32ToSint32Map\": {\n" + + " \"3\": 30\n" + + " },\n" + + " \"int32ToSint64Map\": {\n" + + " \"3\": \"3234567890123456789\"\n" + + " },\n" + + " \"int32ToFixed32Map\": {\n" + + " \"4\": 40\n" + + " },\n" + + " \"int32ToFixed64Map\": {\n" + + " \"4\": \"4234567890123456789\"\n" + + " },\n" + + " \"int32ToSfixed32Map\": {\n" + + " \"5\": 50\n" + + " },\n" + + " \"int32ToSfixed64Map\": {\n" + + " \"5\": \"5234567890123456789\"\n" + + " },\n" + + " \"int32ToFloatMap\": {\n" + + " \"6\": 1.5\n" + + " },\n" + + " \"int32ToDoubleMap\": {\n" + + " \"6\": 1.25\n" + + " },\n" + + " \"int32ToBoolMap\": {\n" + + " \"7\": false\n" + + " },\n" + + " \"int32ToStringMap\": {\n" + + " \"7\": \"World\"\n" + + " },\n" + + " \"int32ToBytesMap\": {\n" + + " \"8\": \"AQID\"\n" + + " },\n" + + " \"int32ToMessageMap\": {\n" + + " \"8\": {\n" + + " \"value\": 1234\n" + + " }\n" + + " },\n" + + " \"int32ToEnumMap\": {\n" + + " \"9\": \"BAR\"\n" + + " }\n" + + "}", + toJsonString(message)); assertRoundTripEquals(message); // Test multiple entries. builder = TestMap.newBuilder(); - builder.getMutableInt32ToInt32Map().put(1, 2); - builder.getMutableInt32ToInt32Map().put(3, 4); + builder.putInt32ToInt32Map(1, 2); + builder.putInt32ToInt32Map(3, 4); message = builder.build(); assertEquals( - "{\n" - + " \"int32ToInt32Map\": {\n" - + " \"1\": 2,\n" - + " \"3\": 4\n" - + " }\n" - + "}", toJsonString(message)); + "{\n" + " \"int32ToInt32Map\": {\n" + " \"1\": 2,\n" + " \"3\": 4\n" + " }\n" + "}", + toJsonString(message)); assertRoundTripEquals(message); } @@ -621,9 +624,10 @@ public class JsonFormatTest extends TestCase { TestMap.Builder builder = TestMap.newBuilder(); mergeFromJson( "{\n" - + " \"int32ToInt32Map\": {null: 1},\n" - + " \"int32ToMessageMap\": {null: 2}\n" - + "}", builder); + + " \"int32ToInt32Map\": {null: 1},\n" + + " \"int32ToMessageMap\": {null: 2}\n" + + "}", + builder); fail(); } catch (InvalidProtocolBufferException e) { // Exception expected. @@ -633,9 +637,10 @@ public class JsonFormatTest extends TestCase { TestMap.Builder builder = TestMap.newBuilder(); mergeFromJson( "{\n" - + " \"int32ToInt32Map\": {\"1\": null},\n" - + " \"int32ToMessageMap\": {\"2\": null}\n" - + "}", builder); + + " \"int32ToInt32Map\": {\"1\": null},\n" + + " \"int32ToMessageMap\": {\"2\": null}\n" + + "}", + builder); fail(); } catch (InvalidProtocolBufferException e) { // Exception expected. @@ -645,10 +650,7 @@ public class JsonFormatTest extends TestCase { public void testParserAcceptNonQuotedObjectKey() throws Exception { TestMap.Builder builder = TestMap.newBuilder(); mergeFromJson( - "{\n" - + " int32ToInt32Map: {1: 2},\n" - + " stringToInt32Map: {hello: 3}\n" - + "}", builder); + "{\n" + " int32ToInt32Map: {1: 2},\n" + " stringToInt32Map: {hello: 3}\n" + "}", builder); TestMap message = builder.build(); assertEquals(2, message.getInt32ToInt32Map().get(1).intValue()); assertEquals(3, message.getStringToInt32Map().get("hello").intValue()); @@ -669,16 +671,17 @@ public class JsonFormatTest extends TestCase { assertEquals( "{\n" - + " \"int32Value\": 0,\n" - + " \"uint32Value\": 0,\n" - + " \"int64Value\": \"0\",\n" - + " \"uint64Value\": \"0\",\n" - + " \"floatValue\": 0.0,\n" - + " \"doubleValue\": 0.0,\n" - + " \"boolValue\": false,\n" - + " \"stringValue\": \"\",\n" - + " \"bytesValue\": \"\"\n" - + "}", toJsonString(message)); + + " \"int32Value\": 0,\n" + + " \"uint32Value\": 0,\n" + + " \"int64Value\": \"0\",\n" + + " \"uint64Value\": \"0\",\n" + + " \"floatValue\": 0.0,\n" + + " \"doubleValue\": 0.0,\n" + + " \"boolValue\": false,\n" + + " \"stringValue\": \"\",\n" + + " \"bytesValue\": \"\"\n" + + "}", + toJsonString(message)); assertRoundTripEquals(message); builder = TestWrappers.newBuilder(); @@ -690,57 +693,52 @@ public class JsonFormatTest extends TestCase { builder.getFloatValueBuilder().setValue(5.0f); builder.getDoubleValueBuilder().setValue(6.0); builder.getStringValueBuilder().setValue("7"); - builder.getBytesValueBuilder().setValue(ByteString.copyFrom(new byte[]{8})); + builder.getBytesValueBuilder().setValue(ByteString.copyFrom(new byte[] {8})); message = builder.build(); assertEquals( "{\n" - + " \"int32Value\": 1,\n" - + " \"uint32Value\": 3,\n" - + " \"int64Value\": \"2\",\n" - + " \"uint64Value\": \"4\",\n" - + " \"floatValue\": 5.0,\n" - + " \"doubleValue\": 6.0,\n" - + " \"boolValue\": true,\n" - + " \"stringValue\": \"7\",\n" - + " \"bytesValue\": \"CA==\"\n" - + "}", toJsonString(message)); + + " \"int32Value\": 1,\n" + + " \"uint32Value\": 3,\n" + + " \"int64Value\": \"2\",\n" + + " \"uint64Value\": \"4\",\n" + + " \"floatValue\": 5.0,\n" + + " \"doubleValue\": 6.0,\n" + + " \"boolValue\": true,\n" + + " \"stringValue\": \"7\",\n" + + " \"bytesValue\": \"CA==\"\n" + + "}", + toJsonString(message)); assertRoundTripEquals(message); } public void testTimestamp() throws Exception { - TestTimestamp message = TestTimestamp.newBuilder() - .setTimestampValue(TimeUtil.parseTimestamp("1970-01-01T00:00:00Z")) - .build(); + TestTimestamp message = + TestTimestamp.newBuilder() + .setTimestampValue(Timestamps.parse("1970-01-01T00:00:00Z")) + .build(); assertEquals( - "{\n" - + " \"timestampValue\": \"1970-01-01T00:00:00Z\"\n" - + "}", toJsonString(message)); + "{\n" + " \"timestampValue\": \"1970-01-01T00:00:00Z\"\n" + "}", toJsonString(message)); assertRoundTripEquals(message); } public void testDuration() throws Exception { - TestDuration message = TestDuration.newBuilder() - .setDurationValue(TimeUtil.parseDuration("12345s")) - .build(); + TestDuration message = + TestDuration.newBuilder().setDurationValue(Durations.parse("12345s")).build(); - assertEquals( - "{\n" - + " \"durationValue\": \"12345s\"\n" - + "}", toJsonString(message)); + assertEquals("{\n" + " \"durationValue\": \"12345s\"\n" + "}", toJsonString(message)); assertRoundTripEquals(message); } public void testFieldMask() throws Exception { - TestFieldMask message = TestFieldMask.newBuilder() - .setFieldMaskValue(FieldMaskUtil.fromString("foo.bar,baz")) - .build(); + TestFieldMask message = + TestFieldMask.newBuilder() + .setFieldMaskValue(FieldMaskUtil.fromString("foo.bar,baz,foo_bar.baz")) + .build(); assertEquals( - "{\n" - + " \"fieldMaskValue\": \"foo.bar,baz\"\n" - + "}", toJsonString(message)); + "{\n" + " \"fieldMaskValue\": \"foo.bar,baz,fooBar.baz\"\n" + "}", toJsonString(message)); assertRoundTripEquals(message); } @@ -748,45 +746,39 @@ public class JsonFormatTest extends TestCase { // Build a struct with all possible values. TestStruct.Builder builder = TestStruct.newBuilder(); Struct.Builder structBuilder = builder.getStructValueBuilder(); - structBuilder.getMutableFields().put( - "null_value", Value.newBuilder().setNullValueValue(0).build()); - structBuilder.getMutableFields().put( - "number_value", Value.newBuilder().setNumberValue(1.25).build()); - structBuilder.getMutableFields().put( - "string_value", Value.newBuilder().setStringValue("hello").build()); + structBuilder.putFields("null_value", Value.newBuilder().setNullValueValue(0).build()); + structBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1.25).build()); + structBuilder.putFields("string_value", Value.newBuilder().setStringValue("hello").build()); Struct.Builder subStructBuilder = Struct.newBuilder(); - subStructBuilder.getMutableFields().put( - "number_value", Value.newBuilder().setNumberValue(1234).build()); - structBuilder.getMutableFields().put( + subStructBuilder.putFields("number_value", Value.newBuilder().setNumberValue(1234).build()); + structBuilder.putFields( "struct_value", Value.newBuilder().setStructValue(subStructBuilder.build()).build()); ListValue.Builder listBuilder = ListValue.newBuilder(); listBuilder.addValues(Value.newBuilder().setNumberValue(1.125).build()); listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build()); - structBuilder.getMutableFields().put( + structBuilder.putFields( "list_value", Value.newBuilder().setListValue(listBuilder.build()).build()); TestStruct message = builder.build(); assertEquals( "{\n" - + " \"structValue\": {\n" - + " \"null_value\": null,\n" - + " \"number_value\": 1.25,\n" - + " \"string_value\": \"hello\",\n" - + " \"struct_value\": {\n" - + " \"number_value\": 1234.0\n" - + " },\n" - + " \"list_value\": [1.125, null]\n" - + " }\n" - + "}", toJsonString(message)); + + " \"structValue\": {\n" + + " \"null_value\": null,\n" + + " \"number_value\": 1.25,\n" + + " \"string_value\": \"hello\",\n" + + " \"struct_value\": {\n" + + " \"number_value\": 1234.0\n" + + " },\n" + + " \"list_value\": [1.125, null]\n" + + " }\n" + + "}", + toJsonString(message)); assertRoundTripEquals(message); builder = TestStruct.newBuilder(); builder.setValue(Value.newBuilder().setNullValueValue(0).build()); message = builder.build(); - assertEquals( - "{\n" - + " \"value\": null\n" - + "}", toJsonString(message)); + assertEquals("{\n" + " \"value\": null\n" + "}", toJsonString(message)); assertRoundTripEquals(message); builder = TestStruct.newBuilder(); @@ -794,10 +786,7 @@ public class JsonFormatTest extends TestCase { listBuilder.addValues(Value.newBuilder().setNumberValue(31831.125).build()); listBuilder.addValues(Value.newBuilder().setNullValueValue(0).build()); message = builder.build(); - assertEquals( - "{\n" - + " \"listValue\": [31831.125, null]\n" - + "}", toJsonString(message)); + assertEquals("{\n" + " \"listValue\": [31831.125, null]\n" + "}", toJsonString(message)); assertRoundTripEquals(message); } @@ -813,158 +802,169 @@ public class JsonFormatTest extends TestCase { // Expected. } - JsonFormat.TypeRegistry registry = JsonFormat.TypeRegistry.newBuilder() - .add(TestAllTypes.getDescriptor()).build(); + JsonFormat.TypeRegistry registry = + JsonFormat.TypeRegistry.newBuilder().add(TestAllTypes.getDescriptor()).build(); JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry); assertEquals( "{\n" - + " \"anyValue\": {\n" - + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" - + " \"optionalInt32\": 1234\n" - + " }\n" - + "}" , printer.print(message)); + + " \"anyValue\": {\n" + + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" + + " \"optionalInt32\": 1234\n" + + " }\n" + + "}", + printer.print(message)); assertRoundTripEquals(message, registry); - // Well-known types have a special formatting when embedded in Any. // // 1. Any in Any. Any anyMessage = Any.pack(Any.pack(content)); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n" - + " \"value\": {\n" - + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" - + " \"optionalInt32\": 1234\n" - + " }\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.Any\",\n" + + " \"value\": {\n" + + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" + + " \"optionalInt32\": 1234\n" + + " }\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); // 2. Wrappers in Any. anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n" - + " \"value\": 12345\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.Int32Value\",\n" + + " \"value\": 12345\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.UInt32Value\",\n" - + " \"value\": 12345\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.UInt32Value\",\n" + + " \"value\": 12345\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n" - + " \"value\": \"12345\"\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.Int64Value\",\n" + + " \"value\": \"12345\"\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.UInt64Value\",\n" - + " \"value\": \"12345\"\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.UInt64Value\",\n" + + " \"value\": \"12345\"\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.FloatValue\",\n" - + " \"value\": 12345.0\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.FloatValue\",\n" + + " \"value\": 12345.0\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.DoubleValue\",\n" - + " \"value\": 12345.0\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.DoubleValue\",\n" + + " \"value\": 12345.0\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n" - + " \"value\": true\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n" + + " \"value\": true\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.StringValue\",\n" - + " \"value\": \"Hello\"\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.StringValue\",\n" + + " \"value\": \"Hello\"\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); - anyMessage = Any.pack(BytesValue.newBuilder().setValue( - ByteString.copyFrom(new byte[]{1, 2})).build()); + anyMessage = + Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] {1, 2})).build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.BytesValue\",\n" - + " \"value\": \"AQI=\"\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.BytesValue\",\n" + + " \"value\": \"AQI=\"\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); // 3. Timestamp in Any. - anyMessage = Any.pack(TimeUtil.parseTimestamp("1969-12-31T23:59:59Z")); + anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n" - + " \"value\": \"1969-12-31T23:59:59Z\"\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\",\n" + + " \"value\": \"1969-12-31T23:59:59Z\"\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); // 4. Duration in Any - anyMessage = Any.pack(TimeUtil.parseDuration("12345.10s")); + anyMessage = Any.pack(Durations.parse("12345.10s")); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n" - + " \"value\": \"12345.100s\"\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n" + + " \"value\": \"12345.100s\"\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); // 5. FieldMask in Any anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz")); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n" - + " \"value\": \"foo.bar,baz\"\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.FieldMask\",\n" + + " \"value\": \"foo.bar,baz\"\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); // 6. Struct in Any Struct.Builder structBuilder = Struct.newBuilder(); - structBuilder.getMutableFields().put( - "number", Value.newBuilder().setNumberValue(1.125).build()); + structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build()); anyMessage = Any.pack(structBuilder.build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n" - + " \"value\": {\n" - + " \"number\": 1.125\n" - + " }\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.Struct\",\n" + + " \"value\": {\n" + + " \"number\": 1.125\n" + + " }\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); Value.Builder valueBuilder = Value.newBuilder(); valueBuilder.setNumberValue(1); anyMessage = Any.pack(valueBuilder.build()); assertEquals( "{\n" - + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n" - + " \"value\": 1.0\n" - + "}", printer.print(anyMessage)); + + " \"@type\": \"type.googleapis.com/google.protobuf.Value\",\n" + + " \"value\": 1.0\n" + + "}", + printer.print(anyMessage)); assertRoundTripEquals(anyMessage, registry); } public void testParserMissingTypeUrl() throws Exception { try { Any.Builder builder = Any.newBuilder(); - mergeFromJson( - "{\n" - + " \"optionalInt32\": 1234\n" - + "}", builder); + mergeFromJson("{\n" + " \"optionalInt32\": 1234\n" + "}", builder); fail("Exception is expected."); } catch (IOException e) { // Expected. @@ -976,9 +976,10 @@ public class JsonFormatTest extends TestCase { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); mergeFromJson( "{\n" - + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" - + " \"optionalInt32\": 12345\n" - + "}", builder); + + " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n" + + " \"optionalInt32\": 12345\n" + + "}", + builder); fail("Exception is expected."); } catch (IOException e) { // Expected. @@ -988,10 +989,7 @@ public class JsonFormatTest extends TestCase { public void testParserRejectTrailingComma() throws Exception { try { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - mergeFromJson( - "{\n" - + " \"optionalInt32\": 12345,\n" - + "}", builder); + mergeFromJson("{\n" + " \"optionalInt32\": 12345,\n" + "}", builder); fail("Exception is expected."); } catch (IOException e) { // Expected. @@ -1022,10 +1020,7 @@ public class JsonFormatTest extends TestCase { public void testParserRejectInvalidEnumValue() throws Exception { try { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); - mergeFromJson( - "{\n" - + " \"optionalNestedEnum\": \"XXX\"\n" - + "}", builder); + mergeFromJson("{\n" + " \"optionalNestedEnum\": \"XXX\"\n" + "}", builder); fail("Exception is expected."); } catch (InvalidProtocolBufferException e) { // Expected. diff --git a/java/util/src/test/java/com/google/protobuf/util/TimeUtilTest.java b/java/util/src/test/java/com/google/protobuf/util/TimeUtilTest.java index 4c31b2b3..a41528ec 100644 --- a/java/util/src/test/java/com/google/protobuf/util/TimeUtilTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/TimeUtilTest.java @@ -84,6 +84,7 @@ public class TimeUtilTest extends TestCase { private class ParseTimestampThread extends Thread { private final String[] strings; private final Timestamp[] values; + public ParseTimestampThread(String[] strings, Timestamp[] values) { this.strings = strings; this.values = values; @@ -102,8 +103,8 @@ public class TimeUtilTest extends TestCase { } if (result.getSeconds() != values[index].getSeconds() || result.getNanos() != values[index].getNanos()) { - errorMessage = "Actual result: " + result.toString() + ", expected: " - + values[index].toString(); + errorMessage = + "Actual result: " + result.toString() + ", expected: " + values[index].toString(); break; } index = (index + 1) % strings.length; @@ -112,26 +113,26 @@ public class TimeUtilTest extends TestCase { } public void testTimestampConcurrentParsing() throws Exception { - String[] timestampStrings = new String[]{ - "0001-01-01T00:00:00Z", - "9999-12-31T23:59:59.999999999Z", - "1970-01-01T00:00:00Z", - "1969-12-31T23:59:59.999Z", - }; + String[] timestampStrings = + new String[] { + "0001-01-01T00:00:00Z", + "9999-12-31T23:59:59.999999999Z", + "1970-01-01T00:00:00Z", + "1969-12-31T23:59:59.999Z", + }; Timestamp[] timestampValues = new Timestamp[timestampStrings.length]; for (int i = 0; i < timestampStrings.length; i++) { timestampValues[i] = TimeUtil.parseTimestamp(timestampStrings[i]); } final int THREAD_COUNT = 16; - final int RUNNING_TIME = 5000; // in milliseconds. + final int RUNNING_TIME = 5000; // in milliseconds. final List<Thread> threads = new ArrayList<Thread>(); stopParsingThreads = false; errorMessage = ""; for (int i = 0; i < THREAD_COUNT; i++) { - Thread thread = new ParseTimestampThread( - timestampStrings, timestampValues); + Thread thread = new ParseTimestampThread(timestampStrings, timestampValues); thread.start(); threads.add(thread); } @@ -146,8 +147,8 @@ public class TimeUtilTest extends TestCase { public void testTimetampInvalidFormat() throws Exception { try { // Value too small. - Timestamp value = Timestamp.newBuilder() - .setSeconds(TimeUtil.TIMESTAMP_SECONDS_MIN - 1).build(); + Timestamp value = + Timestamp.newBuilder().setSeconds(TimeUtil.TIMESTAMP_SECONDS_MIN - 1).build(); TimeUtil.toString(value); Assert.fail("Exception is expected."); } catch (IllegalArgumentException e) { @@ -156,14 +157,14 @@ public class TimeUtilTest extends TestCase { try { // Value too large. - Timestamp value = Timestamp.newBuilder() - .setSeconds(TimeUtil.TIMESTAMP_SECONDS_MAX + 1).build(); + Timestamp value = + Timestamp.newBuilder().setSeconds(TimeUtil.TIMESTAMP_SECONDS_MAX + 1).build(); TimeUtil.toString(value); Assert.fail("Exception is expected."); } catch (IllegalArgumentException e) { // Expected. } - + try { // Invalid nanos value. Timestamp value = Timestamp.newBuilder().setNanos(-1).build(); @@ -279,8 +280,7 @@ public class TimeUtilTest extends TestCase { public void testDurationInvalidFormat() throws Exception { try { // Value too small. - Duration value = Duration.newBuilder() - .setSeconds(TimeUtil.DURATION_SECONDS_MIN - 1).build(); + Duration value = Duration.newBuilder().setSeconds(TimeUtil.DURATION_SECONDS_MIN - 1).build(); TimeUtil.toString(value); Assert.fail("Exception is expected."); } catch (IllegalArgumentException e) { @@ -289,18 +289,16 @@ public class TimeUtilTest extends TestCase { try { // Value too large. - Duration value = Duration.newBuilder() - .setSeconds(TimeUtil.DURATION_SECONDS_MAX + 1).build(); + Duration value = Duration.newBuilder().setSeconds(TimeUtil.DURATION_SECONDS_MAX + 1).build(); TimeUtil.toString(value); Assert.fail("Exception is expected."); } catch (IllegalArgumentException e) { // Expected. } - + try { // Invalid nanos value. - Duration value = Duration.newBuilder().setSeconds(1).setNanos(-1) - .build(); + Duration value = Duration.newBuilder().setSeconds(1).setNanos(-1).build(); TimeUtil.toString(value); Assert.fail("Exception is expected."); } catch (IllegalArgumentException e) { @@ -309,8 +307,7 @@ public class TimeUtilTest extends TestCase { try { // Invalid nanos value. - Duration value = Duration.newBuilder().setSeconds(-1).setNanos(1) - .build(); + Duration value = Duration.newBuilder().setSeconds(-1).setNanos(1).build(); TimeUtil.toString(value); Assert.fail("Exception is expected."); } catch (IllegalArgumentException e) { @@ -367,8 +364,7 @@ public class TimeUtilTest extends TestCase { } public void testTimestampConversion() throws Exception { - Timestamp timestamp = - TimeUtil.parseTimestamp("1970-01-01T00:00:01.111111111Z"); + Timestamp timestamp = TimeUtil.parseTimestamp("1970-01-01T00:00:01.111111111Z"); assertEquals(1111111111, TimeUtil.toNanos(timestamp)); assertEquals(1111111, TimeUtil.toMicros(timestamp)); assertEquals(1111, TimeUtil.toMillis(timestamp)); @@ -378,7 +374,7 @@ public class TimeUtilTest extends TestCase { assertEquals("1970-01-01T00:00:01.111111Z", TimeUtil.toString(timestamp)); timestamp = TimeUtil.createTimestampFromMillis(1111); assertEquals("1970-01-01T00:00:01.111Z", TimeUtil.toString(timestamp)); - + timestamp = TimeUtil.parseTimestamp("1969-12-31T23:59:59.111111111Z"); assertEquals(-888888889, TimeUtil.toNanos(timestamp)); assertEquals(-888889, TimeUtil.toMicros(timestamp)); @@ -402,7 +398,7 @@ public class TimeUtilTest extends TestCase { assertEquals("1.111111s", TimeUtil.toString(duration)); duration = TimeUtil.createDurationFromMillis(1111); assertEquals("1.111s", TimeUtil.toString(duration)); - + duration = TimeUtil.parseDuration("-1.111111111s"); assertEquals(-1111111111, TimeUtil.toNanos(duration)); assertEquals(-1111111, TimeUtil.toMicros(duration)); @@ -459,29 +455,28 @@ public class TimeUtilTest extends TestCase { duration = TimeUtil.add(duration, duration); assertEquals("-2.250s", TimeUtil.toString(duration)); - + duration = TimeUtil.subtract(duration, TimeUtil.parseDuration("-1s")); assertEquals("-1.250s", TimeUtil.toString(duration)); - + // Multiplications (with results larger than Long.MAX_VALUE in nanoseconds). duration = TimeUtil.parseDuration("0.999999999s"); - assertEquals("315575999684.424s", - TimeUtil.toString(TimeUtil.multiply(duration, 315576000000L))); + assertEquals( + "315575999684.424s", TimeUtil.toString(TimeUtil.multiply(duration, 315576000000L))); duration = TimeUtil.parseDuration("-0.999999999s"); - assertEquals("-315575999684.424s", - TimeUtil.toString(TimeUtil.multiply(duration, 315576000000L))); - assertEquals("315575999684.424s", - TimeUtil.toString(TimeUtil.multiply(duration, -315576000000L))); - + assertEquals( + "-315575999684.424s", TimeUtil.toString(TimeUtil.multiply(duration, 315576000000L))); + assertEquals( + "315575999684.424s", TimeUtil.toString(TimeUtil.multiply(duration, -315576000000L))); + // Divisions (with values larger than Long.MAX_VALUE in nanoseconds). Duration d1 = TimeUtil.parseDuration("315576000000s"); Duration d2 = TimeUtil.subtract(d1, TimeUtil.createDurationFromNanos(1)); assertEquals(1, TimeUtil.divide(d1, d2)); assertEquals(0, TimeUtil.divide(d2, d1)); assertEquals("0.000000001s", TimeUtil.toString(TimeUtil.remainder(d1, d2))); - assertEquals("315575999999.999999999s", - TimeUtil.toString(TimeUtil.remainder(d2, d1))); - + assertEquals("315575999999.999999999s", TimeUtil.toString(TimeUtil.remainder(d2, d1))); + // Divisions involving negative values. // // (-5) / 2 = -2, remainder = -1 diff --git a/java/util/src/test/proto/com/google/protobuf/util/json_test.proto b/java/util/src/test/proto/com/google/protobuf/util/json_test.proto index 686edc42..4bf223f2 100644 --- a/java/util/src/test/proto/com/google/protobuf/util/json_test.proto +++ b/java/util/src/test/proto/com/google/protobuf/util/json_test.proto @@ -124,6 +124,7 @@ message TestOneof { oneof oneof_field { int32 oneof_int32 = 1; TestAllTypes.NestedMessage oneof_nested_message = 2; + google.protobuf.NullValue oneof_null_value = 3; } } |