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
859 lines (771 loc) · 40.9 KB
/
JSONAssertTest.java
File metadata and controls
859 lines (771 loc) · 40.9 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.skyscreamer.jsonassert;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT_ORDER;
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 org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.skyscreamer.jsonassert.comparator.JSONComparator;
/**
* 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", Integer.valueOf(12345));
actual.put("id", Long.valueOf(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", Integer.valueOf(12345));
actual.put("id", Double.valueOf(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", Integer.valueOf(12345));
actual.put("id", Double.valueOf(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", Integer.valueOf(12345));
actual.put("id", Double.valueOf(12345));
JSONAssert.assertNotEquals(expected, actual, false);
}
@Test()
public void testAssertNotEqualsWhenEqualDiffObjectsStrict() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
expected.put("name", "Joe");
actual.put("id", Double.valueOf(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", Integer.valueOf(12345));
expected.put("name", "Joe");
actual.put("name", "Joe");
actual.put("id", Double.valueOf(12345));
JSONAssert.assertNotEquals(expected, actual, false);
}
@Test()
public void testAssertNotEqualsWhenDifferentStrict() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("id", Double.valueOf(12346));
JSONAssert.assertNotEquals(expected, actual, true);
}
@Test()
public void testAssertNotEqualsWhenDifferentLenient() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("id", Double.valueOf(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 testAssertEqualsString() throws JSONException {
JSONAssert.assertEquals("[1,2,3]", "[1,2,3]", true);
JSONAssert.assertEquals("{id:12345}", "{id:12345}", false);
JSONAssert.assertEquals("{id:12345}", "{id:12345, name:\"john\"}", LENIENT);
JSONAssert.assertEquals("{id:12345}", "{id:12345}", LENIENT);
JSONAssert.assertEquals("{id:12345}", "{id:12345, name:\"john\"}", LENIENT);
}
@Test()
public void testAssertNotEqualsStringAndJSONObject() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", Double.valueOf(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);
}
@Test
public void testAssertEqualsStringJSONArrayBooleanWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertEquals("Message", "[1,2,3]", actual, false);
performAssertEqualsTestForMessageVerification("[1,2,4]", actual, false);
performAssertEqualsTestForMessageVerification("[1,3,2]", actual, true);
}
@Test
public void testAssertEqualsStringJSONArrayCompareModeWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertEquals("Message", "[1,2,3]", actual, LENIENT);
performAssertEqualsTestForMessageVerification("[1,2,4]", actual, LENIENT);
performAssertEqualsTestForMessageVerification("[1,3,2]", actual, STRICT);
}
@Test
public void testAssertEqualsJSONArray2BooleanWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, false);
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, false);
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);
}
@Test
public void testAssertEqualsJSONArray2JSONCompareWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, LENIENT);
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, LENIENT);
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, STRICT);
}
@Test
public void testAssertEqualsString2Boolean() throws JSONException {
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", false);
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", false);
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", true);
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:123456}", false);
}
@Test
public void testAssertEqualsString2JSONCompare() throws JSONException {
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", LENIENT);
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", LENIENT);
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", STRICT);
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:123456}", LENIENT);
}
@Test
public void testAssertEqualsStringJSONObjectBoolean() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", Double.valueOf(12345));
JSONAssert.assertEquals("Message", "{id:12345}", actual, false);
performAssertEqualsTestForMessageVerification("{id:12346}", actual, false);
performAssertEqualsTestForMessageVerification("[1,2,3]", "[1,3,2]", true);
}
@Test
public void testAssertEqualsStringJSONObjectJSONCompare() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", Double.valueOf(12345));
JSONAssert.assertEquals("Message", "{id:12345}", actual, LENIENT);
performAssertEqualsTestForMessageVerification("{id:12346}", actual, LENIENT);
performAssertEqualsTestForMessageVerification("[1,2,3]", "[1,3,2]", STRICT);
}
@Test
public void testAssertEqualsJSONObject2JSONCompare() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("name", "Joe");
actual.put("id", Integer.valueOf(12345));
JSONAssert.assertEquals("Message", expected, actual, LENIENT);
expected.put("street", "St. Paul");
performAssertEqualsTestForMessageVerification(expected, actual, LENIENT);
expected = new JSONObject();
actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("id", Double.valueOf(12346));
performAssertEqualsTestForMessageVerification(expected, actual, STRICT);
}
@Test
public void testAssertEqualsJSONObject2Boolean() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("name", "Joe");
actual.put("id", Integer.valueOf(12345));
JSONAssert.assertEquals("Message", expected, actual, false);
expected.put("street", "St. Paul");
performAssertEqualsTestForMessageVerification(expected, actual, false);
expected = new JSONObject();
actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("id", Double.valueOf(12346));
performAssertEqualsTestForMessageVerification(expected, actual, true);
}
@Test
public void testAssertEqualsString2JsonComparator() throws IllegalArgumentException, JSONException {
JSONAssert.assertEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}",
new CustomComparator(
JSONCompareMode.STRICT,
new Customization("entry.id",
new RegularExpressionValueMatcher<Object>("\\d"))
));
performAssertEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":as}}",
new CustomComparator(
JSONCompareMode.STRICT,
new Customization("entry.id",
new RegularExpressionValueMatcher<Object>("\\d"))
));
}
@Test
public void testAssertNotEqualsStringJSONArrayBooleanWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, false);
JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, true);
performAssertNotEqualsTestForMessageVerification("[1,3,2]", actual, false);
performAssertNotEqualsTestForMessageVerification("[1,2,3]", actual, true);
}
@Test
public void testAssertNotEqualsStringJSONArrayCompareModeWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, LENIENT);
JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, STRICT);
performAssertNotEqualsTestForMessageVerification("[1,3,2]", actual, LENIENT);
performAssertNotEqualsTestForMessageVerification("[1,2,3]", actual, STRICT);
}
@Test
public void testAssertNotEqualsJSONArray2BooleanWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, false);
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, false);
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 3)), actual, true);
}
@Test
public void testAssertNotEqualsJSONArray2JSONCompareWithMessage() throws JSONException {
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, LENIENT);
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, LENIENT);
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 3)), actual, STRICT);
}
@Test
public void testAssertNotEqualsString2Boolean() throws JSONException {
JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:45}", false);
JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:345, name:\"john\"}", false);
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345}", true);
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"John\"}", false);
}
@Test
public void testAssertNotEqualsString2JSONCompare() throws JSONException {
JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:123}", LENIENT);
JSONAssert.assertNotEquals("Message", "{id:12345, name:\"John\"}", "{id:12345}", LENIENT);
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", LENIENT);
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345}", STRICT);
}
@Test
public void testAssertNotEqualsStringJSONObjectBoolean() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", Double.valueOf(12345));
JSONAssert.assertNotEquals("Message", "{id:1234}", actual, false);
performAssertNotEqualsTestForMessageVerification("{id:12345}", actual, false);
performAssertNotEqualsTestForMessageVerification("[1,2,3]", "[1,2,3]", true);
}
@Test
public void testAssertNotEqualsStringJSONObjectJSONCompare() throws JSONException {
JSONObject actual = new JSONObject();
actual.put("id", Double.valueOf(12345));
JSONAssert.assertNotEquals("Message", "{id:1234}", actual, LENIENT);
performAssertNotEqualsTestForMessageVerification("{id:12345}", actual, LENIENT);
performAssertNotEqualsTestForMessageVerification("[1,2,3]", "[1,2,3]", STRICT);
}
@Test
public void testAssertNtEqualsJSONObject2JSONCompare() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("name", "Joe");
actual.put("id", Integer.valueOf(123));
JSONAssert.assertNotEquals("Message", expected, actual, LENIENT);
actual.remove("id");
actual.put("id", Integer.valueOf(12345));
performAssertNotEqualsTestForMessageVerification(expected, actual, LENIENT);
expected = new JSONObject();
actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("id", Double.valueOf(12345));
performAssertNotEqualsTestForMessageVerification(expected, actual, STRICT);
}
@Test
public void testAssertNotEqualsJSONObject2Boolean() throws JSONException {
JSONObject expected = new JSONObject();
JSONObject actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("name", "Joe");
actual.put("id", Integer.valueOf(123));
JSONAssert.assertNotEquals("Message", expected, actual, false);
actual.remove("id");
actual.put("id", Integer.valueOf(12345));
performAssertNotEqualsTestForMessageVerification(expected, actual, false);
expected = new JSONObject();
actual = new JSONObject();
expected.put("id", Integer.valueOf(12345));
actual.put("id", Double.valueOf(12345));
performAssertNotEqualsTestForMessageVerification(expected, actual, true);
}
@Test
public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentException, JSONException {
JSONAssert.assertNotEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":hh}}",
new CustomComparator(
JSONCompareMode.STRICT,
new Customization("entry.id",
new RegularExpressionValueMatcher<Object>("\\d"))
));
performAssertNotEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}",
new CustomComparator(
JSONCompareMode.STRICT,
new Customization("entry.id",
new RegularExpressionValueMatcher<Object>("\\d"))
));
}
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());
}
private void performAssertEqualsTestForMessageVerification(
Object expected,
Object actual,
Object strictMode) throws JSONException {
String message = "Message";
String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified.";
String strictModeMessage = "strictMode must be an instance of JSONCompareMode or Boolean";
boolean assertEqualsFailed = true;
if(expected instanceof String && actual instanceof String && strictMode instanceof JSONComparator) {
try {
JSONAssert.assertEquals(message, (String) expected, (String) actual, (JSONComparator) strictMode);
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
}
else if(expected instanceof String && actual instanceof JSONArray) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertEquals(message, (String) expected, (JSONArray) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertEquals(message, (String) expected, (JSONArray) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof JSONArray && actual instanceof JSONArray) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertEquals(message, (JSONArray) expected, (JSONArray) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertEquals(message, (JSONArray) expected, (JSONArray) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof String && actual instanceof String) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertEquals(message, (String) expected, (String) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertEquals(message, (String) expected, (String) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof String && actual instanceof JSONObject) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertEquals(message, (String) expected, (JSONObject) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertEquals(message, (String) expected, (JSONObject) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof JSONObject && actual instanceof JSONObject) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertEquals(message, (JSONObject) expected, (JSONObject) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertEquals(message, (JSONObject) expected, (JSONObject) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else {
fail("No overloaded method found to call");
}
}
private void performAssertNotEqualsTestForMessageVerification(
Object expected,
Object actual,
Object strictMode)
throws JSONException {
String message = "Message";
String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified.";
String strictModeMessage = "strictMode must be an instance of JSONCompareMode or Boolean";
boolean assertEqualsFailed = true;
if(expected instanceof String && actual instanceof String && strictMode instanceof JSONComparator) {
try {
JSONAssert.assertNotEquals(message, (String) expected, (String) actual, (JSONComparator) strictMode);
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
}
else if(expected instanceof String && actual instanceof JSONArray) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertNotEquals(message, (String) expected, (JSONArray) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertNotEquals(message, (String) expected, (JSONArray) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof JSONArray && actual instanceof JSONArray) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertNotEquals(message, (JSONArray) expected, (JSONArray) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertNotEquals(message, (JSONArray) expected, (JSONArray) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof String && actual instanceof String) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertNotEquals(message, (String) expected, (String) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertNotEquals(message, (String) expected, (String) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof String && actual instanceof JSONObject) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertNotEquals(message, (String) expected, (JSONObject) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertNotEquals(message, (String) expected, (JSONObject) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else if(expected instanceof JSONObject && actual instanceof JSONObject) {
try {
if(strictMode instanceof JSONCompareMode) {
JSONAssert.assertNotEquals(message, (JSONObject) expected, (JSONObject) actual, (JSONCompareMode) strictMode);
} else if(strictMode instanceof Boolean) {
JSONAssert.assertNotEquals(message, (JSONObject) expected, (JSONObject) actual, (Boolean) strictMode);
} else {
fail(strictModeMessage);
}
assertEqualsFailed = false;
fail(testShouldFailMessage); //will throw AssertionError
} catch (AssertionError ae) {
handleAssertionError(message, assertEqualsFailed, ae);
}
} else {
fail("No overloaded method found to call");
}
}
private void handleAssertionError(String message, boolean assertEqualsFailed, AssertionError ae) throws AssertionError {
if(assertEqualsFailed) {
verifyErrorMessage(message, ae);
} else {
throw ae;
}
}
private void verifyErrorMessage(String message, AssertionError ae) {
assertTrue(ae.getMessage().contains(message));
assertTrue(ae.getMessage().startsWith(message));
}
}