1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

XMLTest, in progress

This commit is contained in:
stleary 2015-03-22 20:37:24 -05:00
parent 8b9c3cbf47
commit 4fbe651e57
4 changed files with 160 additions and 83 deletions

View file

@ -1,8 +1,5 @@
package org.json.junit; package org.json.junit;
import java.util.Iterator;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
@ -127,7 +124,7 @@ public class CDLTest {
JSONArray jsonArray = CDL.toJSONArray(lines); JSONArray jsonArray = CDL.toJSONArray(lines);
// This array is built from JSON parsing // This array is built from JSON parsing
JSONArray expectedJsonArray = new JSONArray(expectedLines); JSONArray expectedJsonArray = new JSONArray(expectedLines);
compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
} }
@Test @Test
@ -137,7 +134,7 @@ public class CDLTest {
JSONArray nameJSONArray = new JSONArray(nameArrayStr); JSONArray nameJSONArray = new JSONArray(nameArrayStr);
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values); JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]"); JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]");
compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
} }
@Test @Test
@ -151,39 +148,8 @@ public class CDLTest {
String jsonStr = CDL.toString(jsonArray); String jsonStr = CDL.toString(jsonArray);
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
JSONArray expectedJsonArray = new JSONArray(expectedLines); 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<String> 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()));
}
}
}
} }

View file

@ -6,7 +6,8 @@ import org.junit.runners.Suite;
@Suite.SuiteClasses({ @Suite.SuiteClasses({
CDLTest.class, CDLTest.class,
CookieTest.class, CookieTest.class,
PropertyTest.class PropertyTest.class,
XMLTest.class
}) })
public class JunitTestSuite { public class JunitTestSuite {
} }

107
Util.java
View file

@ -2,55 +2,72 @@ package org.json.junit;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.*;
import org.json.*;
public class Util { public class Util {
/////////////////////////// UTILITY METHODS /////////////////////////
/** /**
* Utility method to check for a target string, then remove it from * Compares two json arrays for equality
* the string to be searched. * @param jsonArray created by the code to be tested
* @param jsonStr the string to be searched * @param expectedJsonArray created specifically for compar
* @param expectedStr the target string to search for
* @param assertStr the error message for the assert
* @return new string with target substring removed
*/ */
public static String checkAndRemoveString( public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
String jsonStr, String expectedStr, String assertStr) { JSONArray expectedJsonArray) {
int idx = jsonStr.indexOf(expectedStr); assertTrue("jsonArray lengths should be equal",
assertTrue(assertStr, idx != -1); jsonArray.length() == expectedJsonArray.length());
String newStr = jsonStr.substring(0, idx)+ for (int i = 0; i < jsonArray.length(); ++i) {
jsonStr.substring(idx+expectedStr.length()); JSONObject jsonObject = jsonArray.getJSONObject(i);
return newStr; JSONObject expectedJsonObject = expectedJsonArray.getJSONObject(i);
} assertTrue("jsonObjects should have the same length",
jsonObject.length() == expectedJsonObject.length());
/** Iterator<String> keys = jsonObject.keys();
* Utility method to strip out selected punctuation chars and confirm while (keys.hasNext()) {
* that jsonStr is now empty // TODO: check for nonstring types
* @param jsonStr the string to be verified String key = keys.next();
* @param regexStr regex string of the chars to remove Object value = jsonObject.get(key);
* @param assertStr the error message for the assert String testStr = "row: "+i+" key: "+key+" val: "+value.toString();
*/ String actualStr = expectedJsonObject .get(key).toString();
public static void verifyEmptyJsonStr(String jsonStr, String regexStr, assertTrue("values should be equal for actual: "+testStr+
String assertStr) { " expected: "+actualStr,
jsonStr = jsonStr.replaceAll(regexStr, ""); value.equals(expectedJsonArray.getJSONObject(i).
assertTrue(assertStr, jsonStr.length() == 0); get(key).toString()));
} }
}
/** }
* Utility method to check for a set of target strings,
* then remove them from the string to be searched. public static void compareActualVsExpectedJsonObjects(
* When completed, punctuation marks are stripped out and JSONObject jsonObject, JSONObject expectedJsonObject) {
* the string to be searched is confirmed as empty assertTrue("jsonObjects should have the same length",
* @param jsonStr the string to be searched jsonObject.length() == expectedJsonObject.length());
* @param expectedStrArray the target strings to search for Iterator<String> keys = jsonObject.keys();
* @param regexStr regex string of the chars to remove while (keys.hasNext()) {
* @param methodStr the method name String key = keys.next();
*/ Object value = jsonObject.get(key);
public static void checkAndRemoveStrings(String jsonStr, Object expectedValue = expectedJsonObject.get(key);
String[] expectedStr, String regexStr, String methodStr) { if (value instanceof JSONObject) {
for (int i = 0; i < expectedStr.length; ++i) { JSONObject childJsonObject = jsonObject.getJSONObject(key);
jsonStr = Util.checkAndRemoveString(jsonStr, expectedStr[i], JSONObject expectedChildJsonObject =
methodStr+expectedStr+" should be included in string output"); 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");
} }
} }

View file

@ -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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>Joe Tester</name>\n"+
" <street>Baker street 5</street>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addr&esses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>[CDATA[Joe Tester]]</name>\n"+
" <street>Baker street 5</street>\n"+
" </address>\n"+
"</addr&esses>";
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);
}
}