01: package junit.framework;
02:
03: import java.util.List;
04:
05: import org.junit.Ignore;
06: import org.junit.runner.Description;
07: import org.junit.runner.Request;
08: import org.junit.runner.Runner;
09: import org.junit.runner.manipulation.Filter;
10: import org.junit.runner.manipulation.NoTestsRemainException;
11: import org.junit.runner.manipulation.Sorter;
12:
13: public class JUnit4TestAdapter implements Test {
14: private final Class<?> fNewTestClass;
15:
16: private Runner fRunner;
17:
18: private JUnit4TestAdapterCache fCache;
19:
20: public JUnit4TestAdapter(Class<?> newTestClass) {
21: this (newTestClass, JUnit4TestAdapterCache.getDefault());
22: }
23:
24: public JUnit4TestAdapter(final Class<?> newTestClass,
25: JUnit4TestAdapterCache cache) {
26: fCache = cache;
27: fNewTestClass = newTestClass;
28: fRunner = Request.classWithoutSuiteMethod(newTestClass)
29: .getRunner();
30: }
31:
32: public int countTestCases() {
33: return fRunner.testCount();
34: }
35:
36: public void run(TestResult result) {
37: fRunner.run(fCache.getNotifier(result, this ));
38: }
39:
40: // reflective interface for Eclipse
41: public List<Test> getTests() {
42: return fCache.asTestList(getDescription());
43: }
44:
45: // reflective interface for Eclipse
46: public Class<?> getTestClass() {
47: return fNewTestClass;
48: }
49:
50: public Description getDescription() {
51: Description description = fRunner.getDescription();
52: return removeIgnored(description);
53: }
54:
55: private Description removeIgnored(Description description) {
56: if (isIgnored(description))
57: return Description.EMPTY;
58: Description result = description.childlessCopy();
59: for (Description each : description.getChildren()) {
60: Description child = removeIgnored(each);
61: if (!child.isEmpty())
62: result.addChild(child);
63: }
64: return result;
65: }
66:
67: private boolean isIgnored(Description description) {
68: return description.getAnnotation(Ignore.class) != null;
69: }
70:
71: @Override
72: public String toString() {
73: return fNewTestClass.getName();
74: }
75:
76: public void filter(Filter filter) throws NoTestsRemainException {
77: filter.apply(fRunner);
78: }
79:
80: public void sort(Sorter sorter) {
81: sorter.apply(fRunner);
82: }
83: }
|