Skip to content

Commit ac01d2a

Browse files
committed
JUNit tests for assertNotEquals
1 parent 46980dc commit ac01d2a

1 file changed

Lines changed: 249 additions & 24 deletions

File tree

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

Lines changed: 249 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -379,70 +379,69 @@ public void testAssertNotEqualsJSONArray() throws JSONException {
379379
public void testAssertEqualsStringJSONArrayBooleanWithMessage() throws JSONException {
380380
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
381381
JSONAssert.assertEquals("Message", "[1,2,3]", actual, false);
382-
performTestForMessageVerification("[1,2,4]", actual, false);
383-
performTestForMessageVerification("[1,3,2]", actual, true);
382+
performAssertEqualsTestForMessageVerification("[1,2,4]", actual, false);
383+
performAssertEqualsTestForMessageVerification("[1,3,2]", actual, true);
384384
}
385385

386386
@Test
387387
public void testAssertEqualsStringJSONArrayCompareModeWithMessage() throws JSONException {
388388
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
389389
JSONAssert.assertEquals("Message", "[1,2,3]", actual, LENIENT);
390-
performTestForMessageVerification("[1,2,4]", actual, LENIENT);
391-
performTestForMessageVerification("[1,3,2]", actual, STRICT);
390+
performAssertEqualsTestForMessageVerification("[1,2,4]", actual, LENIENT);
391+
performAssertEqualsTestForMessageVerification("[1,3,2]", actual, STRICT);
392392
}
393-
393+
394394
@Test
395395
public void testAssertEqualsJSONArray2BooleanWithMessage() throws JSONException {
396396
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
397397
JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, false);
398-
performTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, false);
399-
performTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);
398+
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, false);
399+
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);
400400
}
401401

402402
@Test
403403
public void testAssertEqualsJSONArray2JSONCompareWithMessage() throws JSONException {
404404
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
405405

406406
JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, LENIENT);
407-
performTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, LENIENT);
408-
performTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, STRICT);
409-
407+
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, LENIENT);
408+
performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, STRICT);
410409
}
411410

412411
@Test
413412
public void testAssertEqualsString2Boolean() throws JSONException {
414413
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", false);
415414
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", false);
416415

417-
performTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", true);
418-
performTestForMessageVerification("{id:12345}", "{id:123456}", false);
416+
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", true);
417+
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:123456}", false);
419418
}
420419

421420
@Test
422421
public void testAssertEqualsString2JSONCompare() throws JSONException {
423422
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", LENIENT);
424423
JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", LENIENT);
425424

426-
performTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", STRICT);
427-
performTestForMessageVerification("{id:12345}", "{id:123456}", LENIENT);
425+
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", STRICT);
426+
performAssertEqualsTestForMessageVerification("{id:12345}", "{id:123456}", LENIENT);
428427
}
429428

430429
@Test
431430
public void testAssertEqualsStringJSONObjectBoolean() throws JSONException {
432431
JSONObject actual = new JSONObject();
433432
actual.put("id", Double.valueOf(12345));
434433
JSONAssert.assertEquals("Message", "{id:12345}", actual, false);
435-
performTestForMessageVerification("{id:12346}", actual, false);
436-
performTestForMessageVerification("[1,2,3]", "[1,3,2]", true);
434+
performAssertEqualsTestForMessageVerification("{id:12346}", actual, false);
435+
performAssertEqualsTestForMessageVerification("[1,2,3]", "[1,3,2]", true);
437436
}
438437

439438
@Test
440439
public void testAssertEqualsStringJSONObjectJSONCompare() throws JSONException {
441440
JSONObject actual = new JSONObject();
442441
actual.put("id", Double.valueOf(12345));
443442
JSONAssert.assertEquals("Message", "{id:12345}", actual, LENIENT);
444-
performTestForMessageVerification("{id:12346}", actual, LENIENT);
445-
performTestForMessageVerification("[1,2,3]", "[1,3,2]", STRICT);
443+
performAssertEqualsTestForMessageVerification("{id:12346}", actual, LENIENT);
444+
performAssertEqualsTestForMessageVerification("[1,2,3]", "[1,3,2]", STRICT);
446445
}
447446

448447
@Test
@@ -455,13 +454,13 @@ public void testAssertEqualsJSONObject2JSONCompare() throws JSONException {
455454
JSONAssert.assertEquals("Message", expected, actual, LENIENT);
456455

457456
expected.put("street", "St. Paul");
458-
performTestForMessageVerification(expected, actual, LENIENT);
457+
performAssertEqualsTestForMessageVerification(expected, actual, LENIENT);
459458

460459
expected = new JSONObject();
461460
actual = new JSONObject();
462461
expected.put("id", Integer.valueOf(12345));
463462
actual.put("id", Double.valueOf(12346));
464-
performTestForMessageVerification(expected, actual, STRICT);
463+
performAssertEqualsTestForMessageVerification(expected, actual, STRICT);
465464
}
466465

467466
@Test
@@ -474,13 +473,13 @@ public void testAssertEqualsJSONObject2Boolean() throws JSONException {
474473
JSONAssert.assertEquals("Message", expected, actual, false);
475474

476475
expected.put("street", "St. Paul");
477-
performTestForMessageVerification(expected, actual, false);
476+
performAssertEqualsTestForMessageVerification(expected, actual, false);
478477

479478
expected = new JSONObject();
480479
actual = new JSONObject();
481480
expected.put("id", Integer.valueOf(12345));
482481
actual.put("id", Double.valueOf(12346));
483-
performTestForMessageVerification(expected, actual, true);
482+
performAssertEqualsTestForMessageVerification(expected, actual, true);
484483
}
485484

486485
@Test
@@ -492,7 +491,135 @@ public void testAssertEqualsString2JsonComparator() throws IllegalArgumentExcept
492491
new RegularExpressionValueMatcher<Object>("\\d"))
493492
));
494493

