Skip to content

Commit

Permalink
Add methods for content equals comparision
Browse files Browse the repository at this point in the history
  • Loading branch information
btrajkovski committed Oct 4, 2017
1 parent 376c2fc commit 7e7d297
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/org/junit/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,40 @@ public static void assertNotSame(Object unexpected, Object actual) {
assertNotSame(null, unexpected, actual);
}

/**
* Asserts that two {@link CharSequence} instances have same content.
* If they do not, an {@link AssertionError} is thrown. If
* <code>expected</code> and <code>actual</code> are <code>null</code>,
* they are considered same content.
*
* @param expected the expected contents
* @param actual the object to compare to
*/
public static void assertContentEquals(String expected, CharSequence actual) {
assertContentEquals(null, expected, actual);
}

/**
* Asserts that two {@link CharSequence} instances have same content.
* If they do not, an {@link AssertionError} is thrown with the given message. If
* <code>expected</code> and <code>actual</code> are <code>null</code>,
* they are considered same content.
*
* @param message the identifying message for the {@link AssertionError}
* @param expected the expected contents
* @param actual the object to compare to
*/
public static void assertContentEquals(String message, String expected, CharSequence actual) {
if (equalsRegardingNull(expected, actual)) {
return;
}
if (expected == null || actual == null) {
failNotEquals(message, expected, actual);
}

assertEquals(message, expected, actual.toString());
}

private static void failSame(String message) {
String formatted = "";
if (message != null) {
Expand Down
85 changes: 85 additions & 0 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertContentEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
Expand Down Expand Up @@ -956,6 +957,90 @@ public void expectThrowsUsesCanonicalNameWhenRequiredExceptionNotThrown() {
throw new AssertionError(ASSERTION_ERROR_EXPECTED);
}

@Test
public void assertContentEqualsPass() throws Exception {
String expected = "StringValue";
CharSequence charSequence = new String("StringValue");
assertContentEquals(expected, charSequence);
}

@Test
public void assertContentEqualsPassBothNull() throws Exception {
String expected = null;
CharSequence charSequence = null;
assertContentEquals(expected, charSequence);
}

@Test
public void assertContentsActualNull() {
String expected = "StringValue";
CharSequence charSequence = null;
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<StringValue> but was:<null>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentsExpectedNull() {
String expected = null;
CharSequence charSequence = new String("StringValue");
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<null> but was:<StringValue>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentEqualsNotEqualButSameLength() {
String expected = "StringValue";
CharSequence charSequence = new String("NotTheSame!");
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<[StringValue]> but was:<[NotTheSame!]>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentEqualsNotEqualDifferentLength() {
String expected = "StringValue";
CharSequence charSequence = new String("NotTheSame");
try {
assertContentEquals(expected, charSequence);
} catch (AssertionError exception) {
String expectedException = "expected:<[StringValu]e> but was:<[NotTheSam]e>";
assertEquals(expectedException, exception.getMessage());
return;
}
fail("Expected an AssertionError");
}

@Test
public void assertContentEqualsPassCustomMessage() throws Exception {
String expected = "StringValue";
CharSequence charSequence = new String("StringValue");
assertContentEquals("My Message", expected, charSequence);
}

@Test
public void assertContentEqualsPassBothNullCustomMessage() throws Exception {
String expected = null;
CharSequence charSequence = null;
assertContentEquals("My Message", expected, charSequence);
}

private static class NestedException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
Expand Down

0 comments on commit 7e7d297

Please sign in to comment.