1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #80 from johnjaylward/UnclosedJSONArray

New test to verify unclosed array
This commit is contained in:
Sean Leary 2017-11-02 20:03:00 -05:00 committed by GitHub
commit bf26eba0d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,7 +30,7 @@ import com.jayway.jsonpath.JsonPath;
* Tests for JSON-Java JSONArray.java * Tests for JSON-Java JSONArray.java
*/ */
public class JSONArrayTest { public class JSONArrayTest {
String arrayStr = private final String arrayStr =
"["+ "["+
"true,"+ "true,"+
"false,"+ "false,"+
@ -79,6 +79,51 @@ public class JSONArrayTest {
e.getMessage()); e.getMessage());
} }
} }
/**
* Attempt to create a JSONArray with an unclosed array.
* Expects an exception
*/
@Test
public void unclosedArray() {
try {
assertNull("Should throw an exception", new JSONArray("["));
} catch (JSONException e) {
assertEquals("Expected an exception message",
"Expected a ',' or ']' at 1 [character 2 line 1]",
e.getMessage());
}
}
/**
* Attempt to create a JSONArray with an unclosed array.
* Expects an exception
*/
@Test
public void unclosedArray2() {
try {
assertNull("Should throw an exception", new JSONArray("[\"test\""));
} catch (JSONException e) {
assertEquals("Expected an exception message",
"Expected a ',' or ']' at 7 [character 8 line 1]",
e.getMessage());
}
}
/**
* Attempt to create a JSONArray with an unclosed array.
* Expects an exception
*/
@Test
public void unclosedArray3() {
try {
assertNull("Should throw an exception", new JSONArray("[\"test\","));
} catch (JSONException e) {
assertEquals("Expected an exception message",
"Expected a ',' or ']' at 8 [character 9 line 1]",
e.getMessage());
}
}
/** /**
* Attempt to create a JSONArray with a string as object that is * Attempt to create a JSONArray with a string as object that is
@ -357,7 +402,7 @@ public class JSONArrayTest {
assertTrue("expected empty JSONArray length 0", assertTrue("expected empty JSONArray length 0",
new JSONArray().length() == 0); new JSONArray().length() == 0);
JSONArray jsonArray = new JSONArray(this.arrayStr); JSONArray jsonArray = new JSONArray(this.arrayStr);
assertTrue("expected JSONArray length 13", jsonArray.length() == 13); assertTrue("expected JSONArray length 13. instead found "+jsonArray.length(), jsonArray.length() == 13);
JSONArray nestedJsonArray = jsonArray.getJSONArray(9); JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1); assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
} }