KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > internal > runners > CompositeRunner


1 package org.junit.internal.runners;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Comparator JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
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 JavaDoc<Runner> fRunners= new ArrayList JavaDoc<Runner>();
20     private final String JavaDoc fName;
21     
22     public CompositeRunner(String JavaDoc name) {
23         fName= name;
24     }
25     
26     @Override JavaDoc
27     public void run(RunNotifier notifier) {
28         for (Runner each : fRunners)
29             each.run(notifier);
30     }
31
32     @Override JavaDoc
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 JavaDoc<Runner> getRunners() {
41         return fRunners;
42     }
43
44     public void addAll(List JavaDoc<? 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 JavaDoc<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 JavaDoc getName() {
63         return fName;
64     }
65
66     public void sort(final Sorter sorter) {
67         Collections.sort(fRunners, new Comparator JavaDoc<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 }
76
Popular Tags