Merge pull request #969 from marilynel/master

refactored large test for strict mode
This commit is contained in:
Sean Leary 2025-04-18 11:48:25 -05:00 committed by GitHub
commit 82a02d879e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2316,25 +2316,8 @@ public class JSONObjectTest {
} }
} }
/**
* Explore how JSONObject handles parsing errors.
*/
@SuppressWarnings({"boxing", "unused"})
@Test @Test
public void jsonObjectParsingErrors() { public void parsingErrorTrailingCurlyBrace () {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
if (jsonParserConfiguration.isStrictMode()) {
System.out.println("Skipping JSONObjectTest jsonObjectParsingErrors() when strictMode default is true");
} else {
try {
// does not start with '{'
String str = "abc";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"A JSONObject text must begin with '{' at 1 [character 2 line 1]",
e.getMessage());
}
try { try {
// does not end with '}' // does not end with '}'
String str = "{"; String str = "{";
@ -2344,6 +2327,23 @@ public class JSONObjectTest {
"A JSONObject text must end with '}' at 1 [character 2 line 1]", "A JSONObject text must end with '}' at 1 [character 2 line 1]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorInitialCurlyBrace() {
try {
// does not start with '{'
String str = "abc";
assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) {
assertEquals("Expecting an exception message",
"A JSONObject text must begin with '{' at 1 [character 2 line 1]",
e.getMessage());
}
}
@Test
public void parsingErrorNoColon() {
try { try {
// key with no ':' // key with no ':'
String str = "{\"myKey\" = true}"; String str = "{\"myKey\" = true}";
@ -2353,6 +2353,10 @@ public class JSONObjectTest {
"Expected a ':' after a key at 10 [character 11 line 1]", "Expected a ':' after a key at 10 [character 11 line 1]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorNoCommaSeparator() {
try { try {
// entries with no ',' separator // entries with no ',' separator
String str = "{\"myKey\":true \"myOtherKey\":false}"; String str = "{\"myKey\":true \"myOtherKey\":false}";
@ -2362,6 +2366,10 @@ public class JSONObjectTest {
"Expected a ',' or '}' at 15 [character 16 line 1]", "Expected a ',' or '}' at 15 [character 16 line 1]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorKeyIsNestedMap() {
try { try {
// key is a nested map // key is a nested map
String str = "{{\"foo\": \"bar\"}: \"baz\"}"; String str = "{{\"foo\": \"bar\"}: \"baz\"}";
@ -2371,6 +2379,10 @@ public class JSONObjectTest {
"Missing value at 1 [character 2 line 1]", "Missing value at 1 [character 2 line 1]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorKeyIsNestedArrayWithMap() {
try { try {
// key is a nested array containing a map // key is a nested array containing a map
String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}"; String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}";
@ -2380,24 +2392,36 @@ public class JSONObjectTest {
"Missing value at 9 [character 10 line 1]", "Missing value at 9 [character 10 line 1]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorKeyContainsCurlyBrace() {
try { try {
// key contains } // key contains }
String str = "{foo}: 2}"; String str = "{foo}: 2}";
assertNull("Expected an exception", new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", // assertEquals("Expecting an exception message",
"Expected a ':' after a key at 5 [character 6 line 1]", // "Expected a ':' after a key at 5 [character 6 line 1]",
e.getMessage()); // e.getMessage());
} }
}
@Test
public void parsingErrorKeyContainsSquareBrace() {
try { try {
// key contains ] // key contains ]
String str = "{foo]: 2}"; String str = "{foo]: 2}";
assertNull("Expected an exception", new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", // assertEquals("Expecting an exception message",
"Expected a ':' after a key at 5 [character 6 line 1]", // "Expected a ':' after a key at 5 [character 6 line 1]",
e.getMessage()); // e.getMessage());
} }
}
@Test
public void parsingErrorKeyContainsBinaryZero() {
try { try {
// \0 after , // \0 after ,
String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
@ -2407,8 +2431,12 @@ public class JSONObjectTest {
"A JSONObject text must end with '}' at 15 [character 16 line 1]", "A JSONObject text must end with '}' at 15 [character 16 line 1]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorAppendToWrongValue() {
try { try {
// append to wrong key // append to wrong value
String str = "{\"myKey\":true, \"myOtherKey\":false}"; String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
jsonObject.append("myKey", "hello"); jsonObject.append("myKey", "hello");
@ -2418,8 +2446,12 @@ public class JSONObjectTest {
"JSONObject[\"myKey\"] is not a JSONArray (null).", "JSONObject[\"myKey\"] is not a JSONArray (null).",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorIncrementWrongValue() {
try { try {
// increment wrong key // increment wrong value
String str = "{\"myKey\":true, \"myOtherKey\":false}"; String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
jsonObject.increment("myKey"); jsonObject.increment("myKey");
@ -2429,6 +2461,9 @@ public class JSONObjectTest {
"Unable to increment [\"myKey\"].", "Unable to increment [\"myKey\"].",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorInvalidKey() {
try { try {
// invalid key // invalid key
String str = "{\"myKey\":true, \"myOtherKey\":false}"; String str = "{\"myKey\":true, \"myOtherKey\":false}";
@ -2440,6 +2475,10 @@ public class JSONObjectTest {
"Null key.", "Null key.",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorNumberToString() {
try { try {
// invalid numberToString() // invalid numberToString()
JSONObject.numberToString((Number) null); JSONObject.numberToString((Number) null);
@ -2449,7 +2488,10 @@ public class JSONObjectTest {
"Null pointer", "Null pointer",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorPutOnceDuplicateKey() {
try { try {
// multiple putOnce key // multiple putOnce key
JSONObject jsonObject = new JSONObject("{}"); JSONObject jsonObject = new JSONObject("{}");
@ -2459,6 +2501,10 @@ public class JSONObjectTest {
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertTrue("", true);
} }
}
@Test
public void parsingErrorInvalidDouble() {
try { try {
// test validity of invalid double // test validity of invalid double
JSONObject.testValidity(Double.NaN); JSONObject.testValidity(Double.NaN);
@ -2466,6 +2512,10 @@ public class JSONObjectTest {
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertTrue("", true);
} }
}
@Test
public void parsingErrorInvalidFloat() {
try { try {
// test validity of invalid float // test validity of invalid float
JSONObject.testValidity(Float.NEGATIVE_INFINITY); JSONObject.testValidity(Float.NEGATIVE_INFINITY);
@ -2473,6 +2523,10 @@ public class JSONObjectTest {
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertTrue("", true);
} }
}
@Test
public void parsingErrorDuplicateKeyException() {
try { try {
// test exception message when including a duplicate key (level 0) // test exception message when including a duplicate key (level 0)
String str = "{\n" String str = "{\n"
@ -2488,6 +2542,10 @@ public class JSONObjectTest {
"Duplicate key \"attr03\" at 90 [character 13 line 5]", "Duplicate key \"attr03\" at 90 [character 13 line 5]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorNestedDuplicateKeyException() {
try { try {
// test exception message when including a duplicate key (level 0) holding an object // test exception message when including a duplicate key (level 0) holding an object
String str = "{\n" String str = "{\n"
@ -2507,6 +2565,10 @@ public class JSONObjectTest {
"Duplicate key \"attr03\" at 90 [character 13 line 5]", "Duplicate key \"attr03\" at 90 [character 13 line 5]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorNestedDuplicateKeyWithArrayException() {
try { try {
// test exception message when including a duplicate key (level 0) holding an array // test exception message when including a duplicate key (level 0) holding an array
String str = "{\n" String str = "{\n"
@ -2528,6 +2590,10 @@ public class JSONObjectTest {
"Duplicate key \"attr03\" at 90 [character 13 line 5]", "Duplicate key \"attr03\" at 90 [character 13 line 5]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorDuplicateKeyWithinNestedDictExceptionMessage() {
try { try {
// test exception message when including a duplicate key (level 1) // test exception message when including a duplicate key (level 1)
String str = "{\n" String str = "{\n"
@ -2548,8 +2614,13 @@ public class JSONObjectTest {
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]", "Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorDuplicateKeyDoubleNestedDictExceptionMessage() {
try { try {
// test exception message when including a duplicate key (level 1) holding an object // test exception message when including a duplicate key (level 1) holding an
// object
String str = "{\n" String str = "{\n"
+ " \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
@ -2572,8 +2643,13 @@ public class JSONObjectTest {
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]", "Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorDuplicateKeyNestedWithArrayExceptionMessage() {
try { try {
// test exception message when including a duplicate key (level 1) holding an array // test exception message when including a duplicate key (level 1) holding an
// array
String str = "{\n" String str = "{\n"
+ " \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+ " \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
@ -2598,8 +2674,13 @@ public class JSONObjectTest {
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]", "Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorDuplicateKeyWithinArrayExceptionMessage() {
try { try {
// test exception message when including a duplicate key in object (level 0) within an array // test exception message when including a duplicate key in object (level 0)
// within an array
String str = "[\n" String str = "[\n"
+ " {\n" + " {\n"
+ " \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
@ -2617,8 +2698,13 @@ public class JSONObjectTest {
"Duplicate key \"attr01\" at 124 [character 17 line 8]", "Duplicate key \"attr01\" at 124 [character 17 line 8]",
e.getMessage()); e.getMessage());
} }
}
@Test
public void parsingErrorDuplicateKeyDoubleNestedWithinArrayExceptionMessage() {
try { try {
// test exception message when including a duplicate key in object (level 1) within an array // test exception message when including a duplicate key in object (level 1)
// within an array
String str = "[\n" String str = "[\n"
+ " {\n" + " {\n"
+ " \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
@ -2643,7 +2729,6 @@ public class JSONObjectTest {
e.getMessage()); e.getMessage());
} }
} }
}
/** /**
* Confirm behavior when putOnce() is called with null parameters * Confirm behavior when putOnce() is called with null parameters