From 2a0687c8905d9f9d1c8d4e1dee255a8ceabba5e5 Mon Sep 17 00:00:00 2001
From: David Srbecky
+ * The most illustrative use case is for tests that make use of the
+ * {@link Timeout} rule, when ran in debug mode the test may terminate on
+ * timeout abruptly during debugging. Developers may disable the timeout, or
+ * increase the timeout by making a code change on tests that need debugging and
+ * remember revert the change afterwards or rules such as {@link Timeout} that
+ * may be disabled during debugging may be wrapped in a {@code DisableOnDebug}.
+ *
+ * The important benefit of this feature is that you can disable such rules
+ * without any making any modifications to your test class to remove them during
+ * debugging.
+ *
+ * This does nothing to tackle timeouts or time sensitive code under test when
+ * debugging and may make this less useful in such circumstances.
+ *
+ * Example usage:
+ *
+ *
+ * Options specified in:
+ * To use it, add the following to a test class:
- *
* A final option is to do the same for a large array of test classes.
*
- * A good example of using assumptions is in Theories where they are needed to exclude certain datapoints that aren't suitable or allowed for a certain test case.
+ * A good example of using assumptions is in Theories where they are needed to exclude certain datapoints that aren't suitable or allowed for a certain test case.
*
+ * public static class DisableTimeoutOnDebugSampleTest {
+ *
+ * @Rule
+ * public TestRule timeout = new DisableOnDebug(new Timeout(20));
+ *
+ * @Test
+ * public void myTest() {
+ * int i = 0;
+ * assertEquals(0, i); // suppose you had a break point here to inspect i
+ * }
+ * }
+ *
+ *
+ * @since 4.12
+ */
+public class DisableOnDebug implements TestRule {
+ private final TestRule rule;
+ private final boolean debugging;
+
+ /**
+ * Create a {@code DisableOnDebug} instance with the timeout specified in
+ * milliseconds.
+ *
+ * @param rule to disable during debugging
+ */
+ public DisableOnDebug(TestRule rule) {
+ this(rule, ManagementFactory.getRuntimeMXBean()
+ .getInputArguments());
+ }
+
+ /**
+ * Visible for testing purposes only.
+ *
+ * @param rule the rule to disable during debugging
+ * @param inputArguments
+ * arguments provided to the Java runtime
+ */
+ DisableOnDebug(TestRule rule, List
+ *
null
*/
- public static void assertNull(Object object) {
+ static public void assertNull(Object object) {
if (object != null) {
assertNull("Expected:
- public static Test suite() {
- return new JUnit4TestAdapter(YourJUnit4TestClass.class);
- }
-
- */
-public class JUnit4TestAdapter implements Test, Filterable, Orderable, Describable {
+public class JUnit4TestAdapter implements Test, Filterable, Sortable, Describable {
private final Class> fNewTestClass;
private final Runner fRunner;
@@ -95,13 +83,4 @@ public class JUnit4TestAdapter implements Test, Filterable, Orderable, Describab
public void sort(Sorter sorter) {
sorter.apply(fRunner);
}
-
- /**
- * {@inheritDoc}
- *
- * @since 4.13
- */
- public void order(Orderer orderer) throws InvalidOrderingException {
- orderer.apply(fRunner);
- }
}
\ No newline at end of file
diff --git a/src/main/java/junit/framework/Protectable.java b/src/main/java/junit/framework/Protectable.java
index c5ceb16..9f30b10 100644
--- a/src/main/java/junit/framework/Protectable.java
+++ b/src/main/java/junit/framework/Protectable.java
@@ -8,7 +8,7 @@ package junit.framework;
public interface Protectable {
/**
- * Run the following method protected.
+ * Run the the following method protected.
*/
public abstract void protect() throws Throwable;
-}
+}
\ No newline at end of file
diff --git a/src/main/java/junit/framework/TestCase.java b/src/main/java/junit/framework/TestCase.java
index e474a64..b89ce71 100644
--- a/src/main/java/junit/framework/TestCase.java
+++ b/src/main/java/junit/framework/TestCase.java
@@ -73,7 +73,6 @@ import java.lang.reflect.Modifier;
* @see TestResult
* @see TestSuite
*/
-@SuppressWarnings("deprecation")
public abstract class TestCase extends Assert implements Test {
/**
* the name of the test case
@@ -103,7 +102,7 @@ public abstract class TestCase extends Assert implements Test {
}
/**
- * Creates a default TestResult object.
+ * Creates a default TestResult object
*
* @see TestResult
*/
@@ -188,6 +187,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that a condition is true. If it isn't it throws
* an AssertionFailedError with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertTrue(String message, boolean condition) {
Assert.assertTrue(message, condition);
}
@@ -196,6 +196,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that a condition is true. If it isn't it throws
* an AssertionFailedError.
*/
+ @SuppressWarnings("deprecation")
public static void assertTrue(boolean condition) {
Assert.assertTrue(condition);
}
@@ -204,6 +205,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that a condition is false. If it isn't it throws
* an AssertionFailedError with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertFalse(String message, boolean condition) {
Assert.assertFalse(message, condition);
}
@@ -212,6 +214,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that a condition is false. If it isn't it throws
* an AssertionFailedError.
*/
+ @SuppressWarnings("deprecation")
public static void assertFalse(boolean condition) {
Assert.assertFalse(condition);
}
@@ -219,6 +222,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Fails a test with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void fail(String message) {
Assert.fail(message);
}
@@ -226,6 +230,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Fails a test with no message.
*/
+ @SuppressWarnings("deprecation")
public static void fail() {
Assert.fail();
}
@@ -234,6 +239,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two objects are equal. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, Object expected, Object actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -242,6 +248,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two objects are equal. If they are not
* an AssertionFailedError is thrown.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(Object expected, Object actual) {
Assert.assertEquals(expected, actual);
}
@@ -249,6 +256,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two Strings are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, String expected, String actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -256,6 +264,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two Strings are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String expected, String actual) {
Assert.assertEquals(expected, actual);
}
@@ -265,6 +274,7 @@ public abstract class TestCase extends Assert implements Test {
* an AssertionFailedError is thrown with the given message. If the expected
* value is infinity then the delta value is ignored.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, double expected, double actual, double delta) {
Assert.assertEquals(message, expected, actual, delta);
}
@@ -273,6 +283,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two doubles are equal concerning a delta. If the expected
* value is infinity then the delta value is ignored.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(double expected, double actual, double delta) {
Assert.assertEquals(expected, actual, delta);
}
@@ -282,6 +293,7 @@ public abstract class TestCase extends Assert implements Test {
* are not an AssertionFailedError is thrown with the given message. If the
* expected value is infinity then the delta value is ignored.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, float expected, float actual, float delta) {
Assert.assertEquals(message, expected, actual, delta);
}
@@ -290,6 +302,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two floats are equal concerning a delta. If the expected
* value is infinity then the delta value is ignored.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(float expected, float actual, float delta) {
Assert.assertEquals(expected, actual, delta);
}
@@ -298,6 +311,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two longs are equal. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, long expected, long actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -305,6 +319,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two longs are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(long expected, long actual) {
Assert.assertEquals(expected, actual);
}
@@ -313,6 +328,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two booleans are equal. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, boolean expected, boolean actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -320,6 +336,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two booleans are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(boolean expected, boolean actual) {
Assert.assertEquals(expected, actual);
}
@@ -328,6 +345,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two bytes are equal. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, byte expected, byte actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -335,6 +353,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two bytes are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(byte expected, byte actual) {
Assert.assertEquals(expected, actual);
}
@@ -343,6 +362,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two chars are equal. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, char expected, char actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -350,6 +370,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two chars are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(char expected, char actual) {
Assert.assertEquals(expected, actual);
}
@@ -358,6 +379,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two shorts are equal. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, short expected, short actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -365,6 +387,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two shorts are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(short expected, short actual) {
Assert.assertEquals(expected, actual);
}
@@ -373,6 +396,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two ints are equal. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(String message, int expected, int actual) {
Assert.assertEquals(message, expected, actual);
}
@@ -380,6 +404,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that two ints are equal.
*/
+ @SuppressWarnings("deprecation")
public static void assertEquals(int expected, int actual) {
Assert.assertEquals(expected, actual);
}
@@ -387,6 +412,7 @@ public abstract class TestCase extends Assert implements Test {
/**
* Asserts that an object isn't null.
*/
+ @SuppressWarnings("deprecation")
public static void assertNotNull(Object object) {
Assert.assertNotNull(object);
}
@@ -395,6 +421,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that an object isn't null. If it is
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertNotNull(String message, Object object) {
Assert.assertNotNull(message, object);
}
@@ -406,6 +433,7 @@ public abstract class TestCase extends Assert implements Test {
*
* @param object Object to check or null
*/
+ @SuppressWarnings("deprecation")
public static void assertNull(Object object) {
Assert.assertNull(object);
}
@@ -414,6 +442,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that an object is null. If it is not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertNull(String message, Object object) {
Assert.assertNull(message, object);
}
@@ -422,6 +451,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two objects refer to the same object. If they are not
* an AssertionFailedError is thrown with the given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertSame(String message, Object expected, Object actual) {
Assert.assertSame(message, expected, actual);
}
@@ -430,6 +460,7 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two objects refer to the same object. If they are not
* the same an AssertionFailedError is thrown.
*/
+ @SuppressWarnings("deprecation")
public static void assertSame(Object expected, Object actual) {
Assert.assertSame(expected, actual);
}
@@ -439,6 +470,7 @@ public abstract class TestCase extends Assert implements Test {
* refer to the same object an AssertionFailedError is thrown with the
* given message.
*/
+ @SuppressWarnings("deprecation")
public static void assertNotSame(String message, Object expected, Object actual) {
Assert.assertNotSame(message, expected, actual);
}
@@ -447,22 +479,27 @@ public abstract class TestCase extends Assert implements Test {
* Asserts that two objects do not refer to the same object. If they do
* refer to the same object an AssertionFailedError is thrown.
*/
+ @SuppressWarnings("deprecation")
public static void assertNotSame(Object expected, Object actual) {
Assert.assertNotSame(expected, actual);
}
+ @SuppressWarnings("deprecation")
public static void failSame(String message) {
Assert.failSame(message);
}
+ @SuppressWarnings("deprecation")
public static void failNotSame(String message, Object expected, Object actual) {
Assert.failNotSame(message, expected, actual);
}
+ @SuppressWarnings("deprecation")
public static void failNotEquals(String message, Object expected, Object actual) {
Assert.failNotEquals(message, expected, actual);
}
+ @SuppressWarnings("deprecation")
public static String format(String message, Object expected, Object actual) {
return Assert.format(message, expected, actual);
}
@@ -482,7 +519,7 @@ public abstract class TestCase extends Assert implements Test {
}
/**
- * Returns a string representation of the test case.
+ * Returns a string representation of the test case
*/
@Override
public String toString() {
@@ -490,7 +527,7 @@ public abstract class TestCase extends Assert implements Test {
}
/**
- * Gets the name of a TestCase.
+ * Gets the name of a TestCase
*
* @return the name of the TestCase
*/
@@ -499,7 +536,7 @@ public abstract class TestCase extends Assert implements Test {
}
/**
- * Sets the name of a TestCase.
+ * Sets the name of a TestCase
*
* @param name the name to set
*/
diff --git a/src/main/java/junit/framework/TestFailure.java b/src/main/java/junit/framework/TestFailure.java
index d1ddfbc..6168b58 100644
--- a/src/main/java/junit/framework/TestFailure.java
+++ b/src/main/java/junit/framework/TestFailure.java
@@ -1,6 +1,7 @@
package junit.framework;
-import org.junit.internal.Throwables;
+import java.io.PrintWriter;
+import java.io.StringWriter;
/**
@@ -48,7 +49,10 @@ public class TestFailure {
* thrown by TestFailure.
*/
public String trace() {
- return Throwables.getStacktrace(thrownException());
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ thrownException().printStackTrace(writer);
+ return stringWriter.toString();
}
/**
diff --git a/src/main/java/junit/framework/TestResult.java b/src/main/java/junit/framework/TestResult.java
index e01a2b0..8332542 100644
--- a/src/main/java/junit/framework/TestResult.java
+++ b/src/main/java/junit/framework/TestResult.java
@@ -52,14 +52,14 @@ public class TestResult {
}
/**
- * Registers a TestListener.
+ * Registers a TestListener
*/
public synchronized void addListener(TestListener listener) {
fListeners.add(listener);
}
/**
- * Unregisters a TestListener.
+ * Unregisters a TestListener
*/
public synchronized void removeListener(TestListener listener) {
fListeners.remove(listener);
@@ -91,7 +91,7 @@ public class TestResult {
}
/**
- * Returns an Enumeration for the errors.
+ * Returns an Enumeration for the errors
*/
public synchronized EnumerationTestSuite
is a Composite
of Tests.
@@ -34,7 +35,7 @@ import org.junit.internal.Throwables;
*
- * Class[] testClasses = { MathTest.class, AnotherTest.class };
+ * Class[] testClasses = { MathTest.class, AnotherTest.class }
* TestSuite suite= new TestSuite(testClasses);
*
*
@@ -64,11 +65,11 @@ public class TestSuite implements Test {
test = constructor.newInstance(new Object[]{name});
}
} catch (InstantiationException e) {
- return (warning("Cannot instantiate test case: " + name + " (" + Throwables.getStacktrace(e) + ")"));
+ return (warning("Cannot instantiate test case: " + name + " (" + exceptionToString(e) + ")"));
} catch (InvocationTargetException e) {
- return (warning("Exception in constructor: " + name + " (" + Throwables.getStacktrace(e.getTargetException()) + ")"));
+ return (warning("Exception in constructor: " + name + " (" + exceptionToString(e.getTargetException()) + ")"));
} catch (IllegalAccessException e) {
- return (warning("Cannot access test case: " + name + " (" + Throwables.getStacktrace(e) + ")"));
+ return (warning("Cannot access test case: " + name + " (" + exceptionToString(e) + ")"));
}
return (Test) test;
}
@@ -98,6 +99,16 @@ public class TestSuite implements Test {
};
}
+ /**
+ * Converts the stack trace into a string
+ */
+ private static String exceptionToString(Throwable e) {
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter writer = new PrintWriter(stringWriter);
+ e.printStackTrace(writer);
+ return stringWriter.toString();
+ }
+
private String fName;
private Vectorexpected
*/
- 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 unexpected
*/
- 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 unexpected
*/
- 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 unexpected
*/
- 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 unexpected
*/
- 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 {
* actual
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 {
* actual
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 {
* actual
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 expected
and
@@ -549,7 +547,7 @@ public class Assert {
* actual
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 {
* actual
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 {
* actual
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 {
* actual
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 {
* actual
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 null
*/
- 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 null
*/
- 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 null
*/
- 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 null
*/
- 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 expected
*/
- 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 expected
*/
- 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 unexpected
*/
- 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 unexpected
*/
- 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 null
- * 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
objects
, the test will halt and be ignored.
*/
public static void assumeNotNull(Object... objects) {
- assumeThat(objects, notNullValue());
assumeThat(asList(objects), everyItem(notNullValue()));
}
diff --git a/src/main/java/org/junit/AssumptionViolatedException.java b/src/main/java/org/junit/AssumptionViolatedException.java
index 1d62190..e48ddf0 100644
--- a/src/main/java/org/junit/AssumptionViolatedException.java
+++ b/src/main/java/org/junit/AssumptionViolatedException.java
@@ -40,7 +40,7 @@ public class AssumptionViolatedException extends org.junit.internal.AssumptionVi
/**
* An assumption exception with the given message and a cause.
*/
- public AssumptionViolatedException(String message, Throwable t) {
- super(message, t);
+ public AssumptionViolatedException(String assumption, Throwable t) {
+ super(assumption, t);
}
}
diff --git a/src/main/java/org/junit/ClassRule.java b/src/main/java/org/junit/ClassRule.java
index 94ee29f..02c40a7 100644
--- a/src/main/java/org/junit/ClassRule.java
+++ b/src/main/java/org/junit/ClassRule.java
@@ -28,10 +28,7 @@ import java.lang.annotation.Target;
* annotated {@link ClassRule}s on a class, they will be applied in an order
* that depends on your JVM's implementation of the reflection API, which is
* undefined, in general. However, Rules defined by fields will always be applied
- * after Rules defined by methods, i.e. the Statements returned by the former will
- * be executed around those returned by the latter.
- *
- * * For example, here is a test suite that connects to a server once before * all the test classes run, and disconnects after they are finished: @@ -82,37 +79,9 @@ import java.lang.annotation.Target; *
* For more information and more examples, see {@link org.junit.rules.TestRule}. * - *
- * You can use {@link #order()} if you want to have control over the order in - * which the Rules are applied. - * - *
- * public class ThreeClassRules { - * @ClassRule(order = 0) - * public static LoggingRule outer = new LoggingRule("outer rule"); - * - * @ClassRule(order = 1) - * public static LoggingRule middle = new LoggingRule("middle rule"); - * - * @ClassRule(order = 2) - * public static LoggingRule inner = new LoggingRule("inner rule"); - * - * // ... - * } - *- * * @since 4.9 */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.METHOD}) public @interface ClassRule { - - /** - * Specifies the order in which rules are applied. The rules with a higher value are inner. - * - * @since 4.13 - */ - int order() default Rule.DEFAULT_ORDER; - } diff --git a/src/main/java/org/junit/ComparisonFailure.java b/src/main/java/org/junit/ComparisonFailure.java index d1daa86..9563e61 100644 --- a/src/main/java/org/junit/ComparisonFailure.java +++ b/src/main/java/org/junit/ComparisonFailure.java @@ -21,7 +21,7 @@ public class ComparisonFailure extends AssertionError { /* * We have to use the f prefix until the next major release to ensure * serialization compatibility. - * See https://github.com/junit-team/junit4/issues/976 + * See https://github.com/junit-team/junit/issues/976 */ private String fExpected; private String fActual; diff --git a/src/main/java/org/junit/Rule.java b/src/main/java/org/junit/Rule.java index 9370e94..711235c 100644 --- a/src/main/java/org/junit/Rule.java +++ b/src/main/java/org/junit/Rule.java @@ -16,14 +16,12 @@ import java.lang.annotation.Target; * to the {@link org.junit.rules.TestRule} will run any {@link Before} methods, * then the {@link Test} method, and finally any {@link After} methods, * throwing an exception if any of these fail. If there are multiple - * annotated {@link Rule}s on a class, they will be applied in order of methods first, then fields. + * annotated {@link Rule}s on a class, they will be applied in order of fields first, then methods. * However, if there are multiple fields (or methods) they will be applied in an order * that depends on your JVM's implementation of the reflection API, which is * undefined, in general. Rules defined by fields will always be applied - * after Rules defined by methods, i.e. the Statements returned by the former will - * be executed around those returned by the latter. - * - *
* For example, here is a test class that creates a temporary folder before * each test method, and deletes it after each: @@ -63,39 +61,10 @@ import java.lang.annotation.Target; * For more information and more examples, see * {@link org.junit.rules.TestRule}. * - *
- * You can use {@link #order()} if you want to have control over the order in - * which the Rules are applied. - * - *
- * public class ThreeRules { - * @Rule(order = 0) - * public LoggingRule outer = new LoggingRule("outer rule"); - * - * @Rule(order = 1) - * public LoggingRule middle = new LoggingRule("middle rule"); - * - * @Rule(order = 2) - * public LoggingRule inner = new LoggingRule("inner rule"); - * - * // ... - * } - *- * * @since 4.7 */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.METHOD}) public @interface Rule { - int DEFAULT_ORDER = -1; - - /** - * Specifies the order in which rules are applied. The rules with a higher value are inner. - * - * @since 4.13 - */ - int order() default DEFAULT_ORDER; - -} +} \ No newline at end of file diff --git a/src/main/java/org/junit/Test.java b/src/main/java/org/junit/Test.java index 1db6fc7..71ac428 100644 --- a/src/main/java/org/junit/Test.java +++ b/src/main/java/org/junit/Test.java @@ -1,7 +1,5 @@ package org.junit; -import org.junit.function.ThrowingRunnable; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -25,40 +23,24 @@ import java.lang.annotation.Target; * } * *
- * The Test
annotation supports two optional parameters for
- * exception testing and for limiting test execution time.
- *
- *
- * The parameter expected
declares that a test method should throw
+ * The Test
annotation supports two optional parameters.
+ * The first, expected
, declares that a test method should throw
* an exception. If it doesn't throw an exception or if it throws a different exception
* than the one declared, the test fails. For example, the following test succeeds:
*
- * @Test(expected=IndexOutOfBoundsException.class) - * public void outOfBounds() { + * @Test(expected=IndexOutOfBoundsException.class) public void outOfBounds() { * new ArrayList<Object>().get(1); * } *- * - * Using the parameter
expected
for exception testing comes with
- * some limitations: only the exception's type can be checked and it is not
- * possible to precisely specify the code that throws the exception. Therefore
- * JUnit 4 has improved its support for exception testing with
- * {@link Assert#assertThrows(Class, ThrowingRunnable)} and the
- * {@link org.junit.rules.ExpectedException ExpectedException} rule.
- * With assertThrows
the code that throws the exception can be
- * precisely specified. If the exception's message or one of its properties
- * should be verified, the ExpectedException
rule can be used. Further
+ * If the exception's message or one of its properties should be verified, the
+ * {@link org.junit.rules.ExpectedException ExpectedException} rule can be used. Further
* information about exception testing can be found at the
- * JUnit Wiki.
- *
- *
- * The parameter timeout
causes a test to fail if it takes
+ * The second optional parameter, timeout
, causes a test to fail if it takes
* longer than a specified amount of clock time (measured in milliseconds). The following test fails:
*
- * @Test(timeout=100) - * public void infinity() { + * @Test(timeout=100) public void infinity() { * while(true); * } *@@ -67,8 +49,7 @@ import java.lang.annotation.Target; * following test may or may not fail depending on how the operating system * schedules threads: *
- * @Test(timeout=100) - * public void sleep100() { + * @Test(timeout=100) public void sleep100() { * Thread.sleep(100); * } *@@ -85,7 +66,7 @@ import java.lang.annotation.Target; public @interface Test { /** - * Default empty exception. + * Default empty exception */ static class None extends Throwable { private static final long serialVersionUID = 1L; diff --git a/src/main/java/org/junit/TestCouldNotBeSkippedException.java b/src/main/java/org/junit/TestCouldNotBeSkippedException.java deleted file mode 100644 index 4804493..0000000 --- a/src/main/java/org/junit/TestCouldNotBeSkippedException.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.junit; - -/** - * Indicates that a test that indicated that it should be skipped could not be skipped. - * This can be thrown if a test uses the methods in {@link Assume} to indicate that - * it should be skipped, but before processing of the test was completed, other failures - * occured. - * - * @see org.junit.Assume - * @since 4.13 - */ -public class TestCouldNotBeSkippedException extends RuntimeException { - private static final long serialVersionUID = 1L; - - /** Creates an instance using the given assumption failure. */ - public TestCouldNotBeSkippedException(org.junit.internal.AssumptionViolatedException cause) { - super("Test could not be skipped due to other failures", cause); - } -} diff --git a/src/main/java/org/junit/experimental/categories/Categories.java b/src/main/java/org/junit/experimental/categories/Categories.java index 0c73ed8..290c180 100644 --- a/src/main/java/org/junit/experimental/categories/Categories.java +++ b/src/main/java/org/junit/experimental/categories/Categories.java @@ -2,10 +2,8 @@ package org.junit.experimental.categories; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.util.Arrays; import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.Set; import org.junit.runner.Description; @@ -78,7 +76,7 @@ import org.junit.runners.model.RunnerBuilder; * * * @version 4.12 - * @see Categories at JUnit wiki + * @see Categories at JUnit wiki */ public class Categories extends Suite { @@ -88,13 +86,13 @@ public class Categories extends Suite { * Determines the tests to run that are annotated with categories specified in * the value of this annotation or their subtypes unless excluded with {@link ExcludeCategory}. */ - Class>[] value() default {}; + public Class>[] value() default {}; /** * If true, runs tests annotated with any of the categories in * {@link IncludeCategory#value()}. Otherwise, runs tests only if annotated with all of the categories. */ - boolean matchAny() default true; + public boolean matchAny() default true; } @Retention(RetentionPolicy.RUNTIME) @@ -103,13 +101,13 @@ public class Categories extends Suite { * Determines the tests which do not run if they are annotated with categories specified in the * value of this annotation or their subtypes regardless of being included in {@link IncludeCategory#value()}. */ - Class>[] value() default {}; + public Class>[] value() default {}; /** * If true, the tests annotated with any of the categories in {@link ExcludeCategory#value()} * do not run. Otherwise, the tests do not run if and only if annotated with all categories. */ - boolean matchAny() default true; + public boolean matchAny() default true; } public static class CategoryFilter extends Filter { @@ -119,7 +117,10 @@ public class Categories extends Suite { private final boolean excludedAny; public static CategoryFilter include(boolean matchAny, Class>... categories) { - return new CategoryFilter(matchAny, categories, true, null); + if (hasNull(categories)) { + throw new NullPointerException("has null category"); + } + return categoryFilter(matchAny, createSet(categories), true, null); } public static CategoryFilter include(Class> category) { @@ -131,7 +132,10 @@ public class Categories extends Suite { } public static CategoryFilter exclude(boolean matchAny, Class>... categories) { - return new CategoryFilter(true, null, matchAny, categories); + if (hasNull(categories)) { + throw new NullPointerException("has null category"); + } + return categoryFilter(true, null, matchAny, createSet(categories)); } public static CategoryFilter exclude(Class> category) { @@ -147,30 +151,14 @@ public class Categories extends Suite { return new CategoryFilter(matchAnyInclusions, inclusions, matchAnyExclusions, exclusions); } - @Deprecated - public CategoryFilter(Class> includedCategory, Class> excludedCategory) { - includedAny = true; - excludedAny = true; - included = nullableClassToSet(includedCategory); - excluded = nullableClassToSet(excludedCategory); - } - protected CategoryFilter(boolean matchAnyIncludes, Set
diff --git a/src/main/java/org/junit/experimental/theories/Theories.java b/src/main/java/org/junit/experimental/theories/Theories.java index ac88a36..817f553 100644 --- a/src/main/java/org/junit/experimental/theories/Theories.java +++ b/src/main/java/org/junit/experimental/theories/Theories.java @@ -51,11 +51,11 @@ import org.junit.runners.model.TestClass; * } * } *- * This makes it clear that the username should be included in the config file name, + * This makes it clear that the user's filename should be included in the config file name, * only if it doesn't contain a slash. Another test or theory might define what happens when a username does contain * a slash.
UserTest
will attempt to run filenameIncludesUsername
on every compatible data
* point defined in the class. If any of the assumptions fail, the data point is silently ignored. If all of the
- * assumptions pass, but an assertion fails, the test fails. If no parameters can be found that satisfy all assumptions, the test fails.
+ * assumptions pass, but an assertion fails, the test fails.
*
* Defining general statements as theories allows data point reuse across a bunch of functionality tests and also
* allows automated tools to search for new, unexpected data points that expose bugs.
@@ -73,11 +73,6 @@ public class Theories extends BlockJUnit4ClassRunner {
super(klass);
}
- /** @since 4.13 */
- protected Theories(TestClass testClass) throws InitializationError {
- super(testClass);
- }
-
@Override
protected void collectInitializationErrors(List Always returns an empty list.
- */
- public List Always throws an {@link UnsupportedOperationException}
- */
- public long getThreadCpuTime(long id) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * Always returns false.
- */
- public boolean isThreadCpuTimeSupported() {
- return false;
- }
-
-}
-
diff --git a/src/main/java/org/junit/internal/management/ManagementFactory.java b/src/main/java/org/junit/internal/management/ManagementFactory.java
deleted file mode 100644
index 5be1447..0000000
--- a/src/main/java/org/junit/internal/management/ManagementFactory.java
+++ /dev/null
@@ -1,77 +0,0 @@
-package org.junit.internal.management;
-
-import org.junit.internal.Classes;
-
-import java.lang.reflect.InvocationTargetException;
-
-/**
- * Reflective wrapper around {@link java.lang.management.ManagementFactory}
- */
-public class ManagementFactory {
- private static final class FactoryHolder {
- private static final Class> MANAGEMENT_FACTORY_CLASS;
-
- static {
- Class> managementFactoryClass = null;
- try {
- managementFactoryClass = Classes.getClass("java.lang.management.ManagementFactory");
- } catch (ClassNotFoundException e) {
- // do nothing, managementFactoryClass will be none on failure
- }
- MANAGEMENT_FACTORY_CLASS = managementFactoryClass;
- }
-
- static Object getBeanObject(String methodName) {
- if (MANAGEMENT_FACTORY_CLASS != null) {
- try {
- return MANAGEMENT_FACTORY_CLASS.getMethod(methodName).invoke(null);
- } catch (IllegalAccessException e) {
- // fallthrough
- } catch (IllegalArgumentException e) {
- // fallthrough
- } catch (InvocationTargetException e) {
- // fallthrough
- } catch (NoSuchMethodException e) {
- // fallthrough
- } catch (SecurityException e) {
- // fallthrough
- }
- }
- return null;
- }
- }
-
- private static final class RuntimeHolder {
- private static final RuntimeMXBean RUNTIME_MX_BEAN =
- getBean(FactoryHolder.getBeanObject("getRuntimeMXBean"));
-
- private static final RuntimeMXBean getBean(Object runtimeMxBean) {
- return runtimeMxBean != null
- ? new ReflectiveRuntimeMXBean(runtimeMxBean) : new FakeRuntimeMXBean();
- }
- }
-
- private static final class ThreadHolder {
- private static final ThreadMXBean THREAD_MX_BEAN =
- getBean(FactoryHolder.getBeanObject("getThreadMXBean"));
-
- private static final ThreadMXBean getBean(Object threadMxBean) {
- return threadMxBean != null
- ? new ReflectiveThreadMXBean(threadMxBean) : new FakeThreadMXBean();
- }
- }
-
- /**
- * @see java.lang.management.ManagementFactory#getRuntimeMXBean()
- */
- public static RuntimeMXBean getRuntimeMXBean() {
- return RuntimeHolder.RUNTIME_MX_BEAN;
- }
-
- /**
- * @see java.lang.management.ManagementFactory#getThreadMXBean()
- */
- public static ThreadMXBean getThreadMXBean() {
- return ThreadHolder.THREAD_MX_BEAN;
- }
-}
diff --git a/src/main/java/org/junit/internal/management/ReflectiveRuntimeMXBean.java b/src/main/java/org/junit/internal/management/ReflectiveRuntimeMXBean.java
deleted file mode 100644
index 289587a..0000000
--- a/src/main/java/org/junit/internal/management/ReflectiveRuntimeMXBean.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package org.junit.internal.management;
-
-import org.junit.internal.Classes;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Implementation of {@link RuntimeMXBean} using the JVM reflectively.
- */
-final class ReflectiveRuntimeMXBean implements RuntimeMXBean {
- private final Object runtimeMxBean;
-
- private static final class Holder {
- private static final Method getInputArgumentsMethod;
- static {
- Method inputArguments = null;
- try {
- Class> threadMXBeanClass = Classes.getClass("java.lang.management.RuntimeMXBean");
- inputArguments = threadMXBeanClass.getMethod("getInputArguments");
- } catch (ClassNotFoundException e) {
- // do nothing, input arguments will be null on failure
- } catch (NoSuchMethodException e) {
- // do nothing, input arguments will be null on failure
- } catch (SecurityException e) {
- // do nothing, input arguments will be null on failure
- }
- getInputArgumentsMethod = inputArguments;
- }
- }
-
- ReflectiveRuntimeMXBean(Object runtimeMxBean) {
- super();
- this.runtimeMxBean = runtimeMxBean;
- }
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("unchecked")
- public List
- * The most illustrative use case is for tests that make use of the
- * {@link Timeout} rule, when ran in debug mode the test may terminate on
- * timeout abruptly during debugging. Developers may disable the timeout, or
- * increase the timeout by making a code change on tests that need debugging and
- * remember revert the change afterwards or rules such as {@link Timeout} that
- * may be disabled during debugging may be wrapped in a {@code DisableOnDebug}.
- *
- * The important benefit of this feature is that you can disable such rules
- * without any making any modifications to your test class to remove them during
- * debugging.
- *
- * This does nothing to tackle timeouts or time sensitive code under test when
- * debugging and may make this less useful in such circumstances.
- *
- * Example usage:
- *
- *
- * Options specified in:
- * You have to add the {@code ExpectedException} rule to your test.
+ *
+ * You have to add the {@code ExpectedException} rule to your test.
* This doesn't affect your existing tests (see {@code throwsNothing()}).
- * After specifying the type of the expected exception your test is
+ * After specifiying the type of the expected exception your test is
* successful when such an exception is thrown and it fails if a
* different or no exception is thrown.
*
- * This rule does not perform any special magic to make execution continue
- * as if the exception had not been thrown. So it is nearly always a mistake
- * for a test method to have statements after the one that is expected to
- * throw the exception.
- *
- * Instead of specifying the exception's type you can characterize the
- * expected exception based on other criteria, too:
+ *
+ * Instead of specifying the exception's type you can characterize the
+ * expected exception based on other criterias, too:
*
* You can combine any of the presented expect-methods. The test is
+ *
+ * You can combine any of the presented expect-methods. The test is
* successful if all specifications are met.
* It is recommended to set the {@link org.junit.Rule#order() order} of the
- * {@code ExpectedException} to {@code Integer.MAX_VALUE} if it is used together
- * with another rule that handles exceptions, e.g. {@link ErrorCollector}.
- * Otherwise failing tests may be successful.
- * JUnit uses {@link AssumptionViolatedException}s for indicating that a test
+ *
+ * JUnit uses {@link AssumptionViolatedException}s for indicating that a test
* provides no useful information. (See {@link org.junit.Assume} for more
* information.) You have to call {@code assume} methods before you set
* expectations of the {@code ExpectedException} rule. In this case the rule
@@ -87,7 +80,8 @@ import org.junit.runners.model.Statement;
*
* JUnit uses {@link AssertionError}s for indicating that a test is failing. You
+ *
+ * JUnit uses {@link AssertionError}s for indicating that a test is failing. You
* have to call {@code assert} methods before you set expectations of the
* {@code ExpectedException} rule, if they should be handled by the framework.
* E.g. the following test fails because of the {@code assertTrue} statement.
@@ -99,7 +93,8 @@ import org.junit.runners.model.Statement;
* }
*
* By default missing exceptions are reported with an error message
+ *
+ * By default missing exceptions are reported with an error message
* like "Expected test to throw an instance of foo". You can configure a different
* message by means of {@link #reportMissingExceptionWithMessage(String)}. You
* can use a {@code %s} placeholder for the description of the expected
@@ -112,13 +107,7 @@ public class ExpectedException implements TestRule {
/**
* Returns a {@linkplain TestRule rule} that expects no exception to
* be thrown (identical to behavior without this rule).
- *
- * @deprecated Since 4.13
- * {@link org.junit.Assert#assertThrows(Class, org.junit.function.ThrowingRunnable)
- * Assert.assertThrows} can be used to verify that your code throws a specific
- * exception.
*/
- @Deprecated
public static ExpectedException none() {
return new ExpectedException();
}
@@ -233,18 +222,10 @@ public class ExpectedException implements TestRule {
* throw new IllegalArgumentException("What happened?", cause);
* }
*/
- public void expectCause(Matcher> expectedCause) {
+ public void expectCause(Matcher extends Throwable> expectedCause) {
expect(hasCause(expectedCause));
}
- /**
- * Check if any Exception is expected.
- * @since 4.13
- */
- public final boolean isAnyExceptionExpected() {
- return matcherBuilder.expectsThrowable();
- }
-
private class ExpectedExceptionStatement extends Statement {
private final Statement next;
@@ -274,6 +255,10 @@ public class ExpectedException implements TestRule {
}
}
+ private boolean isAnyExceptionExpected() {
+ return matcherBuilder.expectsThrowable();
+ }
+
private void failDueToMissingException() throws AssertionError {
fail(missingExceptionMessage());
}
diff --git a/src/main/java/org/junit/rules/ExternalResource.java b/src/main/java/org/junit/rules/ExternalResource.java
index 71fc842..71ca287 100644
--- a/src/main/java/org/junit/rules/ExternalResource.java
+++ b/src/main/java/org/junit/rules/ExternalResource.java
@@ -1,10 +1,6 @@
package org.junit.rules;
-import java.util.ArrayList;
-import java.util.List;
-
import org.junit.runner.Description;
-import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
/**
@@ -48,20 +44,11 @@ public abstract class ExternalResource implements TestRule {
@Override
public void evaluate() throws Throwable {
before();
-
- List Note that {@link MethodRule} has been replaced by {@link TestRule},
+ * Example of usage:
* TemporaryFolder rule supports assured deletion mode, which
- * will fail the test in case deletion fails with {@link AssertionError}.
- *
- * Creating TemporaryFolder with assured deletion:
- * It is recommended to always set the {@link Rule#order() order} of the
- * {@code TestWatcher} to {@code Integer.MIN_VALUE} so that it encloses all
- * other rules. Otherwise it may see failed tests as successful and vice versa
- * if some rule changes the result of a test (e.g. {@link ErrorCollector} or
- * {@link ExpectedException}).
*
* @since 4.9
*/
@@ -60,7 +54,7 @@ public abstract class TestWatcher implements TestRule {
try {
base.evaluate();
succeededQuietly(description, errors);
- } catch (org.junit.internal.AssumptionViolatedException e) {
+ } catch (@SuppressWarnings("deprecation") org.junit.internal.AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable e) {
@@ -93,6 +87,7 @@ public abstract class TestWatcher implements TestRule {
}
}
+ @SuppressWarnings("deprecation")
private void skippedQuietly(
org.junit.internal.AssumptionViolatedException e, Description description,
List Returns an empty {@code Request} if {@code desiredDescription} is not a single test and filters all but the single
- * test if {@code desiredDescription} is a single test.
- * For example, here is code to run a test suite in reverse order:
- * Beware of using this interface to cope with order dependencies between tests.
- * Tests that are isolated from each other are less expensive to maintain and
- * can be run individually.
- *
- * @since 4.13
- */
-public interface Orderable extends Sortable {
-
- /**
- * Orders the tests using In general you will not need to use a For a factory to be used with {@code @OrderWith} it needs to have a public no-arg
- * constructor.
- */
- public interface Factory {
- /**
- * Creates an Ordering instance using the given context. Implementations
- * of this method that do not need to use the context can return the
- * same instance every time.
- */
- Ordering create(Context context);
- }
-}
diff --git a/src/main/java/org/junit/runner/manipulation/Sortable.java b/src/main/java/org/junit/runner/manipulation/Sortable.java
index 0c59f33..9ac864c 100644
--- a/src/main/java/org/junit/runner/manipulation/Sortable.java
+++ b/src/main/java/org/junit/runner/manipulation/Sortable.java
@@ -15,6 +15,6 @@ public interface Sortable {
*
* @param sorter the {@link Sorter} to use for sorting the tests
*/
- void sort(Sorter sorter);
+ public void sort(Sorter sorter);
}
diff --git a/src/main/java/org/junit/runner/manipulation/Sorter.java b/src/main/java/org/junit/runner/manipulation/Sorter.java
index 4b5274c..20192d0 100644
--- a/src/main/java/org/junit/runner/manipulation/Sorter.java
+++ b/src/main/java/org/junit/runner/manipulation/Sorter.java
@@ -1,21 +1,16 @@
package org.junit.runner.manipulation;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
import java.util.Comparator;
-import java.util.List;
import org.junit.runner.Description;
/**
* A Note that not all runners will call this method, so runners should
- * be prepared to handle {@link #testStarted(Description)} calls for tests
- * where there was no corresponding {@code testSuiteStarted()} call for
- * the parent {@code Description}.
- *
- * @param description the description of the test suite that is about to be run
- * (generally a class name)
- * @since 4.13
- */
- public void testSuiteStarted(Description description) throws Exception {
- }
-
- /**
- * Called when a test suite has finished, whether the test suite succeeds or fails.
- * This method will not be called for a given {@link Description} unless
- * {@link #testSuiteStarted(Description)} was called for the same @code Description}.
- *
- * @param description the description of the test suite that just ran
- * @since 4.13
- */
- public void testSuiteFinished(Description description) throws Exception {
- }
-
/**
* Called when an atomic test is about to be started.
*
diff --git a/src/main/java/org/junit/runner/notification/RunNotifier.java b/src/main/java/org/junit/runner/notification/RunNotifier.java
index 752fa3b..6875f76 100644
--- a/src/main/java/org/junit/runner/notification/RunNotifier.java
+++ b/src/main/java/org/junit/runner/notification/RunNotifier.java
@@ -65,8 +65,8 @@ public class RunNotifier {
void run() {
int capacity = currentListeners.size();
- List This class synchronizes all listener calls on a RunNotifier instance. This is done because
* prior to JUnit 4.12, all listeners were called in a synchronized block in RunNotifier,
* so no two listeners were ever called concurrently. If we instead made the methods here
- * synchronized, clients that added multiple listeners that called common code might see
+ * sychronized, clients that added multiple listeners that called common code might see
* issues due to the reduced synchronization.
*
* @author Tibor Digana (tibor17)
@@ -43,37 +43,6 @@ final class SynchronizedRunListener extends RunListener {
}
}
- /**
- * {@inheritDoc}
- *
- * For example, to test the
- * Each instance of
* In order that you can easily identify the individual tests, you may provide a
@@ -79,36 +69,33 @@ import org.junit.runners.parameterized.TestWithParameters;
*
*
* In the example given above, the
* You can also write:
*
- * Each instance of
- * If your test needs to perform some preparation or cleanup based on the
- * parameters, this can be done by adding public static methods annotated with
- * {@code @BeforeParam}/{@code @AfterParam}. Such methods should either have no
- * parameters or the same parameters as the test.
- *
* By default the {@code Parameterized} runner creates a slightly modified
@@ -166,7 +141,7 @@ import org.junit.runners.parameterized.TestWithParameters;
* The factory must have a public zero-arg constructor.
*
* With {@link org.junit.Assume assumptions} you can dynamically skip tests.
- * Assumptions are also supported by the ArrayComparisonFailure
with an error text and the array's
@@ -33,8 +32,7 @@ public class ArrayComparisonFailure extends AssertionError {
*/
public ArrayComparisonFailure(String message, AssertionError cause, int index) {
this.fMessage = message;
- this.fCause = cause;
- initCause(fCause);
+ initCause(cause);
addDimension(index);
}
@@ -42,11 +40,6 @@ public class ArrayComparisonFailure extends AssertionError {
fIndices.add(0, index);
}
- @Override
- public synchronized Throwable getCause() {
- return super.getCause() == null ? fCause : super.getCause();
- }
-
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder();
diff --git a/src/main/java/org/junit/internal/AssumptionViolatedException.java b/src/main/java/org/junit/internal/AssumptionViolatedException.java
index 0e79b56..880d73f 100644
--- a/src/main/java/org/junit/internal/AssumptionViolatedException.java
+++ b/src/main/java/org/junit/internal/AssumptionViolatedException.java
@@ -1,8 +1,5 @@
package org.junit.internal;
-import java.io.IOException;
-import java.io.ObjectOutputStream;
-
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.SelfDescribing;
@@ -21,7 +18,7 @@ public class AssumptionViolatedException extends RuntimeException implements Sel
/*
* We have to use the f prefix until the next major release to ensure
* serialization compatibility.
- * See https://github.com/junit-team/junit4/issues/976
+ * See https://github.com/junit-team/junit/issues/976
*/
private final String fAssumption;
private final boolean fValueMatcher;
@@ -111,29 +108,4 @@ public class AssumptionViolatedException extends RuntimeException implements Sel
}
}
}
-
- /**
- * Override default Java object serialization to correctly deal with potentially unserializable matchers or values.
- * By not implementing readObject, we assure ourselves of backwards compatibility and compatibility with the
- * standard way of Java serialization.
- *
- * @param objectOutputStream The outputStream to write our representation to
- * @throws IOException When serialization fails
- */
- private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
- ObjectOutputStream.PutField putField = objectOutputStream.putFields();
- putField.put("fAssumption", fAssumption);
- putField.put("fValueMatcher", fValueMatcher);
-
- // We have to wrap the matcher into a serializable form.
- putField.put("fMatcher", SerializableMatcherDescription.asSerializableMatcher(fMatcher));
-
- // We have to wrap the value inside a non-String class (instead of serializing the String value directly) as
- // A Description will handle a String and non-String object differently (1st is surrounded by '"' while the
- // latter will be surrounded by '<' '>'. Wrapping it makes sure that the description of a serialized and
- // non-serialized instance produce the exact same description
- putField.put("fValue", SerializableValueDescription.asSerializableValue(fValue));
-
- objectOutputStream.writeFields();
- }
}
diff --git a/src/main/java/org/junit/internal/Checks.java b/src/main/java/org/junit/internal/Checks.java
deleted file mode 100644
index 9724947..0000000
--- a/src/main/java/org/junit/internal/Checks.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package org.junit.internal;
-
-/** @since 4.13 */
-public final class Checks {
-
- private Checks() {}
-
- /**
- * Checks that the given value is not {@code null}.
- *
- * @param value object reference to check
- * @return the passed-in value, if not {@code null}
- * @throws NullPointerException if {@code value} is {@code null}
- */
- public static
- * public static class DisableTimeoutOnDebugSampleTest {
- *
- * @Rule
- * public TestRule timeout = new DisableOnDebug(new Timeout(20));
- *
- * @Test
- * public void myTest() {
- * int i = 0;
- * assertEquals(0, i); // suppose you had a break point here to inspect i
- * }
- * }
- *
- *
- * @since 4.12
- */
-public class DisableOnDebug implements TestRule {
- private final TestRule rule;
- private final boolean debugging;
-
- /**
- * Create a {@code DisableOnDebug} instance with the timeout specified in
- * milliseconds.
- *
- * @param rule to disable during debugging
- */
- public DisableOnDebug(TestRule rule) {
- this(rule, ManagementFactory.getRuntimeMXBean()
- .getInputArguments());
- }
-
- /**
- * Visible for testing purposes only.
- *
- * @param rule the rule to disable during debugging
- * @param inputArguments
- * arguments provided to the Java runtime
- */
- DisableOnDebug(TestRule rule, List
- *
public class SimpleExpectedExceptionTest {
* @Rule
- * public ExpectedException thrown = ExpectedException.none();
+ * public ExpectedException thrown= ExpectedException.none();
*
* @Test
* public void throwsNothing() {
@@ -34,19 +35,16 @@ import org.junit.runners.model.Statement;
* }
* }
*
- *
*
*
- * @Test
* public void throwsException() {
@@ -64,15 +63,9 @@ import org.junit.runners.model.Statement;
* throw new NullPointerException("What happened?");
* }
*
- * @Rule(order = Integer.MAX_VALUE)
- * public ExpectedException thrown = ExpectedException.none();
- *
* AssumptionViolatedExceptions
- * AssertionErrors
*
- * Missing Exceptions
- *
+ *
+ *
+ * Note that {@link MethodRule} has been replaced by {@link TestRule},
* which has the added benefit of supporting class rules.
*
* @since 4.7
diff --git a/src/main/java/org/junit/rules/RuleChain.java b/src/main/java/org/junit/rules/RuleChain.java
index bf93aae..f43d8f5 100644
--- a/src/main/java/org/junit/rules/RuleChain.java
+++ b/src/main/java/org/junit/rules/RuleChain.java
@@ -4,34 +4,26 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import org.junit.Rule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
- * The {@code RuleChain} can be used for creating composite rules. You create a
+ * The RuleChain rule allows ordering of TestRules. You create a
* {@code RuleChain} with {@link #outerRule(TestRule)} and subsequent calls of
* {@link #around(TestRule)}:
*
*
- * public abstract class CompositeRules {
- * public static TestRule extendedLogging() {
- * return RuleChain.outerRule(new LoggingRule("outer rule"))
- * .around(new LoggingRule("middle rule"))
- * .around(new LoggingRule("inner rule"));
- * }
- * }
- *
- *
- *
- * public class UseRuleChain {
- * @Rule
- * public final TestRule extendedLogging = CompositeRules.extendedLogging();
+ * public static class UseRuleChain {
+ * @Rule
+ * public RuleChain chain= RuleChain
+ * .outerRule(new LoggingRule("outer rule")
+ * .around(new LoggingRule("middle rule")
+ * .around(new LoggingRule("inner rule");
*
- * @Test
- * public void example() {
- * assertTrue(true);
- * }
+ * @Test
+ * public void example() {
+ * assertTrue(true);
+ * }
* }
*
*
@@ -46,13 +38,6 @@ import org.junit.runners.model.Statement;
* finished outer rule
*
*
- * In older versions of JUnit (before 4.13) {@code RuleChain} was used for
- * ordering rules. We recommend to not use it for this purpose anymore. You can
- * use the attribute {@code order} of the annotation {@link Rule#order() Rule}
- * or {@link org.junit.ClassRule#order() ClassRule} for ordering rules.
- *
- * @see org.junit.Rule#order()
- * @see org.junit.ClassRule#order()
* @since 4.10
*/
public class RuleChain implements TestRule {
@@ -87,17 +72,13 @@ public class RuleChain implements TestRule {
}
/**
- * Create a new {@code RuleChain}, which encloses the given {@link TestRule} with
+ * Create a new {@code RuleChain}, which encloses the {@code nextRule} with
* the rules of the current {@code RuleChain}.
*
- * @param enclosedRule the rule to enclose; must not be {@code null}.
+ * @param enclosedRule the rule to enclose.
* @return a new {@code RuleChain}.
- * @throws NullPointerException if the argument {@code enclosedRule} is {@code null}
*/
public RuleChain around(TestRule enclosedRule) {
- if (enclosedRule == null) {
- throw new NullPointerException("The enclosed rule must not be null");
- }
List
@@ -31,104 +26,18 @@ import org.junit.Rule;
* }
*
*
- *
- * @Rule
- * public TemporaryFolder folder= TemporaryFolder.builder().assureDeletion().build();
- *
- *
* @since 4.7
*/
public class TemporaryFolder extends ExternalResource {
private final File parentFolder;
- private final boolean assureDeletion;
private File folder;
- private static final int TEMP_DIR_ATTEMPTS = 10000;
- private static final String TMP_PREFIX = "junit";
-
- /**
- * Create a temporary folder which uses system default temporary-file
- * directory to create temporary resources.
- */
public TemporaryFolder() {
- this((File) null);
+ this(null);
}
- /**
- * Create a temporary folder which uses the specified directory to create
- * temporary resources.
- *
- * @param parentFolder folder where temporary resources will be created.
- * If {@code null} then system default temporary-file directory is used.
- */
public TemporaryFolder(File parentFolder) {
this.parentFolder = parentFolder;
- this.assureDeletion = false;
- }
-
- /**
- * Create a {@link TemporaryFolder} initialized with
- * values from a builder.
- */
- protected TemporaryFolder(Builder builder) {
- this.parentFolder = builder.parentFolder;
- this.assureDeletion = builder.assureDeletion;
- }
-
- /**
- * Returns a new builder for building an instance of {@link TemporaryFolder}.
- *
- * @since 4.13
- */
- public static Builder builder() {
- return new Builder();
- }
-
- /**
- * Builds an instance of {@link TemporaryFolder}.
- *
- * @since 4.13
- */
- public static class Builder {
- private File parentFolder;
- private boolean assureDeletion;
-
- protected Builder() {}
-
- /**
- * Specifies which folder to use for creating temporary resources.
- * If {@code null} then system default temporary-file directory is
- * used.
- *
- * @return this
- */
- public Builder parentFolder(File parentFolder) {
- this.parentFolder = parentFolder;
- return this;
- }
-
- /**
- * Setting this flag assures that no resources are left undeleted. Failure
- * to fulfill the assurance results in failure of tests with an
- * {@link AssertionError}.
- *
- * @return this
- */
- public Builder assureDeletion() {
- this.assureDeletion = true;
- return this;
- }
-
- /**
- * Builds a {@link TemporaryFolder} instance using the values in this builder.
- */
- public TemporaryFolder build() {
- return new TemporaryFolder(this);
- }
}
@Override
@@ -166,129 +75,66 @@ public class TemporaryFolder extends ExternalResource {
* Returns a new fresh file with a random name under the temporary folder.
*/
public File newFile() throws IOException {
- return File.createTempFile(TMP_PREFIX, null, getRoot());
+ return File.createTempFile("junit", null, getRoot());
}
/**
- * Returns a new fresh folder with the given path under the temporary
+ * Returns a new fresh folder with the given name under the temporary
* folder.
*/
- public File newFolder(String path) throws IOException {
- return newFolder(new String[]{path});
+ public File newFolder(String folder) throws IOException {
+ return newFolder(new String[]{folder});
}
/**
- * Returns a new fresh folder with the given paths under the temporary
- * folder. For example, if you pass in the strings {@code "parent"} and {@code "child"}
- * then a directory named {@code "parent"} will be created under the temporary folder
- * and a directory named {@code "child"} will be created under the newly-created
- * {@code "parent"} directory.
+ * Returns a new fresh folder with the given name(s) under the temporary
+ * folder.
*/
- public File newFolder(String... paths) throws IOException {
- if (paths.length == 0) {
- throw new IllegalArgumentException("must pass at least one path");
- }
-
- /*
- * Before checking if the paths are absolute paths, check if create() was ever called,
- * and if it wasn't, throw IllegalStateException.
- */
- File root = getRoot();
- for (String path : paths) {
- if (new File(path).isAbsolute()) {
- throw new IOException("folder path \'" + path + "\' is not a relative path");
- }
- }
-
- File relativePath = null;
- File file = root;
- boolean lastMkdirsCallSuccessful = true;
- for (String path : paths) {
- relativePath = new File(relativePath, path);
- file = new File(root, relativePath.getPath());
-
- lastMkdirsCallSuccessful = file.mkdirs();
- if (!lastMkdirsCallSuccessful && !file.isDirectory()) {
- if (file.exists()) {
- throw new IOException(
- "a file with the path \'" + relativePath.getPath() + "\' exists");
- } else {
- throw new IOException(
- "could not create a folder with the path \'" + relativePath.getPath() + "\'");
- }
+ public File newFolder(String... folderNames) throws IOException {
+ File file = getRoot();
+ for (int i = 0; i < folderNames.length; i++) {
+ String folderName = folderNames[i];
+ validateFolderName(folderName);
+ file = new File(file, folderName);
+ if (!file.mkdir() && isLastElementInArray(i, folderNames)) {
+ throw new IOException(
+ "a folder with the name \'" + folderName + "\' already exists");
}
}
- if (!lastMkdirsCallSuccessful) {
- throw new IOException(
- "a folder with the path \'" + relativePath.getPath() + "\' already exists");
- }
return file;
}
-
+
/**
- * Returns a new fresh folder with a random name under the temporary folder.
+ * Validates if multiple path components were used while creating a folder.
+ *
+ * @param folderName
+ * Name of the folder being created
*/
- public File newFolder() throws IOException {
- return createTemporaryFolderIn(getRoot());
+ private void validateFolderName(String folderName) throws IOException {
+ File tempFile = new File(folderName);
+ if (tempFile.getParent() != null) {
+ String errorMsg = "Folder name cannot consist of multiple path components separated by a file separator."
+ + " Please use newFolder('MyParentFolder','MyFolder') to create hierarchies of folders";
+ throw new IOException(errorMsg);
+ }
}
- private static File createTemporaryFolderIn(File parentFolder) throws IOException {
- try {
- return createTemporaryFolderWithNioApi(parentFolder);
- } catch (ClassNotFoundException ignore) {
- // Fallback for Java 5 and 6
- return createTemporaryFolderWithFileApi(parentFolder);
- } catch (InvocationTargetException e) {
- Throwable cause = e.getCause();
- if (cause instanceof IOException) {
- throw (IOException) cause;
- }
- if (cause instanceof RuntimeException) {
- throw (RuntimeException) cause;
- }
- IOException exception = new IOException("Failed to create temporary folder in " + parentFolder);
- exception.initCause(cause);
- throw exception;
- } catch (Exception e) {
- throw new RuntimeException("Failed to create temporary folder in " + parentFolder, e);
- }
+ private boolean isLastElementInArray(int index, String[] array) {
+ return index == array.length - 1;
}
- private static File createTemporaryFolderWithNioApi(File parentFolder) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
- Class> filesClass = Class.forName("java.nio.file.Files");
- Object fileAttributeArray = Array.newInstance(Class.forName("java.nio.file.attribute.FileAttribute"), 0);
- Class> pathClass = Class.forName("java.nio.file.Path");
- Object tempDir;
- if (parentFolder != null) {
- Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", pathClass, String.class, fileAttributeArray.getClass());
- Object parentPath = File.class.getDeclaredMethod("toPath").invoke(parentFolder);
- tempDir = createTempDirectoryMethod.invoke(null, parentPath, TMP_PREFIX, fileAttributeArray);
- } else {
- Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", String.class, fileAttributeArray.getClass());
- tempDir = createTempDirectoryMethod.invoke(null, TMP_PREFIX, fileAttributeArray);
- }
- return (File) pathClass.getDeclaredMethod("toFile").invoke(tempDir);
+ /**
+ * Returns a new fresh folder with a random name under the temporary folder.
+ */
+ public File newFolder() throws IOException {
+ return createTemporaryFolderIn(getRoot());
}
- private static File createTemporaryFolderWithFileApi(File parentFolder) throws IOException {
- File createdFolder = null;
- for (int i = 0; i < TEMP_DIR_ATTEMPTS; ++i) {
- // Use createTempFile to get a suitable folder name.
- String suffix = ".tmp";
- File tmpFile = File.createTempFile(TMP_PREFIX, suffix, parentFolder);
- String tmpName = tmpFile.toString();
- // Discard .tmp suffix of tmpName.
- String folderName = tmpName.substring(0, tmpName.length() - suffix.length());
- createdFolder = new File(folderName);
- if (createdFolder.mkdir()) {
- tmpFile.delete();
- return createdFolder;
- }
- tmpFile.delete();
- }
- throw new IOException("Unable to create temporary directory in: "
- + parentFolder.toString() + ". Tried " + TEMP_DIR_ATTEMPTS + " times. "
- + "Last attempted to create: " + createdFolder.toString());
+ private File createTemporaryFolderIn(File parentFolder) throws IOException {
+ File createdFolder = File.createTempFile("junit", "", parentFolder);
+ createdFolder.delete();
+ createdFolder.mkdir();
+ return createdFolder;
}
/**
@@ -304,48 +150,21 @@ public class TemporaryFolder extends ExternalResource {
/**
* Delete all files and folders under the temporary folder. Usually not
- * called directly, since it is automatically applied by the {@link Rule}.
- *
- * @throws AssertionError if unable to clean up resources
- * and deletion of resources is assured.
+ * called directly, since it is automatically applied by the {@link Rule}
*/
public void delete() {
- if (!tryDelete()) {
- if (assureDeletion) {
- fail("Unable to clean up temporary folder " + folder);
- }
- }
- }
-
- /**
- * Tries to delete all files and folders under the temporary folder and
- * returns whether deletion was successful or not.
- *
- * @return {@code true} if all resources are deleted successfully,
- * {@code false} otherwise.
- */
- private boolean tryDelete() {
- if (folder == null) {
- return true;
+ if (folder != null) {
+ recursiveDelete(folder);
}
-
- return recursiveDelete(folder);
}
- private boolean recursiveDelete(File file) {
- // Try deleting file before assuming file is a directory
- // to prevent following symbolic links.
- if (file.delete()) {
- return true;
- }
+ private void recursiveDelete(File file) {
File[] files = file.listFiles();
if (files != null) {
for (File each : files) {
- if (!recursiveDelete(each)) {
- return false;
- }
+ recursiveDelete(each);
}
}
- return file.delete();
+ file.delete();
}
}
diff --git a/src/main/java/org/junit/rules/TestName.java b/src/main/java/org/junit/rules/TestName.java
index e2ebc2e..bf72602 100644
--- a/src/main/java/org/junit/rules/TestName.java
+++ b/src/main/java/org/junit/rules/TestName.java
@@ -25,7 +25,7 @@ import org.junit.runner.Description;
* @since 4.7
*/
public class TestName extends TestWatcher {
- private volatile String name;
+ private String name;
@Override
protected void starting(Description d) {
diff --git a/src/main/java/org/junit/rules/TestWatcher.java b/src/main/java/org/junit/rules/TestWatcher.java
index a28514d..5492b6b 100644
--- a/src/main/java/org/junit/rules/TestWatcher.java
+++ b/src/main/java/org/junit/rules/TestWatcher.java
@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List;
import org.junit.AssumptionViolatedException;
-import org.junit.Rule;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
@@ -18,7 +17,7 @@ import org.junit.runners.model.Statement;
* public static class WatchmanTest {
* private static String watchedLog;
*
- * @Rule(order = Integer.MIN_VALUE)
+ * @Rule
* public TestWatcher watchman= new TestWatcher() {
* @Override
* protected void failed(Throwable e, Description description) {
@@ -41,11 +40,6 @@ import org.junit.runners.model.Statement;
* }
* }
*
- * Description
named after testClass
- *
- * @param testClass A not null {@link Class} containing tests
- * @param annotations meta-data about the test, for downstream interpreters
- * @return a Description
of testClass
- */
- public static Description createSuiteDescription(Class> testClass, Annotation... annotations) {
- return new Description(testClass, testClass.getName(), annotations);
- }
-
/**
* Describes a Runner which runs no tests
*/
@@ -150,7 +139,7 @@ public class Description implements Serializable {
/*
* We have to use the f prefix until the next major release to ensure
* serialization compatibility.
- * See https://github.com/junit-team/junit4/issues/976
+ * See https://github.com/junit-team/junit/issues/976
*/
private final Collection@OrderWith
or extends a class annotated
- * with @OrderWith
, JUnit will order the tests in the test class (and child
- * test classes, if any) using the ordering defined by the {@link Ordering} class.
- *
- * @since 4.13
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.TYPE)
-@Inherited
-@ValidateWith(OrderWithValidator.class)
-public @interface OrderWith {
- /**
- * Gets a class that extends {@link Ordering}. The class must have a public no-arg constructor.
- */
- Class extends Ordering.Factory> value();
-}
diff --git a/src/main/java/org/junit/runner/OrderWithValidator.java b/src/main/java/org/junit/runner/OrderWithValidator.java
deleted file mode 100644
index f8eab25..0000000
--- a/src/main/java/org/junit/runner/OrderWithValidator.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package org.junit.runner;
-
-import static java.util.Collections.emptyList;
-import static java.util.Collections.singletonList;
-
-import java.util.List;
-
-import org.junit.FixMethodOrder;
-import org.junit.runners.model.TestClass;
-import org.junit.validator.AnnotationValidator;
-
-/**
- * Validates that there are no errors in the use of the {@code OrderWith}
- * annotation. If there is, a {@code Throwable} object will be added to the list
- * of errors.
- *
- * @since 4.13
- */
-public final class OrderWithValidator extends AnnotationValidator {
-
- /**
- * Adds to {@code errors} a throwable for each problem detected. Looks for
- * {@code FixMethodOrder} annotations.
- *
- * @param testClass that is being validated
- * @return A list of exceptions detected
- *
- * @since 4.13
- */
- @Override
- public ListdesiredDescription
*
- *
* private static Comparator<Description> forward() {
- * return new Comparator<Description>() {
- * public int compare(Description o1, Description o2) {
- * return o1.getDisplayName().compareTo(o2.getDisplayName());
- * }
- * };
+ * return new Comparator<Description>() {
+ * public int compare(Description o1, Description o2) {
+ * return o1.getDisplayName().compareTo(o2.getDisplayName());
+ * }
+ * };
* }
*
* public static main() {
- * new JUnitCore().run(Request.aClass(AllTests.class).sortWith(forward()));
+ * new JUnitCore().run(Request.aClass(AllTests.class).sortWith(forward()));
* }
*
*
@@ -171,32 +167,4 @@ public abstract class Request {
public Request sortWith(Comparatorordering
- *
- * private static Ordering reverse() {
- * return new Ordering() {
- * public List<Description> orderItems(Collection<Description> descriptions) {
- * List<Description> ordered = new ArrayList<>(descriptions);
- * Collections.reverse(ordered);
- * return ordered;
- * }
- * }
- * }
- *
- * public static main() {
- * new JUnitCore().run(Request.aClass(AllTests.class).orderWith(reverse()));
- * }
- *
- *
- * @return a Request with ordered Tests
- * @since 4.13
- */
- public Request orderWith(Ordering ordering) {
- return new OrderingRequest(this, ordering);
- }
}
diff --git a/src/main/java/org/junit/runner/Result.java b/src/main/java/org/junit/runner/Result.java
index 4b5f4a4..73ad059 100644
--- a/src/main/java/org/junit/runner/Result.java
+++ b/src/main/java/org/junit/runner/Result.java
@@ -28,7 +28,6 @@ public class Result implements Serializable {
ObjectStreamClass.lookup(SerializedForm.class).getFields();
private final AtomicInteger count;
private final AtomicInteger ignoreCount;
- private final AtomicInteger assumptionFailureCount;
private final CopyOnWriteArrayListtrue
if all tests succeeded
*/
@@ -154,7 +137,7 @@ public class Result implements Serializable {
@Override
public void testAssumptionFailure(Failure failure) {
- assumptionFailureCount.getAndIncrement();
+ // do nothing: same as passing (for 4.5; may change in 4.6)
}
}
@@ -173,7 +156,6 @@ public class Result implements Serializable {
private static final long serialVersionUID = 1L;
private final AtomicInteger fCount;
private final AtomicInteger fIgnoreCount;
- private final AtomicInteger assumptionFailureCount;
private final Listorderer
- *
- * @throws InvalidOrderingException if orderer does something invalid (like remove or add
- * children)
- */
- void order(Orderer orderer) throws InvalidOrderingException;
-}
diff --git a/src/main/java/org/junit/runner/manipulation/Orderer.java b/src/main/java/org/junit/runner/manipulation/Orderer.java
deleted file mode 100644
index eb13054..0000000
--- a/src/main/java/org/junit/runner/manipulation/Orderer.java
+++ /dev/null
@@ -1,62 +0,0 @@
-package org.junit.runner.manipulation;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import org.junit.runner.Description;
-
-/**
- * Orders tests.
- *
- * @since 4.13
- */
-public final class Orderer {
- private final Ordering ordering;
-
- Orderer(Ordering delegate) {
- this.ordering = delegate;
- }
-
- /**
- * Orders the descriptions.
- *
- * @return descriptions in order
- */
- public Listtarget
.
- *
- * @throws InvalidOrderingException if ordering does something invalid (like remove or add
- * children)
- */
- public void apply(Object target) throws InvalidOrderingException {
- if (target instanceof Orderable) {
- Orderable orderable = (Orderable) target;
- orderable.order(this);
- }
- }
-}
diff --git a/src/main/java/org/junit/runner/manipulation/Ordering.java b/src/main/java/org/junit/runner/manipulation/Ordering.java
deleted file mode 100644
index 0d0ce93..0000000
--- a/src/main/java/org/junit/runner/manipulation/Ordering.java
+++ /dev/null
@@ -1,172 +0,0 @@
-package org.junit.runner.manipulation;
-
-import java.lang.reflect.Constructor;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Random;
-
-import org.junit.runner.Description;
-import org.junit.runner.OrderWith;
-
-/**
- * Reorders tests. An {@code Ordering} can reverse the order of tests, sort the
- * order or even shuffle the order.
- *
- * Ordering
directly.
- * Instead, use {@link org.junit.runner.Request#orderWith(Ordering)}.
- *
- * @since 4.13
- */
-public abstract class Ordering {
- private static final String CONSTRUCTOR_ERROR_FORMAT
- = "Ordering class %s should have a public constructor with signature "
- + "%s(Ordering.Context context)";
-
- /**
- * Creates an {@link Ordering} that shuffles the items using the given
- * {@link Random} instance.
- */
- public static Ordering shuffledBy(final Random random) {
- return new Ordering() {
- @Override
- boolean validateOrderingIsCorrect() {
- return false;
- }
-
- @Override
- protected Listtarget
using this ordering.
- *
- * @throws InvalidOrderingException if ordering does something invalid (like remove or add
- * children)
- */
- public void apply(Object target) throws InvalidOrderingException {
- /*
- * Note that some subclasses of Ordering override apply(). The Sorter
- * subclass of Ordering overrides apply() to apply the sort (this is
- * done because sorting is more efficient than ordering).
- */
- if (target instanceof Orderable) {
- Orderable orderable = (Orderable) target;
- orderable.order(new Orderer(this));
- }
- }
-
- /**
- * Returns {@code true} if this ordering could produce invalid results (i.e.
- * if it could add or remove values).
- */
- boolean validateOrderingIsCorrect() {
- return true;
- }
-
- /**
- * Implemented by sub-classes to order the descriptions.
- *
- * @return descriptions in order
- */
- protected abstract ListSorter
orders tests. In general you will not need
- * to use a Sorter
directly. Instead, use
- * {@link org.junit.runner.Request#sortWith(Comparator)}.
+ * to use a Sorter
directly. Instead, use {@link org.junit.runner.Request#sortWith(Comparator)}.
*
* @since 4.0
*/
-public class Sorter extends Ordering implements ComparatorSorter
that leaves elements in an undefined order
*/
@@ -32,26 +27,17 @@ public class Sorter extends Ordering implements Comparatortarget
using comparator
.
- *
- * @since 4.0
+ * Sorts the test in runner
using comparator
*/
- @Override
- public void apply(Object target) {
- /*
- * Note that all runners that are Orderable are also Sortable (because
- * Orderable extends Sortable). Sorting is more efficient than ordering,
- * so we override the parent behavior so we sort instead.
- */
- if (target instanceof Sortable) {
- Sortable sortable = (Sortable) target;
+ public void apply(Object object) {
+ if (object instanceof Sortable) {
+ Sortable sortable = (Sortable) object;
sortable.sort(this);
}
}
@@ -59,32 +45,4 @@ public class Sorter extends Ordering implements Comparator
- *
+
operator, write:
+ * For example, to test a Fibonacci function, write:
*
* @RunWith(Parameterized.class)
- * public class AdditionTest {
- * @Parameters(name = "{index}: {0} + {1} = {2}")
+ * public class FibonacciTest {
+ * @Parameters(name= "{index}: fib[{0}]={1}")
* public static Iterable<Object[]> data() {
- * return Arrays.asList(new Object[][] { { 0, 0, 0 }, { 1, 1, 2 },
- * { 3, 2, 5 }, { 4, 3, 7 } });
+ * return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
+ * { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
* }
*
- * private int firstSummand;
+ * private int fInput;
*
- * private int secondSummand;
+ * private int fExpected;
*
- * private int sum;
- *
- * public AdditionTest(int firstSummand, int secondSummand, int sum) {
- * this.firstSummand = firstSummand;
- * this.secondSummand = secondSummand;
- * this.sum = sum;
+ * public FibonacciTest(int input, int expected) {
+ * fInput= input;
+ * fExpected= expected;
* }
*
* @Test
* public void test() {
- * assertEquals(sum, firstSummand + secondSummand);
+ * assertEquals(fExpected, Fibonacci.compute(fInput));
* }
* }
*
* AdditionTest
will be constructed using the
- * three-argument constructor and the data values in the
+ * Each instance of FibonacciTest
will be constructed using the
+ * two-argument constructor and the data values in the
* @Parameters
method.
* Parameterized
runner creates
- * names like [2: 3 + 2 = 5]
. If you don't use the name parameter,
+ * names like [1: fib(3)=2]
. If you don't use the name parameter,
* then the current parameter index is used as name.
*
* @RunWith(Parameterized.class)
- * public class AdditionTest {
- * @Parameters(name = "{index}: {0} + {1} = {2}")
- * public static Iterable<Object[]> data() {
- * return Arrays.asList(new Object[][] { { 0, 0, 0 }, { 1, 1, 2 },
- * { 3, 2, 5 }, { 4, 3, 7 } });
- * }
- *
- * @Parameter(0)
- * public int firstSummand;
+ * public class FibonacciTest {
+ * @Parameters
+ * public static Iterable<Object[]> data() {
+ * return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
+ * { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
+ * }
+ *
+ * @Parameter(0)
+ * public int fInput;
*
- * @Parameter(1)
- * public int secondSummand;
+ * @Parameter(1)
+ * public int fExpected;
*
- * @Parameter(2)
- * public int sum;
- *
- * @Test
- * public void test() {
- * assertEquals(sum, firstSummand + secondSummand);
- * }
+ * @Test
+ * public void test() {
+ * assertEquals(fExpected, Fibonacci.compute(fInput));
+ * }
* }
*
* AdditionTest
will be constructed with the default constructor
+ * Each instance of FibonacciTest
will be constructed with the default constructor
* and fields annotated by @Parameter
will be initialized
* with the data values in the @Parameters
method.
*
@@ -118,7 +105,8 @@ import org.junit.runners.parameterized.TestWithParameters;
*
* @Parameters
* public static Object[][] data() {
- * return new Object[][] { { 0, 0, 0 }, { 1, 1, 2 }, { 3, 2, 5 }, { 4, 3, 7 } } };
+ * return new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 },
+ * { 5, 5 }, { 6, 8 } };
* }
*
*
@@ -142,19 +130,6 @@ import org.junit.runners.parameterized.TestWithParameters;
* }
*
*
- * Executing code before/after executing tests for specific parameters
- *
- * @BeforeParam
- * public static void beforeTestsForParameter(String onlyParameter) {
- * System.out.println("Testing " + onlyParameter);
- * }
- *
- *
* Create different runners
*
- * public class YourRunnerFactory implements ParametersRunnerFactory {
+ * public class YourRunnerFactory implements ParameterizedRunnerFactory {
* public Runner createRunnerForTestWithParameters(TestWithParameters test)
* throws InitializationError {
* return YourRunner(test);
@@ -185,21 +160,6 @@ import org.junit.runners.parameterized.TestWithParameters;
* }
*
*
- * Avoid creating parameters
- * @Parameters
method.
- * Creating parameters is stopped when the assumption fails and none of the
- * tests in the test class is executed. JUnit reports a
- * {@link Result#getAssumptionFailureCount() single assumption failure} for the
- * whole test class in this case.
- *
- * @Parameters
- * public static Iterable<? extends Object> data() {
- * String os = System.getProperty("os.name").toLowerCase()
- * Assume.assumeTrue(os.contains("win"));
- * return Arrays.asList("first test", "second test");
- * }
- *
* @since 4.0
*/
public class Parameterized extends Suite {
@@ -210,7 +170,7 @@ public class Parameterized extends Suite {
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
- public @interface Parameters {
+ public static @interface Parameters {
/**
* Optional pattern to derive the test's name from the parameters. Use
* numbers in braces to refer to the parameters or the additional data
@@ -241,7 +201,7 @@ public class Parameterized extends Suite {
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
- public @interface Parameter {
+ public static @interface Parameter {
/**
* Method that returns the index of the parameter in the array
* returned by the method annotated by Parameters
.
@@ -270,235 +230,122 @@ public class Parameterized extends Suite {
Class extends ParametersRunnerFactory> value() default BlockJUnit4ClassRunnerWithParametersFactory.class;
}
- /**
- * Annotation for {@code public static void} methods which should be executed before
- * evaluating tests with particular parameters.
- *
- * @see org.junit.BeforeClass
- * @see org.junit.Before
- * @since 4.13
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface BeforeParam {
- }
+ private static final ParametersRunnerFactory DEFAULT_FACTORY = new BlockJUnit4ClassRunnerWithParametersFactory();
- /**
- * Annotation for {@code public static void} methods which should be executed after
- * evaluating tests with particular parameters.
- *
- * @see org.junit.AfterClass
- * @see org.junit.After
- * @since 4.13
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface AfterParam {
- }
+ private static final List