|
28 | 28 | import java.math.BigDecimal; |
29 | 29 | import java.math.BigInteger; |
30 | 30 | import org.junit.Test; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
31 | 33 |
|
32 | 34 | @SuppressWarnings("resource") |
33 | 35 | public final class JsonWriterTest { |
@@ -114,13 +116,17 @@ public void testTopLevelValueTypes() throws IOException { |
114 | 116 | public void testInvalidTopLevelTypes() throws IOException { |
115 | 117 | StringWriter stringWriter = new StringWriter(); |
116 | 118 | JsonWriter jsonWriter = new JsonWriter(stringWriter); |
117 | | - jsonWriter.name("hello"); // TODO: This should throw, see https://github.com/google/gson/issues/2407 |
118 | | - try { |
119 | | - jsonWriter.value("world"); |
120 | | - fail(); |
121 | | - } catch (IllegalStateException expected) { |
122 | | - assertThat(expected).hasMessageThat().isEqualTo("Nesting problem."); |
123 | | - } |
| 119 | + assertThrows(IllegalStateException.class, () -> jsonWriter.name("hello")); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void closeAllObjectsAndTryToAddElements() throws IOException { |
| 124 | + JsonWriter jsonWriterForNameAddition = getJsonWriterWithObjects(); |
| 125 | + assertThrows(IllegalStateException.class, () -> jsonWriterForNameAddition.name("this_throw_exception_as_all_objects_are_closed")); |
| 126 | + jsonWriterForNameAddition.close(); |
| 127 | + JsonWriter jsonWriterForValueAddition = getJsonWriterWithObjects(); |
| 128 | + assertThrows(IllegalStateException.class, () -> jsonWriterForValueAddition.value("this_throw_exception_as_only_one_top_level_entry")); |
| 129 | + jsonWriterForValueAddition.close(); |
124 | 130 | } |
125 | 131 |
|
126 | 132 | @Test |
@@ -973,4 +979,33 @@ public void testIndentOverwritesFormattingStyle() throws IOException { |
973 | 979 | + "}"; |
974 | 980 | assertThat(stringWriter.toString()).isEqualTo(expected); |
975 | 981 | } |
| 982 | + |
| 983 | + /** |
| 984 | + * This method wites a json object and return a jsonwriter object |
| 985 | + * that we can use for the testing purpose |
| 986 | + * @return JsonWriter Object with nested object and an array |
| 987 | + */ |
| 988 | + private JsonWriter getJsonWriterWithObjects() throws IOException { |
| 989 | + StringWriter stringWriter = new StringWriter(); |
| 990 | + JsonWriter jsonWriter = new JsonWriter(stringWriter); |
| 991 | + jsonWriter.beginObject(); |
| 992 | + jsonWriter.name("a").value(20); |
| 993 | + jsonWriter.name("age").value(30); |
| 994 | + |
| 995 | + // Start the nested "address" object |
| 996 | + jsonWriter.name("address").beginObject(); |
| 997 | + jsonWriter.name("city").value("New York"); |
| 998 | + jsonWriter.name("country").value("USA"); |
| 999 | + jsonWriter.endObject(); // End the nested "address" object |
| 1000 | + jsonWriter.name("random_prop").value(78); |
| 1001 | + // Add an array of phone numbers (list of numbers) |
| 1002 | + List<Integer> phoneNumbers = Arrays.asList(1234567890, 98989, 9909); |
| 1003 | + jsonWriter.name("phoneNumbers").beginArray(); |
| 1004 | + for (Integer phoneNumber : phoneNumbers) { |
| 1005 | + jsonWriter.value(phoneNumber); |
| 1006 | + } |
| 1007 | + jsonWriter.endArray(); // End the array |
| 1008 | + jsonWriter.endObject(); // End the outer object |
| 1009 | + return jsonWriter; |
| 1010 | + } |
976 | 1011 | } |
0 commit comments