diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java index 4f68e2d1..7b824dfb 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java @@ -43,6 +43,12 @@ public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actua return result; } + public final JSONCompareResult compareJSONPreserveContext(String prefix, JSONObject expected, JSONObject actual) throws JSONException{ + JSONCompareResult result = new JSONCompareResult(); + compareJSON(prefix, expected, actual, result); + return result; + } + /** * Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison. * @@ -57,6 +63,12 @@ public final JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) return result; } + public final JSONCompareResult compareJSONPreserveContext(String prefix, JSONArray expected, JSONArray actual) throws JSONException{ + JSONCompareResult result = new JSONCompareResult(); + compareJSONArray(prefix, expected, actual, result); + return result; + } + protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) { Set actualKeys = getKeys(actual); for (String key : actualKeys) { @@ -153,13 +165,13 @@ protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONA continue; } if (expectedElement instanceof JSONObject) { - if (compareJSON((JSONObject) expectedElement, (JSONObject) actualElement).passed()) { + if (compareJSONPreserveContext(key + "[" + i + "]",(JSONObject) expectedElement, (JSONObject) actualElement).passed()) { matched.add(j); matchFound = true; break; } } else if (expectedElement instanceof JSONArray) { - if (compareJSON((JSONArray) expectedElement, (JSONArray) actualElement).passed()) { + if (compareJSONPreserveContext(key + "[" + i + "]", (JSONArray) expectedElement, (JSONArray) actualElement).passed()) { matched.add(j); matchFound = true; break; diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java index 5f4c36ca..d66d1e6c 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCustomComparatorTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; public class JSONCustomComparatorTest { @@ -118,6 +119,15 @@ public class JSONCustomComparatorTest { " }\n" + "}"; + String rootDeepWildcardWithArray = "{\n" + + " \"foo\": [{\n" + + " \"bar\": {\n" + + " \"num\": \"not_a_number\"" + + " }\n" + + " }]\n" + + "}"; + + int comparatorCallCount = 0; ValueMatcher comparator = new ValueMatcher() { @Override @@ -166,4 +176,20 @@ public void whenRootDeepWildcardPathMatchesCallCustomMatcher() throws JSONExcept assertTrue(result.getMessage(), result.passed()); assertEquals(4, comparatorCallCount); } + + @Test + public void whenDeepWildcardPathWithArrayMatchesCallCustomMatcher() throws JSONException { + JSONComparator jsonCmpStrict = new CustomComparator(JSONCompareMode.STRICT, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher("\\d"))); + JSONComparator jsonCmpNonExtensible = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher("\\d"))); + JSONComparator jsonCmpNonLenient = new CustomComparator(JSONCompareMode.LENIENT, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher("\\d"))); + + JSONCompareResult resultStrict = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpStrict); + assertFalse(resultStrict.getMessage(), resultStrict.passed()); + + JSONCompareResult resultNonExtensible = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonExtensible); + assertFalse(resultNonExtensible.getMessage(), resultNonExtensible.passed()); + + JSONCompareResult resultLenient = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonLenient); + assertFalse(resultLenient.getMessage(), resultLenient.passed()); + } }