Skip to content

Commit 3944ff1

Browse files
committed
Add methods for content equals comparision (#1432)
1 parent 376c2fc commit 3944ff1

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/main/java/org/junit/Assert.java

+34
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,40 @@ public static void assertNotSame(Object unexpected, Object actual) {
812812
assertNotSame(null, unexpected, actual);
813813
}
814814

815+
/**
816+
* Asserts that two {@link CharSequence} instances have same content.
817+
* If they do not, an {@link AssertionError} is thrown. If
818+
* <code>expected</code> and <code>actual</code> are <code>null</code>,
819+
* they are considered same content.
820+
*
821+
* @param expected the expected contents
822+
* @param actual the object to compare to
823+
*/
824+
public static void assertContentEquals(String expected, CharSequence actual) {
825+
assertContentEquals(null, expected, actual);
826+
}
827+
828+
/**
829+
* Asserts that two {@link CharSequence} instances have same content.
830+
* If they do not, an {@link AssertionError} is thrown with the given message. If
831+
* <code>expected</code> and <code>actual</code> are <code>null</code>,
832+
* they are considered same content.
833+
*
834+
* @param message the identifying message for the {@link AssertionError}
835+
* @param expected the expected contents
836+
* @param actual the object to compare to
837+
*/
838+
public static void assertContentEquals(String message, String expected, CharSequence actual) {
839+
if (equalsRegardingNull(expected, actual)) {
840+
return;
841+
}
842+
if (expected == null || actual == null) {
843+
failNotEquals(message, expected, actual);
844+
}
845+
846+
assertEquals(message, expected, actual.toString());
847+
}
848+
815849
private static void failSame(String message) {
816850
String formatted = "";
817851
if (message != null) {

src/test/java/org/junit/tests/assertion/AssertionTest.java

+85
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.hamcrest.CoreMatchers.instanceOf;
55
import static org.hamcrest.CoreMatchers.is;
66
import static org.junit.Assert.assertArrayEquals;
7+
import static org.junit.Assert.assertContentEquals;
78
import static org.junit.Assert.assertEquals;
89
import static org.junit.Assert.assertNotEquals;
910
import static org.junit.Assert.assertNotSame;
@@ -956,6 +957,90 @@ public void expectThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() {
956957
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
957958
}
958959

960+
@Test
961+
public void assertContentEqualsPass() throws Exception {
962+
String expected = "StringValue";
963+
CharSequence charSequence = new String("StringValue");
964+
assertContentEquals(expected, charSequence);
965+
}
966+
967+
@Test
968+
public void assertContentEqualsPassBothNull() throws Exception {
969+
String expected = null;
970+
CharSequence charSequence = null;
971+
assertContentEquals(expected, charSequence);
972+
}
973+
974+
@Test
975+
public void assertContentsActualNull() {
976+
String expected = "StringValue";
977+
CharSequence charSequence = null;
978+
try {
979+
assertContentEquals(expected, charSequence);
980+
} catch (AssertionError exception) {
981+
String expectedException = "expected:<StringValue> but was:<null>";
982+
assertEquals(expectedException, exception.getMessage());
983+
return;
984+
}
985+
fail("Expected an AssertionError");
986+
}
987+
988+
@Test
989+
public void assertContentsExpectedNull() {
990+
String expected = null;
991+
CharSequence charSequence = new String("StringValue");
992+
try {
993+
assertContentEquals(expected, charSequence);
994+
} catch (AssertionError exception) {
995+
String expectedException = "expected:<null> but was:<StringValue>";
996+
assertEquals(expectedException, exception.getMessage());
997+
return;
998+
}
999+
fail("Expected an AssertionError");
1000+
}
1001+
1002+
@Test
1003+
public void assertContentEqualsNotEqualButSameLength() {
1004+
String expected = "StringValue";
1005+
CharSequence charSequence = new String("NotTheSame!");
1006+
try {
1007+
assertContentEquals(expected, charSequence);
1008+
} catch (AssertionError exception) {
1009+
String expectedException = "expected:<[StringValue]> but was:<[NotTheSame!]>";
1010+
assertEquals(expectedException, exception.getMessage());
1011+
return;
1012+
}
1013+
fail("Expected an AssertionError");
1014+
}
1015+
1016+
@Test
1017+
public void assertContentEqualsNotEqualDifferentLength() {
1018+
String expected = "StringValue";
1019+
CharSequence charSequence = new String("NotTheSame");
1020+
try {
1021+
assertContentEquals(expected, charSequence);
1022+
} catch (AssertionError exception) {
1023+
String expectedException = "expected:<[StringValu]e> but was:<[NotTheSam]e>";
1024+
assertEquals(expectedException, exception.getMessage());
1025+
return;
1026+
}
1027+
fail("Expected an AssertionError");
1028+
}
1029+
1030+
@Test
1031+
public void assertContentEqualsPassCustomMessage() throws Exception {
1032+
String expected = "StringValue";
1033+
CharSequence charSequence = new String("StringValue");
1034+
assertContentEquals("My Message", expected, charSequence);
1035+
}
1036+
1037+
@Test
1038+
public void assertContentEqualsPassBothNullCustomMessage() throws Exception {
1039+
String expected = null;
1040+
CharSequence charSequence = null;
1041+
assertContentEquals("My Message", expected, charSequence);
1042+
}
1043+
9591044
private static class NestedException extends RuntimeException {
9601045
private static final long serialVersionUID = 1L;
9611046
}

0 commit comments

Comments
 (0)