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