Skip to content

Commit

Permalink
Name change of private method for null comparisons to conform with
Browse files Browse the repository at this point in the history
naming standards.
Changed parameter types to explicitly accept Comparable types.
Added sanity check for accepted parameter types of the comparison
methods.
  • Loading branch information
Alan Escreet committed May 19, 2011
1 parent 8e01b19 commit 4f05d2a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
53 changes: 23 additions & 30 deletions src/main/java/org/junit/Assert.java
Expand Up @@ -794,8 +794,8 @@ public static <T> void assertThat(String reason, T actual,
}
}

private static <T> boolean assertComparableNullSafe(String reason,
Comparable<T> reference, Comparable<T> actual, String comparison) {
private static <T extends Comparable<T>> boolean failComparableWhenNull(
String reason, T reference, T actual, String comparison) {
if (reference != null && actual != null) {
return true;
} else {
Expand All @@ -818,13 +818,10 @@ private static <T> boolean assertComparableNullSafe(String reason,
* @param actual
* The value to check against <code>reference</code>
*/
@SuppressWarnings("unchecked")
public static <T> void assertLessThan(String message,
Comparable<T> reference, Comparable<T> actual) {
if (assertComparableNullSafe(message, reference, actual, "less than")) {
if (actual.compareTo((T) reference) < 0) {
return;
} else {
public static <T extends Comparable<T>> void assertLessThan(String message,
T reference, T actual) {
if (failComparableWhenNull(message, reference, actual, "less than")) {
if (!(actual.compareTo(reference) < 0)) {
failComparable(message, reference, actual, "less than");
}
}
Expand All @@ -841,7 +838,8 @@ public static <T> void assertLessThan(String message,
* @param actual
* The value to check against <code>reference</code>
*/
public static <T> void assertLessThan(Comparable<T> reference, Comparable<T> actual) {
public static <T extends Comparable<T>> void assertLessThan(T reference,
T actual) {
assertLessThan(null, reference, actual);
}

Expand All @@ -859,18 +857,15 @@ public static <T> void assertLessThan(Comparable<T> reference, Comparable<T> act
* @param actual
* The value to check against <code>reference</code>
*/
@SuppressWarnings("unchecked")
public static <T> void assertGreaterThan(String message,
Comparable<T> reference, Comparable<T> actual) {
if (assertComparableNullSafe(message, reference, actual, "greater than")) {
if (actual.compareTo((T) reference) > 0) {
return;
} else {
public static <T extends Comparable<T>> void assertGreaterThan(
String message, T reference, T actual) {
if (failComparableWhenNull(message, reference, actual, "greater than")) {
if (!(actual.compareTo(reference) > 0)) {
failComparable(message, reference, actual, "greater than");
}
}
}

/**
* Asserts that <code>actual</code> is less than <code>reference</code>. If
* not, an {@link AssertionError} is thrown. The comparison will fail if
Expand All @@ -882,10 +877,11 @@ public static <T> void assertGreaterThan(String message,
* @param actual
* The value to check against <code>reference</code>
*/
public static <T> void assertGreaterThan(Comparable<T> reference, Comparable<T> actual) {
public static <T extends Comparable<T>> void assertGreaterThan(T reference,
T actual) {
assertGreaterThan(null, reference, actual);
}

/**
* Asserts that <code>actual</code> is equivalent to <code>reference</code>.
* If not, an {@link AssertionError} is thrown. The comparison will fail if
Expand All @@ -910,18 +906,15 @@ public static <T> void assertGreaterThan(Comparable<T> reference, Comparable<T>
* @param actual
* The value to check against <code>reference</code>
*/
@SuppressWarnings("unchecked")
public static <T> void assertEquivalent(String message,
Comparable<T> reference, Comparable<T> actual) {
if (assertComparableNullSafe(message, reference, actual, "equivalent to")) {
if (actual.compareTo((T) reference) == 0) {
return;
} else {
public static <T extends Comparable<T>> void assertEquivalent(
String message, T reference, T actual) {
if (failComparableWhenNull(message, reference, actual, "equivalent to")) {
if (!(actual.compareTo(reference) == 0)) {
failComparable(message, reference, actual, "equivalent to");
}
}
}

/**
* Asserts that <code>actual</code> is equivalent to <code>reference</code>.
* If not, an {@link AssertionError} is thrown. The comparison will fail if
Expand All @@ -943,8 +936,8 @@ public static <T> void assertEquivalent(String message,
* @param actual
* The value to check against <code>reference</code>
*/
public static <T> void assertEquivalent(Comparable<T> reference,
Comparable<T> actual) {
public static <T extends Comparable<T>> void assertEquivalent(T reference,
T actual) {
assertEquivalent(null, reference, actual);
}
}
38 changes: 36 additions & 2 deletions src/test/java/org/junit/tests/assertion/AssertionTest.java
Expand Up @@ -487,6 +487,7 @@ public void compareBigDecimalAndInteger() {
assertEquals(bigDecimal, integer);
}

private static final BigDecimal NULL_VALUE = null;
private static final BigDecimal REFERENCE_VALUE = BigDecimal.ONE;
private static final BigDecimal LOWER_VALUE = new BigDecimal("0.99");
private static final BigDecimal HIGHER_VALUE = new BigDecimal("1.01");
Expand All @@ -504,7 +505,7 @@ public void assertLessThanFailsWithSelfExplanatoryMessage() {

@Test(expected=AssertionError.class)
public void assertLessThanShouldFailWhenBothValuesNull() {
assertLessThan(null, null);
assertLessThan(NULL_VALUE, NULL_VALUE);
}

@Test(expected=AssertionError.class)
Expand Down Expand Up @@ -534,7 +535,7 @@ public void assertLessThanShouldFailWhenActualGreaterThanReference() {

@Test(expected=AssertionError.class)
public void assertGreaterThanShouldFailWhenBothValuesNull() {
assertGreaterThan(null, null);
assertGreaterThan(NULL_VALUE, NULL_VALUE);
}

@Test(expected=AssertionError.class)
Expand Down Expand Up @@ -581,4 +582,37 @@ public void assertEquivalentShouldFailWhenActualLessThanReference() {
public void assertEquivalentShouldFailWhenActualGreaterThanReference() {
assertEquivalent(REFERENCE_VALUE, HIGHER_VALUE);
}

/**
* Stubbed {@link Comparable} to act as a sanity check for the parameter
* types of the comparison methods.
* <p>
* It is always equivalent to itself.
*/
private class ComparableStub implements Comparable<ComparableStub> {
public int compareTo(ComparableStub o) {
return 0;
}
}

@Test(expected=AssertionError.class)
public void assertLessThanShouldAcceptAnyComparableType() {
ComparableStub comparable_value = new ComparableStub();

assertLessThan(comparable_value, comparable_value);
}

@Test(expected=AssertionError.class)
public void assertGreaterThanShouldAcceptAnyComparableType() {
ComparableStub comparable_value = new ComparableStub();

assertGreaterThan(comparable_value, comparable_value);
}

@Test
public void assertEquivalentShouldAcceptAnyComparableType() {
ComparableStub comparable_value = new ComparableStub();

assertEquivalent(comparable_value, comparable_value);
}
}

0 comments on commit 4f05d2a

Please sign in to comment.