Permalink
Browse files

Added Comparable assertions assertGreaterThan and assertLessThan

  • Loading branch information...
1 parent 01b427b commit 57b49344eda5ea54c9642009db5c05e61466be62 Alan Escreet committed May 5, 2011
Showing with 184 additions and 3 deletions.
  1. +106 −1 src/main/java/org/junit/Assert.java
  2. +78 −2 src/test/java/org/junit/tests/assertion/AssertionTest.java
View
107 src/main/java/org/junit/Assert.java
@@ -646,7 +646,20 @@ static private void failNotEquals(String message, Object expected,
Object actual) {
fail(format(message, expected, actual));
}
-
+
+ static private <T> void failComparable(String message,
+ Comparable<T> reference, Comparable<T> actual, String comparison) {
+ String formatted= "";
+ if (message != null) {
+ formatted= message + " ";
+ }
+ String referenceString = String.valueOf(reference);
+ String actualString = String.valueOf(actual);
+ fail(formatted + "Expected " + comparison + ": "
+ + formatClassAndValue(reference, referenceString)
+ + " but was: " + formatClassAndValue(actual, actualString));
+ }
+
static String format(String message, Object expected, Object actual) {
String formatted= "";
if (message != null && !message.equals(""))
@@ -780,4 +793,96 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
throw new java.lang.AssertionError(description.toString());
}
}
+
+ private static <T> boolean assertComparableNullSafe(String reason,
+ Comparable<T> reference, Comparable<T> actual, String comparison) {
+ if (reference != null && actual != null) {
+ return true;
+ } else {
+ failComparable(reason, reference, actual, comparison);
+ return false;
+ }
+ }
+
+ /**
+ * Asserts that <code>actual</code> is less than <code>reference</code>. If
+ * not, an {@link AssertionError} is thrown with the given message. The
+ * comparison will fail if either <code>actual</code> or
+ * <code>reference</code> is <code>null</code>.
+ *
+ * @param message
+ * 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>
+ */
+ @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 {
+ failComparable(message, reference, actual, "less than");
+ }
+ }
+ }
+
+ /**
+ * Asserts that <code>actual</code> is less than <code>reference</code>. If
+ * not, an {@link AssertionError} is thrown. The comparison will fail if
+ * either <code>actual</code> or <code>reference</code> is <code>null</code>
+ * .
+ *
+ * @param reference
+ * The comparison reference value
+ * @param actual
+ * The value to check against <code>reference</code>
+ */
+ public static <T> void assertLessThan(Comparable<T> reference, Comparable<T> actual) {
+ assertLessThan(null, reference, actual);
+ }
+
+ /**
+ * Asserts that <code>actual</code> is greater than <code>reference</code>.
+ * If not, an {@link AssertionError} is thrown with the given message. The
+ * comparison will fail if either <code>actual</code> or
+ * <code>reference</code> is <code>null</code>.
+ *
+ * @param message
+ * 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>
+ */
+ @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 {
+ 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
+ * either <code>actual</code> or <code>reference</code> is <code>null</code>
+ * .
+ *
+ * @param reference
+ * The comparison reference value
+ * @param actual
+ * The value to check against <code>reference</code>
+ */
+ public static <T> void assertGreaterThan(Comparable<T> reference, Comparable<T> actual) {
+ assertGreaterThan(null, reference, actual);
+ }
}
View
80 src/test/java/org/junit/tests/assertion/AssertionTest.java
@@ -4,9 +4,11 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertGreaterThan;
+import static org.junit.Assert.assertLessThan;
import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@@ -482,5 +484,79 @@ public void compareBigDecimalAndInteger() {
final BigDecimal bigDecimal = new BigDecimal("1.2");
final Integer integer = Integer.valueOf("1");
assertEquals(bigDecimal, integer);
- }
+ }
+
+ 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");
+
+ @Test
+ public void assertLessThanFailsWithSelfExplanatoryMessage() {
+ try {
+ assertLessThan(REFERENCE_VALUE, REFERENCE_VALUE);
+ } catch (AssertionError e) {
+ assertEquals("Expected less than: java.math.BigDecimal<" + REFERENCE_VALUE
+ + "> but was: java.math.BigDecimal<" + REFERENCE_VALUE + '>', e.getMessage());
+ }
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertLessThanShouldFailWhenBothValuesNull() {
+ assertLessThan(null, null);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertLessThanShouldFailWhenReferenceNull() {
+ assertLessThan(null, REFERENCE_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertLessThanShouldFailWhenActualNull() {
+ assertLessThan(REFERENCE_VALUE, null);
+ }
+
+ @Test
+ public void assertLessThanShouldPassWhenActualLessThanReference() {
+ assertLessThan(REFERENCE_VALUE, LOWER_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertLessThanShouldFailWhenActualEqualToReference() {
+ assertLessThan(REFERENCE_VALUE, REFERENCE_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertLessThanShouldFailWhenActualGreaterThanReference() {
+ assertLessThan(REFERENCE_VALUE, HIGHER_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertGreaterThanShouldFailWhenBothValuesNull() {
+ assertGreaterThan(null, null);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertGreaterThanShouldFailWhenReferenceNull() {
+ assertGreaterThan(null, REFERENCE_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertGreaterThanShouldFailWhenActualNull() {
+ assertGreaterThan(REFERENCE_VALUE, null);
+ }
+
+ @Test
+ public void assertGreaterThanShouldPassWhenActualGreaterThanReference() {
+ assertGreaterThan(REFERENCE_VALUE, HIGHER_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertGreaterThanShouldFailWhenActualEqualToReference() {
+ assertGreaterThan(REFERENCE_VALUE, REFERENCE_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertGreaterThanShouldFailWhenActualLessThanReference() {
+ assertGreaterThan(REFERENCE_VALUE, LOWER_VALUE);
+ }
}

0 comments on commit 57b4934

Please sign in to comment.