mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 16:00:51 -07:00
XMLTest, in progress
This commit is contained in:
parent
8b9c3cbf47
commit
4fbe651e57
4 changed files with 160 additions and 83 deletions
107
Util.java
107
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<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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void compareActualVsExpectedJsonObjects(
|
||||
JSONObject jsonObject, JSONObject expectedJsonObject) {
|
||||
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);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue