diff options
Diffstat (limited to 'src/main/java/org/junit/Assert.java')
-rwxr-xr-x[-rw-r--r--] | src/main/java/org/junit/Assert.java | 170 |
1 files changed, 47 insertions, 123 deletions
diff --git a/src/main/java/org/junit/Assert.java b/src/main/java/org/junit/Assert.java index 65bbc9d..d7deb06 100644..100755 --- a/src/main/java/org/junit/Assert.java +++ b/src/main/java/org/junit/Assert.java @@ -2,7 +2,6 @@ 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; @@ -37,7 +36,7 @@ public class Assert { * okay) * @param condition condition to be checked */ - public static void assertTrue(String message, boolean condition) { + static public void assertTrue(String message, boolean condition) { if (!condition) { fail(message); } @@ -49,7 +48,7 @@ public class Assert { * * @param condition condition to be checked */ - public static void assertTrue(boolean condition) { + static public void assertTrue(boolean condition) { assertTrue(null, condition); } @@ -61,7 +60,7 @@ public class Assert { * okay) * @param condition condition to be checked */ - public static void assertFalse(String message, boolean condition) { + static public void assertFalse(String message, boolean condition) { assertTrue(message, !condition); } @@ -71,7 +70,7 @@ public class Assert { * * @param condition condition to be checked */ - public static void assertFalse(boolean condition) { + static public void assertFalse(boolean condition) { assertFalse(null, condition); } @@ -82,7 +81,7 @@ public class Assert { * okay) * @see AssertionError */ - public static void fail(String message) { + static public void fail(String message) { if (message == null) { throw new AssertionError(); } @@ -92,7 +91,7 @@ public class Assert { /** * Fails a test with no message. */ - public static void fail() { + static public void fail() { fail(null); } @@ -107,12 +106,11 @@ public class Assert { * @param expected expected value * @param actual actual value */ - public static void assertEquals(String message, Object expected, + static public void assertEquals(String message, Object expected, Object actual) { if (equalsRegardingNull(expected, actual)) { return; - } - if (expected instanceof String && actual instanceof String) { + } else if (expected instanceof String && actual instanceof String) { String cleanMessage = message == null ? "" : message; throw new ComparisonFailure(cleanMessage, (String) expected, (String) actual); @@ -142,7 +140,7 @@ public class Assert { * @param expected expected value * @param actual the value to check against <code>expected</code> */ - public static void assertEquals(Object expected, Object actual) { + static public void assertEquals(Object expected, Object actual) { assertEquals(null, expected, actual); } @@ -157,7 +155,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - public static void assertNotEquals(String message, Object unexpected, + static public void assertNotEquals(String message, Object unexpected, Object actual) { if (equalsRegardingNull(unexpected, actual)) { failEquals(message, actual); @@ -173,7 +171,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - public static void assertNotEquals(Object unexpected, Object actual) { + static public void assertNotEquals(Object unexpected, Object actual) { assertNotEquals(null, unexpected, actual); } @@ -196,7 +194,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - public static void assertNotEquals(String message, long unexpected, long actual) { + static public void assertNotEquals(String message, long unexpected, long actual) { if (unexpected == actual) { failEquals(message, Long.valueOf(actual)); } @@ -209,7 +207,7 @@ public class Assert { * @param unexpected unexpected value to check * @param actual the value to check against <code>unexpected</code> */ - public static void assertNotEquals(long unexpected, long actual) { + static public void assertNotEquals(long unexpected, long actual) { assertNotEquals(null, unexpected, actual); } @@ -228,7 +226,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertNotEquals(String message, double unexpected, + static public void assertNotEquals(String message, double unexpected, double actual, double delta) { if (!doubleIsDifferent(unexpected, actual, delta)) { failEquals(message, Double.valueOf(actual)); @@ -247,7 +245,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertNotEquals(double unexpected, double actual, double delta) { + static public void assertNotEquals(double unexpected, double actual, double delta) { assertNotEquals(null, unexpected, actual, delta); } @@ -263,7 +261,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertNotEquals(float unexpected, float actual, float delta) { + static public void assertNotEquals(float unexpected, float actual, float delta) { assertNotEquals(null, unexpected, actual, delta); } @@ -299,7 +297,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 @@ -314,8 +312,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 @@ -549,7 +547,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertEquals(String message, double expected, + static public void assertEquals(String message, double expected, double actual, double delta) { if (doubleIsDifferent(expected, actual, delta)) { failNotEquals(message, Double.valueOf(expected), Double.valueOf(actual)); @@ -571,7 +569,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertEquals(String message, float expected, + static public void assertEquals(String message, float expected, float actual, float delta) { if (floatIsDifferent(expected, actual, delta)) { failNotEquals(message, Float.valueOf(expected), Float.valueOf(actual)); @@ -593,14 +591,14 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertNotEquals(String message, float unexpected, + static public void assertNotEquals(String message, float unexpected, float actual, float delta) { if (!floatIsDifferent(unexpected, actual, delta)) { failEquals(message, Float.valueOf(actual)); } } - private static boolean doubleIsDifferent(double d1, double d2, double delta) { + static private boolean doubleIsDifferent(double d1, double d2, double delta) { if (Double.compare(d1, d2) == 0) { return false; } @@ -611,7 +609,7 @@ public class Assert { return true; } - private static boolean floatIsDifferent(float f1, float f2, float delta) { + static private boolean floatIsDifferent(float f1, float f2, float delta) { if (Float.compare(f1, f2) == 0) { return false; } @@ -629,7 +627,7 @@ public class Assert { * @param expected expected long value. * @param actual actual long value */ - public static void assertEquals(long expected, long actual) { + static public void assertEquals(long expected, long actual) { assertEquals(null, expected, actual); } @@ -642,7 +640,7 @@ public class Assert { * @param expected long expected value. * @param actual long actual value */ - public static void assertEquals(String message, long expected, long actual) { + static public void assertEquals(String message, long expected, long actual) { if (expected != actual) { failNotEquals(message, Long.valueOf(expected), Long.valueOf(actual)); } @@ -654,7 +652,7 @@ public class Assert { * instead */ @Deprecated - public static void assertEquals(double expected, double actual) { + static public void assertEquals(double expected, double actual) { assertEquals(null, expected, actual); } @@ -664,7 +662,7 @@ public class Assert { * instead */ @Deprecated - public static void assertEquals(String message, double expected, + static public void assertEquals(String message, double expected, double actual) { fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers"); } @@ -681,7 +679,7 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertEquals(double expected, double actual, double delta) { + static public void assertEquals(double expected, double actual, double delta) { assertEquals(null, expected, actual, delta); } @@ -697,7 +695,8 @@ public class Assert { * <code>actual</code> for which both numbers are still * considered equal. */ - public static void assertEquals(float expected, float actual, float delta) { + + static public void assertEquals(float expected, float actual, float delta) { assertEquals(null, expected, actual, delta); } @@ -709,7 +708,7 @@ public class Assert { * okay) * @param object Object to check or <code>null</code> */ - public static void assertNotNull(String message, Object object) { + static public void assertNotNull(String message, Object object) { assertTrue(message, object != null); } @@ -719,7 +718,7 @@ public class Assert { * * @param object Object to check or <code>null</code> */ - public static void assertNotNull(Object object) { + static public void assertNotNull(Object object) { assertNotNull(null, object); } @@ -731,7 +730,7 @@ public class Assert { * okay) * @param object Object to check or <code>null</code> */ - public static void assertNull(String message, Object object) { + static public void assertNull(String message, Object object) { if (object == null) { return; } @@ -744,11 +743,11 @@ public class Assert { * * @param object Object to check or <code>null</code> */ - public static void assertNull(Object object) { + static public void assertNull(Object object) { assertNull(null, object); } - private static void failNotNull(String message, Object actual) { + static private void failNotNull(String message, Object actual) { String formatted = ""; if (message != null) { formatted = message + " "; @@ -765,7 +764,7 @@ public class Assert { * @param expected the expected object * @param actual the object to compare to <code>expected</code> */ - public static void assertSame(String message, Object expected, Object actual) { + static public void assertSame(String message, Object expected, Object actual) { if (expected == actual) { return; } @@ -779,7 +778,7 @@ public class Assert { * @param expected the expected object * @param actual the object to compare to <code>expected</code> */ - public static void assertSame(Object expected, Object actual) { + static public void assertSame(Object expected, Object actual) { assertSame(null, expected, actual); } @@ -793,7 +792,7 @@ public class Assert { * @param unexpected the object you don't expect * @param actual the object to compare to <code>unexpected</code> */ - public static void assertNotSame(String message, Object unexpected, + static public void assertNotSame(String message, Object unexpected, Object actual) { if (unexpected == actual) { failSame(message); @@ -808,11 +807,11 @@ public class Assert { * @param unexpected the object you don't expect * @param actual the object to compare to <code>unexpected</code> */ - public static void assertNotSame(Object unexpected, Object actual) { + static public void assertNotSame(Object unexpected, Object actual) { assertNotSame(null, unexpected, actual); } - private static void failSame(String message) { + static private void failSame(String message) { String formatted = ""; if (message != null) { formatted = message + " "; @@ -820,7 +819,7 @@ public class Assert { fail(formatted + "expected not same"); } - private static void failNotSame(String message, Object expected, + static private void failNotSame(String message, Object expected, Object actual) { String formatted = ""; if (message != null) { @@ -830,19 +829,19 @@ public class Assert { + ">"); } - private static void failNotEquals(String message, Object expected, + static private 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 && !"".equals(message)) { + if (message != null && !message.equals("")) { formatted = message + " "; } String expectedString = String.valueOf(expected); String actualString = String.valueOf(actual); - if (equalsRegardingNull(expectedString, actualString)) { + if (expectedString.equals(actualString)) { return formatted + "expected: " + formatClassAndValue(expected, expectedString) + " but was: " + formatClassAndValue(actual, actualString); @@ -852,11 +851,6 @@ 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 + ">"; @@ -923,9 +917,8 @@ public class Assert { * @param matcher an expression, built of {@link Matcher}s, specifying allowed * values * @see org.hamcrest.CoreMatchers - * @deprecated use {@code org.hamcrest.MatcherAssert.assertThat()} + * @see org.hamcrest.MatcherAssert */ - @Deprecated public static <T> void assertThat(T actual, Matcher<? super T> matcher) { assertThat("", actual, matcher); } @@ -956,79 +949,10 @@ public class Assert { * @param matcher an expression, built of {@link Matcher}s, specifying allowed * values * @see org.hamcrest.CoreMatchers - * @deprecated use {@code org.hamcrest.MatcherAssert.assertThat()} + * @see org.hamcrest.MatcherAssert */ - @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 + ": " : ""; - } } |