From f3aca4db137104480c0501f29d459f64b1f3f618 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 16 Dec 2016 15:11:13 +0000 Subject: Minor changes to JUnit 4.10 to make it more like 4.12 Some other projects break when built against 4.12. This allows those projects to be updated separately to the JUnit 4.12 upgrade. Assert.java and Description.java are copied directly from upstream 4.12; that brings in new methods from 4.12. The changes are so extensive because the upstream code was reformatted between 4.10 anbd 4.12. Between 4.10 and 4.12 RuleFieldValidator.java was renamed to RuleMemberValidator.java. To allow existing code that uses RuleFieldValidator to be modified to use RuleMemberValidator instead RuleFieldValidator.java has been copied to RuleMemberValidator.java. The change to BlockJUnit4ClassRunner reflects an upstream change made between 4.10 and 4.12. Bug: 33613916 Test: make checkbuild Change-Id: Id8ee6d08268152ec1a22a309c4582f8f6663ae88 --- src/main/java/org/junit/Assert.java | 1701 +++++++++++--------- .../runners/rules/RuleMemberValidator.java | 91 ++ src/main/java/org/junit/runner/Description.java | 522 +++--- .../org/junit/runners/BlockJUnit4ClassRunner.java | 2 +- 4 files changed, 1328 insertions(+), 988 deletions(-) mode change 100644 => 100755 src/main/java/org/junit/Assert.java create mode 100644 src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java (limited to 'src/main/java/org/junit') diff --git a/src/main/java/org/junit/Assert.java b/src/main/java/org/junit/Assert.java old mode 100644 new mode 100755 index b585b87..d7deb06 --- a/src/main/java/org/junit/Assert.java +++ b/src/main/java/org/junit/Assert.java @@ -1,8 +1,7 @@ package org.junit; -import org.hamcrest.Description; import org.hamcrest.Matcher; -import org.hamcrest.StringDescription; +import org.hamcrest.MatcherAssert; import org.junit.internal.ArrayComparisonFailure; import org.junit.internal.ExactComparisonCriteria; import org.junit.internal.InexactComparisonCriteria; @@ -11,773 +10,949 @@ import org.junit.internal.InexactComparisonCriteria; * A set of assertion methods useful for writing tests. Only failed assertions * are recorded. These methods can be used directly: * Assert.assertEquals(...), however, they read better if they - * are referenced through static import:
- * + * are referenced through static import: + * *
  * import static org.junit.Assert.*;
  *    ...
  *    assertEquals(...);
  * 
