Skip to content

Commit

Permalink
Restore classes that are deprecated for 6 years
Browse files Browse the repository at this point in the history
This reverts commit 745ca05. The
deletion of the classes caused some problems for users (see
https://groups.yahoo.com/neo/groups/junit/conversations/topics/24572).

In addition, the new coding style has been applied and "major" has
been added to the deprecation messages in the Javadoc.
  • Loading branch information
stefanbirkner authored and marcphilipp committed Sep 12, 2014
1 parent e561590 commit 883c1bb
Show file tree
Hide file tree
Showing 11 changed files with 823 additions and 5 deletions.
5 changes: 0 additions & 5 deletions doc/ReleaseNotes4.12.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,6 @@ Follow this link for _IntelliJ IDEA_: [http://www.jetbrains.com/idea/webhelp/act
# Miscellaneous


### [Pull request #862:] (https://github.com/junit-team/junit/pull/862) Delete classes that are deprecated for six years

`JUnit4ClassRunner` was deprecated in JUnit 4.3 (six years and nine major releases ago) with a comment saying "This may disappear as soon as 1 April 2009". We started having some problems with running the tests in JDK 7, and we decided to delete the class and its support classes. Although we try very hard to maintain backwards compatibility, `JUnit4ClassRunner` didn't support `Rule`s, it wasn't designed to be extensible, and it was in an internal package. Please use `BlockJUnit4ClassRunner` instead.


### [Pull request #776:](https://github.com/junit-team/junit/pull/776) Add support for [Travis CI](http://travis-ci.org)

Travis CI is a free CI server for public Github repositories. Every pull request is run by Travis CI and Github's web interface shows the CI result for each pull request. Every user can use Travis CI for testing her branches, too.
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/org/junit/internal/runners/ClassRoadie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.junit.internal.runners;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;

/**
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
* removed in the next major release. Please use
* {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
*/
@Deprecated
public class ClassRoadie {
private RunNotifier notifier;
private TestClass testClass;
private Description description;
private final Runnable runnable;

public ClassRoadie(RunNotifier notifier, TestClass testClass,
Description description, Runnable runnable) {
this.notifier = notifier;
this.testClass = testClass;
this.description = description;
this.runnable = runnable;
}

protected void runUnprotected() {
runnable.run();
}

protected void addFailure(Throwable targetException) {
notifier.fireTestFailure(new Failure(description, targetException));
}

public void runProtected() {
try {
runBefores();
runUnprotected();
} catch (FailedBefore e) {
} finally {
runAfters();
}
}

private void runBefores() throws FailedBefore {
try {
try {
List<Method> befores = testClass.getBefores();
for (Method before : befores) {
before.invoke(null);
}
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
} catch (AssumptionViolatedException e) {
throw new FailedBefore();
} catch (Throwable e) {
addFailure(e);
throw new FailedBefore();
}
}

private void runAfters() {
List<Method> afters = testClass.getAfters();
for (Method after : afters) {
try {
after.invoke(null);
} catch (InvocationTargetException e) {
addFailure(e.getTargetException());
} catch (Throwable e) {
addFailure(e); // Untested, but seems impossible
}
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/junit/internal/runners/FailedBefore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.junit.internal.runners;

import org.junit.runners.BlockJUnit4ClassRunner;

/**
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
* removed in the next major release. Please use
* {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
*/
@Deprecated
class FailedBefore extends Exception {
private static final long serialVersionUID = 1L;
}
147 changes: 147 additions & 0 deletions src/main/java/org/junit/internal/runners/JUnit4ClassRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.junit.internal.runners;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;

/**
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
* removed in the next major release. Please use
* {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
*/
@Deprecated
public class JUnit4ClassRunner extends Runner implements Filterable, Sortable {
private final List<Method> testMethods;
private TestClass testClass;

public JUnit4ClassRunner(Class<?> klass) throws InitializationError {
testClass = new TestClass(klass);
testMethods = getTestMethods();
validate();
}

protected List<Method> getTestMethods() {
return testClass.getTestMethods();
}

protected void validate() throws InitializationError {
MethodValidator methodValidator = new MethodValidator(testClass);
methodValidator.validateMethodsForDefaultRunner();
methodValidator.assertValid();
}

@Override
public void run(final RunNotifier notifier) {
new ClassRoadie(notifier, testClass, getDescription(), new Runnable() {
public void run() {
runMethods(notifier);
}
}).runProtected();
}

protected void runMethods(final RunNotifier notifier) {
for (Method method : testMethods) {
invokeTestMethod(method, notifier);
}
}

@Override
public Description getDescription() {
Description spec = Description.createSuiteDescription(getName(), classAnnotations());
List<Method> testMethods = this.testMethods;
for (Method method : testMethods) {
spec.addChild(methodDescription(method));
}
return spec;
}

protected Annotation[] classAnnotations() {
return testClass.getJavaClass().getAnnotations();
}

protected String getName() {
return getTestClass().getName();
}

protected Object createTest() throws Exception {
return getTestClass().getConstructor().newInstance();
}

protected void invokeTestMethod(Method method, RunNotifier notifier) {
Description description = methodDescription(method);
Object test;
try {
test = createTest();
} catch (InvocationTargetException e) {
testAborted(notifier, description, e.getCause());
return;
} catch (Exception e) {
testAborted(notifier, description, e);
return;
}
TestMethod testMethod = wrapMethod(method);
new MethodRoadie(test, testMethod, notifier, description).run();
}

private void testAborted(RunNotifier notifier, Description description,
Throwable e) {
notifier.fireTestStarted(description);
notifier.fireTestFailure(new Failure(description, e));
notifier.fireTestFinished(description);
}

protected TestMethod wrapMethod(Method method) {
return new TestMethod(method, testClass);
}

protected String testName(Method method) {
return method.getName();
}

protected Description methodDescription(Method method) {
return Description.createTestDescription(getTestClass().getJavaClass(), testName(method), testAnnotations(method));
}

protected Annotation[] testAnnotations(Method method) {
return method.getAnnotations();
}

public void filter(Filter filter) throws NoTestsRemainException {
for (Iterator<Method> iter = testMethods.iterator(); iter.hasNext(); ) {
Method method = iter.next();
if (!filter.shouldRun(methodDescription(method))) {
iter.remove();
}
}
if (testMethods.isEmpty()) {
throw new NoTestsRemainException();
}
}

public void sort(final Sorter sorter) {
Collections.sort(testMethods, new Comparator<Method>() {
public int compare(Method o1, Method o2) {
return sorter.compare(methodDescription(o1), methodDescription(o2));
}
});
}

protected TestClass getTestClass() {
return testClass;
}
}
Loading

0 comments on commit 883c1bb

Please sign in to comment.