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