|
30 | 30 | * Tests for JSON-Java JSONArray.java
|
31 | 31 | */
|
32 | 32 | public class JSONArrayTest {
|
33 |
| - String arrayStr = |
| 33 | + private final String arrayStr = |
34 | 34 | "["+
|
35 | 35 | "true,"+
|
36 | 36 | "false,"+
|
@@ -79,6 +79,51 @@ public void emptStr() {
|
79 | 79 | e.getMessage());
|
80 | 80 | }
|
81 | 81 | }
|
| 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 | + } |
82 | 127 |
|
83 | 128 | /**
|
84 | 129 | * Attempt to create a JSONArray with a string as object that is
|
@@ -357,7 +402,7 @@ public void length() {
|
357 | 402 | assertTrue("expected empty JSONArray length 0",
|
358 | 403 | new JSONArray().length() == 0);
|
359 | 404 | 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); |
361 | 406 | JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
|
362 | 407 | assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
|
363 | 408 | }
|
|
0 commit comments