Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/org/skyscreamer/jsonassert/JSONCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ private static JSONComparator getComparatorForMode(JSONCompareMode mode) {
return new DefaultComparator(mode);
}

private static JSONComparator getComparatorForModeWithWildcard(JSONCompareMode mode, String wildcard) {
return new DefaultComparator(mode, wildcard);
}

/**
* Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of
* the comparison.
Expand Down Expand Up @@ -111,6 +115,25 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO
return result;
}

/**
* Compares {@link JSONString} provided to the expected {@code JSONString}, checking that the
* {@link org.json.JSONString#toJSONString()} are equal.
*
* @param expected Expected {@code JSONstring}
* @param actual {@code JSONstring} to compare
* @param wildcard wildcard used in the expceted json string
*/
public static JSONCompareResult compareJson(final JSONString expected, final JSONString actual, String wildcard) {
final JSONCompareResult result = new JSONCompareResult();
final String expectedJson = expected.toJSONString();
final String actualJson = actual.toJSONString();
if (wildcard == null || !wildcard.equals(expectedJson)
|| !expectedJson.equals(actualJson)) {
result.fail("");
}
return result;
}

/**
* Compares JSON string provided to the expected JSON string, and returns the results of the comparison.
*
Expand All @@ -125,6 +148,20 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr
return compareJSON(expectedStr, actualStr, getComparatorForMode(mode));
}

/**
* Compares JSON string provided to the expected JSON string, and returns the results of the comparison.
*
* @param expectedStr Expected JSON string
* @param actualStr JSON string to compare
* @param mode Defines comparison behavior
* @param wildcard wildcard used in expected string
* @throws JSONException
*/
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode, String wildcard)
throws JSONException {
return compareJSON(expectedStr, actualStr, getComparatorForModeWithWildcard(mode, wildcard));
}

/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
Expand All @@ -139,6 +176,19 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu
return compareJSON(expected, actual, getComparatorForMode(mode));
}

/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
* @param expected Expected JSONObject
* @param actual JSONObject to compare
* @param mode Defines comparison behavior
* @param wildcard wildcard used in the expected json object
* @throws JSONException
*/
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONCompareMode mode, String wildcard)
throws JSONException {
return compareJSON(expected, actual, getComparatorForModeWithWildcard(mode, wildcard));
}

/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
Expand All @@ -154,4 +204,18 @@ public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual
return compareJSON(expected, actual, getComparatorForMode(mode));
}

/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
*
* @param expected Expected JSONArray
* @param actual JSONArray to compare
* @param mode Defines comparison behavior
* @param wildcard wildcard used in expected json array
* @throws JSONException
*/
public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONCompareMode mode, String wildcard)
throws JSONException {
return compareJSON(expected, actual, getComparatorForModeWithWildcard(mode, wildcard));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@
public class DefaultComparator extends AbstractComparator {

JSONCompareMode mode;
String wildcard;

public DefaultComparator(JSONCompareMode mode) {
this.mode = mode;
}

public DefaultComparator(JSONCompareMode mode, String wildcard) {
this.mode = mode;
this.wildcard = wildcard;
}

@Override
public void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result)
throws JSONException {
Expand All @@ -50,20 +56,22 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
throws JSONException {
if (areNumbers(expectedValue, actualValue)) {
if (areNotSameDoubles(expectedValue, actualValue)) {
if (wildcard == null || !wildcard.equals(expectedValue)) {
if (areNumbers(expectedValue, actualValue)) {
if (areNotSameDoubles(expectedValue, actualValue)) {
result.fail(prefix, expectedValue, actualValue);
}
} else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
if (expectedValue instanceof JSONArray) {
compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result);
} else if (expectedValue instanceof JSONObject) {
compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result);
} else if (!expectedValue.equals(actualValue)) {
result.fail(prefix, expectedValue, actualValue);
}
} else {
result.fail(prefix, expectedValue, actualValue);
}
} else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
if (expectedValue instanceof JSONArray) {
compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result);
} else if (expectedValue instanceof JSONObject) {
compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result);
} else if (!expectedValue.equals(actualValue)) {
result.fail(prefix, expectedValue, actualValue);
}
} else {
result.fail(prefix, expectedValue, actualValue);
}
}

Expand Down
Loading