From a767718a9694eba3b1b9e07cbb9e414e32daf4e3 Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Mon, 12 Apr 2021 20:46:33 +0800 Subject: [PATCH 1/8] Fix plural problem in some descriptions --- .../skyscreamer/jsonassert/comparator/DefaultComparator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java index bc71eae4..c958ba17 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java @@ -77,7 +77,9 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException { if (expected.length() != actual.length()) { - result.fail(prefix + "[]: Expected " + expected.length() + " values but got " + actual.length()); + String description = " values but got "; + if (expected.length() == 1) description = " value but got "; + result.fail(prefix + "[]: Expected " + expected.length() + description + actual.length()); return; } else if (expected.length() == 0) { return; // Nothing to compare From 513f412b598f68455e0940a1032bf4d6e7fc6b92 Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Fri, 23 Apr 2021 21:09:57 +0800 Subject: [PATCH 2/8] A new version of fix to issue 128 --- .../comparator/DefaultComparator.java | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java index c958ba17..788638a7 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java @@ -73,13 +73,52 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu } } + /** + * Generate error message for JSONArray comparision.
+ * + * Solve problem mentioned in issue 128 + * @author Chengqi Xu + * @param prefix The prefix description given in compareJSONArray + * @param expected The expected length of the JSONArray + * @param actual The actual length of the JSONArray + * @return The description used as error message for JSONArray comparision + **/ + // CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/128 + public String getMessage(String prefix, int expected, int actual){ + StringBuilder msgBuilder = new StringBuilder(prefix); + String description1 = " values but got "; + String description2 = ""; + if (actual > 1) { + description2 = " values."; + } + else { + description2 = " value."; + } + if (expected <= 1) { + description1 = " value but got "; + } + msgBuilder.append("[]: Expected "); + msgBuilder.append(expected); + msgBuilder.append(description1); + msgBuilder.append(actual); + msgBuilder.append(description2); + return msgBuilder.toString(); + } + + /** + * + * @param prefix the path in the json where the comparison happens + * @param expected the expected JSON array + * @param actual the actual JSON array + * @param result stores the actual state of the comparison result + * @throws JSONException Used to indicate the error + */ + // CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/128 @Override public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException { if (expected.length() != actual.length()) { - String description = " values but got "; - if (expected.length() == 1) description = " value but got "; - result.fail(prefix + "[]: Expected " + expected.length() + description + actual.length()); + result.fail(getMessage(prefix,expected.length(), actual.length())); return; } else if (expected.length() == 0) { return; // Nothing to compare From fa0d3717775576dfe063910e197e77c94a8688aa Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Fri, 23 Apr 2021 21:11:10 +0800 Subject: [PATCH 3/8] Add two tests --- .../skyscreamer/jsonassert/JSONCompareTest.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java index 450adfde..cbb9adfc 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java @@ -37,12 +37,23 @@ public void succeedsWithEmptyArrays() throws JSONException { assertTrue(compareJSON("[]", "[]", LENIENT).passed()); } + // CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/128 + // Modify to fit the new error message @Test - public void reportsArraysOfUnequalLength() throws JSONException { - JSONCompareResult result = compareJSON("[4]", "[]", LENIENT); - assertThat(result, failsWithMessage(equalTo("[]: Expected 1 values but got 0"))); + public void arraysOfUnequalLength1() throws JSONException { + JSONCompareResult result = compareJSON("[4]", "[2, 3]", LENIENT); + assertThat(result, failsWithMessage(equalTo("[]: Expected 1 value but got 2 values."))); } + // CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/128 + // Add new test + @Test + public void arraysOfUnequalLength2() throws JSONException{ + JSONCompareResult result = compareJSON("[4,3,5]", "[]", LENIENT); + assertThat(result, failsWithMessage(equalTo("[]: Expected 3 values but got 0 value."))); + } + + @Test public void reportsArrayMissingExpectedElement() throws JSONException { JSONCompareResult result = compareJSON("[4]", "[7]", LENIENT); From f43c08b4f50eeff45a272e5777a14314e42f70b8 Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Mon, 17 May 2021 11:08:18 +0800 Subject: [PATCH 4/8] Add a new mode Provide a new mode which can ignore a list of fields. --- .../skyscreamer/jsonassert/JSONCompare.java | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index b963be9f..e968ef63 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -124,6 +124,199 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr throws JSONException { return compareJSON(expectedStr, actualStr, getComparatorForMode(mode)); } + + //CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * The recursive method to check if the JSONObject has the target field. + * If not, return false; if have, remove the target field and return true. + * + * @param target The target field that we want to check + * @param obj The target JSONObject that we want to check and remove the target field + * @return A boolean value indicates whether the target field is found + * @throws JSONException JSON parsing error + */ + protected static boolean checkAndRemove(String target, JSONObject obj) throws JSONException { + boolean result = false; + Iterator iterator = obj.keys(); + while (iterator.hasNext()){ + String key = iterator.next(); + Object value = obj.get(key); + if (value instanceof JSONObject){ + result = checkAndRemove(target, (JSONObject)value); + }else if (value instanceof JSONArray){ + result = false; + JSONArray valueArray = (JSONArray) value; + for (int i = 0; i < valueArray.length(); i++){ + if (valueArray.get(i) instanceof JSONObject) { + result = result | checkAndRemove(target, ((JSONArray) value).getJSONObject(i)); + }else if (valueArray.get(i) instanceof JSONArray){ + result = result | checkAndRemove(target, (JSONArray) ((JSONArray) value).get(i)); + } + } + } + } + if (obj.has(target)){ + result = true; + obj.remove(target); + } + return result; + } + + //CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * The recursive method to check if a JSON Array contains target field. + * Add this method to avoid recursively constructed JSON Array + * + * @param target The target field that we want to check + * @param obj he target JSONArray that we want to check and remove the target field + * @return A boolean value indicates whether the JSONArray contains the target field. + * @throws JSONException JSON parsing error + */ + protected static boolean checkAndRemove(String target, JSONArray obj) throws JSONException { + boolean result = false; + for (int i = 0; i < obj.length(); i++){ + if (obj.get(i) instanceof JSONArray){ + result = result | checkAndRemove(target, (JSONArray) obj.get(i)); + }else if (obj.get(i) instanceof JSONObject){ + result = result | checkAndRemove(target, (JSONObject) obj.get(i)); + } + } + return result; + } + + //CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Provide a ignore list, compare the two JSONObject ignoring these fields. + * If the ignore list is empty, which means you don't want to ignore anything, then, please use other method without ignore. + * + * @param expect Expected JSONObject + * @param actual JSONObject to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param mode Defines comparison behavior + * @return result of the comparison + * @throws JSONException JSON parsing error + */ + public static JSONCompareResult compareJSONWithIgnore(JSONObject expect, JSONObject actual, ArrayList ignoreList, JSONCompareMode mode) throws JSONException { + if (ignoreList.size() == 0){ + JSONCompareResult result = new JSONCompareResult(); + result.fail("Please use other mode if don't want to ignore any field"); + return result; + } + ArrayList errorFields = new ArrayList(); + for (String toIgnore : ignoreList) { + boolean have = checkAndRemove(toIgnore, expect) | checkAndRemove(toIgnore, actual); + if (!have){ + errorFields.add(toIgnore); + } + } + if (errorFields.size() == 0){ + return compareJSON(expect, actual, mode); + }else{ + JSONCompareResult result = new JSONCompareResult(); + StringBuilder message = new StringBuilder("Following ignore field(s) not found:\n"); + for (String field: errorFields){ + message.append(field); + message.append("\n"); + } + result.fail(message.toString()); + return result; + } + } + + //CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Provide a ignore list, compare the two JSONArray ignoring these fields. + * If the ignore list is empty, which means you don't want to ignore anything, then, please use other method without ignore. + * @param expect Expected JSONArray + * @param actual JSONArray to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param mode Defines comparison behavior + * @return result of the comparison + * @throws JSONException JSON parsing error + */ + public static JSONCompareResult compareJSONWithIgnore(JSONArray expect, JSONArray actual, ArrayList ignoreList, JSONCompareMode mode) throws JSONException{ + if (ignoreList.size() == 0){ + JSONCompareResult result = new JSONCompareResult(); + result.fail("Please use other mode if don't want to ignore any field"); + return result; + } + ArrayList errorFields = new ArrayList(); + for (String toIgnore: ignoreList){ + boolean have = false; + for (int i = 0; i < expect.length(); i++){ + if (expect.get(i) instanceof JSONObject) { + JSONObject element = expect.getJSONObject(i); + have = checkAndRemove(toIgnore, element) | have; + }else if (expect.get(i) instanceof JSONArray){ + JSONArray element = (JSONArray) expect.get(i); + have = checkAndRemove(toIgnore, element); + } + + } + for (int i = 0; i < actual.length(); i++){ + if (actual.get(i) instanceof JSONObject) { + JSONObject element = actual.getJSONObject(i); + have = checkAndRemove(toIgnore, element) | have; + }else if (actual.get(i) instanceof JSONArray){ + JSONArray element = (JSONArray) actual.get(i); + have = checkAndRemove(toIgnore, element) | have; + } + } + if (!have){ + errorFields.add(toIgnore); + } + } + if (errorFields.size() == 0){ + return compareJSON(expect, actual, mode); + }else{ + JSONCompareResult result = new JSONCompareResult(); + StringBuilder message = new StringBuilder("Following ignore field(s) not found:\n"); + for (String field: errorFields){ + message.append(field); + message.append("\n"); + } + result.fail(message.toString()); + return result; + } + } + + //CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Compare 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 ignoreList The list of field name (in String) wanted to ignore + * @param mode Defines comparison behavior + * @return result of the comparison + * @throws JSONException JSON parsing error + */ + public static JSONCompareResult compareJSONWithIgnore(String expectedStr, String actualStr, ArrayList ignoreList, JSONCompareMode mode) throws JSONException { + if (ignoreList.size() == 0){ + JSONCompareResult result = new JSONCompareResult(); + result.fail("Please use other mode if don't want to ignore any field"); + return result; + } + Object _expect = JSONParser.parseJSON(expectedStr); + Object _actual = JSONParser.parseJSON(actualStr); + if ((_expect instanceof JSONObject) && (_actual instanceof JSONObject)){ + return compareJSONWithIgnore((JSONObject)_expect, (JSONObject)_actual, ignoreList, mode); + }else if ((_expect instanceof JSONArray) && (_actual instanceof JSONArray)){ + return compareJSONWithIgnore((JSONArray)_expect, (JSONArray)_actual, ignoreList, mode); + }else if ((_expect instanceof JSONString) && (_actual instanceof JSONString)){ + return compareJSONWithIgnore(((JSONString)_expect).toJSONString(), ((JSONString)_actual).toJSONString(), ignoreList, mode); + }else if (_expect instanceof JSONObject) { + return new JSONCompareResult().fail("", _expect, _actual); + } + else { + return new JSONCompareResult().fail("", _expect, _actual); + } + } /** * Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison. From a6497c806306ea48f06cdf438a4e4796a1801b61 Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Mon, 17 May 2021 11:58:49 +0800 Subject: [PATCH 5/8] Add assert method with ignore --- .../skyscreamer/jsonassert/JSONAssert.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java index efebf43c..199a89e1 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java @@ -833,4 +833,124 @@ private static String getCombinedMessage(String message1, String message2) { } return combinedMessage; } + + //CS304 issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Asserts that the JSONString provided match the expected JSONString. + * If it isn't it throws an {@link AssertionError} + * + * @param expected Expected JSON String + * @param actual JSON String to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param strict Defines comparison behavior + * @throws JSONException JSON parsing error + */ + public static void assertEqualsWithIgnore(String expected, String actual, ArrayList ignoreList, boolean strict) throws JSONException { + JSONCompareResult result = JSONCompare.compareJSONWithIgnore(expected, actual, ignoreList, + strict?JSONCompareMode.STRICT:JSONCompareMode.LENIENT); + if (result.failed()){ + throw new AssertionError(result.getMessage()); + } + } + + //CS304 issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Asserts that the JSONObject provided match the expected JSONObject. + * If it isn't it throws an {@link AssertionError} + * + * @param expected Expected JSON Object + * @param actual JSON Object to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param strict Defines comparison behavior + * @throws JSONException JSON parsing error + */ + public static void assertEqualsWithIgnore(JSONObject expected, JSONObject actual, ArrayList ignoreList, boolean strict) throws JSONException{ + JSONCompareResult result = JSONCompare.compareJSONWithIgnore(expected, actual, ignoreList, + strict?JSONCompareMode.STRICT:JSONCompareMode.LENIENT); + if (result.failed()){ + throw new AssertionError(result.getMessage()); + } + } + + //CS304 issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Asserts that the JSONArray provided match the expected JSONArray. + * If it isn't it throws an {@link AssertionError} + * + * @param expected Expected JSON Array + * @param actual JSON Array to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param strict Defines comparison behavior + * @throws JSONException JSON parsing error + */ + public static void assertEqualsWithIgnore(JSONArray expected, JSONArray actual, ArrayList ignoreList, boolean strict) throws JSONException{ + JSONCompareResult result = JSONCompare.compareJSONWithIgnore(expected, actual, ignoreList, + strict?JSONCompareMode.STRICT:JSONCompareMode.LENIENT); + if (result.failed()){ + throw new AssertionError(result.getMessage()); + } + } + + //CS304 issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Asserts that the JSONString provided does not match the expected JSONString. + * If it is it throws an {@link AssertionError} + * + * @param expected Expected JSON String + * @param actual JSON String to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param strict Defines comparison behavior + * @throws JSONException JSON parsing error + */ + public static void assertNotEqualsWithIgnore(String expected, String actual, ArrayList ignoreList, boolean strict) throws JSONException{ + JSONCompareResult result = JSONCompare.compareJSONWithIgnore(expected, actual, ignoreList, + strict?JSONCompareMode.STRICT:JSONCompareMode.LENIENT); + if (result.passed()){ + throw new AssertionError("Two JSONs are equal!"); + } + } + + //CS304 issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Asserts that the JSONObject provided does not match the expected JSONObject. + * If it is it throws an {@link AssertionError} + * + * @param expected Expected JSON Object + * @param actual JSON Object to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param strict Defines comparison behavior + * @throws JSONException JSON parsing error + */ + public static void assertNotEqualsWithIgnore(JSONObject expected, JSONObject actual, ArrayList ignoreList, boolean strict) throws JSONException{ + JSONCompareResult result = JSONCompare.compareJSONWithIgnore(expected, actual, ignoreList, + strict?JSONCompareMode.STRICT:JSONCompareMode.LENIENT); + if (result.passed()){ + throw new AssertionError("Two JSONs are equal!"); + } + } + + //CS304 issue link: https://github.com/skyscreamer/JSONassert/issues/129 + + /** + * Asserts that the JSONArray provided does not match the expected JSONArray. + * If it is it throws an {@link AssertionError} + * + * @param expected Expected JSON Array + * @param actual JSON Array to compare + * @param ignoreList The list of field name (in String) wanted to ignore + * @param strict Defines comparison behavior + * @throws JSONException JSON parsing error + */ + public static void assertNotEqualsWithIgnore(JSONArray expected, JSONArray actual, ArrayList ignoreList, boolean strict) throws JSONException{ + JSONCompareResult result = JSONCompare.compareJSONWithIgnore(expected, actual, ignoreList, + strict?JSONCompareMode.STRICT:JSONCompareMode.LENIENT); + if (result.passed()){ + throw new AssertionError("Two JSONs are equal!"); + } + } } From 39f8b51e3503833e9c3f79ab32fe6df3dd10bbeb Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Mon, 17 May 2021 12:25:09 +0800 Subject: [PATCH 6/8] Add tests for ignore mode --- .../jsonassert/IgnoreModeTest.java | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 src/test/java/org/skyscreamer/jsonassert/IgnoreModeTest.java diff --git a/src/test/java/org/skyscreamer/jsonassert/IgnoreModeTest.java b/src/test/java/org/skyscreamer/jsonassert/IgnoreModeTest.java new file mode 100644 index 00000000..a99f1b16 --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/IgnoreModeTest.java @@ -0,0 +1,193 @@ +package org.skyscreamer.jsonassert; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Test; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; +import org.junit.internal.matchers.TypeSafeMatcher; + +import static org.skyscreamer.jsonassert.JSONCompare.compareJSONWithIgnore; + +import java.util.ArrayList; + +public class IgnoreModeTest { + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test single JSON object + @Test + public void testNormalString() throws JSONException { + String expected = "{a:12,currentTime:\"20:59\",b:233,c:345}"; + String actual = "{a:12,currentTime:\"21:09\",b:233,c:678}"; + ArrayList ignore = new ArrayList(); + ignore.add("currentTime"); + ignore.add("c"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + ignore.clear(); + ignore.add("c"); + JSONAssert.assertNotEqualsWithIgnore(expected, actual, ignore, true); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test recursively constructed JSON object + @Test + public void testRecursiveIgnore1() throws JSONException{ + String expected = "{a:{b:{c:1,d:2},e:3},f:[4,5],g:6}"; + String actual = "{a:{b:{c:10,d:2},e:3},f:[5,4],g:6}"; + ArrayList ignore = new ArrayList(); + ignore.add("f"); + ignore.add("c"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + ignore.clear(); + ignore.add("f"); + ignore.add("b"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + ignore.clear(); + ignore.add("a"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, false); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test recursively constructed JSON Array + @Test + public void testRecursiveIgnore2() throws JSONException{ + String excepted = "[[{a:1},{b:2}],[{c:3},{d:4}]]"; + String strictActual = "[[{a:1},{b:2}],[{c:3},{d:4}]]"; + String lenientActual = "[[{d:5},{c:3}],[{a:1},{b:2}]]"; + ArrayList ignore = new ArrayList(); + ignore.add("d"); + JSONAssert.assertEqualsWithIgnore(excepted, strictActual, ignore, true); + JSONAssert.assertEqualsWithIgnore(excepted, lenientActual, ignore, false); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test recursively constructed JSON Array and JSON objects + @Test + public void testRecursiveIgnore3() throws JSONException{ + String expected = "[[{a:1,e:[[{f:5},{g:6}]]},{b:2}],[{c:3},{d:4}]]"; + String actual = "[[{a:1,e:[[{f:5},{g:7}]]},{b:2}],[{c:3},{d:4}]]"; + ArrayList ignore = new ArrayList(); + ignore.add("g"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test invalid ignore (ignore list that contains fields not appear) + @Test + public void testInvalidIgnore() throws JSONException{ + String expected = "{a:2,b:3}"; + String actual = "{a:2,b:3}"; + ArrayList ignore = new ArrayList(); + ignore.add("invalid"); + JSONCompareResult result = compareJSONWithIgnore(expected,actual,ignore,JSONCompareMode.STRICT); + assertThat(result, failsWithMessage(equalTo("Following ignore field(s) not found:\ninvalid\n"))); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test empty ignore + @Test + public void testEmptyIgnore() throws JSONException{ + String expected = "{a:2,b:3}"; + String actual = "{a:2,b:3}"; + ArrayList ignore = new ArrayList(); + JSONCompareResult result = compareJSONWithIgnore(expected,actual,ignore,JSONCompareMode.STRICT); + assertThat(result, failsWithMessage(equalTo("Please use other mode if don't want to ignore any field"))); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test normal JSON array + @Test + public void testJSONArray1() throws JSONException{ + String expected = "[{a:1,b:[1,2],c:3},{a:1,b:[4,5],c:3}]"; + String actual = "[{a:1,b:[2,1,3],c:3},{a:1,b:[5,4],c:3}]"; + ArrayList ignore = new ArrayList(); + ignore.add("b"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test normal JSON array + @Test + public void testJSONArray2() throws JSONException{ + String expected = "[{a:{b:{c:1,d:2},e:3},f:[4,5],g:6},{h:7,c:8}]"; + String actual = "[{a:{b:{c:10,d:2},e:3},f:[5,4],g:6},{h:7,c:9}]"; + ArrayList ignore = new ArrayList(); + ignore.add("f"); + ignore.add("c"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test complex case + @Test + public void testComplexCase1() throws JSONException{ + String expected = "{h:[{a:1},{b:2}],curTime:567,c:4,i:[{d:{e:5,f:6},j:10}]}"; + String actual = "{h:[{a:1},{b:2}],curTime:678,c:4,i:[{d:{e:8,f:9},j:10}]}"; + ArrayList ignore = new ArrayList(); + ignore.add("curTime"); + ignore.add("d"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test complex case + @Test + public void testComplexCase2() throws JSONException{ + String expected = "[{a:[{l:{b:{c:[{d:1}],e:2}}},{f:3}]},{i:{g:4,j:{k:5}}},{curTime:345}]"; + String actual = "[{curTime:678},{a:[{l:{b:{c:[{d:7}],e:2}}},{f:8}]},{i:{g:4,j:{k:6}}}]"; + ArrayList ignore = new ArrayList(); + ignore.add("d"); + ignore.add("f"); + ignore.add("k"); + ignore.add("curTime"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, false); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test assert method with JSONObject + @Test + public void testJSONObject() throws JSONException{ + JSONObject expected = new JSONObject(); + JSONObject actual = new JSONObject(); + expected.put("a",1); + expected.put("b",2); + expected.put("c",3); + actual.put("a",1); + actual.put("b",4); + actual.put("c",5); + ArrayList ignore = new ArrayList(); + ignore.add("b"); + ignore.add("c"); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, true); + ignore.remove(0); + JSONAssert.assertNotEqualsWithIgnore(expected, actual, ignore, true); + } + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/129 + //Test assert method with JSONArray + @Test + public void testJSONArray() throws JSONException{ + JSONArray expected = new JSONArray(); + JSONArray actual = new JSONArray(); + JSONObject obj1 = new JSONObject(); + JSONObject obj2 = new JSONObject(); + obj1.put("a", 1); + obj1.put("b", 2); + obj2.put("c", 3); + obj2.put("d", 4); + ArrayList ignore = new ArrayList(); + ignore.add("a"); + ignore.add("b"); + expected.put(obj1); + expected.put(obj2); + actual.put(obj2); + actual.put(obj1); + JSONAssert.assertEqualsWithIgnore(expected, actual, ignore, false); + JSONAssert.assertNotEqualsWithIgnore(expected, actual, ignore, true); + } + + private Matcher failsWithMessage(final Matcher expectedMessage) { + return new TypeSafeMatcher() { + @Override + public void describeTo(Description description) { + description.appendText("a failed comparison with message ").appendDescriptionOf(expectedMessage); + } + + @Override + public boolean matchesSafely(JSONCompareResult item) { + return item.failed() && expectedMessage.matches(item.getMessage()); + } + }; + } +} From 574b72a0d65d07462f5036821947c119a677a116 Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Mon, 17 May 2021 12:29:41 +0800 Subject: [PATCH 7/8] Fix some bugs --- src/main/java/org/skyscreamer/jsonassert/JSONAssert.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java index 199a89e1..3fe9c9e8 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONAssert.java @@ -18,6 +18,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.comparator.JSONComparator; +import java.util.ArrayList; /** *

A set of assertion methods useful for writing tests methods that return JSON.

From 0f6621ff1588d14c94ca36add6a066c60a1e2fcc Mon Sep 17 00:00:00 2001 From: LightingTom <59653302+LightingTom@users.noreply.github.com> Date: Mon, 17 May 2021 12:30:07 +0800 Subject: [PATCH 8/8] Fix some bugs --- src/main/java/org/skyscreamer/jsonassert/JSONCompare.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index e968ef63..8c28d3e0 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -20,6 +20,8 @@ import org.json.JSONString; import org.skyscreamer.jsonassert.comparator.DefaultComparator; import org.skyscreamer.jsonassert.comparator.JSONComparator; +import java.util.ArrayList; +import java.util.Iterator; /** * Provides API to compare two JSON entities. This is the backend to {@link JSONAssert}, but it can