\n"+
"";
- JSONML.toJSONArray(xmlStr);
+ try {
+ JSONML.toJSONArray(xmlStr);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "Misplaced '<' at 194 [character 5 line 10]".
+ equals(e.getMessage()));
+ }
}
- @Test(expected=JSONException.class)
+ /**
+ * Malformed XML text (endtag with no name) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
public void noCloseEndTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -189,10 +311,21 @@ public class JSONMLTest {
" \n"+
" >\n"+
"";
- JSONML.toJSONArray(xmlStr);
+ try {
+ JSONML.toJSONArray(xmlStr);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "Expected a closing name instead of '>'.".
+ equals(e.getMessage()));
+ }
}
- @Test(expected=JSONException.class)
+ /**
+ * Malformed XML text (endtag with no close bracket) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
public void noCloseEndBraceException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -208,10 +341,21 @@ public class JSONMLTest {
" \n"+
" ";
- JSONML.toJSONArray(xmlStr);
+ try {
+ JSONML.toJSONArray(xmlStr);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "Misplaced '<' at 206 [character 1 line 12]".
+ equals(e.getMessage()));
+ }
}
- @Test(expected=JSONException.class)
+ /**
+ * Malformed XML text (incomplete CDATA string) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
public void invalidCDATABangInTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
@@ -227,27 +371,22 @@ public class JSONMLTest {
" \n"+
" \n"+
"";
- JSONML.toJSONArray(xmlStr);
- }
-
- @Test(expected=NullPointerException.class)
- public void nullJSONXMLException() {
- /**
- * Tries to convert a null JSONArray to XML.
- */
- JSONArray jsonArray= null;
- JSONML.toString(jsonArray);
- }
-
- @Test(expected=JSONException.class)
- public void emptyJSONXMLException() {
- /**
- * Tries to convert an empty JSONArray to XML.
- */
- JSONArray jsonArray = new JSONArray();
- JSONML.toString(jsonArray);
+ try {
+ JSONML.toJSONArray(xmlStr);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "Expected 'CDATA[' at 204 [character 11 line 9]".
+ equals(e.getMessage()));
+ }
}
+ /**
+ * Convert an XML document into a JSONArray, then use JSONML.toString()
+ * to convert it into a string. This string is then converted back into
+ * a JSONArray. Both JSONArrays are compared against a control to
+ * confirm the contents.
+ */
@Test
public void toJSONArray() {
/**
@@ -290,6 +429,20 @@ public class JSONMLTest {
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
}
+ /**
+ * Convert an XML document into a JSONObject. Use JSONML.toString() to
+ * convert it back into a string, and then re-convert it into a JSONObject.
+ * Both JSONObjects are compared against a control JSONObject to confirm
+ * the contents.
+ *
+ * Next convert the XML document into a JSONArray. Use JSONML.toString() to
+ * convert it back into a string, and then re-convert it into a JSONArray.
+ * Both JSONArrays are compared against a control JSONArray to confirm
+ * the contents.
+ *
+ * This test gives a comprehensive example of how the JSONML
+ * transformations work.
+ */
@Test
public void toJSONObjectToJSONArray() {
/**
@@ -505,7 +658,14 @@ public class JSONMLTest {
Util.compareXML(jsonObjectXmlToStr, jsonArrayXmlToStr);
}
-
+ /**
+ * Convert an XML document which contains embedded comments into
+ * a JSONArray. Use JSONML.toString() to turn it into a string, then
+ * reconvert it into a JSONArray. Compare both JSONArrays to a control
+ * JSONArray to confirm the contents.
+ *
+ * This test shows how XML comments are handled.
+ */
@Test
public void commentsInXML() {