Skip to content

Commit 4a3bbed

Browse files
committed
Verify exception messages. Move method comments so JavaDoc will pick them up.
1 parent 41bfdad commit 4a3bbed

1 file changed

Lines changed: 119 additions & 32 deletions

File tree

JSONArrayTest.java

Lines changed: 119 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,59 @@ public class JSONArrayTest {
3636
"\"-1\""+
3737
"]";
3838

39+
/**
40+
* Attempt to create a JSONArray with a null string.
41+
* Expects a NullPointerException.
42+
*/
3943
@Test(expected=NullPointerException.class)
4044
public void nullException() {
4145
String str = null;
4246
new JSONArray(str);
4347
}
4448

45-
@Test(expected=JSONException.class)
49+
/**
50+
* Attempt to create a JSONArray with an empty string.
51+
* Expects a JSONException.
52+
*/
53+
@Test
4654
public void emptStr() {
4755
String str = "";
48-
new JSONArray(str);
56+
try {
57+
new JSONArray(str);
58+
assertTrue("Should throw an exception", false);
59+
} catch (JSONException e) {
60+
assertTrue("Expected an exception message",
61+
"A JSONArray text must start with '[' at 1 [character 2 line 1]".
62+
equals(e.getMessage()));
63+
}
4964
}
5065

51-
@Test(expected=JSONException.class)
66+
/**
67+
* Attempt to create a JSONArray with a string as object that is
68+
* not a JSON array doc.
69+
* Expects a JSONException.
70+
*/
71+
@Test
5272
public void badObject() {
5373
String str = "abc";
54-
new JSONArray((Object)str);
74+
try {
75+
new JSONArray((Object)str);
76+
assertTrue("Should throw an exception", false);
77+
} catch (JSONException e) {
78+
assertTrue("Expected an exception message",
79+
"JSONArray initial value should be a string or collection or array.".
80+
equals(e.getMessage()));
81+
}
5582
}
5683

84+
/**
85+
* Create a JSONArray doc with a variety of different elements.
86+
* Confirm that the values can be accessed via the get[type]() API methods
87+
*/
5788
@Test
5889
public void getArrayValues() {
5990
JSONArray jsonArray = new JSONArray(arrayStr);
91+
// booleans
6092
assertTrue("Array true",
6193
true == jsonArray.getBoolean(0));
6294
assertTrue("Array false",
@@ -65,83 +97,105 @@ public void getArrayValues() {
6597
true == jsonArray.getBoolean(2));
6698
assertTrue("Array string false",
6799
false == jsonArray.getBoolean(3));
68-
100+
// strings
101+
assertTrue("Array value string",
102+
"hello".equals(jsonArray.getString(4)));
103+
// doubles
69104
assertTrue("Array double",
70105
new Double(23.45e-4).equals(jsonArray.getDouble(5)));
71106
assertTrue("Array string double",
72107
new Double(23.45).equals(jsonArray.getDouble(6)));
73-
108+
// ints
74109
assertTrue("Array value int",
75110
new Integer(42).equals(jsonArray.getInt(7)));
76111
assertTrue("Array value string int",
77112
new Integer(43).equals(jsonArray.getInt(8)));
78-
113+
// nested objects
79114
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
80115
assertTrue("Array value JSONArray", nestedJsonArray != null);
81-
82116
JSONObject nestedJsonObject = jsonArray.getJSONObject(10);
83117
assertTrue("Array value JSONObject", nestedJsonObject != null);
84-
118+
// longs
85119
assertTrue("Array value long",
86120
new Long(0).equals(jsonArray.getLong(11)));
87121
assertTrue("Array value string long",
88122
new Long(-1).equals(jsonArray.getLong(12)));
89123

90-
assertTrue("Array value string",
91-
"hello".equals(jsonArray.getString(4)));
92-
93124
assertTrue("Array value null", jsonArray.isNull(-1));
94125
}
95126

127+
/**
128+
* Create a JSONArray doc with a variety of different elements.
129+
* Confirm that attempting to get the wrong types via the get[type]()
130+
* API methods result in JSONExceptions
131+
*/
96132
@Test
97133
public void failedGetArrayValues() {
98-
int tryCount = 0;
99-
int exceptionCount = 0;
100134
JSONArray jsonArray = new JSONArray(arrayStr);
101135
try {
102-
tryCount++;
103136
jsonArray.getBoolean(4);
104137
assertTrue("expected getBoolean to fail", false);
105-
} catch (JSONException ignored) { exceptionCount++; }
138+
} catch (JSONException e) {
139+
assertTrue("Expected an exception message",
140+
"JSONArray[4] is not a boolean.".equals(e.getMessage()));
141+
}
106142
try {
107-
tryCount++;
108143
jsonArray.get(-1);
109144
assertTrue("expected get to fail", false);
110-
} catch (JSONException ignored) { exceptionCount++; }
145+
} catch (JSONException e) {
146+
assertTrue("Expected an exception message",
147+
"JSONArray[-1] not found.".equals(e.getMessage()));
148+
}
111149
try {
112-
tryCount++;
113150
jsonArray.getDouble(4);
114151
assertTrue("expected getDouble to fail", false);
115-
} catch (JSONException ignored) { exceptionCount++; }
152+
} catch (JSONException e) {
153+
assertTrue("Expected an exception message",
154+
"JSONArray[4] is not a number.".equals(e.getMessage()));
155+
}
116156
try {
117-
tryCount++;
118157
jsonArray.getInt(4);
119158
assertTrue("expected getInt to fail", false);
120-
} catch (JSONException ignored) { exceptionCount++; }
159+
} catch (JSONException e) {
160+
assertTrue("Expected an exception message",
161+
"JSONArray[4] is not a number.".equals(e.getMessage()));
162+
}
121163
try {
122-
tryCount++;
123164
jsonArray.getJSONArray(4);
124165
assertTrue("expected getJSONArray to fail", false);
125-
} catch (JSONException ignored) { exceptionCount++; }
166+
} catch (JSONException e) {
167+
assertTrue("Expected an exception message",
168+
"JSONArray[4] is not a JSONArray.".equals(e.getMessage()));
169+
}
126170
try {
127-
tryCount++;
128171
jsonArray.getJSONObject(4);
129172
assertTrue("expected getJSONObject to fail", false);
130-
} catch (JSONException ignored) { exceptionCount++; }
173+
} catch (JSONException e) {
174+
assertTrue("Expected an exception message",
175+
"JSONArray[4] is not a JSONObject.".equals(e.getMessage()));
176+
}
131177
try {
132-
tryCount++;
133178
jsonArray.getLong(4);
134179
assertTrue("expected getLong to fail", false);
135-
} catch (JSONException ignored) { exceptionCount++; }
180+
} catch (JSONException e) {
181+
assertTrue("Expected an exception message",
182+
"JSONArray[4] is not a number.".equals(e.getMessage()));
183+
}
136184
try {
137-
tryCount++;
138185
jsonArray.getString(5);
139186
assertTrue("expected getString to fail", false);
140-
} catch (JSONException ignored) { exceptionCount++; }
141-
assertTrue("tryCount should match exceptionCount",
142-
tryCount == exceptionCount);
187+
} catch (JSONException e) {
188+
assertTrue("Expected an exception message",
189+
"JSONArray[5] not a string.".equals(e.getMessage()));
190+
}
143191
}
144192

