1b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpackage org.junit.internal.runners;
2b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
3b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.lang.annotation.Annotation;
4b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.lang.reflect.InvocationTargetException;
5b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.lang.reflect.Method;
6b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.Collections;
7b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.Comparator;
8b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.Iterator;
9b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.List;
10b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
11b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.Description;
12b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.Runner;
13b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.manipulation.Filter;
14b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.manipulation.Filterable;
15b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.manipulation.NoTestsRemainException;
16b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.manipulation.Sortable;
17b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.manipulation.Sorter;
18b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.notification.Failure;
19b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.notification.RunNotifier;
20b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runners.BlockJUnit4ClassRunner;
21b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
22b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot/**
23b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @deprecated Included for backwards compatibility with JUnit 4.4. Will be
24b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot *             removed in the next release. Please use
25b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot *             {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
26b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot *
27b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot *             This may disappear as soon as 1 April 2009
28b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */
29b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot@Deprecated
30b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpublic class JUnit4ClassRunner extends Runner implements Filterable, Sortable {
31b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	private final List<Method> fTestMethods;
32b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	private TestClass fTestClass;
33b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
34b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public JUnit4ClassRunner(Class<?> klass) throws InitializationError {
35b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		fTestClass= new TestClass(klass);
36b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		fTestMethods= getTestMethods();
37b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		validate();
38b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
39b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
40b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected List<Method> getTestMethods() {
41b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return fTestClass.getTestMethods();
42b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
43b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
44b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected void validate() throws InitializationError {
45b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		MethodValidator methodValidator= new MethodValidator(fTestClass);
46b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		methodValidator.validateMethodsForDefaultRunner();
47b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		methodValidator.assertValid();
48b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
49b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
50b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	@Override
51b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public void run(final RunNotifier notifier) {
52b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() {
53b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			public void run() {
54b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot				runMethods(notifier);
55b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			}
56b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		}).runProtected();
57b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
58b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
59b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected void runMethods(final RunNotifier notifier) {
60b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		for (Method method : fTestMethods)
61b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			invokeTestMethod(method, notifier);
62b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
63b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
64b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	@Override
65b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public Description getDescription() {
66b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		Description spec= Description.createSuiteDescription(getName(), classAnnotations());
67b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		List<Method> testMethods= fTestMethods;
68b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		for (Method method : testMethods)
69b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			spec.addChild(methodDescription(method));
70b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return spec;
71b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
72b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
73b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected Annotation[] classAnnotations() {
74b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return fTestClass.getJavaClass().getAnnotations();
75b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
76b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
77b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected String getName() {
78b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return getTestClass().getName();
79b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
80b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
81b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected Object createTest() throws Exception {
82b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return getTestClass().getConstructor().newInstance();
83b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
84b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
85b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected void invokeTestMethod(Method method, RunNotifier notifier) {
86b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		Description description= methodDescription(method);
87b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		Object test;
88b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		try {
89b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			test= createTest();
90b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		} catch (InvocationTargetException e) {
91b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			testAborted(notifier, description, e.getCause());
92b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			return;
93b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		} catch (Exception e) {
94b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			testAborted(notifier, description, e);
95b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			return;
96b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		}
97b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		TestMethod testMethod= wrapMethod(method);
98b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		new MethodRoadie(test, testMethod, notifier, description).run();
99b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
100b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
101b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	private void testAborted(RunNotifier notifier, Description description,
102b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			Throwable e) {
103b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		notifier.fireTestStarted(description);
104b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		notifier.fireTestFailure(new Failure(description, e));
105b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		notifier.fireTestFinished(description);
106b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
107b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
108b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected TestMethod wrapMethod(Method method) {
109b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return new TestMethod(method, fTestClass);
110b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
111b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
112b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected String testName(Method method) {
113b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return method.getName();
114b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
115b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
116b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected Description methodDescription(Method method) {
117b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return Description.createTestDescription(getTestClass().getJavaClass(), testName(method), testAnnotations(method));
118b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
119b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
120b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected Annotation[] testAnnotations(Method method) {
121b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return method.getAnnotations();
122b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
123b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
124b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public void filter(Filter filter) throws NoTestsRemainException {
125b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		for (Iterator<Method> iter= fTestMethods.iterator(); iter.hasNext();) {
126b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			Method method= iter.next();
127b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			if (!filter.shouldRun(methodDescription(method)))
128b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot				iter.remove();
129b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		}
130b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		if (fTestMethods.isEmpty())
131b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			throw new NoTestsRemainException();
132b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
133b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
134b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	public void sort(final Sorter sorter) {
135b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		Collections.sort(fTestMethods, new Comparator<Method>() {
136b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			public int compare(Method o1, Method o2) {
137b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot				return sorter.compare(methodDescription(o1), methodDescription(o2));
138b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot			}
139b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		});
140b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
141b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot
142b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	protected TestClass getTestClass() {
143b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot		return fTestClass;
144b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot	}
145b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot}