From 461199b69ce8f72b57b5f3d8af1b407f953460f1 Mon Sep 17 00:00:00 2001 From: GreatBryan <50838630+GreatBryan@users.noreply.github.com> Date: Thu, 15 Apr 2021 19:36:17 +0800 Subject: [PATCH 1/3] Update JSONCompare.java --- .../skyscreamer/jsonassert/JSONCompare.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index b963be9f..10daea09 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -21,6 +21,9 @@ import org.skyscreamer.jsonassert.comparator.DefaultComparator; import org.skyscreamer.jsonassert.comparator.JSONComparator; +import java.util.regex.*; + + /** * Provides API to compare two JSON entities. This is the backend to {@link JSONAssert}, but it can * be programmed against directly to access the functionality. (eg, to make something that works with a @@ -46,6 +49,12 @@ private static JSONComparator getComparatorForMode(JSONCompareMode mode) { */ public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) throws JSONException { + /** + * CS304 Issue {@link: https://github.com/skyscreamer/JSONassert/issues/107} + */ + expectedStr = NumberParse(expectedStr); // Put double quotation marks on the numbers in expectedStr + actualStr = NumberParse(actualStr); // Put double quotation marks on the numbers in expectedStr + Object expected = JSONParser.parseJSON(expectedStr); Object actual = JSONParser.parseJSON(actualStr); if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) { @@ -65,6 +74,40 @@ else if (expected instanceof JSONObject) { } } + /** + * @param str Put double quotation marks on the numbers in String str + * intend to fix issue 107 + * CS304 Issue {@link: https://github.com/skyscreamer/JSONassert/issues/107} + * To fix the bug that JSONCompareResult is pass when the + * difference of two number exceeds the accuracy of Double, + * I create a static method NumberParse. + * Since the number stored in JSONObject is default double, it will lost accuracy + * when the number in string exceeds the accuracy of Double. + * So, method NumberParse puts double quotation marks on the numbers in expectedStr + * which transform the number into String to ensure the accuracy will not be lost. + * @Time 2021.4.15 Tingyan Feng + */ + public static String NumberParse(String str){ + Pattern p1 = Pattern.compile(":\\s*\\d+\\.?\\d+\\s*"); + Pattern p2 = Pattern.compile("\\d+\\.?\\d+"); + Matcher m =p1.matcher(str); + + while(true){ + if (m.find()) { + Matcher m2 = p2.matcher(m.group()); + if(m2.find()){ + str = str.replaceAll(m2.group(), "\""+ m2.group() +"\""); + }else{ + break; + } + }else{ + break; + } + m = p1.matcher(str); + } + return str; + } + /** * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of * the comparison. From 32f1328106b0d2127304bf349557fb82ca0c3eee Mon Sep 17 00:00:00 2001 From: GreatBryan <50838630+GreatBryan@users.noreply.github.com> Date: Thu, 15 Apr 2021 19:37:35 +0800 Subject: [PATCH 2/3] Update JSONAssertTest.java --- .../jsonassert/JSONAssertTest.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java index e39c80ba..adf0770b 100644 --- a/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java +++ b/src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java @@ -65,6 +65,42 @@ public void testNumber() throws JSONException { testFail("310.1e-1", "31.01", STRICT); // should fail though numbers are the same? } + /** + * Test cases for issue 107. + * CS304 Issue {@link: https://github.com/skyscreamer/JSONassert/issues/107} + * In origin case, there is a bug that JSONCompareResult assumes pass when the + * difference of two number exceeds the accuracy of Double. + * I create some new testcases below for this bug. + * @Time 2021.4.15 Tingyan Feng + */ + @Test + public void testBigNumber() throws JSONException{ + testPass("{ \"value\": 1234567890.1234567890123456 }", + "{ \"value\": 1234567890.1234567890123456 }", + STRICT); // Pass when big number is the same. + testPass("{ \"value\": 1234567890.1234567890123456 }", + "{ \"value\": 1234567890.1234567890123456 }", + LENIENT);// Pass when big number is the same. + testPass("{ \"value\": 12345678901234567890123456 }", + "{ \"value\": 12345678901234567890123456 }", + STRICT);// Pass when big number is the same. + testPass("{ \"value\": 12345678901234567890123456 }", + "{ \"value\": 12345678901234567890123456 }", + LENIENT);// Pass when big number is the same. + testFail("{ \"value\": 1234567890.1234567890123456 }", + "{ \"value\": 1234567890.1234567000000000 }", + STRICT);// Fail when the number is the different and the difference exceeds the accuracy of double. + testFail("{ \"value\": 1234567890.1234567890123456 }", + "{ \"value\": 1234567890.1234567000000000 }", + LENIENT);// Fail when the number is the different and the difference exceeds the accuracy of double. + testFail("{ \"value\": 12345678901234567890123456 }", + "{ \"value\": 12345678901234567800000000 }", + STRICT);// Fail when the number is the different and the difference exceeds the accuracy of double. + testFail("{ \"value\": 12345678901234567890123456 }", + "{ \"value\": 12345678901234567800000000 }", + LENIENT);// Fail when the number is the different and the difference exceeds the accuracy of double. + } + @Test public void testSimple() throws JSONException { testPass("{id:1}", "{id:1}", STRICT); @@ -631,7 +667,7 @@ public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentExc JSONCompareMode.STRICT, new Customization("entry.id", new RegularExpressionValueMatcher("\\d")) - )); + )); performAssertNotEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}", new CustomComparator( From c42fec664176d9fda7dca2acc1bae98a45cd5652 Mon Sep 17 00:00:00 2001 From: GreatBryan <50838630+GreatBryan@users.noreply.github.com> Date: Mon, 10 May 2021 12:10:59 +0800 Subject: [PATCH 3/3] Add files via upload --- .../skyscreamer/jsonassert/issue109Test.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/test/java/org/skyscreamer/jsonassert/issue109Test.java diff --git a/src/test/java/org/skyscreamer/jsonassert/issue109Test.java b/src/test/java/org/skyscreamer/jsonassert/issue109Test.java new file mode 100644 index 00000000..006ad6c1 --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/issue109Test.java @@ -0,0 +1,140 @@ +package org.skyscreamer.jsonassert; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; +import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT; +import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.internal.matchers.TypeSafeMatcher; +import org.skyscreamer.jsonassert.comparator.CustomComparator; + + + +public class issue109Test { + + + /** + * intend to fix issue 109 + * CS304 Issue {@link: https://github.com/skyscreamer/JSONassert/issues/109} + * Some users need to compare two JSONObject by ignoring some fields, + * Could use JSONCompareMode.STRICT and new Customization to achieve this goal. + * @Time 2021.5.13 Tingyan Feng + */ + @Test + public void test1() throws JSONException { + JSONObject json1 = new JSONObject("{" + + "\t\"level00\": {\n" + + "\t\t\"level01\": [{\n" + + "\t\t\t\"id\": \"11111111-1111-1111-1111-111111111111\",\n" + + "\t\t\t\"name\": \"Name01\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:11:11.111Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:11:11.111Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t},\n" + + "\t\t{\n" + + "\t\t\t\"id\": \"22222222-2222-2222-2222-222222222222\",\n" + + "\t\t\t\"name\": \"Name02\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:22:22.222Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:22:22.222Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t}]\n" + + "\t}\n" + + "}"); + JSONObject json2 = new JSONObject("{\n" + + "\t\"level00\": {\n" + + "\t\t\"level01\": [{\n" + + "\t\t\t\"id\": \"11111111-1111-1111-1111-111111111111\",\n" + + "\t\t\t\"name\": \"Name01\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:33:33.333Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:33:33.333Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t},\n" + + "\t\t{\n" + + "\t\t\t\"id\": \"22222222-2222-2222-2222-222222222222\",\n" + + "\t\t\t\"name\": \"Name02\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:44:44.444Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:44:44.444Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t}]\n" + + "\t}\n" + + "}"); + + + JSONAssert.assertEquals(json1, json2, + new CustomComparator(JSONCompareMode.STRICT, + new Customization("**.updatedAt", (o1, o2) -> true), + new Customization("**.createdAt", (o1, o2) -> true) + )); + } + + @Test + public void test2() throws JSONException { + JSONObject json1 = new JSONObject("{" + + "\t\"level00\": {\n" + + "\t\t\"level01\": [{\n" + + "\t\t\t\"id\": \"11111111-1111-1111-1111-111111111111\",\n" + + "\t\t\t\"name\": \"Name01\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:11:11.111Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:11:11.111Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t},\n" + + "\t\t{\n" + + "\t\t\t\"id\": \"22222222-2222-2222-2222-222222222222\",\n" + + "\t\t\t\"name\": \"Name02\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:22:22.222Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:22:22.222Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t}]\n" + + "\t}\n" + + "}"); + JSONObject json2 = new JSONObject("{\n" + + "\t\"level00\": {\n" + + "\t\t\"level01\": [{\n" + + "\t\t\t\"id\": \"11111111-1111-1111-1111-0\",\n" + + "\t\t\t\"name\": \"Name02\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:11:11.111Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:11:11.111Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t},\n" + + "\t\t{\n" + + "\t\t\t\"id\": \"22222222-2222-2222-2222-222222222222\",\n" + + "\t\t\t\"name\": \"Name02\",\n" + + "\t\t\t\"createdAt\": \"2018-01-01T11:22:22.222Z\",\n" + + "\t\t\t\"updatedAt\": \"2018-01-01T11:22:22.222Z\",\n" + + "\t\t\t\"clientApplication\": \"AAAAA\",\n" + + "\t\t\t\"version\": 1,\n" + + "\t\t\t\"options\": null\n" + + "\t\t}]\n" + + "\t}\n" + + "}"); + + + JSONAssert.assertEquals(json1, json2, + new CustomComparator(JSONCompareMode.STRICT, + new Customization("**.id", (o1, o2) -> true), + new Customization("**.name", (o1, o2) -> true) + )); + } +}