- * + * * @see AssertionError + * @since 4.0 */ public class Assert { - /** - * Protect constructor since it is a static only class - */ - protected Assert() { - } - - /** - * Asserts that a condition is true. If it isn't it throws an - * {@link AssertionError} with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param condition - * condition to be checked - */ - static public void assertTrue(String message, boolean condition) { - if (!condition) - fail(message); - } - - /** - * Asserts that a condition is true. If it isn't it throws an - * {@link AssertionError} without a message. - * - * @param condition - * condition to be checked - */ - static public void assertTrue(boolean condition) { - assertTrue(null, condition); - } - - /** - * Asserts that a condition is false. If it isn't it throws an - * {@link AssertionError} with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param condition - * condition to be checked - */ - static public void assertFalse(String message, boolean condition) { - assertTrue(message, !condition); - } - - /** - * Asserts that a condition is false. If it isn't it throws an - * {@link AssertionError} without a message. - * - * @param condition - * condition to be checked - */ - static public void assertFalse(boolean condition) { - assertFalse(null, condition); - } - - /** - * Fails a test with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @see AssertionError - */ - static public void fail(String message) { - if (message == null) - throw new AssertionError(); - throw new AssertionError(message); - } - - /** - * Fails a test with no message. - */ - static public void fail() { - fail(null); - } - - /** - * Asserts that two objects are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If - * expected and actual are null, - * they are considered equal. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expected - * expected value - * @param actual - * actual value - */ - static public void assertEquals(String message, Object expected, - Object actual) { - if (expected == null && actual == null) - return; - if (expected != null && isEquals(expected, actual)) - return; - else if (expected instanceof String && actual instanceof String) { - String cleanMessage= message == null ? "" : message; - throw new ComparisonFailure(cleanMessage, (String) expected, - (String) actual); - } else - failNotEquals(message, expected, actual); - } - - private static boolean isEquals(Object expected, Object actual) { - return expected.equals(actual); - } - - /** - * Asserts that two objects are equal. If they are not, an - * {@link AssertionError} without a message is thrown. If - * expected and actual are null, - * they are considered equal. - * - * @param expected - * expected value - * @param actual - * the value to check against expected - */ - static public void assertEquals(Object expected, Object actual) { - assertEquals(null, expected, actual); - } - - /** - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If - * expecteds and actuals are null, - * they are considered equal. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * Object array or array of arrays (multi-dimensional array) with - * expected values. - * @param actuals - * Object array or array of arrays (multi-dimensional array) with - * actual values - */ - public static void assertArrayEquals(String message, Object[] expecteds, - Object[] actuals) throws ArrayComparisonFailure { - internalArrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown. If expected and - * actual are null, they are considered - * equal. - * - * @param expecteds - * Object array or array of arrays (multi-dimensional array) with - * expected values - * @param actuals - * Object array or array of arrays (multi-dimensional array) with - * actual values - */ - public static void assertArrayEquals(Object[] expecteds, Object[] actuals) { - assertArrayEquals(null, expecteds, actuals); - } - - /** - * Asserts that two byte arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * byte array with expected values. - * @param actuals - * byte array with actual values - */ - public static void assertArrayEquals(String message, byte[] expecteds, - byte[] actuals) throws ArrayComparisonFailure { - internalArrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two byte arrays are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expecteds - * byte array with expected values. - * @param actuals - * byte array with actual values - */ - public static void assertArrayEquals(byte[] expecteds, byte[] actuals) { - assertArrayEquals(null, expecteds, actuals); - } - - /** - * Asserts that two char arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * char array with expected values. - * @param actuals - * char array with actual values - */ - public static void assertArrayEquals(String message, char[] expecteds, - char[] actuals) throws ArrayComparisonFailure { - internalArrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two char arrays are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expecteds - * char array with expected values. - * @param actuals - * char array with actual values - */ - public static void assertArrayEquals(char[] expecteds, char[] actuals) { - assertArrayEquals(null, expecteds, actuals); - } - - /** - * Asserts that two short arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * short array with expected values. - * @param actuals - * short array with actual values - */ - public static void assertArrayEquals(String message, short[] expecteds, - short[] actuals) throws ArrayComparisonFailure { - internalArrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two short arrays are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expecteds - * short array with expected values. - * @param actuals - * short array with actual values - */ - public static void assertArrayEquals(short[] expecteds, short[] actuals) { - assertArrayEquals(null, expecteds, actuals); - } - - /** - * Asserts that two int arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * int array with expected values. - * @param actuals - * int array with actual values - */ - public static void assertArrayEquals(String message, int[] expecteds, - int[] actuals) throws ArrayComparisonFailure { - internalArrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two int arrays are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expecteds - * int array with expected values. - * @param actuals - * int array with actual values - */ - public static void assertArrayEquals(int[] expecteds, int[] actuals) { - assertArrayEquals(null, expecteds, actuals); - } - - /** - * Asserts that two long arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * long array with expected values. - * @param actuals - * long array with actual values - */ - public static void assertArrayEquals(String message, long[] expecteds, - long[] actuals) throws ArrayComparisonFailure { - internalArrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two long arrays are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expecteds - * long array with expected values. - * @param actuals - * long array with actual values - */ - public static void assertArrayEquals(long[] expecteds, long[] actuals) { - assertArrayEquals(null, expecteds, actuals); - } - - /** - * Asserts that two double arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * double array with expected values. - * @param actuals - * double array with actual values - */ - public static void assertArrayEquals(String message, double[] expecteds, - double[] actuals, double delta) throws ArrayComparisonFailure { - new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two double arrays are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expecteds - * double array with expected values. - * @param actuals - * double array with actual values - */ - public static void assertArrayEquals(double[] expecteds, double[] actuals, double delta) { - assertArrayEquals(null, expecteds, actuals, delta); - } - - /** - * Asserts that two float arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * float array with expected values. - * @param actuals - * float array with actual values - */ - public static void assertArrayEquals(String message, float[] expecteds, - float[] actuals, float delta) throws ArrayComparisonFailure { - new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two float arrays are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expecteds - * float array with expected values. - * @param actuals - * float array with actual values - */ - public static void assertArrayEquals(float[] expecteds, float[] actuals, float delta) { - assertArrayEquals(null, expecteds, actuals, delta); - } - - /** - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If - * expecteds and actuals are null, - * they are considered equal. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * Object array or array of arrays (multi-dimensional array) with - * expected values. - * @param actuals - * Object array or array of arrays (multi-dimensional array) with - * actual values - */ - private static void internalArrayEquals(String message, Object expecteds, - Object actuals) throws ArrayComparisonFailure { - new ExactComparisonCriteria().arrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two doubles or floats are equal to within a positive delta. - * If they are not, an {@link AssertionError} is thrown with the given - * message. If the expected value is infinity then the delta value is - * ignored. NaNs are considered equal: - * assertEquals(Double.NaN, Double.NaN, *) passes - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expected - * expected value - * @param actual - * the value to check against expected - * @param delta - * the maximum delta between expected and - * actual for which both numbers are still - * considered equal. - */ - static public void assertEquals(String message, double expected, - double actual, double delta) { - if (Double.compare(expected, actual) == 0) - return; - if (!(Math.abs(expected - actual) <= delta)) - failNotEquals(message, new Double(expected), new Double(actual)); - } - - /** - * Asserts that two longs are equal. If they are not, an - * {@link AssertionError} is thrown. - * - * @param expected - * expected long value. - * @param actual - * actual long value - */ - static public void assertEquals(long expected, long actual) { - assertEquals(null, expected, actual); - } - - /** - * Asserts that two longs are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expected - * long expected value. - * @param actual - * long actual value - */ - static public void assertEquals(String message, long expected, long actual) { - assertEquals(message, (Long) expected, (Long) actual); - } - - /** - * @deprecated Use - * assertEquals(double expected, double actual, double delta) - * instead - */ - @Deprecated - static public void assertEquals(double expected, double actual) { - assertEquals(null, expected, actual); - } - - /** - * @deprecated Use - * assertEquals(String message, double expected, double actual, double delta) - * instead - */ - @Deprecated - static public void assertEquals(String message, double expected, - double actual) { - fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers"); - } - - /** - * Asserts that two doubles or floats are equal to within a positive delta. - * If they are not, an {@link AssertionError} is thrown. If the expected - * value is infinity then the delta value is ignored.NaNs are considered - * equal: assertEquals(Double.NaN, Double.NaN, *) passes - * - * @param expected - * expected value - * @param actual - * the value to check against expected - * @param delta - * the maximum delta between expected and - * actual for which both numbers are still - * considered equal. - */ - static public void assertEquals(double expected, double actual, double delta) { - assertEquals(null, expected, actual, delta); - } - - /** - * Asserts that an object isn't null. If it is an {@link AssertionError} is - * thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param object - * Object to check or null - */ - static public void assertNotNull(String message, Object object) { - assertTrue(message, object != null); - } - - /** - * Asserts that an object isn't null. If it is an {@link AssertionError} is - * thrown. - * - * @param object - * Object to check or null - */ - static public void assertNotNull(Object object) { - assertNotNull(null, object); - } - - /** - * Asserts that an object is null. If it is not, an {@link AssertionError} - * is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param object - * Object to check or null - */ - static public void assertNull(String message, Object object) { - assertTrue(message, object == null); - } - - /** - * Asserts that an object is null. If it isn't an {@link AssertionError} is - * thrown. - * - * @param object - * Object to check or null - */ - static public void assertNull(Object object) { - assertNull(null, object); - } - - /** - * Asserts that two objects refer to the same object. If they are not, an - * {@link AssertionError} is thrown with the given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expected - * the expected object - * @param actual - * the object to compare to expected - */ - static public void assertSame(String message, Object expected, Object actual) { - if (expected == actual) - return; - failNotSame(message, expected, actual); - } - - /** - * Asserts that two objects refer to the same object. If they are not the - * same, an {@link AssertionError} without a message is thrown. - * - * @param expected - * the expected object - * @param actual - * the object to compare to expected - */ - static public void assertSame(Object expected, Object actual) { - assertSame(null, expected, actual); - } - - /** - * Asserts that two objects do not refer to the same object. If they do - * refer to the same object, an {@link AssertionError} is thrown with the - * given message. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param unexpected - * the object you don't expect - * @param actual - * the object to compare to unexpected - */ - static public void assertNotSame(String message, Object unexpected, - Object actual) { - if (unexpected == actual) - failSame(message); - } - - /** - * Asserts that two objects do not refer to the same object. If they do - * refer to the same object, an {@link AssertionError} without a message is - * thrown. - * - * @param unexpected - * the object you don't expect - * @param actual - * the object to compare to unexpected - */ - static public void assertNotSame(Object unexpected, Object actual) { - assertNotSame(null, unexpected, actual); - } - - static private void failSame(String message) { - String formatted= ""; - if (message != null) - formatted= message + " "; - fail(formatted + "expected not same"); - } - - static private void failNotSame(String message, Object expected, - Object actual) { - String formatted= ""; - if (message != null) - formatted= message + " "; - fail(formatted + "expected same:<" + expected + "> was not:<" + actual - + ">"); - } - - 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 && !message.equals("")) - formatted= message + " "; - String expectedString= String.valueOf(expected); - String actualString= String.valueOf(actual); - if (expectedString.equals(actualString)) - return formatted + "expected: " - + formatClassAndValue(expected, expectedString) - + " but was: " + formatClassAndValue(actual, actualString); - else - return formatted + "expected:<" + expectedString + "> but was:<" - + actualString + ">"; - } - - private static String formatClassAndValue(Object value, String valueString) { - String className= value == null ? "null" : value.getClass().getName(); - return className + "<" + valueString + ">"; - } - - /** - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown with the given message. If - * expecteds and actuals are null, - * they are considered equal. - * - * @param message - * the identifying message for the {@link AssertionError} (null - * okay) - * @param expecteds - * Object array or array of arrays (multi-dimensional array) with - * expected values. - * @param actuals - * Object array or array of arrays (multi-dimensional array) with - * actual values - * @deprecated use assertArrayEquals - */ - @Deprecated - public static void assertEquals(String message, Object[] expecteds, - Object[] actuals) { - assertArrayEquals(message, expecteds, actuals); - } - - /** - * Asserts that two object arrays are equal. If they are not, an - * {@link AssertionError} is thrown. If expected and - * actual are null, they are considered - * equal. - * - * @param expecteds - * Object array or array of arrays (multi-dimensional array) with - * expected values - * @param actuals - * Object array or array of arrays (multi-dimensional array) with - * actual values - * @deprecated use assertArrayEquals - */ - @Deprecated - public static void assertEquals(Object[] expecteds, Object[] actuals) { - assertArrayEquals(expecteds, actuals); - } - - /** - * Asserts that actual satisfies the condition specified by - * matcher. If not, an {@link AssertionError} is thrown with - * information about the matcher and failing value. Example: - * - *
-	 *   assertThat(0, is(1)); // fails:
-	 *     // failure message:
-	 *     // expected: is <1> 
-	 *     // got value: <0>
-	 *   assertThat(0, is(not(1))) // passes
-	 * 
- * - * @param - * the static type accepted by the matcher (this can flag obvious - * compile-time problems such as {@code assertThat(1, is("a"))} - * @param actual - * the computed value being compared - * @param matcher - * an expression, built of {@link Matcher}s, specifying allowed - * values - * - * @see org.hamcrest.CoreMatchers - * @see org.junit.matchers.JUnitMatchers - */ - public static void assertThat(T actual, Matcher matcher) { - assertThat("", actual, matcher); - } - - /** - * Asserts that actual satisfies the condition specified by - * matcher. If not, an {@link AssertionError} is thrown with - * the reason and information about the matcher and failing value. Example: - * - *
-	 * :
-	 *   assertThat("Help! Integers don't work", 0, is(1)); // fails:
-	 *     // failure message:
-	 *     // Help! Integers don't work
-	 *     // expected: is <1> 
-	 *     // got value: <0>
-	 *   assertThat("Zero is one", 0, is(not(1))) // passes
-	 * 
- * - * @param reason - * additional information about the error - * @param - * the static type accepted by the matcher (this can flag obvious - * compile-time problems such as {@code assertThat(1, is("a"))} - * @param actual - * the computed value being compared - * @param matcher - * an expression, built of {@link Matcher}s, specifying allowed - * values - * - * @see org.hamcrest.CoreMatchers - * @see org.junit.matchers.JUnitMatchers - */ - public static void assertThat(String reason, T actual, - Matcher matcher) { - if (!matcher.matches(actual)) { - Description description= new StringDescription(); - description.appendText(reason); - description.appendText("\nExpected: "); - description.appendDescriptionOf(matcher); - description.appendText("\n got: "); - description.appendValue(actual); - description.appendText("\n"); - throw new java.lang.AssertionError(description.toString()); - } - } + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + + /** + * Asserts that a condition is true. If it isn't it throws an + * {@link AssertionError} with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param condition condition to be checked + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) { + fail(message); + } + } + + /** + * Asserts that a condition is true. If it isn't it throws an + * {@link AssertionError} without a message. + * + * @param condition condition to be checked + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + + /** + * Asserts that a condition is false. If it isn't it throws an + * {@link AssertionError} with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param condition condition to be checked + */ + static public void assertFalse(String message, boolean condition) { + assertTrue(message, !condition); + } + + /** + * Asserts that a condition is false. If it isn't it throws an + * {@link AssertionError} without a message. + * + * @param condition condition to be checked + */ + static public void assertFalse(boolean condition) { + assertFalse(null, condition); + } + + /** + * Fails a test with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @see AssertionError + */ + static public void fail(String message) { + if (message == null) { + throw new AssertionError(); + } + throw new AssertionError(message); + } + + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + + /** + * Asserts that two objects are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. If + * expected and actual are null, + * they are considered equal. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expected expected value + * @param actual actual value + */ + static public void assertEquals(String message, Object expected, + Object actual) { + if (equalsRegardingNull(expected, actual)) { + return; + } else if (expected instanceof String && actual instanceof String) { + String cleanMessage = message == null ? "" : message; + throw new ComparisonFailure(cleanMessage, (String) expected, + (String) actual); + } else { + failNotEquals(message, expected, actual); + } + } + + private static boolean equalsRegardingNull(Object expected, Object actual) { + if (expected == null) { + return actual == null; + } + + return isEquals(expected, actual); + } + + private static boolean isEquals(Object expected, Object actual) { + return expected.equals(actual); + } + + /** + * Asserts that two objects are equal. If they are not, an + * {@link AssertionError} without a message is thrown. If + * expected and actual are null, + * they are considered equal. + * + * @param expected expected value + * @param actual the value to check against expected + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + + /** + * Asserts that two objects are not equals. If they are, an + * {@link AssertionError} is thrown with the given message. If + * unexpected and actual are null, + * they are considered equal. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param unexpected unexpected value to check + * @param actual the value to check against unexpected + */ + static public void assertNotEquals(String message, Object unexpected, + Object actual) { + if (equalsRegardingNull(unexpected, actual)) { + failEquals(message, actual); + } + } + + /** + * Asserts that two objects are not equals. If they are, an + * {@link AssertionError} without a message is thrown. If + * unexpected and actual are null, + * they are considered equal. + * + * @param unexpected unexpected value to check + * @param actual the value to check against unexpected + */ + static public void assertNotEquals(Object unexpected, Object actual) { + assertNotEquals(null, unexpected, actual); + } + + private static void failEquals(String message, Object actual) { + String formatted = "Values should be different. "; + if (message != null) { + formatted = message + ". "; + } + + formatted += "Actual: " + actual; + fail(formatted); + } + + /** + * Asserts that two longs are not equals. If they are, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param unexpected unexpected value to check + * @param actual the value to check against unexpected + */ + static public void assertNotEquals(String message, long unexpected, long actual) { + if (unexpected == actual) { + failEquals(message, Long.valueOf(actual)); + } + } + + /** + * Asserts that two longs are not equals. If they are, an + * {@link AssertionError} without a message is thrown. + * + * @param unexpected unexpected value to check + * @param actual the value to check against unexpected + */ + static public void assertNotEquals(long unexpected, long actual) { + assertNotEquals(null, unexpected, actual); + } + + /** + * Asserts that two doubles are not equal to within a positive delta. + * If they are, an {@link AssertionError} is thrown with the given + * message. If the unexpected value is infinity then the delta value is + * ignored. NaNs are considered equal: + * assertNotEquals(Double.NaN, Double.NaN, *) fails + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param unexpected unexpected value + * @param actual the value to check against unexpected + * @param delta the maximum delta between unexpected and + * actual for which both numbers are still + * considered equal. + */ + static public void assertNotEquals(String message, double unexpected, + double actual, double delta) { + if (!doubleIsDifferent(unexpected, actual, delta)) { + failEquals(message, Double.valueOf(actual)); + } + } + + /** + * Asserts that two doubles are not equal to within a positive delta. + * If they are, an {@link AssertionError} is thrown. If the unexpected + * value is infinity then the delta value is ignored.NaNs are considered + * equal: assertNotEquals(Double.NaN, Double.NaN, *) fails + * + * @param unexpected unexpected value + * @param actual the value to check against unexpected + * @param delta the maximum delta between unexpected and + * actual for which both numbers are still + * considered equal. + */ + static public void assertNotEquals(double unexpected, double actual, double delta) { + assertNotEquals(null, unexpected, actual, delta); + } + + /** + * Asserts that two floats are not equal to within a positive delta. + * If they are, an {@link AssertionError} is thrown. If the unexpected + * value is infinity then the delta value is ignored.NaNs are considered + * equal: assertNotEquals(Float.NaN, Float.NaN, *) fails + * + * @param unexpected unexpected value + * @param actual the value to check against unexpected + * @param delta the maximum delta between unexpected and + * actual for which both numbers are still + * considered equal. + */ + static public void assertNotEquals(float unexpected, float actual, float delta) { + assertNotEquals(null, unexpected, actual, delta); + } + + /** + * Asserts that two object arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. If + * expecteds and actuals are null, + * they are considered equal. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds Object array or array of arrays (multi-dimensional array) with + * expected values. + * @param actuals Object array or array of arrays (multi-dimensional array) with + * actual values + */ + public static void assertArrayEquals(String message, Object[] expecteds, + Object[] actuals) throws ArrayComparisonFailure { + internalArrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two object arrays are equal. If they are not, an + * {@link AssertionError} is thrown. If expected and + * actual are null, they are considered + * equal. + * + * @param expecteds Object array or array of arrays (multi-dimensional array) with + * expected values + * @param actuals Object array or array of arrays (multi-dimensional array) with + * actual values + */ + 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 + * expecteds and actuals are null, + * they are considered equal. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds boolean array with expected values. + * @param actuals boolean array with expected values. + */ + 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 expected and + * actual are null, they are considered + * equal. + * + * @param expecteds boolean array with expected values. + * @param actuals boolean array with expected values. + */ + public static void assertArrayEquals(boolean[] expecteds, boolean[] actuals) { + assertArrayEquals(null, expecteds, actuals); + } + + /** + * Asserts that two byte arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds byte array with expected values. + * @param actuals byte array with actual values + */ + public static void assertArrayEquals(String message, byte[] expecteds, + byte[] actuals) throws ArrayComparisonFailure { + internalArrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two byte arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expecteds byte array with expected values. + * @param actuals byte array with actual values + */ + public static void assertArrayEquals(byte[] expecteds, byte[] actuals) { + assertArrayEquals(null, expecteds, actuals); + } + + /** + * Asserts that two char arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds char array with expected values. + * @param actuals char array with actual values + */ + public static void assertArrayEquals(String message, char[] expecteds, + char[] actuals) throws ArrayComparisonFailure { + internalArrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two char arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expecteds char array with expected values. + * @param actuals char array with actual values + */ + public static void assertArrayEquals(char[] expecteds, char[] actuals) { + assertArrayEquals(null, expecteds, actuals); + } + + /** + * Asserts that two short arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds short array with expected values. + * @param actuals short array with actual values + */ + public static void assertArrayEquals(String message, short[] expecteds, + short[] actuals) throws ArrayComparisonFailure { + internalArrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two short arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expecteds short array with expected values. + * @param actuals short array with actual values + */ + public static void assertArrayEquals(short[] expecteds, short[] actuals) { + assertArrayEquals(null, expecteds, actuals); + } + + /** + * Asserts that two int arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds int array with expected values. + * @param actuals int array with actual values + */ + public static void assertArrayEquals(String message, int[] expecteds, + int[] actuals) throws ArrayComparisonFailure { + internalArrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two int arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expecteds int array with expected values. + * @param actuals int array with actual values + */ + public static void assertArrayEquals(int[] expecteds, int[] actuals) { + assertArrayEquals(null, expecteds, actuals); + } + + /** + * Asserts that two long arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds long array with expected values. + * @param actuals long array with actual values + */ + public static void assertArrayEquals(String message, long[] expecteds, + long[] actuals) throws ArrayComparisonFailure { + internalArrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two long arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expecteds long array with expected values. + * @param actuals long array with actual values + */ + public static void assertArrayEquals(long[] expecteds, long[] actuals) { + assertArrayEquals(null, expecteds, actuals); + } + + /** + * Asserts that two double arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds double array with expected values. + * @param actuals double array with actual values + * @param delta the maximum delta between expecteds[i] and + * actuals[i] for which both numbers are still + * considered equal. + */ + public static void assertArrayEquals(String message, double[] expecteds, + double[] actuals, double delta) throws ArrayComparisonFailure { + new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two double arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expecteds double array with expected values. + * @param actuals double array with actual values + * @param delta the maximum delta between expecteds[i] and + * actuals[i] for which both numbers are still + * considered equal. + */ + public static void assertArrayEquals(double[] expecteds, double[] actuals, double delta) { + assertArrayEquals(null, expecteds, actuals, delta); + } + + /** + * Asserts that two float arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds float array with expected values. + * @param actuals float array with actual values + * @param delta the maximum delta between expecteds[i] and + * actuals[i] for which both numbers are still + * considered equal. + */ + public static void assertArrayEquals(String message, float[] expecteds, + float[] actuals, float delta) throws ArrayComparisonFailure { + new InexactComparisonCriteria(delta).arrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two float arrays are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expecteds float array with expected values. + * @param actuals float array with actual values + * @param delta the maximum delta between expecteds[i] and + * actuals[i] for which both numbers are still + * considered equal. + */ + public static void assertArrayEquals(float[] expecteds, float[] actuals, float delta) { + assertArrayEquals(null, expecteds, actuals, delta); + } + + /** + * Asserts that two object arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. If + * expecteds and actuals are null, + * they are considered equal. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds Object array or array of arrays (multi-dimensional array) with + * expected values. + * @param actuals Object array or array of arrays (multi-dimensional array) with + * actual values + */ + private static void internalArrayEquals(String message, Object expecteds, + Object actuals) throws ArrayComparisonFailure { + new ExactComparisonCriteria().arrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two doubles are equal to within a positive delta. + * If they are not, an {@link AssertionError} is thrown with the given + * message. If the expected value is infinity then the delta value is + * ignored. NaNs are considered equal: + * assertEquals(Double.NaN, Double.NaN, *) passes + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expected expected value + * @param actual the value to check against expected + * @param delta the maximum delta between expected and + * actual for which both numbers are still + * considered equal. + */ + 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)); + } + } + + /** + * Asserts that two floats are equal to within a positive delta. + * If they are not, an {@link AssertionError} is thrown with the given + * message. If the expected value is infinity then the delta value is + * ignored. NaNs are considered equal: + * assertEquals(Float.NaN, Float.NaN, *) passes + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expected expected value + * @param actual the value to check against expected + * @param delta the maximum delta between expected and + * actual for which both numbers are still + * considered equal. + */ + 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)); + } + } + + /** + * Asserts that two floats are not equal to within a positive delta. + * If they are, an {@link AssertionError} is thrown with the given + * message. If the unexpected value is infinity then the delta value is + * ignored. NaNs are considered equal: + * assertNotEquals(Float.NaN, Float.NaN, *) fails + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param unexpected unexpected value + * @param actual the value to check against unexpected + * @param delta the maximum delta between unexpected and + * actual for which both numbers are still + * considered equal. + */ + static public 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) { + if (Double.compare(d1, d2) == 0) { + return false; + } + if ((Math.abs(d1 - d2) <= delta)) { + return false; + } + + return true; + } + + static private boolean floatIsDifferent(float f1, float f2, float delta) { + if (Float.compare(f1, f2) == 0) { + return false; + } + if ((Math.abs(f1 - f2) <= delta)) { + return false; + } + + return true; + } + + /** + * Asserts that two longs are equal. If they are not, an + * {@link AssertionError} is thrown. + * + * @param expected expected long value. + * @param actual actual long value + */ + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + + /** + * Asserts that two longs are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expected long expected value. + * @param actual long actual value + */ + static public void assertEquals(String message, long expected, long actual) { + if (expected != actual) { + failNotEquals(message, Long.valueOf(expected), Long.valueOf(actual)); + } + } + + /** + * @deprecated Use + * assertEquals(double expected, double actual, double delta) + * instead + */ + @Deprecated + static public void assertEquals(double expected, double actual) { + assertEquals(null, expected, actual); + } + + /** + * @deprecated Use + * assertEquals(String message, double expected, double actual, double delta) + * instead + */ + @Deprecated + static public void assertEquals(String message, double expected, + double actual) { + fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers"); + } + + /** + * Asserts that two doubles are equal to within a positive delta. + * If they are not, an {@link AssertionError} is thrown. If the expected + * value is infinity then the delta value is ignored.NaNs are considered + * equal: assertEquals(Double.NaN, Double.NaN, *) passes + * + * @param expected expected value + * @param actual the value to check against expected + * @param delta the maximum delta between expected and + * actual for which both numbers are still + * considered equal. + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + + /** + * Asserts that two floats are equal to within a positive delta. + * If they are not, an {@link AssertionError} is thrown. If the expected + * value is infinity then the delta value is ignored. NaNs are considered + * equal: assertEquals(Float.NaN, Float.NaN, *) passes + * + * @param expected expected value + * @param actual the value to check against expected + * @param delta the maximum delta between expected and + * actual for which both numbers are still + * considered equal. + */ + + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + + /** + * Asserts that an object isn't null. If it is an {@link AssertionError} is + * thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param object Object to check or null + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + + /** + * Asserts that an object isn't null. If it is an {@link AssertionError} is + * thrown. + * + * @param object Object to check or null + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + + /** + * Asserts that an object is null. If it is not, an {@link AssertionError} + * is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param object Object to check or null + */ + static public void assertNull(String message, Object object) { + if (object == null) { + return; + } + failNotNull(message, object); + } + + /** + * Asserts that an object is null. If it isn't an {@link AssertionError} is + * thrown. + * + * @param object Object to check or null + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + + static private void failNotNull(String message, Object actual) { + String formatted = ""; + if (message != null) { + formatted = message + " "; + } + fail(formatted + "expected null, but was:<" + actual + ">"); + } + + /** + * Asserts that two objects refer to the same object. If they are not, an + * {@link AssertionError} is thrown with the given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expected the expected object + * @param actual the object to compare to expected + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) { + return; + } + failNotSame(message, expected, actual); + } + + /** + * Asserts that two objects refer to the same object. If they are not the + * same, an {@link AssertionError} without a message is thrown. + * + * @param expected the expected object + * @param actual the object to compare to expected + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + /** + * Asserts that two objects do not refer to the same object. If they do + * refer to the same object, an {@link AssertionError} is thrown with the + * given message. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param unexpected the object you don't expect + * @param actual the object to compare to unexpected + */ + static public void assertNotSame(String message, Object unexpected, + Object actual) { + if (unexpected == actual) { + failSame(message); + } + } + + /** + * Asserts that two objects do not refer to the same object. If they do + * refer to the same object, an {@link AssertionError} without a message is + * thrown. + * + * @param unexpected the object you don't expect + * @param actual the object to compare to unexpected + */ + static public void assertNotSame(Object unexpected, Object actual) { + assertNotSame(null, unexpected, actual); + } + + static private void failSame(String message) { + String formatted = ""; + if (message != null) { + formatted = message + " "; + } + fail(formatted + "expected not same"); + } + + static private void failNotSame(String message, Object expected, + Object actual) { + String formatted = ""; + if (message != null) { + formatted = message + " "; + } + fail(formatted + "expected same:<" + expected + "> was not:<" + actual + + ">"); + } + + 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 && !message.equals("")) { + formatted = message + " "; + } + String expectedString = String.valueOf(expected); + String actualString = String.valueOf(actual); + if (expectedString.equals(actualString)) { + return formatted + "expected: " + + formatClassAndValue(expected, expectedString) + + " but was: " + formatClassAndValue(actual, actualString); + } else { + return formatted + "expected:<" + expectedString + "> but was:<" + + actualString + ">"; + } + } + + private static String formatClassAndValue(Object value, String valueString) { + String className = value == null ? "null" : value.getClass().getName(); + return className + "<" + valueString + ">"; + } + + /** + * Asserts that two object arrays are equal. If they are not, an + * {@link AssertionError} is thrown with the given message. If + * expecteds and actuals are null, + * they are considered equal. + * + * @param message the identifying message for the {@link AssertionError} (null + * okay) + * @param expecteds Object array or array of arrays (multi-dimensional array) with + * expected values. + * @param actuals Object array or array of arrays (multi-dimensional array) with + * actual values + * @deprecated use assertArrayEquals + */ + @Deprecated + public static void assertEquals(String message, Object[] expecteds, + Object[] actuals) { + assertArrayEquals(message, expecteds, actuals); + } + + /** + * Asserts that two object arrays are equal. If they are not, an + * {@link AssertionError} is thrown. If expected and + * actual are null, they are considered + * equal. + * + * @param expecteds Object array or array of arrays (multi-dimensional array) with + * expected values + * @param actuals Object array or array of arrays (multi-dimensional array) with + * actual values + * @deprecated use assertArrayEquals + */ + @Deprecated + public static void assertEquals(Object[] expecteds, Object[] actuals) { + assertArrayEquals(expecteds, actuals); + } + + /** + * Asserts that actual satisfies the condition specified by + * matcher. If not, an {@link AssertionError} is thrown with + * information about the matcher and failing value. Example: + * + *
+     *   assertThat(0, is(1)); // fails:
+     *     // failure message:
+     *     // expected: is <1>
+     *     // got value: <0>
+     *   assertThat(0, is(not(1))) // passes
+     * 
+ * + * org.hamcrest.Matcher does not currently document the meaning + * of its type parameter T. This method assumes that a matcher + * typed as Matcher<T> can be meaningfully applied only + * to values that could be assigned to a variable of type T. + * + * @param the static type accepted by the matcher (this can flag obvious + * compile-time problems such as {@code assertThat(1, is("a"))} + * @param actual the computed value being compared + * @param matcher an expression, built of {@link Matcher}s, specifying allowed + * values + * @see org.hamcrest.CoreMatchers + * @see org.hamcrest.MatcherAssert + */ + public static void assertThat(T actual, Matcher matcher) { + assertThat("", actual, matcher); + } + + /** + * Asserts that actual satisfies the condition specified by + * matcher. If not, an {@link AssertionError} is thrown with + * the reason and information about the matcher and failing value. Example: + * + *
+     *   assertThat("Help! Integers don't work", 0, is(1)); // fails:
+     *     // failure message:
+     *     // Help! Integers don't work
+     *     // expected: is <1>
+     *     // got value: <0>
+     *   assertThat("Zero is one", 0, is(not(1))) // passes
+     * 
+ * + * org.hamcrest.Matcher does not currently document the meaning + * of its type parameter T. This method assumes that a matcher + * typed as Matcher<T> can be meaningfully applied only + * to values that could be assigned to a variable of type T. + * + * @param reason additional information about the error + * @param the static type accepted by the matcher (this can flag obvious + * compile-time problems such as {@code assertThat(1, is("a"))} + * @param actual the computed value being compared + * @param matcher an expression, built of {@link Matcher}s, specifying allowed + * values + * @see org.hamcrest.CoreMatchers + * @see org.hamcrest.MatcherAssert + */ + public static void assertThat(String reason, T actual, + Matcher matcher) { + MatcherAssert.assertThat(reason, actual, matcher); + } } diff --git a/src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java b/src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java new file mode 100644 index 0000000..23f921b --- /dev/null +++ b/src/main/java/org/junit/internal/runners/rules/RuleMemberValidator.java @@ -0,0 +1,91 @@ +package org.junit.internal.runners.rules; + +import java.lang.annotation.Annotation; +import java.util.List; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.rules.TestRule; +import org.junit.runners.model.FrameworkField; +import org.junit.runners.model.TestClass; + +/** + * A RuleFieldValidator validates the rule fields of a + * {@link TestClass}. All reasons for rejecting the + * {@code TestClass} are written to a list of errors. + * + * There are two slightly different validators. The {@link #CLASS_RULE_VALIDATOR} + * validates fields with a {@link ClassRule} annotation and the + * {@link #RULE_VALIDATOR} validates fields with a {@link Rule} annotation. + */ +public enum RuleMemberValidator { + /** + * Validates fields with a {@link ClassRule} annotation. + */ + CLASS_RULE_VALIDATOR(ClassRule.class, true), + /** + * Validates fields with a {@link Rule} annotation. + */ + RULE_VALIDATOR(Rule.class, false); + + private final Class fAnnotation; + + private final boolean fOnlyStaticFields; + + private RuleMemberValidator(Class annotation, + boolean onlyStaticFields) { + this.fAnnotation= annotation; + this.fOnlyStaticFields= onlyStaticFields; + } + + /** + * Validate the {@link TestClass} and adds reasons + * for rejecting the class to a list of errors. + * @param target the {@code TestClass} to validate. + * @param errors the list of errors. + */ + public void validate(TestClass target, List errors) { + List fields= target.getAnnotatedFields(fAnnotation); + for (FrameworkField each : fields) + validateField(each, errors); + } + + private void validateField(FrameworkField field, List errors) { + optionallyValidateStatic(field, errors); + validatePublic(field, errors); + validateTestRuleOrMethodRule(field, errors); + } + + private void optionallyValidateStatic(FrameworkField field, + List errors) { + if (fOnlyStaticFields && !field.isStatic()) + addError(errors, field, "must be static."); + } + + private void validatePublic(FrameworkField field, List errors) { + if (!field.isPublic()) + addError(errors, field, "must be public."); + } + + private void validateTestRuleOrMethodRule(FrameworkField field, + List errors) { + if (!isMethodRule(field) && !isTestRule(field)) + addError(errors, field, "must implement MethodRule or TestRule."); + } + + private boolean isTestRule(FrameworkField target) { + return TestRule.class.isAssignableFrom(target.getType()); + } + + @SuppressWarnings("deprecation") + private boolean isMethodRule(FrameworkField target) { + return org.junit.rules.MethodRule.class.isAssignableFrom(target + .getType()); + } + + private void addError(List errors, FrameworkField field, + String suffix) { + String message= "The @" + fAnnotation.getSimpleName() + " '" + + field.getName() + "' " + suffix; + errors.add(new Exception(message)); + } +} diff --git a/src/main/java/org/junit/runner/Description.java b/src/main/java/org/junit/runner/Description.java index b3083d5..fe47eac 100644 --- a/src/main/java/org/junit/runner/Description.java +++ b/src/main/java/org/junit/runner/Description.java @@ -5,238 +5,312 @@ import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - *

A Description describes a test which is to be run or has been run. Descriptions + * A Description describes a test which is to be run or has been run. Descriptions * can be atomic (a single test) or compound (containing children tests). Descriptions are used * to provide feedback about the tests that are about to run (for example, the tree view - * visible in many IDEs) or tests that have been run (for example, the failures view).

- * - *

Descriptions are implemented as a single class rather than a Composite because - * they are entirely informational. They contain no logic aside from counting their tests.

- * - *

In the past, we used the raw {@link junit.framework.TestCase}s and {@link junit.framework.TestSuite}s - * to display the tree of tests. This was no longer viable in JUnit 4 because atomic tests no longer have - * a superclass below {@link Object}. We needed a way to pass a class and name together. Description - * emerged from this.

- * + * visible in many IDEs) or tests that have been run (for example, the failures view). + *

+ * Descriptions are implemented as a single class rather than a Composite because + * they are entirely informational. They contain no logic aside from counting their tests. + *

+ * In the past, we used the raw {@link junit.framework.TestCase}s and {@link junit.framework.TestSuite}s + * to display the tree of tests. This was no longer viable in JUnit 4 because atomic tests no longer have + * a superclass below {@link Object}. We needed a way to pass a class and name together. Description + * emerged from this. + * * @see org.junit.runner.Request * @see org.junit.runner.Runner + * @since 4.0 */ public class Description implements Serializable { - private static final long serialVersionUID = 1L; - - /** - * Create a Description named name. - * Generally, you will add children to this Description. - * @param name the name of the Description - * @param annotations - * @return a Description named name - */ - public static Description createSuiteDescription(String name, Annotation... annotations) { - if (name.length() == 0) - throw new IllegalArgumentException("name must have non-zero length"); - return new Description(name, annotations); - } - - /** - * Create a Description of a single test named name in the class clazz. - * Generally, this will be a leaf Description. - * @param clazz the class of the test - * @param name the name of the test (a method name for test annotated with {@link org.junit.Test}) - * @param annotations meta-data about the test, for downstream interpreters - * @return a Description named name - */ - public static Description createTestDescription(Class clazz, String name, Annotation... annotations) { - return new Description(String.format("%s(%s)", name, clazz.getName()), annotations); - } - - /** - * Create a Description of a single test named name in the class clazz. - * Generally, this will be a leaf Description. - * (This remains for binary compatibility with clients of JUnit 4.3) - * @param clazz the class of the test - * @param name the name of the test (a method name for test annotated with {@link org.junit.Test}) - * @return a Description named name - */ - public static Description createTestDescription(Class clazz, String name) { - return createTestDescription(clazz, name, new Annotation[0]); - } - - /** - * Create a Description named after testClass - * @param testClass A {@link Class} containing tests - * @return a Description of testClass - */ - public static Description createSuiteDescription(Class testClass) { - return new Description(testClass.getName(), testClass.getAnnotations()); - } - - /** - * Describes a Runner which runs no tests - */ - public static final Description EMPTY= new Description("No Tests"); - - /** - * Describes a step in the test-running mechanism that goes so wrong no - * other description can be used (for example, an exception thrown from a Runner's - * constructor - */ - public static final Description TEST_MECHANISM= new Description("Test mechanism"); - - private final ArrayList fChildren= new ArrayList(); - private final String fDisplayName; - - private final Annotation[] fAnnotations; - - private Description(final String displayName, Annotation... annotations) { - fDisplayName= displayName; - fAnnotations= annotations; - } - - /** - * @return a user-understandable label - */ - public String getDisplayName() { - return fDisplayName; - } - - /** - * Add Description as a child of the receiver. - * @param description the soon-to-be child. - */ - public void addChild(Description description) { - getChildren().add(description); - } - - /** - * @return the receiver's children, if any - */ - public ArrayList getChildren() { - return fChildren; - } - - /** - * @return true if the receiver is a suite - */ - public boolean isSuite() { - return !isTest(); - } - - /** - * @return true if the receiver is an atomic test - */ - public boolean isTest() { - return getChildren().isEmpty(); - } - - /** - * @return the total number of atomic tests in the receiver - */ - public int testCount() { - if (isTest()) - return 1; - int result= 0; - for (Description child : getChildren()) - result+= child.testCount(); - return result; - } - - @Override - public int hashCode() { - return getDisplayName().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Description)) - return false; - Description d = (Description) obj; - return getDisplayName().equals(d.getDisplayName()); - } - - @Override - public String toString() { - return getDisplayName(); - } - - /** - * @return true if this is a description of a Runner that runs no tests - */ - public boolean isEmpty() { - return equals(EMPTY); - } - - /** - * @return a copy of this description, with no children (on the assumption that some of the - * children will be added back) - */ - public Description childlessCopy() { - return new Description(fDisplayName, fAnnotations); - } - - /** - * @return the annotation of type annotationType that is attached to this description node, - * or null if none exists - */ - public T getAnnotation(Class annotationType) { - for (Annotation each : fAnnotations) - if (each.annotationType().equals(annotationType)) - return annotationType.cast(each); - return null; - } - - /** - * @return all of the annotations attached to this description node - */ - public Collection getAnnotations() { - return Arrays.asList(fAnnotations); - } - - /** - * @return If this describes a method invocation, - * the class of the test instance. - */ - public Class getTestClass() { - String name= getClassName(); - if (name == null) - return null; - try { - return Class.forName(name); - } catch (ClassNotFoundException e) { - return null; - } - } - - /** - * @return If this describes a method invocation, - * the name of the class of the test instance - */ - public String getClassName() { - Matcher matcher= methodStringMatcher(); - return matcher.matches() - ? matcher.group(2) - : toString(); - } - - /** - * @return If this describes a method invocation, - * the name of the method (or null if not) - */ - public String getMethodName() { - return parseMethod(); - } - - private String parseMethod() { - Matcher matcher= methodStringMatcher(); - if (matcher.matches()) - return matcher.group(1); - return null; - } - - private Matcher methodStringMatcher() { - return Pattern.compile("(.*)\\((.*)\\)").matcher(toString()); - } + private static final long serialVersionUID = 1L; + + private static final Pattern METHOD_AND_CLASS_NAME_PATTERN = Pattern + .compile("([\\s\\S]*)\\((.*)\\)"); + + /** + * Create a Description named name. + * Generally, you will add children to this Description. + * + * @param name the name of the Description + * @param annotations meta-data about the test, for downstream interpreters + * @return a Description named name + */ + public static Description createSuiteDescription(String name, Annotation... annotations) { + return new Description(null, name, annotations); + } + + /** + * Create a Description named name. + * Generally, you will add children to this Description. + * + * @param name the name of the Description + * @param uniqueId an arbitrary object used to define uniqueness (in {@link #equals(Object)} + * @param annotations meta-data about the test, for downstream interpreters + * @return a Description named name + */ + public static Description createSuiteDescription(String name, Serializable uniqueId, Annotation... annotations) { + return new Description(null, name, uniqueId, annotations); + } + + /** + * Create a Description of a single test named name in the 'class' named + * className. Generally, this will be a leaf Description. This method is a better choice + * than {@link #createTestDescription(Class, String, Annotation...)} for test runners whose test cases are not + * defined in an actual Java Class. + * + * @param className the class name of the test + * @param name the name of the test (a method name for test annotated with {@link org.junit.Test}) + * @param annotations meta-data about the test, for downstream interpreters + * @return a Description named name + */ + public static Description createTestDescription(String className, String name, Annotation... annotations) { + return new Description(null, formatDisplayName(name, className), annotations); + } + + /** + * Create a Description of a single test named name in the class clazz. + * Generally, this will be a leaf Description. + * + * @param clazz the class of the test + * @param name the name of the test (a method name for test annotated with {@link org.junit.Test}) + * @param annotations meta-data about the test, for downstream interpreters + * @return a Description named name + */ + public static Description createTestDescription(Class clazz, String name, Annotation... annotations) { + return new Description(clazz, formatDisplayName(name, clazz.getName()), annotations); + } + + /** + * Create a Description of a single test named name in the class clazz. + * Generally, this will be a leaf Description. + * (This remains for binary compatibility with clients of JUnit 4.3) + * + * @param clazz the class of the test + * @param name the name of the test (a method name for test annotated with {@link org.junit.Test}) + * @return a Description named name + */ + public static Description createTestDescription(Class clazz, String name) { + return new Description(clazz, formatDisplayName(name, clazz.getName())); + } + + /** + * Create a Description of a single test named name in the class clazz. + * Generally, this will be a leaf Description. + * + * @param name the name of the test (a method name for test annotated with {@link org.junit.Test}) + * @return a Description named name + */ + public static Description createTestDescription(String className, String name, Serializable uniqueId) { + return new Description(null, formatDisplayName(name, className), uniqueId); + } + + private static String formatDisplayName(String name, String className) { + return String.format("%s(%s)", name, className); + } + + /** + * Create a Description named after testClass + * + * @param testClass A {@link Class} containing tests + * @return a Description of testClass + */ + public static Description createSuiteDescription(Class testClass) { + return new Description(testClass, testClass.getName(), testClass.getAnnotations()); + } + + /** + * Describes a Runner which runs no tests + */ + public static final Description EMPTY = new Description(null, "No Tests"); + + /** + * Describes a step in the test-running mechanism that goes so wrong no + * other description can be used (for example, an exception thrown from a Runner's + * constructor + */ + public static final Description TEST_MECHANISM = new Description(null, "Test mechanism"); + + /* + * We have to use the f prefix until the next major release to ensure + * serialization compatibility. + * See https://github.com/junit-team/junit/issues/976 + */ + private final Collection fChildren = new ConcurrentLinkedQueue(); + private final String fDisplayName; + private final Serializable fUniqueId; + private final Annotation[] fAnnotations; + private volatile /* write-once */ Class fTestClass; + + private Description(Class clazz, String displayName, Annotation... annotations) { + this(clazz, displayName, displayName, annotations); + } + + private Description(Class testClass, String displayName, Serializable uniqueId, Annotation... annotations) { + if ((displayName == null) || (displayName.length() == 0)) { + throw new IllegalArgumentException( + "The display name must not be empty."); + } + if ((uniqueId == null)) { + throw new IllegalArgumentException( + "The unique id must not be null."); + } + this.fTestClass = testClass; + this.fDisplayName = displayName; + this.fUniqueId = uniqueId; + this.fAnnotations = annotations; + } + + /** + * @return a user-understandable label + */ + public String getDisplayName() { + return fDisplayName; + } + + /** + * Add Description as a child of the receiver. + * + * @param description the soon-to-be child. + */ + public void addChild(Description description) { + fChildren.add(description); + } + + /** + * Gets the copy of the children of this {@code Description}. + * Returns an empty list if there are no children. + */ + public ArrayList getChildren() { + return new ArrayList(fChildren); + } + + /** + * @return true if the receiver is a suite + */ + public boolean isSuite() { + return !isTest(); + } + + /** + * @return true if the receiver is an atomic test + */ + public boolean isTest() { + return fChildren.isEmpty(); + } + + /** + * @return the total number of atomic tests in the receiver + */ + public int testCount() { + if (isTest()) { + return 1; + } + int result = 0; + for (Description child : fChildren) { + result += child.testCount(); + } + return result; + } + + @Override + public int hashCode() { + return fUniqueId.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Description)) { + return false; + } + Description d = (Description) obj; + return fUniqueId.equals(d.fUniqueId); + } + + @Override + public String toString() { + return getDisplayName(); + } + + /** + * @return true if this is a description of a Runner that runs no tests + */ + public boolean isEmpty() { + return equals(EMPTY); + } + + /** + * @return a copy of this description, with no children (on the assumption that some of the + * children will be added back) + */ + public Description childlessCopy() { + return new Description(fTestClass, fDisplayName, fAnnotations); + } + + /** + * @return the annotation of type annotationType that is attached to this description node, + * or null if none exists + */ + public T getAnnotation(Class annotationType) { + for (Annotation each : fAnnotations) { + if (each.annotationType().equals(annotationType)) { + return annotationType.cast(each); + } + } + return null; + } + + /** + * @return all of the annotations attached to this description node + */ + public Collection getAnnotations() { + return Arrays.asList(fAnnotations); + } + + /** + * @return If this describes a method invocation, + * the class of the test instance. + */ + public Class getTestClass() { + if (fTestClass != null) { + return fTestClass; + } + String name = getClassName(); + if (name == null) { + return null; + } + try { + fTestClass = Class.forName(name, false, getClass().getClassLoader()); + return fTestClass; + } catch (ClassNotFoundException e) { + return null; + } + } + + /** + * @return If this describes a method invocation, + * the name of the class of the test instance + */ + public String getClassName() { + return fTestClass != null ? fTestClass.getName() : methodAndClassNamePatternGroupOrDefault(2, toString()); + } + + /** + * @return If this describes a method invocation, + * the name of the method (or null if not) + */ + public String getMethodName() { + return methodAndClassNamePatternGroupOrDefault(1, null); + } + + private String methodAndClassNamePatternGroupOrDefault(int group, + String defaultString) { + Matcher matcher = METHOD_AND_CLASS_NAME_PATTERN.matcher(toString()); + return matcher.matches() ? matcher.group(group) : defaultString; + } } \ No newline at end of file diff --git a/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java b/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java index 92e0d07..397da19 100644 --- a/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java +++ b/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java @@ -166,7 +166,7 @@ public class BlockJUnit4ClassRunner extends ParentRunner { errors.add(new Exception("No runnable methods")); } - private void validateFields(List errors) { + protected void validateFields(List errors) { RULE_VALIDATOR.validate(getTestClass(), errors); } -- cgit v1.2.3