193+
/**
194+
* Exercise JSONArray.join() by converting a JSONArray into a
195+
* comma-separated string. Since this is very nearly a JSON document,
196+
* array braces are added to the beginning and end, and it is reconverted
197+
* back to a JSONArray for comparison.
198+
*/
145199
@Test
146200
public void join() {
147201
String expectedStr =
@@ -176,6 +230,9 @@ public void join() {
176230
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
177231
}
178232

233+
/**
234+
* Confirm the JSONArray.length() method
235+
*/
179236
@Test
180237
public void length() {
181238
assertTrue("expected empty JSONArray length 0",
@@ -186,6 +243,11 @@ public void length() {
186243
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
187244
}
188245

246+
/**
247+
* Create a JSONArray doc with a variety of different elements.
248+
* Confirm that the values can be accessed via the opt[type](index)
249+
* and opt[type](index, default) API methods.
250+
*/
189251
@Test
190252
public void opt() {
191253
JSONArray jsonArray = new JSONArray(arrayStr);
@@ -238,6 +300,10 @@ public void opt() {
238300
"".equals(jsonArray.optString(-1)));
239301
}
240302

303+
/**
304+
* Exercise the JSONArray.put(value) method with various parameters
305+
* and confirm the resulting JSONArray.
306+
*/
241307
@Test
242308
public void put() {
243309
String expectedStr =
@@ -314,6 +380,10 @@ public void put() {
314380
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
315381
}
316382

383+
/**
384+
* Exercise the JSONArray.put(index, value) method with various parameters
385+
* and confirm the resulting JSONArray.
386+
*/
317387
@Test
318388
public void putIndex() {
319389
String expectedStr =
@@ -393,6 +463,10 @@ public void putIndex() {
393463
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
394464
}
395465

466+
/**
467+
* Exercise the JSONArray.remove(index) method
468+
* and confirm the resulting JSONArray.
469+
*/
396470
@Test
397471
public void remove() {
398472
String arrayStr =
@@ -406,6 +480,10 @@ public void remove() {
406480
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
407481
}
408482

483+
/**
484+
* Exercise the JSONArray.similar() method with various parameters
485+
* and confirm the results when not similar.
486+
*/
409487
@Test
410488
public void notSimilar() {
411489
String arrayStr =
@@ -441,6 +519,9 @@ public void notSimilar() {
441519
!jsonArray.similar(otherJsonArray));
442520
}
443521

522+
/**
523+
* Convert an empty JSONArray to JSONObject
524+
*/
444525
@Test
445526
public void toJSONObject() {
446527
JSONArray names = new JSONArray();
@@ -449,6 +530,9 @@ public void toJSONObject() {
449530
null == jsonArray.toJSONObject(names));
450531
}
451532

533+
/**
534+
* Confirm the creation of a JSONArray from an array of ints
535+
*/
452536
@Test
453537
public void objectArrayVsIsArray() {
454538
String expectedStr =
@@ -462,6 +546,9 @@ public void objectArrayVsIsArray() {
462546
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
463547
}
464548

549+
/**
550+
* Exercise the JSONArray iterator.
551+
*/
465552
@Test
466553
public void iterator() {
467554
JSONArray jsonArray = new JSONArray(arrayStr);

0 commit comments

Comments
 (0)