aboutsummaryrefslogtreecommitdiffhomepage
path: root/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java')
-rw-r--r--java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java137
1 files changed, 126 insertions, 11 deletions
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 de02c117..6943093f 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
@@ -34,6 +34,7 @@ import com.google.protobuf.Any;
import com.google.protobuf.BoolValue;
import com.google.protobuf.ByteString;
import com.google.protobuf.BytesValue;
+import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.DoubleValue;
import com.google.protobuf.FloatValue;
import com.google.protobuf.Int32Value;
@@ -68,9 +69,12 @@ import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
+import java.util.Set;
import junit.framework.TestCase;
public class JsonFormatTest extends TestCase {
@@ -284,8 +288,8 @@ public class JsonFormatTest extends TestCase {
assertEquals(9012, message.getOptionalSint32());
assertEquals(3456, message.getOptionalFixed32());
assertEquals(7890, message.getOptionalSfixed32());
- assertEquals(1.5f, message.getOptionalFloat());
- assertEquals(1.25, message.getOptionalDouble());
+ assertEquals(1.5f, message.getOptionalFloat(), 0.0f);
+ assertEquals(1.25, message.getOptionalDouble(), 0.0);
assertEquals(true, message.getOptionalBool());
}
@@ -1215,6 +1219,115 @@ public class JsonFormatTest extends TestCase {
+ "}",
JsonFormat.printer().includingDefaultValueFields().print(message));
+ Set<FieldDescriptor> fixedFields = new HashSet<FieldDescriptor>();
+ for (FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
+ if (fieldDesc.getName().contains("_fixed")) {
+ fixedFields.add(fieldDesc);
+ }
+ }
+
+ assertEquals(
+ "{\n"
+ + " \"optionalFixed32\": 0,\n"
+ + " \"optionalFixed64\": \"0\",\n"
+ + " \"repeatedFixed32\": [],\n"
+ + " \"repeatedFixed64\": []\n"
+ + "}",
+ JsonFormat.printer().includingDefaultValueFields(fixedFields).print(message));
+
+ TestAllTypes messageNonDefaults =
+ message.toBuilder().setOptionalInt64(1234).setOptionalFixed32(3232).build();
+ assertEquals(
+ "{\n"
+ + " \"optionalInt64\": \"1234\",\n"
+ + " \"optionalFixed32\": 3232,\n"
+ + " \"optionalFixed64\": \"0\",\n"
+ + " \"repeatedFixed32\": [],\n"
+ + " \"repeatedFixed64\": []\n"
+ + "}",
+ JsonFormat.printer().includingDefaultValueFields(fixedFields).print(messageNonDefaults));
+
+ try {
+ JsonFormat.printer().includingDefaultValueFields().includingDefaultValueFields();
+ fail("IllegalStateException is expected.");
+ } catch (IllegalStateException e) {
+ // Expected.
+ assertTrue(
+ "Exception message should mention includingDefaultValueFields.",
+ e.getMessage().contains("includingDefaultValueFields"));
+ }
+
+ try {
+ JsonFormat.printer().includingDefaultValueFields().includingDefaultValueFields(fixedFields);
+ fail("IllegalStateException is expected.");
+ } catch (IllegalStateException e) {
+ // Expected.
+ assertTrue(
+ "Exception message should mention includingDefaultValueFields.",
+ e.getMessage().contains("includingDefaultValueFields"));
+ }
+
+ try {
+ JsonFormat.printer().includingDefaultValueFields(fixedFields).includingDefaultValueFields();
+ fail("IllegalStateException is expected.");
+ } catch (IllegalStateException e) {
+ // Expected.
+ assertTrue(
+ "Exception message should mention includingDefaultValueFields.",
+ e.getMessage().contains("includingDefaultValueFields"));
+ }
+
+ try {
+ JsonFormat.printer()
+ .includingDefaultValueFields(fixedFields)
+ .includingDefaultValueFields(fixedFields);
+ fail("IllegalStateException is expected.");
+ } catch (IllegalStateException e) {
+ // Expected.
+ assertTrue(
+ "Exception message should mention includingDefaultValueFields.",
+ e.getMessage().contains("includingDefaultValueFields"));
+ }
+
+ Set<FieldDescriptor> intFields = new HashSet<FieldDescriptor>();
+ for (FieldDescriptor fieldDesc : TestAllTypes.getDescriptor().getFields()) {
+ if (fieldDesc.getName().contains("_int")) {
+ intFields.add(fieldDesc);
+ }
+ }
+
+ try {
+ JsonFormat.printer()
+ .includingDefaultValueFields(intFields)
+ .includingDefaultValueFields(fixedFields);
+ fail("IllegalStateException is expected.");
+ } catch (IllegalStateException e) {
+ // Expected.
+ assertTrue(
+ "Exception message should mention includingDefaultValueFields.",
+ e.getMessage().contains("includingDefaultValueFields"));
+ }
+
+ try {
+ JsonFormat.printer().includingDefaultValueFields(null);
+ fail("IllegalArgumentException is expected.");
+ } catch (IllegalArgumentException e) {
+ // Expected.
+ assertTrue(
+ "Exception message should mention includingDefaultValueFields.",
+ e.getMessage().contains("includingDefaultValueFields"));
+ }
+
+ try {
+ JsonFormat.printer().includingDefaultValueFields(Collections.emptySet());
+ fail("IllegalArgumentException is expected.");
+ } catch (IllegalArgumentException e) {
+ // Expected.
+ assertTrue(
+ "Exception message should mention includingDefaultValueFields.",
+ e.getMessage().contains("includingDefaultValueFields"));
+ }
+
TestMap mapMessage = TestMap.getDefaultInstance();
assertEquals("{\n}", JsonFormat.printer().print(mapMessage));
assertEquals(
@@ -1283,16 +1396,17 @@ public class JsonFormatTest extends TestCase {
assertEquals("{\n}", JsonFormat.printer().includingDefaultValueFields().print(oneofMessage));
oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
- assertEquals("{\n \"oneofInt32\": 42\n}",
- JsonFormat.printer().print(oneofMessage));
- assertEquals("{\n \"oneofInt32\": 42\n}",
+ assertEquals("{\n \"oneofInt32\": 42\n}", JsonFormat.printer().print(oneofMessage));
+ assertEquals(
+ "{\n \"oneofInt32\": 42\n}",
JsonFormat.printer().includingDefaultValueFields().print(oneofMessage));
TestOneof.Builder oneofBuilder = TestOneof.newBuilder();
mergeFromJson("{\n" + " \"oneofNullValue\": null \n" + "}", oneofBuilder);
oneofMessage = oneofBuilder.build();
assertEquals("{\n \"oneofNullValue\": null\n}", JsonFormat.printer().print(oneofMessage));
- assertEquals("{\n \"oneofNullValue\": null\n}",
+ assertEquals(
+ "{\n \"oneofNullValue\": null\n}",
JsonFormat.printer().includingDefaultValueFields().print(oneofMessage));
}
@@ -1424,11 +1538,12 @@ public class JsonFormatTest extends TestCase {
// Test that we are not leaking out JSON exceptions.
public void testJsonException() throws Exception {
- InputStream throwingInputStream = new InputStream() {
- public int read() throws IOException {
- throw new IOException("12345");
- }
- };
+ InputStream throwingInputStream =
+ new InputStream() {
+ public int read() throws IOException {
+ throw new IOException("12345");
+ }
+ };
InputStreamReader throwingReader = new InputStreamReader(throwingInputStream);
// When the underlying reader throws IOException, JsonFormat should forward
// through this IOException.