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

coverage XMLTest 81.2% / XMLTokener 82.2%

This commit is contained in:
stleary 2015-03-26 22:58:11 -05:00
parent a18e9f7a25
commit 89f359e4f8
2 changed files with 225 additions and 59 deletions

View file

@ -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<String> 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<String> 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()));
}
}
}