From bc07b5196b5a90cd0bfdbf62f669683ef5b96790 Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 27 Mar 2015 17:52:41 -0500 Subject: [PATCH] starting jsonml test --- JSONMLTest.java | 297 ++++++++++++++++++++++++++++++++++++++++++++ JunitTestSuite.java | 3 +- 2 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 JSONMLTest.java diff --git a/JSONMLTest.java b/JSONMLTest.java new file mode 100644 index 0000000..ca7c2d1 --- /dev/null +++ b/JSONMLTest.java @@ -0,0 +1,297 @@ +package org.json.junit; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java JSONML.java + */ +public class JSONMLTest { + + @Test(expected=NullPointerException.class) + public void shouldHandleNullXML() { + + String xmlStr = null; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test(expected=JSONException.class) + public void shouldHandleEmptyXML() { + + String xmlStr = ""; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test + public void shouldHandleNonXML() { + String xmlStr = "{ \"this is\": \"not xml\"}"; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + assertTrue("xml string should be empty", jsonObject.length() == 0); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidSlashInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " abc street\n"+ + "
\n"+ + "
"; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidBangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + "
\n"+ + "
"; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidBangNoCloseInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleNoCloseStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidCDATABangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " \n"+ + "
\n"+ + "
"; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=NullPointerException.class) + public void shouldHandleNullJSONXML() { + JSONObject jsonObject= null; + JSONML.toString(jsonObject); + } + + @Test + public void shouldHandleEmptyJSONXML() { + JSONObject jsonObject= new JSONObject(); + String xmlStr = JSONML.toString(jsonObject); + assertTrue("xml string should be empty", xmlStr.length() == 0); + } + + @Test + public void shouldHandleNoStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " >\n"+ + "
\n"+ + "
"; + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleSimpleXML() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " [CDATA[Baker street 5]\n"+ + " \n"+ + " true\n"+ + " false\n"+ + " null\n"+ + " 42\n"+ + " -23\n"+ + " -23.45\n"+ + " -23x.45\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+ + "\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+ + "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+ + "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject expectedJsonObject = new JSONObject(expectedStr); + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleCommentsInXML() { + + String xmlStr = + "\n"+ + "\n"+ + "\n"+ + "
\n"+ + " comment ]]>\n"+ + " Joe Tester\n"+ + " \n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+ + "street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+ + " comment \"}}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleToString() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " [CDATA[Joe & T > e < s " t ' er]]\n"+ + " Baker street 5\n"+ + " 1, 2, 3, 4.1, 5.2\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+ + "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ + "},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + String xmlToStr = JSONML.toString(jsonObject); + JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + + @Test + public void shouldHandleContentNoArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = JSONML.toString(expectedJsonObject); + String expectedFinalStr = "
>"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleContentArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ + "content\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = JSONML.toString(expectedJsonObject); + String expectedFinalStr = "
"+ + "1\n2\n3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleArraytoString() { + String expectedStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject expectedJsonObject = new JSONObject(expectedStr); + String finalStr = JSONML.toString(expectedJsonObject); + String expectedFinalStr = "
"+ + "123"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ + finalStr+"]", expectedFinalStr.equals(finalStr)); + } + + @Test + public void shouldHandleNestedArraytoString() { + String xmlStr = + "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ + "\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ + "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; + JSONObject jsonObject = new JSONObject(xmlStr); + String finalStr = JSONML.toString(jsonObject); + JSONObject finalJsonObject = JSONML.toJSONObject(finalStr); + String expectedStr = "
"+ + "12"+ + "3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + JSONObject expectedJsonObject = JSONML.toJSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } + +} diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 0d2b543..8c680a0 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -7,7 +7,8 @@ import org.junit.runners.Suite; CDLTest.class, CookieTest.class, PropertyTest.class, - XMLTest.class + XMLTest.class, + JSONMLTest.class }) public class JunitTestSuite { } \ No newline at end of file