mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Merge pull request #969 from marilynel/master
refactored large test for strict mode
This commit is contained in:
commit
82a02d879e
@ -2316,25 +2316,8 @@ public class JSONObjectTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Explore how JSONObject handles parsing errors.
|
||||
*/
|
||||
@SuppressWarnings({"boxing", "unused"})
|
||||
@Test
|
||||
public void jsonObjectParsingErrors() {
|
||||
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());
|
||||
}
|
||||
public void parsingErrorTrailingCurlyBrace () {
|
||||
try {
|
||||
// does not end with '}'
|
||||
String str = "{";
|
||||
@ -2344,6 +2327,23 @@ public class JSONObjectTest {
|
||||
"A JSONObject text must end with '}' at 1 [character 2 line 1]",
|
||||
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 {
|
||||
// key with no ':'
|
||||
String str = "{\"myKey\" = true}";
|
||||
@ -2353,6 +2353,10 @@ public class JSONObjectTest {
|
||||
"Expected a ':' after a key at 10 [character 11 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorNoCommaSeparator() {
|
||||
try {
|
||||
// entries with no ',' separator
|
||||
String str = "{\"myKey\":true \"myOtherKey\":false}";
|
||||
@ -2362,6 +2366,10 @@ public class JSONObjectTest {
|
||||
"Expected a ',' or '}' at 15 [character 16 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorKeyIsNestedMap() {
|
||||
try {
|
||||
// key is a nested map
|
||||
String str = "{{\"foo\": \"bar\"}: \"baz\"}";
|
||||
@ -2371,6 +2379,10 @@ public class JSONObjectTest {
|
||||
"Missing value at 1 [character 2 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorKeyIsNestedArrayWithMap() {
|
||||
try {
|
||||
// key is a nested array containing a map
|
||||
String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}";
|
||||
@ -2380,24 +2392,36 @@ public class JSONObjectTest {
|
||||
"Missing value at 9 [character 10 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorKeyContainsCurlyBrace() {
|
||||
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());
|
||||
// assertEquals("Expecting an exception message",
|
||||
// "Expected a ':' after a key at 5 [character 6 line 1]",
|
||||
// e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorKeyContainsSquareBrace() {
|
||||
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());
|
||||
// assertEquals("Expecting an exception message",
|
||||
// "Expected a ':' after a key at 5 [character 6 line 1]",
|
||||
// e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorKeyContainsBinaryZero() {
|
||||
try {
|
||||
// \0 after ,
|
||||
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]",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorAppendToWrongValue() {
|
||||
try {
|
||||
// append to wrong key
|
||||
// append to wrong value
|
||||
String str = "{\"myKey\":true, \"myOtherKey\":false}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
jsonObject.append("myKey", "hello");
|
||||
@ -2418,8 +2446,12 @@ public class JSONObjectTest {
|
||||
"JSONObject[\"myKey\"] is not a JSONArray (null).",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorIncrementWrongValue() {
|
||||
try {
|
||||
// increment wrong key
|
||||
// increment wrong value
|
||||
String str = "{\"myKey\":true, \"myOtherKey\":false}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
jsonObject.increment("myKey");
|
||||
@ -2429,6 +2461,9 @@ public class JSONObjectTest {
|
||||
"Unable to increment [\"myKey\"].",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void parsingErrorInvalidKey() {
|
||||
try {
|
||||
// invalid key
|
||||
String str = "{\"myKey\":true, \"myOtherKey\":false}";
|
||||
@ -2440,6 +2475,10 @@ public class JSONObjectTest {
|
||||
"Null key.",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorNumberToString() {
|
||||
try {
|
||||
// invalid numberToString()
|
||||
JSONObject.numberToString((Number) null);
|
||||
@ -2449,7 +2488,10 @@ public class JSONObjectTest {
|
||||
"Null pointer",
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorPutOnceDuplicateKey() {
|
||||
try {
|
||||
// multiple putOnce key
|
||||
JSONObject jsonObject = new JSONObject("{}");
|
||||
@ -2459,6 +2501,10 @@ public class JSONObjectTest {
|
||||
} catch (JSONException e) {
|
||||
assertTrue("", true);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorInvalidDouble() {
|
||||
try {
|
||||
// test validity of invalid double
|
||||
JSONObject.testValidity(Double.NaN);
|
||||
@ -2466,6 +2512,10 @@ public class JSONObjectTest {
|
||||
} catch (JSONException e) {
|
||||
assertTrue("", true);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorInvalidFloat() {
|
||||
try {
|
||||
// test validity of invalid float
|
||||
JSONObject.testValidity(Float.NEGATIVE_INFINITY);
|
||||
@ -2473,6 +2523,10 @@ public class JSONObjectTest {
|
||||
} catch (JSONException e) {
|
||||
assertTrue("", true);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsingErrorDuplicateKeyException() {
|
||||
try {
|
||||
// test exception message when including a duplicate key (level 0)
|
||||
String str = "{\n"
|
||||
@ -2488,6 +2542,10 @@ public class JSONObjectTest {
|
||||
"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"
|
||||
@ -2507,6 +2565,10 @@ public class JSONObjectTest {
|
||||
"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"
|
||||
@ -2528,6 +2590,10 @@ public class JSONObjectTest {
|
||||
"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"
|
||||
@ -2548,8 +2614,13 @@ public class JSONObjectTest {
|
||||
"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
|
||||
// test exception message when including a duplicate key (level 1) holding an
|
||||
// object
|
||||
String str = "{\n"
|
||||
+ " \"attr01\":\"value-01\",\n"
|
||||
+ " \"attr02\":\"value-02\",\n"
|
||||
@ -2572,8 +2643,13 @@ public class JSONObjectTest {
|
||||
"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
|
||||
// test exception message when including a duplicate key (level 1) holding an
|
||||
// array
|
||||
String str = "{\n"
|
||||
+ " \"attr01\":\"value-01\",\n"
|
||||
+ " \"attr02\":\"value-02\",\n"
|
||||
@ -2598,8 +2674,13 @@ public class JSONObjectTest {
|
||||
"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
|
||||
// test exception message when including a duplicate key in object (level 0)
|
||||
// within an array
|
||||
String str = "[\n"
|
||||
+ " {\n"
|
||||
+ " \"attr01\":\"value-01\",\n"
|
||||
@ -2617,8 +2698,13 @@ public class JSONObjectTest {
|
||||
"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
|
||||
// test exception message when including a duplicate key in object (level 1)
|
||||
// within an array
|
||||
String str = "[\n"
|
||||
+ " {\n"
|
||||
+ " \"attr01\":\"value-01\",\n"
|
||||
@ -2643,7 +2729,6 @@ public class JSONObjectTest {
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm behavior when putOnce() is called with null parameters
|
||||
|
Loading…
x
Reference in New Issue
Block a user