Skip to content

Commit 8e48cae

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

1 file changed

Lines changed: 211 additions & 51 deletions

File tree

JSONMLTest.java

Lines changed: 211 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,93 @@
2323
*/
2424
public class JSONMLTest {
2525

26+
/**
27+
* Attempts to transform a null XML string to JSON.
28+
* Expects a NullPointerException
29+
*/
2630
@Test(expected=NullPointerException.class)
2731
public void nullXMLException() {
28-
/**
29-
* Attempts to transform a null XML string to JSON
30-
*/
3132
String xmlStr = null;
3233
JSONML.toJSONArray(xmlStr);
3334
}
3435

35-
@Test(expected=JSONException.class)
36+
/**
37+
* Attempts to transform an empty string to JSON.
38+
* Expects a JSONException
39+
*/
40+
@Test
3641
public void emptyXMLException() {
42+
String xmlStr = "";
43+
try {
44+
JSONML.toJSONArray(xmlStr);
45+
assertTrue("Expecting an exception", false);
46+
} catch (JSONException e) {
47+
assertTrue("Expecting an exception message",
48+
"Bad XML at 1 [character 2 line 1]".
49+
equals(e.getMessage()));
50+
}
51+
}
52+
53+
/**
54+
* Attempts to call JSONML.toString() with a null JSONArray.
55+
* Expects a NullPointerException.
56+
*/
57+
@Test(expected=NullPointerException.class)
58+
public void nullJSONXMLException() {
3759
/**
38-
* Attempts to transform an empty XML string to JSON
60+
* Tries to convert a null JSONArray to XML.
3961
*/
40-
String xmlStr = "";
41-
JSONML.toJSONArray(xmlStr);
62+
JSONArray jsonArray= null;
63+
JSONML.toString(jsonArray);
64+
}
65+
66+
/**
67+
* Attempts to call JSONML.toString() with a null JSONArray.
68+
* Expects a JSONException.
69+
*/
70+
@Test
71+
public void emptyJSONXMLException() {
72+
/**
73+
* Tries to convert an empty JSONArray to XML.
74+
*/
75+
JSONArray jsonArray = new JSONArray();
76+
try {
77+
JSONML.toString(jsonArray);
78+
assertTrue("Expecting an exception", false);
79+
} catch (JSONException e) {
80+
assertTrue("Expecting an exception message",
81+
"JSONArray[0] not found.".
82+
equals(e.getMessage()));
83+
}
4284
}
4385

44-
@Test(expected=JSONException.class)
86+
/**
87+
* Attempts to transform an non-XML string to JSON.
88+
* Expects a JSONException
89+
*/
90+
@Test
4591
public void nonXMLException() {
4692
/**
4793
* Attempts to transform a nonXML string to JSON
4894
*/
4995
String xmlStr = "{ \"this is\": \"not xml\"}";
50-
JSONML.toJSONArray(xmlStr);
96+
try {
97+
JSONML.toJSONArray(xmlStr);
98+
assertTrue("Expecting an exception", false);
99+
} catch (JSONException e) {
100+
assertTrue("Expecting an exception message",
101+
"Bad XML at 25 [character 26 line 1]".
102+
equals(e.getMessage()));
103+
}
51104
}
52105

53-
@Test(expected=JSONException.class)
106+
/**
107+
* Attempts to transform a JSON document with XML content that
108+
* does not follow JSONML conventions (element name is not first value
109+
* in a nested JSONArray) to a JSONArray then back to string.
110+
* Expects a JSONException
111+
*/
112+
@Test
54113
public void emptyTagException() {
55114
/**
56115
* jsonArrayStr is used to build a JSONArray which is then
@@ -70,10 +129,22 @@ public void emptyTagException() {
70129
"]"+
71130
"]";
72131
JSONArray jsonArray = new JSONArray(jsonArrayStr);
73-
JSONML.toString(jsonArray);
132+
try {
133+
JSONML.toString(jsonArray);
134+
assertTrue("Expecting an exception", false);
135+
} catch (JSONException e) {
136+
assertTrue("Expecting an exception message",
137+
"JSONArray[0] not a string.".
138+
equals(e.getMessage()));
139+
}
74140
}
75141

76-
@Test(expected=JSONException.class)
142+
/**
143+
* Attempts to transform a JSON document with XML content that
144+
* does not follow JSONML conventions (element tag has an embedded space)
145+
* to a JSONArray then back to string. Expects a JSONException
146+
*/
147+
@Test
77148
public void spaceInTagException() {
78149
/**
79150
* jsonArrayStr is used to build a JSONArray which is then
@@ -94,10 +165,22 @@ public void spaceInTagException() {
94165
"]"+
95166
"]";
96167
JSONArray jsonArray = new JSONArray(jsonArrayStr);
97-
JSONML.toString(jsonArray);
168+
try {
169+
JSONML.toString(jsonArray);
170+
assertTrue("Expecting an exception", false);
171+
} catch (JSONException e) {
172+
assertTrue("Expecting an exception message",
173+
"'addr esses' contains a space character.".
174+
equals(e.getMessage()));
175+
}
98176
}
99177

100-
@Test(expected=JSONException.class)
178+
/**
179+
* Attempts to transform a malformed XML document
180+
* (element tag has a frontslash) to a JSONArray.\
181+
* Expects a JSONException
182+
*/
183+
@Test
101184
public void invalidSlashInTagException() {
102185
/**
103186
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -113,16 +196,22 @@ public void invalidSlashInTagException() {
113196
" <street>abc street</street>\n"+
114197
" </address>\n"+
115198
"</addresses>";
116-
JSONML.toJSONArray(xmlStr);
199+
try {
200+
JSONML.toJSONArray(xmlStr);
201+
assertTrue("Expecting an exception", false);
202+
} catch (JSONException e) {
203+
assertTrue("Expecting an exception message",
204+
"Misshaped tag at 176 [character 14 line 7]".
205+
equals(e.getMessage()));
206+
}
117207
}
118208

119-
@Test(expected=JSONException.class)
209+
/**
210+
* Malformed XML text (invalid tagname) is transformed into a JSONArray.
211+
* Expects a JSONException.
212+
*/
213+
@Test
120214
public void invalidBangInTagException() {
121-
/**
122-
* xmlStr contains XML text which is transformed into a JSONArray.
123-
* In this case, the XML is invalid because an element
124-
* has the invalid name '!'.
125-
*/
126215
String xmlStr =
127216
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
128217
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
@@ -132,10 +221,21 @@ public void invalidBangInTagException() {
132221
" <!>\n"+
133222
" </address>\n"+
134223
"</addresses>";
135-
JSONML.toJSONArray(xmlStr);
224+
try {
225+
JSONML.toJSONArray(xmlStr);
226+
assertTrue("Expecting an exception", false);
227+
} catch (JSONException e) {
228+
assertTrue("Expecting an exception message",
229+
"Misshaped meta tag at 216 [character 13 line 11]".
230+
equals(e.getMessage()));
231+
}
136232
}
137233

138-
@Test(expected=JSONException.class)
234+
/**
235+
* Malformed XML text (invalid tagname, no close bracket) is transformed\
236+
* into a JSONArray. Expects a JSONException.
237+
*/
238+
@Test
139239
public void invalidBangNoCloseInTagException() {
140240
/**
141241
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -151,10 +251,21 @@ public void invalidBangNoCloseInTagException() {
151251
" <!\n"+
152252
" </address>\n"+
153253
"</addresses>";
154-
JSONML.toJSONArray(xmlStr);
254+
try {
255+
JSONML.toJSONArray(xmlStr);
256+
assertTrue("Expecting an exception", false);
257+
} catch (JSONException e) {
258+
assertTrue("Expecting an exception message",
259+
"Misshaped meta tag at 215 [character 13 line 11]".
260+
equals(e.getMessage()));
261+
}
155262
}
156263

157-
@Test(expected=JSONException.class)
264+
/**
265+
* Malformed XML text (tagname with no close bracket) is transformed\
266+
* into a JSONArray. Expects a JSONException.
267+
*/
268+
@Test
158269
public void noCloseStartTagException() {
159270
/**
160271
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -170,10 +281,21 @@ public void noCloseStartTagException() {
170281
" <abc\n"+
171282
" </address>\n"+
172283
"</addresses>";
173-
JSONML.toJSONArray(xmlStr);
284+
try {
285+
JSONML.toJSONArray(xmlStr);
286+
assertTrue("Expecting an exception", false);
287+
} catch (JSONException e) {
288+
assertTrue("Expecting an exception message",
289+
"Misplaced '<' at 194 [character 5 line 10]".
290+
equals(e.getMessage()));
291+
}
174292
}
175293

176-
@Test(expected=JSONException.class)
294+
/**
295+
* Malformed XML text (endtag with no name) is transformed\
296+
* into a JSONArray. Expects a JSONException.
297+
*/
298+
@Test
177299
public void noCloseEndTagException() {
178300
/**
179301
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -189,10 +311,21 @@ public void noCloseEndTagException() {
189311
" <abc/>\n"+
190312
" </>\n"+
191313
"</addresses>";
192-
JSONML.toJSONArray(xmlStr);
314+
try {
315+
JSONML.toJSONArray(xmlStr);
316+
assertTrue("Expecting an exception", false);
317+
} catch (JSONException e) {
318+
assertTrue("Expecting an exception message",
319+
"Expected a closing name instead of '>'.".
320+
equals(e.getMessage()));
321+
}
193322
}
194323

195-
@Test(expected=JSONException.class)
324+
/**
325+
* Malformed XML text (endtag with no close bracket) is transformed\
326+
* into a JSONArray. Expects a JSONException.
327+
*/
328+
@Test
196329
public void noCloseEndBraceException() {
197330
/**
198331
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -208,10 +341,21 @@ public void noCloseEndBraceException() {
208341
" <abc/>\n"+
209342
" </address\n"+
210343
"</addresses>";
211-
JSONML.toJSONArray(xmlStr);
344+
try {
345+
JSONML.toJSONArray(xmlStr);
346+
assertTrue("Expecting an exception", false);
347+
} catch (JSONException e) {
348+
assertTrue("Expecting an exception message",
349+
"Misplaced '<' at 206 [character 1 line 12]".
350+
equals(e.getMessage()));
351+
}
212352
}
213353

214-
@Test(expected=JSONException.class)
354+
/**
355+
* Malformed XML text (incomplete CDATA string) is transformed\
356+
* into a JSONArray. Expects a JSONException.
357+
*/
358+
@Test
215359
public void invalidCDATABangInTagException() {
216360
/**
217361
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -227,27 +371,22 @@ public void invalidCDATABangInTagException() {
227371
" <![[]>\n"+
228372
" </address>\n"+
229373
"</addresses>";
230-
JSONML.toJSONArray(xmlStr);
231-
}
232-
233-
@Test(expected=NullPointerException.class)
234-
public void nullJSONXMLException() {
235-
/**
236-
* Tries to convert a null JSONArray to XML.
237-
*/
238-
JSONArray jsonArray= null;
239-
JSONML.toString(jsonArray);
240-
}
241-
242-
@Test(expected=JSONException.class)
243-
public void emptyJSONXMLException() {
244-
/**
245-
* Tries to convert an empty JSONArray to XML.
246-
*/
247-
JSONArray jsonArray = new JSONArray();
248-
JSONML.toString(jsonArray);
374+
try {
375+
JSONML.toJSONArray(xmlStr);
376+
assertTrue("Expecting an exception", false);
377+
} catch (JSONException e) {
378+
assertTrue("Expecting an exception message",
379+
"Expected 'CDATA[' at 204 [character 11 line 9]".
380+
equals(e.getMessage()));
381+
}
249382
}
250383

384+
/**
385+
* Convert an XML document into a JSONArray, then use JSONML.toString()
386+
* to convert it into a string. This string is then converted back into
387+
* a JSONArray. Both JSONArrays are compared against a control to
388+
* confirm the contents.
389+
*/
251390
@Test
252391
public void toJSONArray() {
253392
/**
@@ -290,6 +429,20 @@ public void toJSONArray() {
290429
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
291430
}
292431

432+
/**
433+
* Convert an XML document into a JSONObject. Use JSONML.toString() to
434+
* convert it back into a string, and then re-convert it into a JSONObject.
435+
* Both JSONObjects are compared against a control JSONObject to confirm
436+
* the contents.
437+
* <p>
438+
* Next convert the XML document into a JSONArray. Use JSONML.toString() to
439+
* convert it back into a string, and then re-convert it into a JSONArray.
440+
* Both JSONArrays are compared against a control JSONArray to confirm
441+
* the contents.
442+
* <p>
443+
* This test gives a comprehensive example of how the JSONML
444+
* transformations work.
445+
*/
293446
@Test
294447
public void toJSONObjectToJSONArray() {
295448
/**
@@ -505,7 +658,14 @@ public void toJSONObjectToJSONArray() {
505658
Util.compareXML(jsonObjectXmlToStr, jsonArrayXmlToStr);
506659
}
507660

508-
661+
/**
662+
* Convert an XML document which contains embedded comments into
663+
* a JSONArray. Use JSONML.toString() to turn it into a string, then
664+
* reconvert it into a JSONArray. Compare both JSONArrays to a control
665+
* JSONArray to confirm the contents.
666+
* <p>
667+
* This test shows how XML comments are handled.
668+
*/
509669
@Test
510670
public void commentsInXML() {
511671

0 commit comments

Comments
 (0)