diff options
Diffstat (limited to 'src/main/java/org/junit/Assert.java')
-rw-r--r--[-rwxr-xr-x] | src/main/java/org/junit/Assert.java | 170 |
1 files changed, 123 insertions, 47 deletions
diff --git a/src/main/java/org/junit/Assert.java b/src/main/java/org/junit/Assert.java index d7deb06..65bbc9d 100755..100644 --- a/src/main/java/org/junit/Assert.java +++ b/src/main/java/org/junit/Assert.java @@ -2,6 +2,7 @@ package org.junit; import org.hamcrest.Matcher; import org.hamcrest.MatcherAssert; +import org.junit.function.ThrowingRunnable; import org.junit.internal.ArrayComparisonFailure; import org.junit.internal.ExactComparisonCriteria; import org.junit.internal.InexactComparisonCriteria; @@ -36,7 +37,7 @@ public class Assert { * okay) * @param condition condition to be checked */ - static public void assertTrue(String message, boolean condition) { + public static void assertTrue(String message, boolean condition) { if (!condition) { fail(message); } @@ -48,7 +49,7 @@ public class Assert { * * @param condition condition to be checked */ - static public void assertTrue(boolean condition) { + public static void assertTrue(boolean condition) { assertTrue(null, condition); } @@ -60,7 +61,7 @@ public class Assert { * okay) * @param condition condition to be checked */ - static public void assertFalse(String message, boolean condition) { + public static void assertFalse(String message, boolean condition) { assertTrue(message, !condition); } @@ -70,7 +71,7 @@ public class Assert { * * @param condition condition to be checked */ - static public void assertFalse(boolean condition) { + public static void assertFalse(boolean condition) { assertFalse(null, condition); } @@ -81,7 +82,7 @@ public class Assert { * okay) * @see AssertionError */ - static public void fail(String message) { + public static void fail(String message) { if (message == null) { throw new AssertionError(); } @@ -91,7 +92,7 @@ public class Assert { /** * Fails a test with no message. */ - static public void fail() { + public static void fail() { fail(null); } @@ -106,11 +107,12 @@ public class Assert { * @param expected expected value * @param actual actual value */ - static public void assertEquals(String message, Object expected, + public static void assertEquals(String message, Object expected, Object actual) { if (equalsRegardingNull(expected, actual)) { return; - } else if (expected instanceof String && actual instanceof String) { + } + if (expected instanceof String && actual instanceof String) { String cleanMessage = message == null ? "" : message; throw new ComparisonFailure(cleanMessage, (String) expected, (String) actual); @@ -140,7 +142,7 @@ public class Assert { * @param expected expected value * @param actual the value to check against <code>expected</code> */ - static public void assertEquals(Object expected, Object actual) { + public static void assertEquals(Object expected, Object actual) { assertEquals(null, expected, actual); } @@ -155,7 +157,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - static public void assertNotEquals(String message, Object unexpected, + public static void assertNotEquals(String message, Object unexpected, Object actual) { if (equalsRegardingNull(unexpected, actual)) { failEquals(message, actual); @@ -171,7 +173,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - static public void assertNotEquals(Object unexpected, Object actual) { + public static void assertNotEquals(Object unexpected, Object actual) { assertNotEquals(null, unexpected, actual); } @@ -194,7 +196,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - static public void assertNotEquals(String message, long unexpected, long actual) { + public static void assertNotEquals(String message, long unexpected, long actual) { if (unexpected == actual) { failEquals(message, Long.valueOf(actual)); } @@ -207,7 +209,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - static public void assertNotEquals(long unexpected, long actual) { + public static void assertNotEquals(long unexpected, long actual) { assertNotEquals(null, unexpected, actual); } @@ -226,7 +228,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - static public void assertNotEquals(String message, double unexpected, + public static void assertNotEquals(String message, double unexpected, double actual, double delta) { if (!doubleIsDifferent(unexpected, actual, delta)) { failEquals(message, Double.valueOf(actual)); @@ -245,7 +247,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - static public void assertNotEquals(double unexpected, double actual, double delta) { + public static void assertNotEquals(double unexpected, double actual, double delta) { assertNotEquals(null, unexpected, actual, delta); } @@ -261,7 +263,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - static public void assertNotEquals(float unexpected, float actual, float delta) { + public static void assertNotEquals(float unexpected, float actual, float delta) { assertNotEquals(null, unexpected, actual, delta); } @@ -297,7 +299,7 @@ public class Assert { public static void assertArrayEquals(Object[] expecteds, Object[] actuals) { assertArrayEquals(null, expecteds, actuals); } - + /** * Asserts that two boolean arrays are equal. If they are not, an * {@link AssertionError} is thrown with the given message. If @@ -312,8 +314,8 @@ public class Assert { public static void assertArrayEquals(String message, boolean[] expecteds, boolean[] actuals) throws ArrayComparisonFailure { internalArrayEquals(message, expecteds, actuals); - } - + } + /** * Asserts that two boolean arrays are equal. If they are not, an * {@link AssertionError} is thrown. If <code>expected</code> and @@ -547,7 +549,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - static public void assertEquals(String message, double expected, + public static void assertEquals(String message, double expected, double actual, double delta) { if (doubleIsDifferent(expected, actual, delta)) { failNotEquals(message, Double.valueOf(expected), Double.valueOf(actual)); @@ -569,7 +571,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - static public void assertEquals(String message, float expected, + public static void assertEquals(String message, float expected, float actual, float delta) { if (floatIsDifferent(expected, actual, delta)) { failNotEquals(message, Float.valueOf(expected), Float.valueOf(actual)); @@ -591,14 +593,14 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - static public void assertNotEquals(String message, float unexpected, + public static void assertNotEquals(String message, float unexpected, float actual, float delta) { if (!floatIsDifferent(unexpected, actual, delta)) { failEquals(message, Float.valueOf(actual)); } } - static private boolean doubleIsDifferent(double d1, double d2, double delta) { + private static boolean doubleIsDifferent(double d1, double d2, double delta) { if (Double.compare(d1, d2) == 0) { return false; } @@ -609,7 +611,7 @@ public class Assert { return true; } - static private boolean floatIsDifferent(float f1, float f2, float delta) { + private static boolean floatIsDifferent(float f1, float f2, float delta) { if (Float.compare(f1, f2) == 0) { return false; } @@ -627,7 +629,7 @@ public class Assert { * @param expected expected long value. * @param actual actual long value */ - static public void assertEquals(long expected, long actual) { + public static void assertEquals(long expected, long actual) { assertEquals(null, expected, actual); } @@ -640,7 +642,7 @@ public class Assert { * @param expected long expected value. * @param actual long actual value */ - static public void assertEquals(String message, long expected, long actual) { + public static void assertEquals(String message, long expected, long actual) { if (expected != actual) { failNotEquals(message, Long.valueOf(expected), Long.valueOf(actual)); } @@ -652,7 +654,7 @@ public class Assert { * instead */ @Deprecated - static public void assertEquals(double expected, double actual) { + public static void assertEquals(double expected, double actual) { assertEquals(null, expected, actual); } @@ -662,7 +664,7 @@ public class Assert { * instead */ @Deprecated - static public void assertEquals(String message, double expected, + public static void assertEquals(String message, double expected, double actual) { fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers"); } @@ -679,7 +681,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - static public void assertEquals(double expected, double actual, double delta) { + public static void assertEquals(double expected, double actual, double delta) { assertEquals(null, expected, actual, delta); } @@ -695,8 +697,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - - static public void assertEquals(float expected, float actual, float delta) { + public static void assertEquals(float expected, float actual, float delta) { assertEquals(null, expected, actual, delta); } @@ -708,7 +709,7 @@ public class Assert { * okay) * @param object Object to check or <code>null</code> */ - static public void assertNotNull(String message, Object object) { + public static void assertNotNull(String message, Object object) { assertTrue(message, object != null); } @@ -718,7 +719,7 @@ public class Assert { * * @param object Object to check or <code>null</code> */ - static public void assertNotNull(Object object) { + public static void assertNotNull(Object object) { assertNotNull(null, object); } @@ -730,7 +731,7 @@ public class Assert { * okay) * @param object Object to check or <code>null</code> */ - static public void assertNull(String message, Object object) { + public static void assertNull(String message, Object object) { if (object == null) { return; } @@ -743,11 +744,11 @@ public class Assert { * * @param object Object to check or <code>null</code> */ - static public void assertNull(Object object) { + public static void assertNull(Object object) { assertNull(null, object); } - static private void failNotNull(String message, Object actual) { + private static void failNotNull(String message, Object actual) { String formatted = ""; if (message != null) { formatted = message + " "; @@ -764,7 +765,7 @@ public class Assert { * @param expected the expected object * @param actual the object to compare to <code>expected</code> */ - static public void assertSame(String message, Object expected, Object actual) { + public static void assertSame(String message, Object expected, Object actual) { if (expected == actual) { return; } @@ -778,7 +779,7 @@ public class Assert { * @param expected the expected object * @param actual the object to compare to <code>expected</code> */ - static public void assertSame(Object expected, Object actual) { + public static void assertSame(Object expected, Object actual) { assertSame(null, expected, actual); } @@ -792,7 +793,7 @@ public class Assert { * @param unexpected the object you don't expect * @param actual the object to compare to <code>unexpected</code> */ - static public void assertNotSame(String message, Object unexpected, + public static void assertNotSame(String message, Object unexpected, Object actual) { if (unexpected == actual) { failSame(message); @@ -807,11 +808,11 @@ public class Assert { * @param unexpected the object you don't expect * @param actual the object to compare to <code>unexpected</code> */ - static public void assertNotSame(Object unexpected, Object actual) { + public static void assertNotSame(Object unexpected, Object actual) { assertNotSame(null, unexpected, actual); } - static private void failSame(String message) { + private static void failSame(String message) { String formatted = ""; if (message != null) { formatted = message + " "; @@ -819,7 +820,7 @@ public class Assert { fail(formatted + "expected not same"); } - static private void failNotSame(String message, Object expected, + private static void failNotSame(String message, Object expected, Object actual) { String formatted = ""; if (message != null) { @@ -829,19 +830,19 @@ public class Assert { + ">"); } - static private void failNotEquals(String message, Object expected, + private static void failNotEquals(String message, Object expected, Object actual) { fail(format(message, expected, actual)); } static String format(String message, Object expected, Object actual) { String formatted = ""; - if (message != null && !message.equals("")) { + if (message != null && !"".equals(message)) { formatted = message + " "; } String expectedString = String.valueOf(expected); String actualString = String.valueOf(actual); - if (expectedString.equals(actualString)) { + if (equalsRegardingNull(expectedString, actualString)) { return formatted + "expected: " + formatClassAndValue(expected, expectedString) + " but was: " + formatClassAndValue(actual, actualString); @@ -851,6 +852,11 @@ public class Assert { } } + private static String formatClass(Class<?> value) { + String className = value.getCanonicalName(); + return className == null ? value.getName() : className; + } + private static String formatClassAndValue(Object value, String valueString) { String className = value == null ? "null" : value.getClass().getName(); return className + "<" + valueString + ">"; @@ -917,8 +923,9 @@ public class Assert { * @param matcher an expression, built of {@link Matcher}s, specifying allowed * values * @see org.hamcrest.CoreMatchers - * @see org.hamcrest.MatcherAssert + * @deprecated use {@code org.hamcrest.MatcherAssert.assertThat()} */ + @Deprecated public static <T> void assertThat(T actual, Matcher<? super T> matcher) { assertThat("", actual, matcher); } @@ -949,10 +956,79 @@ public class Assert { * @param matcher an expression, built of {@link Matcher}s, specifying allowed * values * @see org.hamcrest.CoreMatchers - * @see org.hamcrest.MatcherAssert + * @deprecated use {@code org.hamcrest.MatcherAssert.assertThat()} */ + @Deprecated public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) { MatcherAssert.assertThat(reason, actual, matcher); } + + /** + * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when + * executed. If it does, the exception object is returned. If it does not throw an exception, an + * {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code + * AssertionError} is thrown describing the mismatch; the exception that was actually thrown can + * be obtained by calling {@link AssertionError#getCause}. + * + * @param expectedThrowable the expected type of the exception + * @param runnable a function that is expected to throw an exception when executed + * @return the exception thrown by {@code runnable} + * @since 4.13 + */ + public static <T extends Throwable> T assertThrows(Class<T> expectedThrowable, + ThrowingRunnable runnable) { + return assertThrows(null, expectedThrowable, runnable); + } + + /** + * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when + * executed. If it does, the exception object is returned. If it does not throw an exception, an + * {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code + * AssertionError} is thrown describing the mismatch; the exception that was actually thrown can + * be obtained by calling {@link AssertionError#getCause}. + * + * @param message the identifying message for the {@link AssertionError} (<code>null</code> + * okay) + * @param expectedThrowable the expected type of the exception + * @param runnable a function that is expected to throw an exception when executed + * @return the exception thrown by {@code runnable} + * @since 4.13 + */ + public static <T extends Throwable> T assertThrows(String message, Class<T> expectedThrowable, + ThrowingRunnable runnable) { + try { + runnable.run(); + } catch (Throwable actualThrown) { + if (expectedThrowable.isInstance(actualThrown)) { + @SuppressWarnings("unchecked") T retVal = (T) actualThrown; + return retVal; + } else { + String expected = formatClass(expectedThrowable); + Class<? extends Throwable> actualThrowable = actualThrown.getClass(); + String actual = formatClass(actualThrowable); + if (expected.equals(actual)) { + // There must be multiple class loaders. Add the identity hash code so the message + // doesn't say "expected: java.lang.String<my.package.MyException> ..." + expected += "@" + Integer.toHexString(System.identityHashCode(expectedThrowable)); + actual += "@" + Integer.toHexString(System.identityHashCode(actualThrowable)); + } + String mismatchMessage = buildPrefix(message) + + format("unexpected exception type thrown;", expected, actual); + + // The AssertionError(String, Throwable) ctor is only available on JDK7. + AssertionError assertionError = new AssertionError(mismatchMessage); + assertionError.initCause(actualThrown); + throw assertionError; + } + } + String notThrownMessage = buildPrefix(message) + String + .format("expected %s to be thrown, but nothing was thrown", + formatClass(expectedThrowable)); + throw new AssertionError(notThrownMessage); + } + + private static String buildPrefix(String message) { + return message != null && message.length() != 0 ? message + ": " : ""; + } } |