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 #130 #136

Open
wants to merge 5 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,29 @@ public JSONCompareResult fail(String field, ValueMatcherException exception) {
return this;
}

/**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/127
* modified by adding a case when {@code expected} or {@code actual} == null,
* which the JSON may be an invalid JSON,
* return a warning sentence to inform the tester.
*/
private String formatFailureMessage(String field, Object expected, Object actual) {
return field
String describeExp = describe(expected);
String describeAct = describe(actual);
String message = field
+ "\nExpected: "
+ describe(expected)
+ describeExp
+ "\n got: "
+ describe(actual)
+ describeAct
+ "\n";
if(describeExp.equals("null")){
message += "the expected JSON may be an invalid JSON"
+ "\n";
}
if(describeAct.equals("null")){
message += "the actual JSON may be an invalid JSON"
+ "\n";
}
return message;
}

/**
Expand Down Expand Up @@ -239,12 +255,20 @@ private String formatUnexpected(String field, Object actual) {
+ "\n";
}

/**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130
* modified by adding a case when {@code value} == null,
* return a String "null" to avoid {@exception NullPointer}
*/
private static String describe(Object value) {
if (value instanceof JSONArray) {
return "a JSON array";
} else if (value instanceof JSONObject) {
return "a JSON object";
} else {
}
else if(value == null){
return "null";
}
else {
return value.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
}
}


/**CS304 Issue link: https://github.com/skyscreamer/JSONassert/issues/130
* modified by adding {@code return} to the second {@code if}.
* because one of the {@param expectedValue} and {@param actualValue}
* should be null can enter the the second {@code if},
* after that if continue running, the next function {@code .getClass()}
* will always cause a {@exception NullPointer}.
* to avoid that case the {@code return} is added to the second {@code if}.
*/
@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
throws JSONException {
Expand All @@ -55,6 +64,7 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu
}
if ((expectedValue == null && actualValue != null) || (expectedValue != null && actualValue == null)) {
result.fail(prefix, expectedValue, actualValue);
return;
}
if (areNumbers(expectedValue, actualValue)) {
if (areNotSameDoubles(expectedValue, actualValue)) {
Expand Down