Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS304 Issue {@link: https://github.com/skyscreamer/JSONassert/issues/107}{@link: https://github.com/skyscreamer/JSONassert/issues/109} #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/org/skyscreamer/jsonassert/JSONCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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.
Expand Down
38 changes: 37 additions & 1 deletion src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -631,7 +667,7 @@ public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentExc
JSONCompareMode.STRICT,
new Customization("entry.id",
new RegularExpressionValueMatcher<Object>("\\d"))
));
));

performAssertNotEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}",
new CustomComparator(
Expand Down
140 changes: 140 additions & 0 deletions src/test/java/org/skyscreamer/jsonassert/issue109Test.java
Original file line number Diff line number Diff line change
@@ -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)
));
}
}