|
1 package org.junit.internal.runners; |
|
2 |
|
3 import java.util.ArrayList; |
|
4 import java.util.Collections; |
|
5 import java.util.Comparator; |
|
6 import java.util.Iterator; |
|
7 import java.util.List; |
|
8 |
|
9 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, Sortable { |
|
19 private final List<Runner> fRunners= new ArrayList<Runner>(); |
|
20 private final String fName; |
|
21 |
|
22 public CompositeRunner(String name) { |
|
23 fName= name; |
|
24 } |
|
25 |
|
26 @Override |
|
27 public void run(RunNotifier notifier) { |
|
28 for (Runner each : fRunners) |
|
29 each.run(notifier); |
|
30 } |
|
31 |
|
32 @Override |
|
33 public Description getDescription() { |
|
34 Description spec= Description.createSuiteDescription(fName); |
|
35 for (Runner runner : fRunners) |
|
36 spec.addChild(runner.getDescription()); |
|
37 return spec; |
|
38 } |
|
39 |
|
40 public List<Runner> getRunners() { |
|
41 return fRunners; |
|
42 } |
|
43 |
|
44 public void addAll(List<? extends Runner> runners) { |
|
45 fRunners.addAll(runners); |
|
46 } |
|
47 |
|
48 public void add(Runner runner) { |
|
49 fRunners.add(runner); |
|
50 } |
|
51 |
|
52 public void filter(Filter filter) throws NoTestsRemainException { |
|
53 for (Iterator<Runner> iter= fRunners.iterator(); iter.hasNext();) { |
|
54 Runner runner= iter.next(); |
|
55 if (filter.shouldRun(runner.getDescription())) |
|
56 filter.apply(runner); |
|
57 else |
|
58 iter.remove(); |
|
59 } |
|
60 } |
|
61 |
|
62 protected String getName() { |
|
63 return fName; |
|
64 } |
|
65 |
|
66 public void sort(final Sorter sorter) { |
|
67 Collections.sort(fRunners, new Comparator<Runner>() { |
|
68 public int compare(Runner o1, Runner o2) { |
|
69 return sorter.compare(o1.getDescription(), o2.getDescription()); |
|
70 } |
|
71 }); |
|
72 for (Runner each : fRunners) |
|
73 sorter.apply(each); |
|
74 } |
|
75 } |