01: package org.junit.internal.runners;
02:
03: import java.util.ArrayList;
04: import java.util.Collections;
05: import java.util.Comparator;
06: import java.util.Iterator;
07: import java.util.List;
08:
09: import org.junit.runner.Description;
10: import org.junit.runner.Runner;
11: import org.junit.runner.manipulation.Filter;
12: import org.junit.runner.manipulation.Filterable;
13: import org.junit.runner.manipulation.NoTestsRemainException;
14: import org.junit.runner.manipulation.Sortable;
15: import org.junit.runner.manipulation.Sorter;
16: import org.junit.runner.notification.RunNotifier;
17:
18: public class CompositeRunner extends Runner implements Filterable,
19: Sortable {
20: private final List<Runner> fRunners = new ArrayList<Runner>();
21: private final String fName;
22:
23: public CompositeRunner(String name) {
24: fName = name;
25: }
26:
27: @Override
28: public void run(RunNotifier notifier) {
29: runChildren(notifier);
30: }
31:
32: protected void runChildren(RunNotifier notifier) {
33: for (Runner each : fRunners)
34: each.run(notifier);
35: }
36:
37: @Override
38: public Description getDescription() {
39: Description spec = Description.createSuiteDescription(fName);
40: for (Runner runner : fRunners)
41: spec.addChild(runner.getDescription());
42: return spec;
43: }
44:
45: public List<Runner> getRunners() {
46: return fRunners;
47: }
48:
49: public void addAll(List<? extends Runner> runners) {
50: fRunners.addAll(runners);
51: }
52:
53: public void add(Runner runner) {
54: fRunners.add(runner);
55: }
56:
57: public void filter(Filter filter) throws NoTestsRemainException {
58: for (Iterator<Runner> iter = fRunners.iterator(); iter
59: .hasNext();) {
60: Runner runner = iter.next();
61: if (filter.shouldRun(runner.getDescription()))
62: filter.apply(runner);
63: else
64: iter.remove();
65: }
66: }
67:
68: protected String getName() {
69: return fName;
70: }
71:
72: public void sort(final Sorter sorter) {
73: Collections.sort(fRunners, new Comparator<Runner>() {
74: public int compare(Runner o1, Runner o2) {
75: return sorter.compare(o1.getDescription(), o2
76: .getDescription());
77: }
78: });
79: for (Runner each : fRunners)
80: sorter.apply(each);
81: }
82: }
|