diff --git a/CDLTest.java b/CDLTest.java index 4ff9c38..53284d0 100644 --- a/CDLTest.java +++ b/CDLTest.java @@ -1,8 +1,5 @@ package org.json.junit; - -import java.util.Iterator; - import static org.junit.Assert.*; import org.junit.Test; @@ -127,7 +124,7 @@ public class CDLTest { JSONArray jsonArray = CDL.toJSONArray(lines); // This array is built from JSON parsing JSONArray expectedJsonArray = new JSONArray(expectedLines); - compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @Test @@ -137,7 +134,7 @@ public class CDLTest { JSONArray nameJSONArray = new JSONArray(nameArrayStr); JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]"); - compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @Test @@ -151,39 +148,8 @@ public class CDLTest { String jsonStr = CDL.toString(jsonArray); JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); JSONArray expectedJsonArray = new JSONArray(expectedLines); - compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } - - /////////////////////////// UTILITY METHODS ///////////////////////// - - /** - * Compares two json arrays for equality - * @param jsonArray created by the code to be tested - * @param expectedJsonArray created specifically for compar - */ - private void compareActualVsExpectedJsonArrays(JSONArray jsonArray, - JSONArray expectedJsonArray) { - 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(); - 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())); - } - } - } - } \ No newline at end of file diff --git a/JunitTestSuite.java b/JunitTestSuite.java index b9abad1..0d2b543 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -6,7 +6,8 @@ import org.junit.runners.Suite; @Suite.SuiteClasses({ CDLTest.class, CookieTest.class, - PropertyTest.class + PropertyTest.class, + XMLTest.class }) public class JunitTestSuite { } \ No newline at end of file diff --git a/Util.java b/Util.java index 89743c0..13407af 100644 --- a/Util.java +++ b/Util.java @@ -2,55 +2,72 @@ package org.json.junit; import static org.junit.Assert.*; +import java.util.*; + +import org.json.*; + public class Util { + + /////////////////////////// UTILITY METHODS ///////////////////////// + /** - * Utility method to check for a target string, then remove it from - * the string to be searched. - * @param jsonStr the string to be searched - * @param expectedStr the target string to search for - * @param assertStr the error message for the assert - * @return new string with target substring removed + * Compares two json arrays for equality + * @param jsonArray created by the code to be tested + * @param expectedJsonArray created specifically for compar */ - public static String checkAndRemoveString( - String jsonStr, String expectedStr, String assertStr) { - int idx = jsonStr.indexOf(expectedStr); - assertTrue(assertStr, idx != -1); - String newStr = jsonStr.substring(0, idx)+ - jsonStr.substring(idx+expectedStr.length()); - return newStr; - } - - /** - * Utility method to strip out selected punctuation chars and confirm - * that jsonStr is now empty - * @param jsonStr the string to be verified - * @param regexStr regex string of the chars to remove - * @param assertStr the error message for the assert - */ - public static void verifyEmptyJsonStr(String jsonStr, String regexStr, - String assertStr) { - jsonStr = jsonStr.replaceAll(regexStr, ""); - assertTrue(assertStr, jsonStr.length() == 0); - } - - /** - * Utility method to check for a set of target strings, - * then remove them from the string to be searched. - * When completed, punctuation marks are stripped out and - * the string to be searched is confirmed as empty - * @param jsonStr the string to be searched - * @param expectedStrArray the target strings to search for - * @param regexStr regex string of the chars to remove - * @param methodStr the method name - */ - public static void checkAndRemoveStrings(String jsonStr, - String[] expectedStr, String regexStr, String methodStr) { - for (int i = 0; i < expectedStr.length; ++i) { - jsonStr = Util.checkAndRemoveString(jsonStr, expectedStr[i], - methodStr+expectedStr+" should be included in string output"); + public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray, + JSONArray expectedJsonArray) { + 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()) { + // 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())); + } + } + } + + public static void compareActualVsExpectedJsonObjects( + JSONObject jsonObject, JSONObject expectedJsonObject) { + assertTrue("jsonObjects should have the same length", + jsonObject.length() == expectedJsonObject.length()); + 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())); + } } - Util.verifyEmptyJsonStr(jsonStr, regexStr, - methodStr+" jsonStr should be empty"); } } diff --git a/XMLTest.java b/XMLTest.java index e69de29..a6f1bf7 100644 --- a/XMLTest.java +++ b/XMLTest.java @@ -0,0 +1,93 @@ +package org.json.junit; + +import java.util.*; + +import static org.junit.Assert.*; + +import org.json.*; +import org.junit.Test; + + +/** + * Tests for JSON-Java XML.java + */ +public class XMLTest { + + @Test(expected=NullPointerException.class) + public void shouldHandleNullXML() { + + String xmlStr = null; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + assertTrue("jsonObject should be empty", jsonObject.length() == 0); + } + + @Test + public void shouldHandleEmptyXML() { + + String xmlStr = ""; + JSONObject jsonObject = XML.toJSONObject(xmlStr); + 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 shouldHandleEmptyJSONXML() { + JSONObject jsonObject= new JSONObject(); + String xmlStr = XML.toString(jsonObject); + } + + @Test + public void shouldHandleSimpleXML() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " Joe Tester\n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"Joe Tester\"},\"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); + } + + @Test + public void shouldHandleToString() { + String xmlStr = + "\n"+ + "\n"+ + "
\n"+ + " [CDATA[Joe Tester]]\n"+ + " Baker street 5\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{\"addr&esses\":{\"address\":{\"street\":\"Baker street 5\","+ + "\"name\":\"[CDATA[Joe Tester]]\"},\"xsi:noNamespaceSchemaLocation\":"+ + "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ + "XMLSchema-instance\"}}"; + + JSONObject jsonObject = XML.toJSONObject(xmlStr); + String xmlToStr = XML.toString(jsonObject); + JSONObject finalJsonObject = XML.toJSONObject(xmlToStr); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); + } +}