Skip to content

Commit

Permalink
Merge pull request #80 from johnjaylward/UnclosedJSONArray
Browse files Browse the repository at this point in the history
New test to verify unclosed array
  • Loading branch information
stleary authored Nov 3, 2017
2 parents 936db93 + dfa37a2 commit bf26eba
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Tests for JSON-Java JSONArray.java
*/
public class JSONArrayTest {
String arrayStr =
private final String arrayStr =
"["+
"true,"+
"false,"+
Expand Down Expand Up @@ -79,6 +79,51 @@ public void emptStr() {
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
Expand Down Expand Up @@ -357,7 +402,7 @@ public void length() {
assertTrue("expected empty JSONArray length 0",
new JSONArray().length() == 0);
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);
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
}
Expand Down

0 comments on commit bf26eba

Please sign in to comment.