|
1 package org.junit.internal.runners; |
|
2 |
|
3 import java.lang.reflect.InvocationTargetException; |
|
4 import java.lang.reflect.Method; |
|
5 import java.util.Collections; |
|
6 import java.util.Comparator; |
|
7 import java.util.Iterator; |
|
8 import java.util.List; |
|
9 |
|
10 import org.junit.Test; |
|
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.RunNotifier; |
|
19 |
|
20 public class TestClassMethodsRunner extends Runner implements Filterable, Sortable { |
|
21 private final List<Method> fTestMethods; |
|
22 private final Class<?> fTestClass; |
|
23 |
|
24 // This assumes that some containing runner will perform validation of the test methods |
|
25 public TestClassMethodsRunner(Class<?> klass) { |
|
26 fTestClass= klass; |
|
27 fTestMethods= new TestIntrospector(getTestClass()).getTestMethods(Test.class); |
|
28 } |
|
29 |
|
30 @Override |
|
31 public void run(RunNotifier notifier) { |
|
32 if (fTestMethods.isEmpty()) |
|
33 notifier.testAborted(getDescription(), new Exception("No runnable methods")); |
|
34 for (Method method : fTestMethods) |
|
35 invokeTestMethod(method, notifier); |
|
36 } |
|
37 |
|
38 @Override |
|
39 public Description getDescription() { |
|
40 Description spec= Description.createSuiteDescription(getName()); |
|
41 List<Method> testMethods= fTestMethods; |
|
42 for (Method method : testMethods) |
|
43 spec.addChild(methodDescription(method)); |
|
44 return spec; |
|
45 } |
|
46 |
|
47 protected String getName() { |
|
48 return getTestClass().getName(); |
|
49 } |
|
50 |
|
51 protected Object createTest() throws Exception { |
|
52 return getTestClass().getConstructor().newInstance(); |
|
53 } |
|
54 |
|
55 protected void invokeTestMethod(Method method, RunNotifier notifier) { |
|
56 Object test; |
|
57 try { |
|
58 test= createTest(); |
|
59 } catch (InvocationTargetException e) { |
|
60 notifier.testAborted(methodDescription(method), e.getCause()); |
|
61 return; |
|
62 } catch (Exception e) { |
|
63 notifier.testAborted(methodDescription(method), e); |
|
64 return; |
|
65 } |
|
66 createMethodRunner(test, method, notifier).run(); |
|
67 } |
|
68 |
|
69 protected TestMethodRunner createMethodRunner(Object test, Method method, RunNotifier notifier) { |
|
70 return new TestMethodRunner(test, method, notifier, methodDescription(method)); |
|
71 } |
|
72 |
|
73 protected String testName(Method method) { |
|
74 return method.getName(); |
|
75 } |
|
76 |
|
77 protected Description methodDescription(Method method) { |
|
78 return Description.createTestDescription(getTestClass(), testName(method)); |
|
79 } |
|
80 |
|
81 public void filter(Filter filter) throws NoTestsRemainException { |
|
82 for (Iterator<Method> iter= fTestMethods.iterator(); iter.hasNext();) { |
|
83 Method method= iter.next(); |
|
84 if (!filter.shouldRun(methodDescription(method))) |
|
85 iter.remove(); |
|
86 } |
|
87 if (fTestMethods.isEmpty()) |
|
88 throw new NoTestsRemainException(); |
|
89 } |
|
90 |
|
91 public void sort(final Sorter sorter) { |
|
92 Collections.sort(fTestMethods, new Comparator<Method>() { |
|
93 public int compare(Method o1, Method o2) { |
|
94 return sorter.compare(methodDescription(o1), methodDescription(o2)); |
|
95 } |
|
96 }); |
|
97 } |
|
98 |
|
99 protected Class<?> getTestClass() { |
|
100 return fTestClass; |
|
101 } |
|
102 } |