forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONAssertTest.java
More file actions
377 lines (335 loc) · 17.1 KB
/
JSONAssertTest.java
File metadata and controls
377 lines (335 loc) · 17.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package org.skyscreamer.jsonassert;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import static org.skyscreamer.jsonassert.JSONCompareMode.*;
/**
* Unit tests for {@link JSONAssert}
*/
public class JSONAssertTest {
@Test
public void testString() throws JSONException {
testPass("\"Joe\"", "\"Joe\"", STRICT);
testPass("\"Joe\"", "\"Joe\"", LENIENT);
testPass("\"Joe\"", "\"Joe\"", NON_EXTENSIBLE);
testPass("\"Joe\"", "\"Joe\"", STRICT_ORDER);
testFail("\"Joe\"", "\"Joe1\"", STRICT);
testFail("\"Joe\"", "\"Joe2\"", LENIENT);
testFail("\"Joe\"", "\"Joe3\"", NON_EXTENSIBLE);
testFail("\"Joe\"", "\"Joe4\"", STRICT_ORDER);
}
@Test
public void testNumber() throws JSONException {
testPass("123", "123", STRICT);
testPass("123", "123", LENIENT);
testPass("123", "123", NON_EXTENSIBLE);
testPass("123", "123", STRICT_ORDER);
testFail("123", "1231", STRICT);
testFail("123", "1232", LENIENT);
testFail("123", "1233", NON_EXTENSIBLE);
testFail("123", "1234", STRICT_ORDER);
testPass("0", "0", STRICT);
testPass("-1", "-1", STRICT);
testPass("0.1", "0.1", STRICT);
testPass("1.2e5", "1.2e5", STRICT);
testPass("20.4e-1", "20.4e-1", STRICT);
testFail("310.1e-1", "31.01", STRICT); // should fail though numbers are the same?
}
@Test
public void testSimple() throws JSONException {
testPass("{id:1}", "{id:1}", STRICT);
testFail("{id:1}", "{id:2}", STRICT);
testPass("{id:1}", "{id:1}", LENIENT);
testFail("{id:1}", "{id:2}", LENIENT);
testPass("{id:1}", "{id:1}", NON_EXTENSIBLE);
testFail("{id:1}", "{id:2}", NON_EXTENSIBLE);
testPass("{id:1}", "{id:1}", STRICT_ORDER);
testFail("{id:1}", "{id:2}", STRICT_ORDER);
}
@Test
public void testSimpleStrict() throws JSONException {
testPass("{id:1}", "{id:1,name:\"Joe\"}", LENIENT);
testFail("{id:1}", "{id:1,name:\"Joe\"}", STRICT);
testPass("{id:1}", "{id:1,name:\"Joe\"}", STRICT_ORDER);
testFail("{id:1}", "{id:1,name:\"Joe\"}", NON_EXTENSIBLE);
}
@Test
public void testReversed() throws JSONException {
testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", LENIENT);
testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", STRICT);
testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", NON_EXTENSIBLE);
testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", STRICT_ORDER);
}
@Test // Currently JSONAssert assumes JSONObject.
public void testArray() throws JSONException {
testPass("[1,2,3]","[1,2,3]", STRICT);
testPass("[1,2,3]","[1,3,2]", LENIENT);
testFail("[1,2,3]","[1,3,2]", STRICT);
testFail("[1,2,3]","[4,5,6]", LENIENT);
testPass("[1,2,3]","[1,2,3]", STRICT_ORDER);
testPass("[1,2,3]","[1,3,2]", NON_EXTENSIBLE);
testFail("[1,2,3]","[1,3,2]", STRICT_ORDER);
testFail("[1,2,3]","[4,5,6]", NON_EXTENSIBLE);
}
@Test
public void testNested() throws JSONException {
testPass("{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}",
"{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}", STRICT);
testFail("{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}",
"{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Austin\", state:\"TX\"}}", STRICT);
}
@Test
public void testVeryNested() throws JSONException {
testPass("{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}",
"{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}", STRICT);
testFail("{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}",
"{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{z:\"blah\"}}}}}}}}}}}}}}}}", STRICT);
}
@Test
public void testSimpleArray() throws JSONException {
testPass("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Exact to exact (strict)
"{id:1,pets:[\"dog\",\"cat\",\"fish\"]}",
STRICT);
testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order fails (strict)
"{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",
STRICT);
testPass("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order ok
"{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",
LENIENT);
testPass("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order ok
"{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",
NON_EXTENSIBLE);
testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order fails (strict order)
"{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",
STRICT_ORDER);
testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch
"{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",
STRICT);
testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch
"{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",
LENIENT);
testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch
"{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",
STRICT_ORDER);
testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch
"{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",
NON_EXTENSIBLE);
}
@Test
public void testSimpleMixedArray() throws JSONException {
testPass("{stuff:[321, \"abc\"]}", "{stuff:[\"abc\", 321]}", LENIENT);
testFail("{stuff:[321, \"abc\"]}", "{stuff:[\"abc\", 789]}", LENIENT);
}
@Test
public void testComplexMixedStrictArray() throws JSONException {
testPass("{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", "{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", STRICT);
}
@Test
public void testComplexMixedArray() throws JSONException {
testPass("{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", "{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", LENIENT);
}
@Test
public void testComplexArrayNoUniqueID() throws JSONException {
testPass("{stuff:[{address:{addr1:\"123 Main\"}}, {address:{addr1:\"234 Broad\"}}]}",
"{stuff:[{address:{addr1:\"123 Main\"}}, {address:{addr1:\"234 Broad\"}}]}",
LENIENT);
}
@Test
public void testSimpleAndComplexStrictArray() throws JSONException {
testPass("{stuff:[123,{a:\"b\"}]}", "{stuff:[123,{a:\"b\"}]}", STRICT);
}
@Test
public void testSimpleAndComplexArray() throws JSONException {
testPass("{stuff:[123,{a:\"b\"}]}", "{stuff:[123,{a:\"b\"}]}", LENIENT);
}
@Test
public void testComplexArray() throws JSONException {
testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
STRICT); // Exact to exact (strict)
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
STRICT); // Out-of-order fails (strict)
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
STRICT_ORDER); // Out-of-order fails (strict order)
testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
LENIENT); // Out-of-order ok
testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
NON_EXTENSIBLE); // Out-of-order ok
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",
STRICT); // Mismatch (strict)
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",
LENIENT); // Mismatch
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",
STRICT_ORDER); // Mismatch
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",
NON_EXTENSIBLE); // Mismatch
}
@Test
public void testArrayOfArraysStrict() throws JSONException {
testPass("{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", STRICT);
testFail("{id:1,stuff:[[1,2],[2,3],[3,4],[]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", STRICT);
}
@Test
public void testArrayOfArrays() throws JSONException {
testPass("{id:1,stuff:[[4,3],[3,2],[],[1,2]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", LENIENT);
}
@Test
public void testLenientArrayRecursion() throws JSONException {
testPass("[{\"arr\":[5, 2, 1]}]", "[{\"b\":3, \"arr\":[1, 5, 2]}]", LENIENT);
}
@Test
public void testFieldMismatch() throws JSONException {
JSONCompareResult result = JSONCompare.compareJSON("{name:\"Pat\"}", "{name:\"Sue\"}", STRICT);
FieldComparisonFailure comparisonFailure = result.getFieldFailures().iterator().next();
Assert.assertEquals("Pat", comparisonFailure.getExpected());
Assert.assertEquals("Sue", comparisonFailure.getActual());
Assert.assertEquals("name", comparisonFailure.getField());
}
@Test
public void testBooleanArray() throws JSONException {
testPass("[true, false, true, true, false]", "[true, false, true, true, false]", STRICT);
testPass("[false, true, true, false, true]", "[true, false, true, true, false]", LENIENT);
testFail("[false, true, true, false, true]", "[true, false, true, true, false]", STRICT);
testPass("[false, true, true, false, true]", "[true, false, true, true, false]", NON_EXTENSIBLE);
testFail("[false, true, true, false, true]", "[true, false, true, true, false]", STRICT_ORDER);
}
@Test
public void testNullProperty() throws JSONException {
testFail("{id:1,name:\"Joe\"}", "{id:1,name:null}", STRICT);
testFail("{id:1,name:null}", "{id:1,name:\"Joe\"}", STRICT);
}
@Test
public void testIncorrectTypes() throws JSONException {
testFail("{id:1,name:\"Joe\"}", "{id:1,name:[]}", STRICT);
testFail("{id:1,name:[]}", "{id:1,name:\"Joe\"}", STRICT);
}
@Test
public void testNullEquality() throws JSONException {
testPass("{id:1,name:null}", "{id:1,name:null}", STRICT);
}
@Test
public void testExpectedArrayButActualObject() throws JSONException {
testFail("[1]", "{id:1}", LENIENT);
}
@Test
public void testExpectedObjectButActualArray() throws JSONException {
testFail("{id:1}", "[1]", LENIENT);
}
@Test
public void testEquivalentIntAndLong() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
actual.put("id", new Long(12345));
JSONAssert.assertEquals(expected, actual, true);
JSONAssert.assertEquals(actual, expected, true);
}
@Test
public void testEquivalentIntAndDouble() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
actual.put("id", new Double(12345.0));
JSONAssert.assertEquals(expected, actual, true);
JSONAssert.assertEquals(actual, expected, true);
}
@Test(expected = AssertionError.class)
public void testAssertNotEqualsWhenEqualStrict() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
actual.put("id", new Double(12345));
JSONAssert.assertNotEquals(expected, actual, true);
}
@Test(expected = AssertionError.class)
public void testAssertNotEqualsWhenEqualLenient() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
actual.put("id", new Double(12345));
JSONAssert.assertNotEquals(expected, actual, false);
}
@Test()
public void testAssertNotEqualsWhenEqualDiffObjectsStrict() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
expected.put("name", "Joe");
actual.put("id", new Double(12345));
JSONAssert.assertNotEquals(expected, actual, true);
}
@Test(expected = AssertionError.class)
public void testAssertNotEqualsWhenEqualDiffObjectsLenient() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
expected.put("name", "Joe");
actual.put("name", "Joe");
actual.put("id", new Double(12345));
JSONAssert.assertNotEquals(expected, actual, false);
}
@Test()
public void testAssertNotEqualsWhenDifferentStrict() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
actual.put("id", new Double(12346));
JSONAssert.assertNotEquals(expected, actual, true);
}
@Test()
public void testAssertNotEqualsWhenDifferentLenient() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", new Integer(12345));
actual.put("id", new Double(12346));
JSONAssert.assertNotEquals(expected, actual, false);
}
@Test()
public void testAssertNotEqualsString() throws JSONException {
JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", STRICT);
JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", LENIENT);
JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", true);
JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", false);
}
@Test()
public void testAssertNotEqualsStringAndJSONObject() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", new Double(12345));
JSONAssert.assertEquals("{id:12345}", actual, false);
JSONAssert.assertNotEquals("{id:12346}", actual, false);
}
@Test()
public void testAssertNotEqualsJSONArray() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertEquals("[1,2,3]", actual, false);
JSONAssert.assertNotEquals("[1,2,4]", actual, false);
JSONAssert.assertNotEquals("[1,3,2]", actual, true);
JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 2, 4)), actual, false);
JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);
}
private void testPass(String expected, String actual, JSONCompareMode compareMode)
throws JSONException
{
String message = expected + " == " + actual + " (" + compareMode + ")";
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);
Assert.assertTrue(message + "\n " + result.getMessage(), result.passed());
}
private void testFail(String expected, String actual, JSONCompareMode compareMode)
throws JSONException
{
String message = expected + " != " + actual + " (" + compareMode + ")";
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);
Assert.assertTrue(message, result.failed());
}
}