Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Name change of private method for null comparisons to conform with

naming standards.
Changed parameter types to explicitly accept Comparable types.
Added sanity check for accepted parameter types of the comparison
methods.
  • Loading branch information...
commit 4f05d2a56f1cd3bd5141757d1e67672bcc3175e8 1 parent 8e01b19
Alan Escreet authored
View
53 src/main/java/org/junit/Assert.java
@@ -794,8 +794,8 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
}
}
- 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 {
@@ -818,13 +818,10 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* @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");
}
}
@@ -841,7 +838,8 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* @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);
}
@@ -859,18 +857,15 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* @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
@@ -882,10 +877,11 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* @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
@@ -910,18 +906,15 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* @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
@@ -943,8 +936,8 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
* @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);
}
}
View
38 src/test/java/org/junit/tests/assertion/AssertionTest.java
@@ -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");
@@ -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)
@@ -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)
@@ -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);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.