From ef7e0c7d08662739cddf9ef9ad977043855f5969 Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 30 Mar 2015 08:14:34 -0500 Subject: [PATCH] in progress --- JSONMLTest.java | 308 +++++++++++++++++++++++++----------------------- Util.java | 58 ++++----- 2 files changed, 186 insertions(+), 180 deletions(-) diff --git a/JSONMLTest.java b/JSONMLTest.java index ca7c2d1..3271584 100644 --- a/JSONMLTest.java +++ b/JSONMLTest.java @@ -12,30 +12,27 @@ import org.junit.Test; public class JSONMLTest { @Test(expected=NullPointerException.class) - public void shouldHandleNullXML() { + public void nullXMLException() { String xmlStr = null; - JSONObject jsonObject = JSONML.toJSONObject(xmlStr); - assertTrue("jsonObject should be empty", jsonObject.length() == 0); + JSONML.toJSONObject(xmlStr); } @Test(expected=JSONException.class) - public void shouldHandleEmptyXML() { + public void emptyXMLException() { 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); + JSONML.toJSONObject(xmlStr); } @Test(expected=JSONException.class) - public void shouldHandleInvalidSlashInTag() { + public void nonXMLException() { + String xmlStr = "{ \"this is\": \"not xml\"}"; + JSONML.toJSONObject(xmlStr); + } + + @Test(expected=JSONException.class) + public void unvalidSlashInTagException() { String xmlStr = "\n"+ "\n"+ "\n"+ "\n"+ "\n"+ "\n"+ "\n"+ - "
\n"+ - " \n"+ - " >\n"+ - "
\n"+ + "xsi:noNamespaceSchemaLocation='test.xsd'>\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\"}}"; + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"childNodes\":[{"+ + "\"childNodes\":"+ + "[{\"tagName\":\"name\"},"+ + "{\"tagName\":\"nocontent\"},"+ + "\">\"],"+ + "\"tagName\":\"address\"}],"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldHandleSimpleXML() { + public void simpleXML() { 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"+ + "xsi:noNamespaceSchemaLocation='test.xsd'>\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); + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"childNodes\":[{"+ + "\"childNodes\":["+ + "{\"childNodes\":[\"Joe Tester\"],"+ + "\"tagName\":\"name\"},"+ + "{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+ + "\"tagName\":\"street\"},"+ + "{\"tagName\":\"NothingHere\"},"+ + "{\"childNodes\":[true],"+ + "\"tagName\":\"TrueValue\"},"+ + "{\"childNodes\":[false],"+ + "\"tagName\":\"FalseValue\"},"+ + "{\"childNodes\":[null],"+ + "\"tagName\":\"NullValue\"},"+ + "{\"childNodes\":[42],"+ + "\"tagName\":\"PositiveValue\"},"+ + "{\"childNodes\":[-23],"+ + "\"tagName\":\"NegativeValue\"},"+ + "{\"childNodes\":[-23.45],"+ + "\"tagName\":\"DoubleValue\"},"+ + "{\"childNodes\":[\"-23x.45\"],"+ + "\"tagName\":\"Nan\"},"+ + "{\"childNodes\":[\"1, 2, 3, 4.1, 5.2\"],"+ + "\"tagName\":\"ArrayOfNum\"}],"+ + "\"tagName\":\"address\"}],"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldHandleCommentsInXML() { + public void commentsInXML() { String xmlStr = - "\n"+ - "\n"+ - "\n"+ - "
\n"+ - " comment ]]>\n"+ - " Joe Tester\n"+ - " \n"+ - " Baker street 5\n"+ - "
\n"+ - "
"; + "\n"+ + "\n"+ + "\n"+ + "
\n"+ + " comment ]]>\n"+ + "Joe Tester\n"+ + "\n"+ + "Baker street 5\n"+ + "
\n"+ + "
"; + String expectedStr = + "{\"childNodes\":["+ + "{\"childNodes\":["+ + "\" this is -- comment \","+ + "{\"childNodes\":[\"Joe Tester\"],"+ + "\"tagName\":\"name\"},"+ + "{\"childNodes\":[\"Baker street 5\"],"+ + "\"tagName\":\"street\"}],"+ + "\"tagName\":\"address\"}],"+ + "\"tagName\":\"addresses\"}"; 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() { + public void jsonObjectToString() { String xmlStr = "\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\"}}"; + "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ + "\"childNodes\":["+ + "{\"childNodes\":["+ + "{\"childNodes\":[\"[CDATA[Joe & T > e < s \\\" t ' er]]\"],"+ + "\"tagName\":\"name\"},"+ + "{\"childNodes\":[\"Baker street 5\"],"+ + "\"tagName\":\"street\"},"+ + "{\"childNodes\":[\"1, 2, 3, 4.1, 5.2\"],"+ + "\"tagName\":\"ArrayOfNum\"}],"+ + "\"tagName\":\"address\"}],"+ + "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ + "\"tagName\":\"addresses\"}"; JSONObject jsonObject = JSONML.toJSONObject(xmlStr); String xmlToStr = JSONML.toString(jsonObject); @@ -226,72 +259,57 @@ public class JSONMLTest { } @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); + public void jsonArrayToString() { + String xmlStr = + ""+ + ""+ + "5"+ + "10"+ + "15"+ + ""+ + "val2"+ + "val3"+ + ""+ + "-6"+ + "true"+ + ""+ + "false"+ + "null"+ + ""+ + "10"+ + "20"+ + "33.33"+ + "5220"+ + ""+ + ""; + String expectedStr = + "[\"tag0\","+ + "[\"tag1\","+ + "[\"tag2\",5],"+ + "[\"tag2\",10],"+ + "[\"tag2\",15]"+ + "],"+ + "[\"tag2\",\"val2\"],"+ + "[\"tag3\",\"val3\"],"+ + "[\"tag4\","+ + "[\"tag5\",-6],"+ + "[\"tag5\",true]"+ + "],"+ + "[\"tag6\",false],"+ + "[\"tag7\",null],"+ + "[\"tag1\","+ + "[\"tag8\",10],"+ + "[\"tag8\",20],"+ + "[\"tag8\",33.33],"+ + "[\"tag8\",5220]"+ + "]"+ + "]"; + JSONArray jsonArray = JSONML.toJSONArray(xmlStr); + String xmlToStr = JSONML.toString(jsonArray); + JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr); + JSONArray expectedJsonArray= new JSONArray(expectedStr); + Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(finalJsonArray,expectedJsonArray); } } diff --git a/Util.java b/Util.java index 3a1cd09..d494d13 100644 --- a/Util.java +++ b/Util.java @@ -20,15 +20,9 @@ public class Util { assertTrue("jsonArray lengths should be equal", jsonArray.length() == expectedJsonArray.length()); for (int i = 0; i < jsonArray.length(); ++i) { - JSONObject jsonObject = jsonArray.getJSONObject(i); - JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i); - assertTrue("jsonObjects should have the same length", - jsonObject.length() == expectedJsonObject.length()); - Iterator keys = jsonObject.keys(); - while (keys.hasNext()) { - String key = keys.next(); - compareJsonObjectEntries(jsonObject, expectedJsonObject, key); - } + Object value = jsonArray.get(i); + Object expectedValue = expectedJsonArray.get(i); + compareActualVsExpectedObjects(value, expectedValue); } } @@ -44,42 +38,36 @@ public class Util { Iterator keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); - compareJsonObjectEntries(jsonObject, expectedJsonObject, key); + Object value = jsonObject.get(key); + Object expectedValue = expectedJsonObject.get(key); + compareActualVsExpectedObjects(value, expectedValue); } } /** - * Compare two jsonObject entries - * @param jsonObject created by the code to be tested - * @param expectedJsonObject created specifically for comparing + * Compare two objects for equality. Might be JSONArray, JSONObject, + * or something else. + * @param value created by the code to be tested + * @param expectedValue 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); + private static void compareActualVsExpectedObjects(Object value, + Object expectedValue) { + if (value instanceof JSONObject && expectedValue instanceof JSONObject) { + JSONObject jsonObject = (JSONObject)value; + JSONObject expectedJsonObject = (JSONObject)expectedValue; compareActualVsExpectedJsonObjects( - childJsonObject, expectedChildJsonObject); - } else if (value instanceof JSONArray) { - JSONArray childJsonArray = jsonObject.getJSONArray(key); - JSONArray expectedChildJsonArray = - expectedJsonObject.getJSONArray(key); + jsonObject, expectedJsonObject); + } else if (value instanceof JSONArray && expectedValue instanceof JSONArray) { + JSONArray jsonArray = (JSONArray)value; + JSONArray expectedJsonArray = (JSONArray)expectedValue; 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())); + jsonArray, expectedJsonArray); } 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())); + value.toString()+" expected: "+expectedValue.toString(), + value.toString().equals(expectedValue.toString())); } } + }