Skip to content

Commit dfa37a2

Browse files
committed
Add more tests for unclosed arrays
1 parent bde6ba1 commit dfa37a2

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Tests for JSON-Java JSONArray.java
3131
*/
3232
public class JSONArrayTest {
33-
String arrayStr =
33+
private final String arrayStr =
3434
"["+
3535
"true,"+
3636
"false,"+
@@ -94,6 +94,36 @@ public void unclosedArray() {
9494
e.getMessage());
9595
}
9696
}
97+
98+
/**
99+
* Attempt to create a JSONArray with an unclosed array.
100+
* Expects an exception
101+
*/
102+
@Test
103+
public void unclosedArray2() {
104+
try {
105+
assertNull("Should throw an exception", new JSONArray("[\"test\""));
106+
} catch (JSONException e) {
107+
assertEquals("Expected an exception message",
108+
"Expected a ',' or ']' at 7 [character 8 line 1]",
109+
e.getMessage());
110+
}
111+
}
112+
113+
/**
114+
* Attempt to create a JSONArray with an unclosed array.
115+
* Expects an exception
116+
*/
117+
@Test
118+
public void unclosedArray3() {
119+
try {
120+
assertNull("Should throw an exception", new JSONArray("[\"test\","));
121+
} catch (JSONException e) {
122+
assertEquals("Expected an exception message",
123+
"Expected a ',' or ']' at 8 [character 9 line 1]",
124+
e.getMessage());
125+
}
126+
}
97127

98128
/**
99129
* Attempt to create a JSONArray with a string as object that is
@@ -372,7 +402,7 @@ public void length() {
372402
assertTrue("expected empty JSONArray length 0",
373403
new JSONArray().length() == 0);
374404
JSONArray jsonArray = new JSONArray(this.arrayStr);
375-
assertTrue("expected JSONArray length 13", jsonArray.length() == 13);
405+
assertTrue("expected JSONArray length 13. instead found "+jsonArray.length(), jsonArray.length() == 13);
376406
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
377407
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
378408
}

0 commit comments

Comments
 (0)