Browse files

Removed BigDecimal values from comparison tests and replaced with a

generic stubbed Comparable type.
Renamed the null safe assertion back and removed unnecessary boolean
return value and logic.
  • Loading branch information...
1 parent 4f05d2a commit e1581fc3cdb21dd835df632c506dcacffd689135 Alan Escreet committed May 19, 2011
Showing with 57 additions and 56 deletions.
  1. +28 −18 src/main/java/org/junit/Assert.java
  2. +29 −38 src/test/java/org/junit/tests/assertion/AssertionTest.java
View
46 src/main/java/org/junit/Assert.java
@@ -793,14 +793,27 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
throw new java.lang.AssertionError(description.toString());
}
}
-
- private static <T extends Comparable<T>> boolean failComparableWhenNull(
+
+ /**
+ * Asserts that neither <code>reference</code> nor <code>actual</code> are
+ * <code>null</code> because comparisons can never be made against a
+ * <code>null</code> value.
+ *
+ * @param reason
+ * The identifying message for the {@link AssertionError} (
+ * <code>null</code> okay)
+ * @param reference
+ * The comparison reference value
+ * @param actual
+ * The value to check against <code>reference</code>
+ * @param comparison
+ * Text for the type of comparison being performed, in order to
+ * generate a meaningful assertion failure message.
+ */
+ private static <T extends Comparable<T>> void assertComparableNullSafe(
String reason, T reference, T actual, String comparison) {
- if (reference != null && actual != null) {
- return true;
- } else {
+ if (reference == null || actual == null) {
failComparable(reason, reference, actual, comparison);
- return false;
}
}
@@ -820,10 +833,9 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
*/
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");
- }
+ assertComparableNullSafe(message, reference, actual, "less than");
+ if (!(actual.compareTo(reference) < 0)) {
+ failComparable(message, reference, actual, "less than");
}
}
@@ -859,10 +871,9 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
*/
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");
- }
+ assertComparableNullSafe(message, reference, actual, "greater than");
+ if (!(actual.compareTo(reference) > 0)) {
+ failComparable(message, reference, actual, "greater than");
}
}
@@ -908,10 +919,9 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
*/
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");
- }
+ assertComparableNullSafe(message, reference, actual, "equivalent to");
+ if (!(actual.compareTo(reference) == 0)) {
+ failComparable(message, reference, actual, "equivalent to");
}
}
View
67 src/test/java/org/junit/tests/assertion/AssertionTest.java
@@ -486,12 +486,36 @@ public void compareBigDecimalAndInteger() {
final Integer integer = Integer.valueOf("1");
assertEquals(bigDecimal, integer);
}
+
+ /**
+ * Stub to act as a generic {@link Comparable} type in the comparison method
+ * tests.
+ */
+ private static class ComparableStub implements Comparable<ComparableStub> {
+ private final int compareToReturnValue;
+ private final boolean equalsReturnValue;
+
+ public ComparableStub(int compareToReturnValue,
+ boolean equalsReturnValue) {
+ this.compareToReturnValue= compareToReturnValue;
+ this.equalsReturnValue= equalsReturnValue;
+ }
+
+ public int compareTo(ComparableStub o) {
+ return this.compareToReturnValue;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return this.equalsReturnValue;
+ }
+ }
- 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");
- private static final BigDecimal EQUIVALENT_VALUE = new BigDecimal("1.0");
+ private static final ComparableStub NULL_VALUE = null;
+ private static final ComparableStub REFERENCE_VALUE = new ComparableStub(0, true);
+ private static final ComparableStub EQUIVALENT_VALUE = new ComparableStub(0, false);
+ private static final ComparableStub LOWER_VALUE = new ComparableStub(-1, false);
+ private static final ComparableStub HIGHER_VALUE = new ComparableStub(1, false);
@Test
public void assertLessThanFailsWithSelfExplanatoryMessage() {
@@ -582,37 +606,4 @@ 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 e1581fc

Please sign in to comment.