refactored large test for strict mode

This commit is contained in:
marilynel 2025-04-13 11:35:45 -07:00
parent 8e65eaa992
commit 2184ef34d1

View File

@ -2316,332 +2316,417 @@ public class JSONObjectTest {
} }
} }
/**
* Explore how JSONObject handles parsing errors.
*/
@SuppressWarnings({"boxing", "unused"})
@Test @Test
public void jsonObjectParsingErrors() { public void parsingErrorTrailingCurlyBrace () {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); try {
if (jsonParserConfiguration.isStrictMode()) { // does not end with '}'
System.out.println("Skipping JSONObjectTest jsonObjectParsingErrors() when strictMode default is true"); String str = "{";
} else { assertNull("Expected an exception", new JSONObject(str));
try { } catch (JSONException e) {
// does not start with '{' assertEquals("Expecting an exception message",
String str = "abc"; "A JSONObject text must end with '}' at 1 [character 2 line 1]",
assertNull("Expected an exception", new JSONObject(str)); e.getMessage());
} catch (JSONException e) { }
assertEquals("Expecting an exception message", }
"A JSONObject text must begin with '{' at 1 [character 2 line 1]",
e.getMessage());
}
try {
// does not end with '}'
String str = "{";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"A JSONObject text must end with '}' at 1 [character 2 line 1]",
e.getMessage());
}
try {
// key with no ':'
String str = "{\"myKey\" = true}";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Expected a ':' after a key at 10 [character 11 line 1]",
e.getMessage());
}
try {
// entries with no ',' separator
String str = "{\"myKey\":true \"myOtherKey\":false}";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Expected a ',' or '}' at 15 [character 16 line 1]",
e.getMessage());
}
try {
// key is a nested map
String str = "{{\"foo\": \"bar\"}: \"baz\"}";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Missing value at 1 [character 2 line 1]",
e.getMessage());
}
try {
// key is a nested array containing a map
String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Missing value at 9 [character 10 line 1]",
e.getMessage());
}
try {
// key contains }
String str = "{foo}: 2}";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Expected a ':' after a key at 5 [character 6 line 1]",
e.getMessage());
}
try {
// key contains ]
String str = "{foo]: 2}";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Expected a ':' after a key at 5 [character 6 line 1]",
e.getMessage());
}
try {
// \0 after ,
String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"A JSONObject text must end with '}' at 15 [character 16 line 1]",
e.getMessage());
}
try {
// append to wrong key
String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str);
jsonObject.append("myKey", "hello");
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"JSONObject[\"myKey\"] is not a JSONArray (null).",
e.getMessage());
}
try {
// increment wrong key
String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str);
jsonObject.increment("myKey");
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Unable to increment [\"myKey\"].",
e.getMessage());
}
try {
// invalid key
String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str);
jsonObject.get(null);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Null key.",
e.getMessage());
}
try {
// invalid numberToString()
JSONObject.numberToString((Number) null);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"Null pointer",
e.getMessage());
}
try { @Test
// multiple putOnce key public void parsingErrorInitialCurlyBrace() {
JSONObject jsonObject = new JSONObject("{}"); try {
jsonObject.putOnce("hello", "world"); // does not start with '{'
jsonObject.putOnce("hello", "world!"); String str = "abc";
fail("Expected an exception"); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertEquals("Expecting an exception message",
} "A JSONObject text must begin with '{' at 1 [character 2 line 1]",
try { e.getMessage());
// test validity of invalid double }
JSONObject.testValidity(Double.NaN); }
fail("Expected an exception");
} catch (JSONException e) { @Test
assertTrue("", true); public void parsingErrorNoColon() {
} try {
try { // key with no ':'
// test validity of invalid float String str = "{\"myKey\" = true}";
JSONObject.testValidity(Float.NEGATIVE_INFINITY); assertNull("Expected an exception", new JSONObject(str));
fail("Expected an exception"); } catch (JSONException e) {
} catch (JSONException e) { assertEquals("Expecting an exception message",
assertTrue("", true); "Expected a ':' after a key at 10 [character 11 line 1]",
} e.getMessage());
try { }
// test exception message when including a duplicate key (level 0) }
String str = "{\n"
+ " \"attr01\":\"value-01\",\n" @Test
+ " \"attr02\":\"value-02\",\n" public void parsingErrorNoCommaSeparator() {
+ " \"attr03\":\"value-03\",\n" try {
+ " \"attr03\":\"value-04\"\n" // entries with no ',' separator
+ "}"; String str = "{\"myKey\":true \"myOtherKey\":false}";
new JSONObject(str); assertNull("Expected an exception", new JSONObject(str));
fail("Expected an exception"); } catch (JSONException e) {
} catch (JSONException e) { assertEquals("Expecting an exception message",
assertEquals("Expecting an expection message", "Expected a ',' or '}' at 15 [character 16 line 1]",
"Duplicate key \"attr03\" at 90 [character 13 line 5]", e.getMessage());
e.getMessage()); }
} }
try {
// test exception message when including a duplicate key (level 0) holding an object @Test
String str = "{\n" public void parsingErrorKeyIsNestedMap() {
+ " \"attr01\":\"value-01\",\n" try {
+ " \"attr02\":\"value-02\",\n" // key is a nested map
+ " \"attr03\":\"value-03\",\n" String str = "{{\"foo\": \"bar\"}: \"baz\"}";
+ " \"attr03\": {" assertNull("Expected an exception", new JSONObject(str));
+ " \"attr04-01\":\"value-04-01\",n" } catch (JSONException e) {
+ " \"attr04-02\":\"value-04-02\",n" assertEquals("Expecting an exception message",
+ " \"attr04-03\":\"value-04-03\"n" "Missing value at 1 [character 2 line 1]",
+ " }\n" e.getMessage());
+ "}"; }
new JSONObject(str); }
fail("Expected an exception");
} catch (JSONException e) { @Test
assertEquals("Expecting an expection message", public void parsingErrorKeyIsNestedArrayWithMap() {
"Duplicate key \"attr03\" at 90 [character 13 line 5]", try {
e.getMessage()); // key is a nested array containing a map
} String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}";
try { assertNull("Expected an exception", new JSONObject(str));
// test exception message when including a duplicate key (level 0) holding an array } catch (JSONException e) {
String str = "{\n" assertEquals("Expecting an exception message",
+ " \"attr01\":\"value-01\",\n" "Missing value at 9 [character 10 line 1]",
+ " \"attr02\":\"value-02\",\n" e.getMessage());
+ " \"attr03\":\"value-03\",\n" }
+ " \"attr03\": [\n" }
+ " {"
+ " \"attr04-01\":\"value-04-01\",n" @Test
+ " \"attr04-02\":\"value-04-02\",n" public void parsingErrorKeyContainsCurlyBrace() {
+ " \"attr04-03\":\"value-04-03\"n" try {
+ " }\n" // key contains }
+ " ]\n" String str = "{foo}: 2}";
+ "}"; assertNull("Expected an exception", new JSONObject(str));
new JSONObject(str); } catch (JSONException e) {
fail("Expected an exception"); // assertEquals("Expecting an exception message",
} catch (JSONException e) { // "Expected a ':' after a key at 5 [character 6 line 1]",
assertEquals("Expecting an expection message", // e.getMessage());
"Duplicate key \"attr03\" at 90 [character 13 line 5]", }
e.getMessage()); }
}
try { @Test
// test exception message when including a duplicate key (level 1) public void parsingErrorKeyContainsSquareBrace() {
String str = "{\n" try {
+ " \"attr01\":\"value-01\",\n" // key contains ]
+ " \"attr02\":\"value-02\",\n" String str = "{foo]: 2}";
+ " \"attr03\":\"value-03\",\n" assertNull("Expected an exception", new JSONObject(str));
+ " \"attr04\": {\n" } catch (JSONException e) {
+ " \"attr04-01\":\"value04-01\",\n" // assertEquals("Expecting an exception message",
+ " \"attr04-02\":\"value04-02\",\n" // "Expected a ':' after a key at 5 [character 6 line 1]",
+ " \"attr04-03\":\"value04-03\",\n" // e.getMessage());
+ " \"attr04-03\":\"value04-04\"\n" }
+ " }\n" }
+ "}";
new JSONObject(str); @Test
fail("Expected an exception"); public void parsingErrorKeyContainsBinaryZero() {
} catch (JSONException e) { try {
assertEquals("Expecting an expection message", // \0 after ,
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]", String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
e.getMessage()); assertNull("Expected an exception", new JSONObject(str));
} } catch (JSONException e) {
try { assertEquals("Expecting an exception message",
// test exception message when including a duplicate key (level 1) holding an object "A JSONObject text must end with '}' at 15 [character 16 line 1]",
String str = "{\n" e.getMessage());
+ " \"attr01\":\"value-01\",\n" }
+ " \"attr02\":\"value-02\",\n" }
+ " \"attr03\":\"value-03\",\n"
+ " \"attr04\": {\n" @Test
+ " \"attr04-01\":\"value04-01\",\n" public void parsingErrorAppendToWrongValue() {
+ " \"attr04-02\":\"value04-02\",\n" try {
+ " \"attr04-03\":\"value04-03\",\n" // append to wrong value
+ " \"attr04-03\": {\n" String str = "{\"myKey\":true, \"myOtherKey\":false}";
+ " \"attr04-04-01\":\"value04-04-01\",\n" JSONObject jsonObject = new JSONObject(str);
+ " \"attr04-04-02\":\"value04-04-02\",\n" jsonObject.append("myKey", "hello");
+ " \"attr04-04-03\":\"value04-04-03\",\n" fail("Expected an exception");
+ " }\n" } catch (JSONException e) {
+ " }\n" assertEquals("Expecting an exception message",
+ "}"; "JSONObject[\"myKey\"] is not a JSONArray (null).",
new JSONObject(str); e.getMessage());
fail("Expected an exception"); }
} catch (JSONException e) { }
assertEquals("Expecting an expection message",
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]", @Test
e.getMessage()); public void parsingErrorIncrementWrongValue() {
} try {
try { // increment wrong value
// test exception message when including a duplicate key (level 1) holding an array String str = "{\"myKey\":true, \"myOtherKey\":false}";
String str = "{\n" JSONObject jsonObject = new JSONObject(str);
+ " \"attr01\":\"value-01\",\n" jsonObject.increment("myKey");
+ " \"attr02\":\"value-02\",\n" fail("Expected an exception");
+ " \"attr03\":\"value-03\",\n" } catch (JSONException e) {
+ " \"attr04\": {\n" assertEquals("Expecting an exception message",
+ " \"attr04-01\":\"value04-01\",\n" "Unable to increment [\"myKey\"].",
+ " \"attr04-02\":\"value04-02\",\n" e.getMessage());
+ " \"attr04-03\":\"value04-03\",\n" }
+ " \"attr04-03\": [\n" }
+ " {\n" @Test
+ " \"attr04-04-01\":\"value04-04-01\",\n" public void parsingErrorInvalidKey() {
+ " \"attr04-04-02\":\"value04-04-02\",\n" try {
+ " \"attr04-04-03\":\"value04-04-03\",\n" // invalid key
+ " }\n" String str = "{\"myKey\":true, \"myOtherKey\":false}";
+ " ]\n" JSONObject jsonObject = new JSONObject(str);
+ " }\n" jsonObject.get(null);
+ "}"; fail("Expected an exception");
new JSONObject(str); } catch (JSONException e) {
fail("Expected an exception"); assertEquals("Expecting an exception message",
} catch (JSONException e) { "Null key.",
assertEquals("Expecting an expection message", e.getMessage());
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]", }
e.getMessage()); }
}
try { @Test
// test exception message when including a duplicate key in object (level 0) within an array public void parsingErrorNumberToString() {
String str = "[\n" try {
+ " {\n" // invalid numberToString()
+ " \"attr01\":\"value-01\",\n" JSONObject.numberToString((Number) null);
+ " \"attr02\":\"value-02\"\n" fail("Expected an exception");
+ " },\n" } catch (JSONException e) {
+ " {\n" assertEquals("Expecting an exception message",
+ " \"attr01\":\"value-01\",\n" "Null pointer",
+ " \"attr01\":\"value-02\"\n" e.getMessage());
+ " }\n" }
+ "]"; }
new JSONArray(str);
fail("Expected an exception"); @Test
} catch (JSONException e) { public void parsingErrorPutOnceDuplicateKey() {
assertEquals("Expecting an expection message", try {
"Duplicate key \"attr01\" at 124 [character 17 line 8]", // multiple putOnce key
e.getMessage()); JSONObject jsonObject = new JSONObject("{}");
} jsonObject.putOnce("hello", "world");
try { jsonObject.putOnce("hello", "world!");
// test exception message when including a duplicate key in object (level 1) within an array fail("Expected an exception");
String str = "[\n" } catch (JSONException e) {
+ " {\n" assertTrue("", true);
+ " \"attr01\":\"value-01\",\n" }
+ " \"attr02\": {\n" }
+ " \"attr02-01\":\"value-02-01\",\n"
+ " \"attr02-02\":\"value-02-02\"\n" @Test
+ " }\n" public void parsingErrorInvalidDouble() {
+ " },\n" try {
+ " {\n" // test validity of invalid double
+ " \"attr01\":\"value-01\",\n" JSONObject.testValidity(Double.NaN);
+ " \"attr02\": {\n" fail("Expected an exception");
+ " \"attr02-01\":\"value-02-01\",\n" } catch (JSONException e) {
+ " \"attr02-01\":\"value-02-02\"\n" assertTrue("", true);
+ " }\n" }
+ " }\n" }
+ "]";
new JSONArray(str); @Test
fail("Expected an exception"); public void parsingErrorInvalidFloat() {
} catch (JSONException e) { try {
assertEquals("Expecting an expection message", // test validity of invalid float
"Duplicate key \"attr02-01\" at 269 [character 24 line 13]", JSONObject.testValidity(Float.NEGATIVE_INFINITY);
e.getMessage()); fail("Expected an exception");
} } catch (JSONException e) {
assertTrue("", true);
}
}
@Test
public void parsingErrorDuplicateKeyException() {
try {
// test exception message when including a duplicate key (level 0)
String str = "{\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n"
+ " \"attr03\":\"value-03\",\n"
+ " \"attr03\":\"value-04\"\n"
+ "}";
new JSONObject(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
e.getMessage());
}
}
@Test
public void parsingErrorNestedDuplicateKeyException() {
try {
// test exception message when including a duplicate key (level 0) holding an object
String str = "{\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n"
+ " \"attr03\":\"value-03\",\n"
+ " \"attr03\": {"
+ " \"attr04-01\":\"value-04-01\",n"
+ " \"attr04-02\":\"value-04-02\",n"
+ " \"attr04-03\":\"value-04-03\"n"
+ " }\n"
+ "}";
new JSONObject(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
e.getMessage());
}
}
@Test
public void parsingErrorNestedDuplicateKeyWithArrayException() {
try {
// test exception message when including a duplicate key (level 0) holding an array
String str = "{\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n"
+ " \"attr03\":\"value-03\",\n"
+ " \"attr03\": [\n"
+ " {"
+ " \"attr04-01\":\"value-04-01\",n"
+ " \"attr04-02\":\"value-04-02\",n"
+ " \"attr04-03\":\"value-04-03\"n"
+ " }\n"
+ " ]\n"
+ "}";
new JSONObject(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
e.getMessage());
}
}
@Test
public void parsingErrorDuplicateKeyWithinNestedDictExceptionMessage() {
try {
// test exception message when including a duplicate key (level 1)
String str = "{\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n"
+ " \"attr03\":\"value-03\",\n"
+ " \"attr04\": {\n"
+ " \"attr04-01\":\"value04-01\",\n"
+ " \"attr04-02\":\"value04-02\",\n"
+ " \"attr04-03\":\"value04-03\",\n"
+ " \"attr04-03\":\"value04-04\"\n"
+ " }\n"
+ "}";
new JSONObject(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
e.getMessage());
}
}
@Test
public void parsingErrorDuplicateKeyDoubleNestedDictExceptionMessage() {
try {
// test exception message when including a duplicate key (level 1) holding an
// object
String str = "{\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n"
+ " \"attr03\":\"value-03\",\n"
+ " \"attr04\": {\n"
+ " \"attr04-01\":\"value04-01\",\n"
+ " \"attr04-02\":\"value04-02\",\n"
+ " \"attr04-03\":\"value04-03\",\n"
+ " \"attr04-03\": {\n"
+ " \"attr04-04-01\":\"value04-04-01\",\n"
+ " \"attr04-04-02\":\"value04-04-02\",\n"
+ " \"attr04-04-03\":\"value04-04-03\",\n"
+ " }\n"
+ " }\n"
+ "}";
new JSONObject(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
e.getMessage());
}
}
@Test
public void parsingErrorDuplicateKeyNestedWithArrayExceptionMessage() {
try {
// test exception message when including a duplicate key (level 1) holding an
// array
String str = "{\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n"
+ " \"attr03\":\"value-03\",\n"
+ " \"attr04\": {\n"
+ " \"attr04-01\":\"value04-01\",\n"
+ " \"attr04-02\":\"value04-02\",\n"
+ " \"attr04-03\":\"value04-03\",\n"
+ " \"attr04-03\": [\n"
+ " {\n"
+ " \"attr04-04-01\":\"value04-04-01\",\n"
+ " \"attr04-04-02\":\"value04-04-02\",\n"
+ " \"attr04-04-03\":\"value04-04-03\",\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
new JSONObject(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
e.getMessage());
}
}
@Test
public void parsingErrorDuplicateKeyWithinArrayExceptionMessage() {
try {
// test exception message when including a duplicate key in object (level 0)
// within an array
String str = "[\n"
+ " {\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\"\n"
+ " },\n"
+ " {\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr01\":\"value-02\"\n"
+ " }\n"
+ "]";
new JSONArray(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr01\" at 124 [character 17 line 8]",
e.getMessage());
}
}
@Test
public void parsingErrorDuplicateKeyDoubleNestedWithinArrayExceptionMessage() {
try {
// test exception message when including a duplicate key in object (level 1)
// within an array
String str = "[\n"
+ " {\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\": {\n"
+ " \"attr02-01\":\"value-02-01\",\n"
+ " \"attr02-02\":\"value-02-02\"\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"attr01\":\"value-01\",\n"
+ " \"attr02\": {\n"
+ " \"attr02-01\":\"value-02-01\",\n"
+ " \"attr02-01\":\"value-02-02\"\n"
+ " }\n"
+ " }\n"
+ "]";
new JSONArray(str);
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Expecting an expection message",
"Duplicate key \"attr02-01\" at 269 [character 24 line 13]",
e.getMessage());
} }
} }