22
33import static org .junit .Assert .*;
44
5+ import java .io .*;
6+
57import org .json .*;
6- import org .junit .Test ;
8+ import org .junit .*;
9+ import org .junit .rules .*;
710
811
912/**
1013 * Tests for JSON-Java XML.java
1114 * Note: noSpace() will be tested by JSONMLTest
1215 */
1316public class XMLTest {
17+ /**
18+ * JUnit supports temporary files and folders that are cleaned up after the test.
19+ * https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/
20+ */
21+ @ Rule
22+ public TemporaryFolder testFolder = new TemporaryFolder ();
1423
24+ /**
25+ * JSONObject from a null XML string.
26+ * Expects a NullPointerException
27+ */
1528 @ Test (expected =NullPointerException .class )
1629 public void shouldHandleNullXML () {
17-
1830 String xmlStr = null ;
1931 JSONObject jsonObject = XML .toJSONObject (xmlStr );
2032 assertTrue ("jsonObject should be empty" , jsonObject .length () == 0 );
2133 }
2234
35+ /**
36+ * Empty JSONObject from an empty XML string.
37+ */
2338 @ Test
2439 public void shouldHandleEmptyXML () {
2540
@@ -28,14 +43,21 @@ public void shouldHandleEmptyXML() {
2843 assertTrue ("jsonObject should be empty" , jsonObject .length () == 0 );
2944 }
3045
46+ /**
47+ * Empty JSONObject from a non-XML string.
48+ */
3149 @ Test
3250 public void shouldHandleNonXML () {
3351 String xmlStr = "{ \" this is\" : \" not xml\" }" ;
3452 JSONObject jsonObject = XML .toJSONObject (xmlStr );
3553 assertTrue ("xml string should be empty" , jsonObject .length () == 0 );
3654 }
3755
38- @ Test (expected =JSONException .class )
56+ /**
57+ * Invalid XML string (tag contains a frontslash).
58+ * Expects a JSONException
59+ */
60+ @ Test
3961 public void shouldHandleInvalidSlashInTag () {
4062 String xmlStr =
4163 "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
@@ -46,10 +68,21 @@ public void shouldHandleInvalidSlashInTag() {
4668 " <street>abc street</street>\n " +
4769 " </address>\n " +
4870 "</addresses>" ;
49- XML .toJSONObject (xmlStr );
71+ try {
72+ XML .toJSONObject (xmlStr );
73+ assertTrue ("Expecting a JSONException" , false );
74+ } catch (JSONException e ) {
75+ assertTrue ("Expecting an exception message" ,
76+ "Misshaped tag at 176 [character 14 line 5]" .
77+ equals (e .getMessage ()));
78+ }
5079 }
5180
52- @ Test (expected =JSONException .class )
81+ /**
82+ * Invalid XML string ('!' char in tag)
83+ * Expects a JSONException
84+ */
85+ @ Test
5386 public void shouldHandleInvalidBangInTag () {
5487 String xmlStr =
5588 "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
@@ -60,10 +93,21 @@ public void shouldHandleInvalidBangInTag() {
6093 " <!>\n " +
6194 " </address>\n " +
6295 "</addresses>" ;
63- XML .toJSONObject (xmlStr );
96+ try {
97+ XML .toJSONObject (xmlStr );
98+ assertTrue ("Expecting a JSONException" , false );
99+ } catch (JSONException e ) {
100+ assertTrue ("Expecting an exception message" ,
101+ "Misshaped meta tag at 215 [character 13 line 8]" .
102+ equals (e .getMessage ()));
103+ }
64104 }
65105
66- @ Test (expected =JSONException .class )
106+ /**
107+ * Invalid XML string ('!' char and no closing tag brace)
108+ * Expects a JSONException
109+ */
110+ @ Test
67111 public void shouldHandleInvalidBangNoCloseInTag () {
68112 String xmlStr =
69113 "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
@@ -74,10 +118,21 @@ public void shouldHandleInvalidBangNoCloseInTag() {
74118 " <!\n " +
75119 " </address>\n " +
76120 "</addresses>" ;
77- XML .toJSONObject (xmlStr );
121+ try {
122+ XML .toJSONObject (xmlStr );
123+ assertTrue ("Expecting a JSONException" , false );
124+ } catch (JSONException e ) {
125+ assertTrue ("Expecting an exception message" ,
126+ "Misshaped meta tag at 214 [character 13 line 8]" .
127+ equals (e .getMessage ()));
128+ }
78129 }
79130
80- @ Test (expected =JSONException .class )
131+ /**
132+ * Invalid XML string (no end brace for tag)
133+ * Expects JSONException
134+ */
135+ @ Test
81136 public void shouldHandleNoCloseStartTag () {
82137 String xmlStr =
83138 "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
@@ -88,10 +143,21 @@ public void shouldHandleNoCloseStartTag() {
88143 " <abc\n " +
89144 " </address>\n " +
90145 "</addresses>" ;
91- XML .toJSONObject (xmlStr );
146+ try {
147+ XML .toJSONObject (xmlStr );
148+ assertTrue ("Expecting a JSONException" , false );
149+ } catch (JSONException e ) {
150+ assertTrue ("Expecting an exception message" ,
151+ "Misplaced '<' at 193 [character 4 line 7]" .
152+ equals (e .getMessage ()));
153+ }
92154 }
93155
94- @ Test (expected =JSONException .class )
156+ /**
157+ * Invalid XML string (partial CDATA chars in tag name)
158+ * Expects JSONException
159+ */
160+ @ Test
95161 public void shouldHandleInvalidCDATABangInTag () {
96162 String xmlStr =
97163 "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
@@ -102,22 +168,39 @@ public void shouldHandleInvalidCDATABangInTag() {
102168 " <![[]>\n " +
103169 " </address>\n " +
104170 "</addresses>" ;
105- XML .toJSONObject (xmlStr );
171+ try {
172+ XML .toJSONObject (xmlStr );
173+ assertTrue ("Expecting a JSONException" , false );
174+ } catch (JSONException e ) {
175+ assertTrue ("Expecting an exception message" ,
176+ "Expected 'CDATA[' at 204 [character 11 line 6]" .
177+ equals (e .getMessage ()));
178+ }
106179 }
107180
181+ /**
182+ * Null JSONObject in XML.toString()
183+ * Expects NullPointerException
184+ */
108185 @ Test (expected =NullPointerException .class )
109186 public void shouldHandleNullJSONXML () {
110187 JSONObject jsonObject = null ;
111188 XML .toString (jsonObject );
112189 }
113190
191+ /**
192+ * Empty JSONObject in XML.toString()
193+ */
114194 @ Test
115195 public void shouldHandleEmptyJSONXML () {
116196 JSONObject jsonObject = new JSONObject ();
117197 String xmlStr = XML .toString (jsonObject );
118198 assertTrue ("xml string should be empty" , xmlStr .length () == 0 );
119199 }
120200
201+ /**
202+ * No SML start tag. The ending tag ends up being treated as content.
203+ */
121204 @ Test
122205 public void shouldHandleNoStartTag () {
123206 String xmlStr =
@@ -138,6 +221,9 @@ public void shouldHandleNoStartTag() {
138221 Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
139222 }
140223
224+ /**
225+ * Valid XML to JSONObject
226+ */
141227 @ Test
142228 public void shouldHandleSimpleXML () {
143229 String xmlStr =
@@ -168,12 +254,15 @@ public void shouldHandleSimpleXML() {
168254 "},\" xsi:noNamespaceSchemaLocation\" :" +
169255 "\" test.xsd\" ,\" xmlns:xsi\" :\" http://www.w3.org/2001/" +
170256 "XMLSchema-instance\" }}" ;
171-
172- JSONObject expectedJsonObject = new JSONObject ( expectedStr );
173- JSONObject jsonObject = XML . toJSONObject (xmlStr );
174- Util . compareActualVsExpectedJsonObjects ( jsonObject , expectedJsonObject );
257+
258+ compareStringToJSONObject ( xmlStr , expectedStr );
259+ compareReaderToJSONObject (xmlStr , expectedStr );
260+ compareFileToJSONObject ( xmlStr , expectedStr );
175261 }
176262
263+ /**
264+ * Valid XML with comments to JSONObject
265+ */
177266 @ Test
178267 public void shouldHandleCommentsInXML () {
179268
@@ -197,6 +286,9 @@ public void shouldHandleCommentsInXML() {
197286 Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
198287 }
199288
289+ /**
290+ * Valid XML to XML.toString()
291+ */
200292 @ Test
201293 public void shouldHandleToString () {
202294 String xmlStr =
@@ -226,6 +318,10 @@ public void shouldHandleToString() {
226318 Util .compareActualVsExpectedJsonObjects (finalJsonObject ,expectedJsonObject );
227319 }
228320
321+ /**
322+ * Converting a JSON doc containing '>' content to JSONObject, then
323+ * XML.toString() should result in valid XML.
324+ */
229325 @ Test
230326 public void shouldHandleContentNoArraytoString () {
231327 String expectedStr =
@@ -242,6 +338,11 @@ public void shouldHandleContentNoArraytoString() {
242338 finalStr +"]" , expectedFinalStr .equals (finalStr ));
243339 }
244340
341+ /**
342+ * Converting a JSON doc containing a 'content' array to JSONObject, then
343+ * XML.toString() should result in valid XML.
344+ * TODO: This is probably an error in how the 'content' keyword is used.
345+ */
245346 @ Test
246347 public void shouldHandleContentArraytoString () {
247348 String expectedStr =
@@ -259,6 +360,10 @@ public void shouldHandleContentArraytoString() {
259360 finalStr +"]" , expectedFinalStr .equals (finalStr ));
260361 }
261362
363+ /**
364+ * Converting a JSON doc containing a named array to JSONObject, then
365+ * XML.toString() should result in valid XML.
366+ */
262367 @ Test
263368 public void shouldHandleArraytoString () {
264369 String expectedStr =
@@ -276,6 +381,10 @@ public void shouldHandleArraytoString() {
276381 finalStr +"]" , expectedFinalStr .equals (finalStr ));
277382 }
278383
384+ /**
385+ * Converting a JSON doc containing a named array of nested arrays to
386+ * JSONObject, then XML.toString() should result in valid XML.
387+ */
279388 @ Test
280389 public void shouldHandleNestedArraytoString () {
281390 String xmlStr =
@@ -297,10 +406,10 @@ public void shouldHandleNestedArraytoString() {
297406
298407
299408 /**
409+ * Possible bug:
300410 * Illegal node-names must be converted to legal XML-node-names.
301411 * The given example shows 2 nodes which are valid for JSON, but not for XML.
302412 * Therefore illegal arguments should be converted to e.g. an underscore (_).
303- *
304413 */
305414 @ Test
306415 public void shouldHandleIllegalJSONNodeNames ()
@@ -322,6 +431,9 @@ public void shouldHandleIllegalJSONNodeNames()
322431 assertEquals (expected , result );
323432 }
324433
434+ /**
435+ * JSONObject with NULL value, to XML.toString()
436+ */
325437 @ Test
326438 public void shouldHandleNullNodeValue ()
327439 {
@@ -338,12 +450,11 @@ public void shouldHandleNullNodeValue()
338450 assertEquals (actualXML , resultXML );
339451 }
340452
453+ /**
454+ * Investigate exactly how the "content" keyword works
455+ */
341456 @ Test
342457 public void contentOperations () {
343- /**
344- * Make sure we understand exactly how the "content" keyword works
345- */
346-
347458 /**
348459 * When a standalone <!CDATA[...]] structure is found while parsing XML into a
349460 * JSONObject, the contents are placed in a string value with key="content".
@@ -481,4 +592,51 @@ public void contentOperations() {
481592 */
482593 assertTrue ("nothing to test here, see comment on created XML, above" , true );
483594 }
595+
596+ /**
597+ * Convenience method, given an input string and expected result,
598+ * convert to JSONBObject and compare actual to expected result.
599+ * @param xmlStr the string to parse
600+ * @param expectedStr the expected JSON string
601+ */
602+ private void compareStringToJSONObject (String xmlStr , String expectedStr ) {
603+ JSONObject expectedJsonObject = new JSONObject (expectedStr );
604+ JSONObject jsonObject = XML .toJSONObject (xmlStr );
605+ Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
606+ }
607+
608+ /**
609+ * Convenience method, given an input string and expected result,
610+ * convert to JSONBObject via reader and compare actual to expected result.
611+ * @param xmlStr the string to parse
612+ * @param expectedStr the expected JSON string
613+ */
614+ private void compareReaderToJSONObject (String xmlStr , String expectedStr ) {
615+ JSONObject expectedJsonObject = new JSONObject (expectedStr );
616+ Reader reader = new StringReader (xmlStr );
617+ JSONObject jsonObject = XML .toJSONObject (reader );
618+ Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
619+ }
620+
621+ /**
622+ * Convenience method, given an input string and expected result,
623+ * convert to JSONBObject via file and compare actual to expected result.
624+ * @param xmlStr the string to parse
625+ * @param expectedStr the expected JSON string
626+ * @throws IOException
627+ */
628+ private void compareFileToJSONObject (String xmlStr , String expectedStr ) {
629+ try {
630+ JSONObject expectedJsonObject = new JSONObject (expectedStr );
631+ File tempFile = testFolder .newFile ("fileToJSONObject.xml" );
632+ FileWriter fileWriter = new FileWriter (tempFile );
633+ fileWriter .write (xmlStr );
634+ fileWriter .close ();
635+ Reader reader = new FileReader (tempFile );
636+ JSONObject jsonObject = XML .toJSONObject (reader );
637+ Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
638+ } catch (IOException e ) {
639+ assertTrue ("file writer error: " +e .getMessage (), false );
640+ }
641+ }
484642}
0 commit comments