forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONCompareResult.java
More file actions
184 lines (162 loc) · 4.97 KB
/
JSONCompareResult.java
File metadata and controls
184 lines (162 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package org.skyscreamer.jsonassert;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Bean for holding results from JSONCompare.
*/
public class JSONCompareResult {
private boolean _success;
private String _message;
private String _field;
private Object _expected;
private Object _actual;
private final List<FieldComparisonFailure> _fieldFailures = new ArrayList<FieldComparisonFailure>();
/**
* Default constructor.
*/
public JSONCompareResult() {
this(true, null);
}
private JSONCompareResult(boolean success, String message) {
_success = success;
_message = message == null ? "" : message;
}
/**
* Did the comparison pass?
* @return True if it passed
*/
public boolean passed() {
return _success;
}
/**
* Did the comparison fail?
* @return True if it failed
*/
public boolean failed() {
return !_success;
}
/**
* Result message
* @return String explaining why if the comparison failed
*/
public String getMessage() {
return _message;
}
/**
* Get the list of failures on field comparisons
*/
public List<FieldComparisonFailure> getFieldFailures() {
return Collections.unmodifiableList(_fieldFailures);
}
/**
* Actual field value
*
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
* instance, or {@code null} if the comparison did not fail on a
* particular field
* @deprecated Superseded by {@link #getFieldFailures()}
*/
public Object getActual() {
return _actual;
}
/**
* Expected field value
*
* @return a {@code JSONObject}, {@code JSONArray} or other {@code Object}
* instance, or {@code null} if the comparison did not fail on a
* particular field
* @deprecated Superseded by {@link #getFieldFailures()}
*/
public Object getExpected() {
return _expected;
}
/**
* Check if comparison failed on any particular fields
*/
public boolean isFailureOnField() {
return !_fieldFailures.isEmpty();
}
/**
* Dot-separated path the the field that failed comparison
*
* @return a {@code String} instance, or {@code null} if the comparison did
* not fail on a particular field
* @deprecated Superseded by {@link #getFieldFailures()}
*/
public String getField() {
return _field;
}
protected void fail(String message) {
_success = false;
if (_message.length() == 0) {
_message = message;
}
else {
_message += " ; " + message;
}
}
/**
* Identify that the comparison failed
* @param field Which field failed
* @param expected Expected result
* @param actual Actual result
*/
protected JSONCompareResult fail(String field, Object expected, Object actual) {
_fieldFailures.add(new FieldComparisonFailure(field, expected, actual));
this._field = field;
this._expected = expected;
this._actual = actual;
fail(formatFailureMessage(field, expected, actual));
return this;
}
private String formatFailureMessage(String field, Object expected, Object actual) {
StringBuffer message= new StringBuffer();
message.append(field);
message.append("\nExpected: ");
message.append(describe(expected));
message.append("\n got: ");
message.append(describe(actual));
message.append("\n");
return message.toString();
}
public JSONCompareResult missing(String field, Object expected) {
fail(formatMissing(field, expected));
return this;
}
private String formatMissing(String field, Object expected) {
StringBuffer message= new StringBuffer();
message.append(field);
message.append("\nExpected: ");
message.append(describe(expected));
message.append("\n but none found\n");
return message.toString();
}
public JSONCompareResult unexpected(String field, Object value) {
fail(formatUnexpected(field, value));
return this;
}
private String formatUnexpected(String field, Object value) {
StringBuffer message= new StringBuffer();
message.append(field);
message.append("\nUnexpected: ");
message.append(describe(value));
message.append("\n");
return message.toString();
}
private static String describe(Object value) {
if (value instanceof JSONArray) {
return "a JSON array";
} else if (value instanceof JSONObject) {
return "a JSON object";
} else {
return value.toString();
}
}
@Override
public String toString() {
return _message;
}
}