From 9b9522efe03e61c9141737be14f6b36a69f47a72 Mon Sep 17 00:00:00 2001 From: csp <2532880745@qq.com> Date: Fri, 23 Apr 2021 21:23:57 +0800 Subject: [PATCH 1/2] pull request of issue 103 --- pom.xml | 11 ++- .../skyscreamer/jsonassert/JSONCompare.java | 67 +++++++++++++------ .../comparator/ArraySizeComparator.java | 2 +- .../skyscreamer/jsonassert/Issue130Test.java | 44 ++++++++++++ .../TestWithNoneEqualLengthArray.java | 46 +++++++++++++ .../comparator/CrashedJSONCompareTest.java | 31 +++++++++ 6 files changed, 176 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/skyscreamer/jsonassert/Issue130Test.java create mode 100644 src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java create mode 100644 src/test/java/org/skyscreamer/jsonassert/comparator/CrashedJSONCompareTest.java diff --git a/pom.xml b/pom.xml index 6c369586..3970acdd 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.skyscreamer jsonassert - 1.5.1-SNAPSHOT + 1.5.0 jar JSONassert @@ -57,9 +57,14 @@ junit junit - 4.13.1 + 4.10 test + + com.alibaba + fastjson + 1.1.72.android + @@ -67,7 +72,7 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.1 + 3.8.1 1.6 1.6 diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index b963be9f..b6810bc8 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -10,10 +10,11 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -*/ + */ package org.skyscreamer.jsonassert; +import com.alibaba.fastjson.JSON; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -37,11 +38,12 @@ private static JSONComparator getComparatorForMode(JSONCompareMode mode) { /** * Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of * the comparison. + * * @param expectedStr Expected JSON string - * @param actualStr JSON string to compare - * @param comparator Comparator to use + * @param actualStr JSON string to compare + * @param comparator Comparator to use * @return result of the comparison - * @throws JSONException JSON parsing error + * @throws JSONException JSON parsing error * @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr */ public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) @@ -50,26 +52,23 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr Object actual = JSONParser.parseJSON(actualStr); if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) { return compareJSON((JSONObject) expected, (JSONObject) actual, comparator); - } - else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) { - return compareJSON((JSONArray)expected, (JSONArray)actual, comparator); - } - else if (expected instanceof JSONString && actual instanceof JSONString) { + } else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) { + return compareJSON((JSONArray) expected, (JSONArray) actual, comparator); + } else if (expected instanceof JSONString && actual instanceof JSONString) { return compareJson((JSONString) expected, (JSONString) actual); - } - else if (expected instanceof JSONObject) { + } else if (expected instanceof JSONObject) { return new JSONCompareResult().fail("", expected, actual); - } - else { + } else { return new JSONCompareResult().fail("", expected, actual); } } - /** + /** * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of * the comparison. - * @param expected expected json object - * @param actual actual json object + * + * @param expected expected json object + * @param actual actual json object * @param comparator comparator to use * @return result of the comparison * @throws JSONException JSON parsing error @@ -82,8 +81,9 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu /** * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of * the comparison. - * @param expected expected json array - * @param actual actual json array + * + * @param expected expected json array + * @param actual actual json array * @param comparator comparator to use * @return result of the comparison * @throws JSONException JSON parsing error @@ -106,11 +106,12 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO final String expectedJson = expected.toJSONString(); final String actualJson = actual.toJSONString(); if (!expectedJson.equals(actualJson)) { - result.fail(""); + result.fail(""); } return result; } + //CS304 Issue link:https://github.com/skyscreamer/JSONassert/issues/103 /** * Compares JSON string provided to the expected JSON string, and returns the results of the comparison. * @@ -120,11 +121,17 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO * @return result of the comparison * @throws JSONException JSON parsing error */ - public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) + public static JSONCompareResult compareJSON(String expectedStr, String + actualStr, JSONCompareMode mode) throws JSONException { - return compareJSON(expectedStr, actualStr, getComparatorForMode(mode)); + if ((mode == JSONCompareMode.STRICT) && (!isJSON(expectedStr) || !isJSON(actualStr))) { + return new JSONCompareResult().fail("", expectedStr, actualStr); + } + return compareJSON(expectedStr, actualStr, + getComparatorForMode(mode)); } + /** * Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison. * @@ -154,4 +161,22 @@ public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual return compareJSON(expected, actual, getComparatorForMode(mode)); } + //CS304 Issue link:https://github.com/skyscreamer/JSONassert/issues/103 + + /** + * Judge if the given string can be converted into JSON object. + * + * @param str JSON string + * @return if the string can be convert into JSON Object + */ + public static boolean isJSON(String str) { + boolean result = false; + try { + Object obj = JSON.parse(str); + result = true; + } catch (Exception e) { + result = false; + } + return result; + } } diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java index 35a62981..27ee9c5e 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java @@ -53,7 +53,7 @@ public class ArraySizeComparator extends DefaultComparator { * Create new ArraySizeComparator. * * @param mode - * comparison mode, has no impact on ArraySizeComparator but is + * FindBugs comparison mode, has no impact on ArraySizeComparator but is * used by instance of superclass DefaultComparator to control * comparison of JSON items other than arrays. */ diff --git a/src/test/java/org/skyscreamer/jsonassert/Issue130Test.java b/src/test/java/org/skyscreamer/jsonassert/Issue130Test.java new file mode 100644 index 00000000..7e2a4929 --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/Issue130Test.java @@ -0,0 +1,44 @@ +package org.skyscreamer.jsonassert; + +import org.json.JSONException; +import org.junit.Test; + +/**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 + * */ + +public class Issue130Test { + /**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 + * test case: + * to test whether {@throws NullPointerException} + * expected result: + * java.lang.AssertionError: [1] + * Expected: null + * got: a JSON object + */ + @Test + public void test130() throws JSONException { + JSONAssert.assertEquals("[{id:1},]" , "[{id:1},{}]" , true);//not expected + } + /**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 + * test case: + * to test whether {@throws NullPointerException} + * expected result: + * java.lang.AssertionError: [1] + * Expected: a JSON object + * got: null + */ + @Test + public void test130_0() throws JSONException { + JSONAssert.assertEquals("[{id:1},{}]" , "[{id:1},]" , true);//expected + } + /**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 + * test case: + * to test the two same wrong jsons + * expected result: + * Test passed + */ + @Test + public void test130_1() throws JSONException { + JSONAssert.assertEquals("[{id:1},]" , "[{id:1},]" , true); + } +} \ No newline at end of file diff --git a/src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java b/src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java new file mode 100644 index 00000000..345eee8f --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java @@ -0,0 +1,46 @@ +package org.skyscreamer.jsonassert; + + +import org.json.JSONException; +import org.junit.Test; +import org.skyscreamer.jsonassert.JSONAssert; + + +public class MyTestWithNoneEqualLengthArray { + + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/108 + /* + Test two normal json arrays with different size + should throw exceptions about details of each array's content + */ + @Test + public void test1() throws JSONException { + String expected="[1,2,3]"; + String actual="[4,5]"; + JSONAssert.assertEquals(expected,actual,false); + } + + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/108 + /* + Test two complex json arrays with json object in content + should throw exceptions about details of the json objects in each array + */ + @Test + public void test2() throws JSONException { + String expected="[{a1:1},2,3]"; + String actual="[{a1:1},5]"; + JSONAssert.assertEquals(expected,actual,false); + } + + //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/108 + /* + Test more complex json arrays with nested arrays in content + should throw exceptions about details of nested arrays of each complex array + */ + @Test + public void test3() throws JSONException { + String expected="[{a1:1},[2,3],4]"; + String actual="[[2,3],5]"; + JSONAssert.assertEquals(expected,actual,false); + } +} diff --git a/src/test/java/org/skyscreamer/jsonassert/comparator/CrashedJSONCompareTest.java b/src/test/java/org/skyscreamer/jsonassert/comparator/CrashedJSONCompareTest.java new file mode 100644 index 00000000..224e251f --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/comparator/CrashedJSONCompareTest.java @@ -0,0 +1,31 @@ +package org.skyscreamer.jsonassert.comparator; + +import org.json.JSONException; +import org.junit.Test; +import org.skyscreamer.jsonassert.JSONCompare; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.skyscreamer.jsonassert.JSONCompareResult; + +import static org.junit.Assert.assertTrue; +import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; + +//CS304 Issue link:https://github.com/skyscreamer/JSONassert/issues/103 +public class CrashedJSONCompareTest { + @Test + public void shouldJudgeValidJson() throws JSONException { + String validJson = "{\"id\":\"abc\"}"; + String crashedJson1 = "{\"id\":'abc'}"; + String crashedJson2 = "{\"id\":abc}"; + assertTrue(JSONCompare.isJSON(validJson)); + assertTrue(JSONCompare.isJSON(crashedJson1)); + assertTrue(!JSONCompare.isJSON(crashedJson2)); + } + @Test + public void shouldFailOnCrashedJson() throws JSONException{ + String validJson = "{\"id\":\"abc\"}"; + String crashedJson = "{\"id\":abc}"; + JSONCompareResult jsonCompareResult = compareJSON(validJson, + crashedJson, JSONCompareMode.STRICT); + assertTrue(jsonCompareResult.failed()); + } +} From b1bf2528eaae43044fada6fb34adb3eab6b99ce2 Mon Sep 17 00:00:00 2001 From: csp <2532880745@qq.com> Date: Fri, 23 Apr 2021 21:28:03 +0800 Subject: [PATCH 2/2] update for issue 103 --- .../skyscreamer/jsonassert/Issue130Test.java | 44 ------------------ .../TestWithNoneEqualLengthArray.java | 46 ------------------- 2 files changed, 90 deletions(-) delete mode 100644 src/test/java/org/skyscreamer/jsonassert/Issue130Test.java delete mode 100644 src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java diff --git a/src/test/java/org/skyscreamer/jsonassert/Issue130Test.java b/src/test/java/org/skyscreamer/jsonassert/Issue130Test.java deleted file mode 100644 index 7e2a4929..00000000 --- a/src/test/java/org/skyscreamer/jsonassert/Issue130Test.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.skyscreamer.jsonassert; - -import org.json.JSONException; -import org.junit.Test; - -/**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 - * */ - -public class Issue130Test { - /**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 - * test case: - * to test whether {@throws NullPointerException} - * expected result: - * java.lang.AssertionError: [1] - * Expected: null - * got: a JSON object - */ - @Test - public void test130() throws JSONException { - JSONAssert.assertEquals("[{id:1},]" , "[{id:1},{}]" , true);//not expected - } - /**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 - * test case: - * to test whether {@throws NullPointerException} - * expected result: - * java.lang.AssertionError: [1] - * Expected: a JSON object - * got: null - */ - @Test - public void test130_0() throws JSONException { - JSONAssert.assertEquals("[{id:1},{}]" , "[{id:1},]" , true);//expected - } - /**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130 - * test case: - * to test the two same wrong jsons - * expected result: - * Test passed - */ - @Test - public void test130_1() throws JSONException { - JSONAssert.assertEquals("[{id:1},]" , "[{id:1},]" , true); - } -} \ No newline at end of file diff --git a/src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java b/src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java deleted file mode 100644 index 345eee8f..00000000 --- a/src/test/java/org/skyscreamer/jsonassert/TestWithNoneEqualLengthArray.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.skyscreamer.jsonassert; - - -import org.json.JSONException; -import org.junit.Test; -import org.skyscreamer.jsonassert.JSONAssert; - - -public class MyTestWithNoneEqualLengthArray { - - //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/108 - /* - Test two normal json arrays with different size - should throw exceptions about details of each array's content - */ - @Test - public void test1() throws JSONException { - String expected="[1,2,3]"; - String actual="[4,5]"; - JSONAssert.assertEquals(expected,actual,false); - } - - //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/108 - /* - Test two complex json arrays with json object in content - should throw exceptions about details of the json objects in each array - */ - @Test - public void test2() throws JSONException { - String expected="[{a1:1},2,3]"; - String actual="[{a1:1},5]"; - JSONAssert.assertEquals(expected,actual,false); - } - - //CS304 (manually written) Issue link: https://github.com/skyscreamer/JSONassert/issues/108 - /* - Test more complex json arrays with nested arrays in content - should throw exceptions about details of nested arrays of each complex array - */ - @Test - public void test3() throws JSONException { - String expected="[{a1:1},[2,3],4]"; - String actual="[[2,3],5]"; - JSONAssert.assertEquals(expected,actual,false); - } -}