Skip to content

Commit bf26eba

Browse files
authored
Merge pull request #80 from johnjaylward/UnclosedJSONArray
New test to verify unclosed array
2 parents 936db93 + dfa37a2 commit bf26eba

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

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

Lines changed: 47 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,"+
@@ -79,6 +79,51 @@ public void emptStr() {
7979
e.getMessage());
8080
}
8181
}
82+
83+
/**
84+
* Attempt to create a JSONArray with an unclosed array.
85+
* Expects an exception
86+
*/
87+
@Test
88+
public void unclosedArray() {
89+
try {
90+
assertNull("Should throw an exception", new JSONArray("["));
91+
} catch (JSONException e) {
92+
assertEquals("Expected an exception message",
93+
"Expected a ',' or ']' at 1 [character 2 line 1]",
94+
e.getMessage());
95+
}
96+
}
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+
}
82127

83128
/**
84129
* Attempt to create a JSONArray with a string as object that is
@@ -357,7 +402,7 @@ public void length() {
357402
assertTrue("expected empty JSONArray length 0",
358403
new JSONArray().length() == 0);
359404
JSONArray jsonArray = new JSONArray(this.arrayStr);
360-
assertTrue("expected JSONArray length 13", jsonArray.length() == 13);
405+
assertTrue("expected JSONArray length 13. instead found "+jsonArray.length(), jsonArray.length() == 13);
361406
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
362407
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
363408
}

0 commit comments

Comments
 (0)