Skip to content

Commit 02b074b

Browse files
committed
Merge remote-tracking branch 'upstream/master' into enum-enhance
2 parents c8c6fce + b4513d8 commit 02b074b

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

src/main/java/org/skyscreamer/jsonassert/JSONCompare.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@ else if (expectedValue instanceof JSONObject) {
112112
else if (!expectedValue.equals(actualValue)) {
113113
result.fail(fullKey, expectedValue, actualValue);
114114
}
115+
} else {
116+
if (isNull(expectedValue)) {
117+
result.fail(fullKey + ": expected null, but got " + classToType(actualValue));
118+
} else if (isNull(actualValue)) {
119+
result.fail(fullKey + ": expected " + classToType(expectedValue) + ", but got null");
120+
} else {
121+
result.fail("Values of " + fullKey + " have different types: expected " + classToType(expectedValue)
122+
+ ", but got " + classToType(actualValue));
123+
}
124+
}
125+
}
126+
127+
private static boolean isNull(Object value) {
128+
return value.getClass().getSimpleName().equals("Null");
129+
}
130+
131+
private static String classToType(Object value) {
132+
if (value instanceof JSONArray) {
133+
return "an array";
134+
} else if (value instanceof JSONObject) {
135+
return "an object";
136+
} else if (value instanceof String) {
137+
return "a string";
138+
} else {
139+
return value.getClass().getName();
115140
}
116141
}
117142

src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
public class JSONCompareResult {
77
private boolean _success;
88
private String _message;
9+
private String _field;
10+
private Object _expected;
11+
private Object _actual;
912

1013
/**
1114
* Default constructor.
@@ -42,6 +45,45 @@ public boolean failed() {
4245
public String getMessage() {
4346
return _message;
4447
}
48+
49+
/**
50+
* Actual field value
51+
*
52+
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
53+
* instance, or {@code null} if the comparison did not fail on a
54+
* particular field
55+
*/
56+
public Object getActual() {
57+
return _actual;
58+
}
59+
60+
/**
61+
* Expected field value
62+
*
63+
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
64+
* instance, or {@code null} if the comparison did not fail on a
65+
* particular field
66+
*/
67+
public Object getExpected() {
68+
return _expected;
69+
}
70+
71+
/**
72+
* Check if comparison failed on a particular field
73+
*/
74+
public boolean isFailureOnField() {
75+
return _field != null;
76+
}
77+
78+
/**
79+
* Dot-separated path the the field that failed comparison
80+
*
81+
* @return a {@code String} instance, or {@code null} if the comparison did
82+
* not fail on a particular field
83+
*/
84+
public String getField() {
85+
return _field;
86+
}
4587

4688
protected void fail(String message) {
4789
_success = false;
@@ -54,13 +96,20 @@ protected void fail(String message) {
5496
}
5597

5698
protected void fail(String field, Object expected, Object actual) {
99+
this._field = field;
100+
this._expected = expected;
101+
this._actual = actual;
102+
fail(formatFailureMessage(field, expected, actual));
103+
}
104+
105+
private String formatFailureMessage(String field, Object expected, Object actual) {
57106
StringBuffer message= new StringBuffer();
58107
message.append(field);
59108
message.append("\nExpected: ");
60109
message.append(expected + "");
61110
message.append("\n got: ");
62111
message.append(actual + "");
63112
message.append("\n");
64-
fail(message.toString());
113+
return message.toString();
65114
}
66115
}

src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ public void testArrayOfArraysStrict() throws JSONException {
135135
public void testArrayOfArrays() throws JSONException {
136136
testPass("{id:1,stuff:[[4,3],[3,2],[],[1,2]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", false);
137137
}
138+
139+
@Test
140+
public void testFieldMismatch() throws JSONException {
141+
JSONCompareResult result = JSONCompare.compareJSON("{name:\"Pat\"}", "{name:\"Sue\"}", STRICT);
142+
Assert.assertEquals("Pat", result.getExpected());
143+
Assert.assertEquals("Sue", result.getActual());
144+
Assert.assertEquals("name", result.getField());
145+
}
146+
147+
@Test
148+
public void testNullProperty() throws JSONException {
149+
testFail("{id:1,name:\"Joe\"}", "{id:1,name:null}", true);
150+
testFail("{id:1,name:null}", "{id:1,name:\"Joe\"}", true);
151+
}
152+
153+
@Test
154+
public void testIncorrectTypes() throws JSONException {
155+
testFail("{id:1,name:\"Joe\"}", "{id:1,name:[]}", true);
156+
testFail("{id:1,name:[]}", "{id:1,name:\"Joe\"}", true);
157+
}
158+
159+
@Test
160+
public void testNullEquality() throws JSONException {
161+
testPass("{id:1,name:null}", "{id:1,name:null}", true);
162+
}
138163

139164
private void testPass(String expected, String actual, boolean strict)
140165
throws JSONException

0 commit comments

Comments
 (0)