495-
performTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":as}}",
494+
performAssertEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":as}}",
495+
new CustomComparator(
496+
JSONCompareMode.STRICT,
497+
new Customization("entry.id",
498+
new RegularExpressionValueMatcher<Object>("\\d"))
499+
));
500+
}
501+
502+
@Test
503+
public void testAssertNotEqualsStringJSONArrayBooleanWithMessage() throws JSONException {
504+
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
505+
JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, false);
506+
JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, true);
507+
performAssertNotEqualsTestForMessageVerification("[1,3,2]", actual, false);
508+
performAssertNotEqualsTestForMessageVerification("[1,2,3]", actual, true);
509+
}
510+
511+
@Test
512+
public void testAssertNotEqualsStringJSONArrayCompareModeWithMessage() throws JSONException {
513+
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
514+
JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, LENIENT);
515+
JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, STRICT);
516+
performAssertNotEqualsTestForMessageVerification("[1,3,2]", actual, LENIENT);
517+
performAssertNotEqualsTestForMessageVerification("[1,2,3]", actual, STRICT);
518+
}
519+
520+
@Test
521+
public void testAssertNotEqualsJSONArray2BooleanWithMessage() throws JSONException {
522+
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
523+
JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, false);
524+
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, false);
525+
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 3)), actual, true);
526+
}
527+
528+
@Test
529+
public void testAssertNotEqualsJSONArray2JSONCompareWithMessage() throws JSONException {
530+
JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));
531+
532+
JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, LENIENT);
533+
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, LENIENT);
534+
performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 3)), actual, STRICT);
535+
}
536+
537+
@Test
538+
public void testAssertNotEqualsString2Boolean() throws JSONException {
539+
JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:45}", false);
540+
JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:345, name:\"john\"}", false);
541+
542+
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345}", true);
543+
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"John\"}", false);
544+
}
545+
546+
@Test
547+
public void testAssertNotEqualsString2JSONCompare() throws JSONException {
548+
JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:123}", LENIENT);
549+
JSONAssert.assertNotEquals("Message", "{id:12345, name:\"John\"}", "{id:12345}", LENIENT);
550+
551+
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", LENIENT);
552+
performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345}", STRICT);
553+
}
554+
555+
@Test
556+
public void testAssertNotEqualsStringJSONObjectBoolean() throws JSONException {
557+
JSONObject actual = new JSONObject();
558+
actual.put("id", Double.valueOf(12345));
559+
JSONAssert.assertNotEquals("Message", "{id:1234}", actual, false);
560+
performAssertNotEqualsTestForMessageVerification("{id:12345}", actual, false);
561+
performAssertNotEqualsTestForMessageVerification("[1,2,3]", "[1,2,3]", true);
562+
}
563+
564+
@Test
565+
public void testAssertNotEqualsStringJSONObjectJSONCompare() throws JSONException {
566+
JSONObject actual = new JSONObject();
567+
actual.put("id", Double.valueOf(12345));
568+
JSONAssert.assertNotEquals("Message", "{id:1234}", actual, LENIENT);
569+
performAssertNotEqualsTestForMessageVerification("{id:12345}", actual, LENIENT);
570+
performAssertNotEqualsTestForMessageVerification("[1,2,3]", "[1,2,3]", STRICT);
571+
}
572+
573+
@Test
574+
public void testAssertNtEqualsJSONObject2JSONCompare() throws JSONException {
575+
JSONObject expected = new JSONObject();
576+
JSONObject actual = new JSONObject();
577+
expected.put("id", Integer.valueOf(12345));
578+
actual.put("name", "Joe");
579+
actual.put("id", Integer.valueOf(123));
580+
JSONAssert.assertNotEquals("Message", expected, actual, LENIENT);
581+
582+
actual.remove("id");
583+
actual.put("id", Integer.valueOf(12345));
584+
performAssertNotEqualsTestForMessageVerification(expected, actual, LENIENT);
585+
586+
expected = new JSONObject();
587+
actual = new JSONObject();
588+
expected.put("id", Integer.valueOf(12345));
589+
actual.put("id", Double.valueOf(12345));
590+
performAssertNotEqualsTestForMessageVerification(expected, actual, STRICT);
591+
}
592+
593+
@Test
594+
public void testAssertNotEqualsJSONObject2Boolean() throws JSONException {
595+
JSONObject expected = new JSONObject();
596+
JSONObject actual = new JSONObject();
597+
expected.put("id", Integer.valueOf(12345));
598+
actual.put("name", "Joe");
599+
actual.put("id", Integer.valueOf(123));
600+
JSONAssert.assertNotEquals("Message", expected, actual, false);
601+
602+
actual.remove("id");
603+
actual.put("id", Integer.valueOf(12345));
604+
performAssertNotEqualsTestForMessageVerification(expected, actual, false);
605+
606+
expected = new JSONObject();
607+
actual = new JSONObject();
608+
expected.put("id", Integer.valueOf(12345));
609+
actual.put("id", Double.valueOf(12345));
610+
performAssertNotEqualsTestForMessageVerification(expected, actual, true);
611+
}
612+
613+
@Test
614+
public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentException, JSONException {
615+
JSONAssert.assertNotEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":hh}}",
616+
new CustomComparator(
617+
JSONCompareMode.STRICT,
618+
new Customization("entry.id",
619+
new RegularExpressionValueMatcher<Object>("\\d"))
620+
));
621+
622+
performAssertNotEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}",
496623
new CustomComparator(
497624
JSONCompareMode.STRICT,
498625
new Customization("entry.id",
@@ -516,7 +643,11 @@ private void testFail(String expected, String actual, JSONCompareMode compareMod
516643
Assert.assertTrue(message, result.failed());
517644
}
518645

519-
private void performTestForMessageVerification(Object expected, Object actual, Object strictMode) throws JSONException {
646+
private void performAssertEqualsTestForMessageVerification(
647+
Object expected,
648+
Object actual,
649+
Object strictMode) throws JSONException {
650+
520651
String message = "Message";
521652
String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified.";
522653
String strictModeMessage = "strictMode must be an instance of JSONCompareMode or Boolean";
@@ -604,6 +735,100 @@ else if(expected instanceof String && actual instanceof JSONArray) {
604735
fail("No overloaded method found to call");
605736
}
606737
}
738+
739+
private void performAssertNotEqualsTestForMessageVerification(
740+
Object expected,
741+
Object actual,
742+
Object strictMode)
743+
throws JSONException {
744+
745+
String message = "Message";
746+
String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified.";
747+
String strictModeMessage = "strictMode must be an instance of JSONCompareMode or Boolean";
748+
boolean assertEqualsFailed = true;
749+
if(expected instanceof String && actual instanceof String && strictMode instanceof JSONComparator) {
750+
try {
751+
JSONAssert.assertNotEquals(message, (String) expected, (String) actual, (JSONComparator) strictMode);
752+
assertEqualsFailed = false;
753+
fail(testShouldFailMessage); //will throw AssertionError
754+
} catch (AssertionError ae) {
755+
handleAssertionError(message, assertEqualsFailed, ae);
756+
}
757+
}
758+
else if(expected instanceof String && actual instanceof JSONArray) {
759+
try {
760+
if(strictMode instanceof JSONCompareMode) {
761+
JSONAssert.assertNotEquals(message, (String) expected, (JSONArray) actual, (JSONCompareMode) strictMode);
762+
} else if(strictMode instanceof Boolean) {
763+
JSONAssert.assertNotEquals(message, (String) expected, (JSONArray) actual, (Boolean) strictMode);
764+
} else {
765+
fail(strictModeMessage);
766+
}
767+
assertEqualsFailed = false;
768+
fail(testShouldFailMessage); //will throw AssertionError
769+
} catch (AssertionError ae) {
770+
handleAssertionError(message, assertEqualsFailed, ae);
771+
}
772+
} else if(expected instanceof JSONArray && actual instanceof JSONArray) {
773+
try {
774+
if(strictMode instanceof JSONCompareMode) {
775+
JSONAssert.assertNotEquals(message, (JSONArray) expected, (JSONArray) actual, (JSONCompareMode) strictMode);
776+
} else if(strictMode instanceof Boolean) {
777+
JSONAssert.assertNotEquals(message, (JSONArray) expected, (JSONArray) actual, (Boolean) strictMode);
778+
} else {
779+
fail(strictModeMessage);
780+
}
781+
assertEqualsFailed = false;
782+
fail(testShouldFailMessage); //will throw AssertionError
783+
} catch (AssertionError ae) {
784+
handleAssertionError(message, assertEqualsFailed, ae);
785+
}
786+
} else if(expected instanceof String && actual instanceof String) {
787+
try {
788+
if(strictMode instanceof JSONCompareMode) {
789+
JSONAssert.assertNotEquals(message, (String) expected, (String) actual, (JSONCompareMode) strictMode);
790+
} else if(strictMode instanceof Boolean) {
791+
JSONAssert.assertNotEquals(message, (String) expected, (String) actual, (Boolean) strictMode);
792+
} else {
793+
fail(strictModeMessage);
794+
}
795+
assertEqualsFailed = false;
796+
fail(testShouldFailMessage); //will throw AssertionError
797+
} catch (AssertionError ae) {
798+
handleAssertionError(message, assertEqualsFailed, ae);
799+
}
800+
} else if(expected instanceof String && actual instanceof JSONObject) {
801+
try {
802+
if(strictMode instanceof JSONCompareMode) {
803+
JSONAssert.assertNotEquals(message, (String) expected, (JSONObject) actual, (JSONCompareMode) strictMode);
804+
} else if(strictMode instanceof Boolean) {
805+
JSONAssert.assertNotEquals(message, (String) expected, (JSONObject) actual, (Boolean) strictMode);
806+
} else {
807+
fail(strictModeMessage);
808+
}
809+
assertEqualsFailed = false;
810+
fail(testShouldFailMessage); //will throw AssertionError
811+
} catch (AssertionError ae) {
812+
handleAssertionError(message, assertEqualsFailed, ae);
813+
}
814+
} else if(expected instanceof JSONObject && actual instanceof JSONObject) {
815+
try {
816+
if(strictMode instanceof JSONCompareMode) {
817+
JSONAssert.assertNotEquals(message, (JSONObject) expected, (JSONObject) actual, (JSONCompareMode) strictMode);
818+
} else if(strictMode instanceof Boolean) {
819+
JSONAssert.assertNotEquals(message, (JSONObject) expected, (JSONObject) actual, (Boolean) strictMode);
820+
} else {
821+
fail(strictModeMessage);
822+
}
823+
assertEqualsFailed = false;
824+
fail(testShouldFailMessage); //will throw AssertionError
825+
} catch (AssertionError ae) {
826+
handleAssertionError(message, assertEqualsFailed, ae);
827+
}
828+
} else {
829+
fail("No overloaded method found to call");
830+
}
831+
}
607832

608833
private void handleAssertionError(String message, boolean assertEqualsFailed, AssertionError ae) throws AssertionError {
609834
if(assertEqualsFailed) {

0 commit comments

Comments
 (0)