From e31f3d0dbe88ee5ab34d0f0cc340fe9a615fa7c7 Mon Sep 17 00:00:00 2001 From: Arnaud Vandyck Date: Tue, 7 Mar 2017 11:27:11 +0100 Subject: [PATCH] Implements partial ignore #15 --- .../skyscreamer/jsonassert/JSONCompare.java | 64 +++++++++++++++++++ .../comparator/DefaultComparator.java | 32 ++++++---- .../jsonassert/JSONAssertTest.java | 59 ++++++++++++++++- 3 files changed, 140 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index fc41bf4f..3cb1e87e 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -20,6 +20,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. @@ -96,6 +100,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. * @@ -109,6 +132,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. * @@ -122,6 +159,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. @@ -136,4 +186,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)); + } + } diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java index e2f6b57a..f64e88f2 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java @@ -16,11 +16,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 { @@ -36,20 +42,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 (expectedValue instanceof Number && actualValue instanceof Number) { - if (((Number)expectedValue).doubleValue() != ((Number)actualValue).doubleValue()) { + if (wildcard == null || !wildcard.equals(expectedValue)) { + if (expectedValue instanceof Number && actualValue instanceof Number) { + if (((Number) expectedValue).doubleValue() != ((Number) actualValue).doubleValue()) { + 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); } } diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java index 65014b68..82ed4e30 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java @@ -1,14 +1,17 @@ package org.skyscreamer.jsonassert; -import java.util.Arrays; - import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Assert; import org.junit.Test; -import static org.skyscreamer.jsonassert.JSONCompareMode.*; +import java.util.Arrays; + +import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT; +import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE; +import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT; +import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT_ORDER; /** * Unit tests for {@link JSONAssert} @@ -359,6 +362,40 @@ public void testAssertNotEqualsJSONArray() throws JSONException { JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 3, 2)), actual, true); } + @Test() + public void testWildCard() throws JSONException { + testPass("{id:**,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", + STRICT, "**"); // Exact to exact (strict) + testPass("{id:\"**\",name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", + STRICT, "**"); // Exact to exact (strict) + testFail("{id:1,name:**,friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}", + STRICT, "**"); // Out-of-order fails (strict) + testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:**},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}", + STRICT_ORDER, "**"); // Out-of-order fails (strict order) + testPass("{id:1,name:\"Joe\",friends:**,pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}", + LENIENT, "**"); // Out-of-order ok + testPass("{id:1,name:\"Joe\",friends:\"**\",pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}", + LENIENT, "**"); // Out-of-order ok + testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:**}],pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}", + NON_EXTENSIBLE, "**"); // Out-of-order ok + testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:++}", + "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}", + STRICT, "++"); // Mismatch (strict) + testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:++}],pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}", + STRICT, "++"); // Mismatch (strict) + testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:\"++\"}],pets:[]}", + "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}", + STRICT, "++"); // Mismatch (strict) + } + private void testPass(String expected, String actual, JSONCompareMode compareMode) throws JSONException { @@ -374,4 +411,20 @@ private void testFail(String expected, String actual, JSONCompareMode compareMod JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); Assert.assertTrue(message, result.failed()); } + + private void testPass(String expected, String actual, JSONCompareMode compareMode, String wildcard) + throws JSONException + { + String message = expected + " == " + actual + " (" + compareMode + ")"; + JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode, wildcard); + Assert.assertTrue(message + "\n " + result.getMessage(), result.passed()); + } + + private void testFail(String expected, String actual, JSONCompareMode compareMode, String wildcard) + throws JSONException + { + String message = expected + " != " + actual + " (" + compareMode + ")"; + JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode, wildcard); + Assert.assertTrue(message, result.failed()); + } }