diff --git a/Util.java b/Util.java index 13407af..3a1cd09 100644 --- a/Util.java +++ b/Util.java @@ -9,12 +9,11 @@ import org.json.*; public class Util { - /////////////////////////// UTILITY METHODS ///////////////////////// /** * Compares two json arrays for equality * @param jsonArray created by the code to be tested - * @param expectedJsonArray created specifically for compar + * @param expectedJsonArray created specifically for comparing */ public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, JSONArray expectedJsonArray) { @@ -27,19 +26,17 @@ public class Util { jsonObject.length() == expectedJsonObject.length()); Iterator keys = jsonObject.keys(); while (keys.hasNext()) { - // TODO: check for nonstring types String key = keys.next(); - Object value = jsonObject.get(key); - String testStr = "row: "+i+" key: "+key+" val: "+value.toString(); - String actualStr = expectedJsonObject .get(key).toString(); - assertTrue("values should be equal for actual: "+testStr+ - " expected: "+actualStr, - value.equals(expectedJsonArray.getJSONObject(i). - get(key).toString())); + compareJsonObjectEntries(jsonObject, expectedJsonObject, key); } } } + /** + * Compares two json objects for equality + * @param jsonObject created by the code to be tested + * @param expectedJsonObject created specifically for comparing + */ public static void compareActualVsExpectedJsonObjects( JSONObject jsonObject, JSONObject expectedJsonObject) { assertTrue("jsonObjects should have the same length", @@ -47,27 +44,42 @@ public class Util { Iterator keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); - Object value = jsonObject.get(key); - Object expectedValue = expectedJsonObject.get(key); - if (value instanceof JSONObject) { - JSONObject childJsonObject = jsonObject.getJSONObject(key); - JSONObject expectedChildJsonObject = - expectedJsonObject.getJSONObject(key); - compareActualVsExpectedJsonObjects( - childJsonObject, expectedChildJsonObject); - } else if (value instanceof JSONArray) { - JSONArray childJsonArray = jsonObject.getJSONArray(key); - JSONArray expectedChildJsonArray = - expectedJsonObject.getJSONArray(key); - compareActualVsExpectedJsonArrays( - childJsonArray, expectedChildJsonArray); - } else { - String testStr = "key: "+key+" val: "+value.toString(); - String actualStr = expectedValue.toString(); - assertTrue("string values should be equal for actual: "+ - testStr+" expected: "+actualStr, - value.equals(expectedValue.toString())); - } + compareJsonObjectEntries(jsonObject, expectedJsonObject, key); + } + } + + /** + * Compare two jsonObject entries + * @param jsonObject created by the code to be tested + * @param expectedJsonObject created specifically for comparing + * @param key key to the jsonObject entry to be compared + */ + private static void compareJsonObjectEntries(JSONObject jsonObject, + JSONObject expectedJsonObject, String key) { + Object value = jsonObject.get(key); + Object expectedValue = expectedJsonObject.get(key); + if (value instanceof JSONObject) { + JSONObject childJsonObject = jsonObject.getJSONObject(key); + JSONObject expectedChildJsonObject = + expectedJsonObject.getJSONObject(key); + compareActualVsExpectedJsonObjects( + childJsonObject, expectedChildJsonObject); + } else if (value instanceof JSONArray) { + JSONArray childJsonArray = jsonObject.getJSONArray(key); + JSONArray expectedChildJsonArray = + expectedJsonObject.getJSONArray(key); + compareActualVsExpectedJsonArrays( + childJsonArray, expectedChildJsonArray); + } else if (!(value instanceof String) && !(expectedValue instanceof String)) { + assertTrue("string values should be equal for actual: "+ + value.toString()+" expected: "+expectedValue.toString(), + value.toString().equals(expectedValue.toString())); + } else { + String testStr = "key: "+key+" val: "+value.toString(); + String actualStr = expectedValue.toString(); + assertTrue("string values should be equal for actual: "+ + testStr+" expected: "+actualStr, + value.equals(expectedValue.toString())); } } } diff --git a/XMLTest.java b/XMLTest.java index 3f57710..0c86eeb 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -1,7 +1,5 @@ package org.json.junit; -import java.util.*; - import static org.junit.Assert.*; import org.json.*; @@ -22,25 +20,19 @@ public class XMLTest { assertTrue("jsonObject should be empty", jsonObject.length() == 0); } - @Test(expected=NullPointerException.class) - public void shouldHandleNullJSONXML() { - JSONObject jsonObject= null; - String xmlStr = XML.toString(jsonObject); + @Test + public void shouldHandleEmptyXML() { + + String xmlStr = ""; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); } - @Test(expected=JSONException.class) - public void shouldHandleInvalidBangInTag() { - String xmlStr = - "\n"+ - "\n"+ - "
\n"+ - " Joe Tester\n"+ - " abc street\n"+ - "
\n"+ - "
"; + @Test + public void shouldHandleNonXML() { + String xmlStr = "{ \"this is\": \"not xml\"}"; JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue(jsonObject == null); + assertTrue("xml string should be empty", jsonObject.length() == 0); } @Test(expected=JSONException.class) @@ -54,22 +46,96 @@ public class XMLTest { " abc street\n"+ " \n"+ ""; - JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue(jsonObject == null); + XML.toJSONObject(xmlStr); } - @Test - public void shouldHandleEmptyXML() { + @Test(expected=JSONException.class) + public void shouldHandleInvalidBangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + "
\n"+ + "
"; + XML.toJSONObject(xmlStr); + } - String xmlStr = ""; - JSONObject jsonObject = XML.toJSONObject(xmlStr); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + @Test(expected=JSONException.class) + public void shouldHandleInvalidBangNoCloseInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + XML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleNoCloseStartTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " \n"+ + " \n"+ + ""; + XML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void shouldHandleInvalidCDATABangInTag() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " \n"+ + "
\n"+ + "
"; + XML.toJSONObject(xmlStr); + } + + @Test(expected=NullPointerException.class) + public void shouldHandleNullJSONXML() { + JSONObject jsonObject= null; + XML.toString(jsonObject); } @Test public void shouldHandleEmptyJSONXML() { JSONObject jsonObject= new JSONObject(); String xmlStr = XML.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 = XML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test @@ -82,17 +148,28 @@ public class XMLTest { " 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\":\"\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"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 = XML.toJSONObject(xmlStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @@ -113,6 +190,11 @@ public class XMLTest { "
\n"+ "
"; JSONObject jsonObject = XML.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 @@ -124,12 +206,15 @@ public class XMLTest { "
\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]]\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"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\"}}"; @@ -140,4 +225,73 @@ public class XMLTest { 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 = XML.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 = XML.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 = XML.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 = XML.toString(jsonObject); + JSONObject finalJsonObject = XML.toJSONObject(finalStr); + String expectedStr = "
"+ + "12"+ + "3"+ + "
test.xsdhttp://www.w3.org/2001/XMLSche"+ + "ma-instance
"; + JSONObject expectedJsonObject = XML.toJSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } }