KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > runner > Request


1 package org.junit.runner;
2
3 import java.util.Comparator JavaDoc;
4
5 import org.junit.internal.requests.ClassRequest;
6 import org.junit.internal.requests.ClassesRequest;
7 import org.junit.internal.requests.ErrorReportingRequest;
8 import org.junit.internal.requests.FilterRequest;
9 import org.junit.internal.requests.SortingRequest;
10 import org.junit.runner.manipulation.Filter;
11
12 /**
13  * <p>A <code>Request</code> is an abstract description of tests to be run. Older versions of
14  * JUnit did not need such a concept--tests to be run were described either by classes containing
15  * tests or a tree of {@link org.junit.Test}s. However, we want to support filtering and sorting,
16  * so we need a more abstract specification than the tests themselves and a richer
17  * specification than just the classes.</p>
18  *
19  * <p>The flow when JUnit runs tests is that a <code>Request</code> specifies some tests to be run ->
20  * a {@link org.junit.runner.Runner} is created for each class implied by the <code>Request</code> ->
21  * the {@link org.junit.runner.Runner} returns a detailed {@link org.junit.runner.Description}
22  * which is a tree structure of the tests to be run.</p>
23  */

24 public abstract class Request {
25     /**
26      * Create a <code>Request</code> that, when processed, will run a single test.
27      * This is done by filtering out all other tests. This method is used to support rerunning
28      * single tests.
29      * @param clazz the class of the test
30      * @param methodName the name of the test
31      * @return a <code>Request</code> that will cause a single test be run
32      */

33     public static Request method(Class JavaDoc<?> clazz, String JavaDoc methodName) {
34         Description method= Description.createTestDescription(clazz, methodName);
35         return Request.aClass(clazz).filterWith(method);
36     }
37
38     /**
39      * Create a <code>Request</code> that, when processed, will run all the tests
40      * in a class. The odd name is necessary because <code>class</code> is a reserved word.
41      * @param clazz the class containing the tests
42      * @return a <code>Request</code> that will cause all tests in the class to be run
43      */

44     public static Request aClass(Class JavaDoc<?> clazz) {
45         return new ClassRequest(clazz);
46     }
47
48     /**
49      * Create a <code>Request</code> that, when processed, will run all the tests
50      * in a set of classes.
51      * @param collectionName a name to identify this suite of tests
52      * @param classes the classes containing the tests
53      * @return a <code>Request</code> that will cause all tests in the classes to be run
54      */

55     public static Request classes(String JavaDoc collectionName, Class JavaDoc<?>... classes) {
56         return new ClassesRequest(collectionName, classes);
57     }
58
59     public static Request errorReport(Class JavaDoc<?> klass, Throwable JavaDoc cause) {
60         return new ErrorReportingRequest(klass, cause);
61     }
62
63     /**
64      * Returns a {@link Runner} for this Request
65      * @return corresponding {@link Runner} for this Request
66      */

67     public abstract Runner getRunner();
68
69     /**
70      * Returns a Request that only contains those tests that should run when
71      * <code>filter</code> is applied
72      * @param filter The {@link Filter} to apply to this Request
73      * @return the filtered Request
74      */

75     public Request filterWith(Filter filter) {
76         return new FilterRequest(this, filter);
77     }
78
79     /**
80      * Returns a Request that only runs contains tests whose {@link Description}
81      * equals <code>desiredDescription</code>
82      * @param desiredDescription {@link Description} of those tests that should be run
83      * @return the filtered Request
84      */

85     public Request filterWith(final Description desiredDescription) {
86         return filterWith(new Filter() {
87             @Override JavaDoc
88             public boolean shouldRun(Description description) {
89                 // TODO: test for equality even if we have children?
90
if (description.isTest())
91                     return desiredDescription.equals(description);
92                 for (Description each : description.getChildren())
93                     if (shouldRun(each))
94                         return true;
95                 return false;
96             }
97
98             @Override JavaDoc
99             public String JavaDoc describe() {
100                 return String.format("Method %s", desiredDescription.getDisplayName());
101             }
102         });
103     }
104
105     /**
106      * Returns a Request whose Tests can be run in a certain order, defined by
107      * <code>comparator</code>
108      *
109      * For example, here is code to run a test suite in alphabetical order:
110      *
111      * <pre>
112     private static Comparator<Description> forward() {
113         return new Comparator<Description>() {
114             public int compare(Description o1, Description o2) {
115                 return o1.getDisplayName().compareTo(o2.getDisplayName());
116             }
117         };
118     }
119     
120     public static main() {
121         new JUnitCore().run(Request.aClass(AllTests.class).sortWith(forward()));
122     }
123      * </pre>
124      *
125      * @param comparator definition of the order of the tests in this Request
126      * @return a Request with ordered Tests
127      */

128     public Request sortWith(Comparator JavaDoc<Description> comparator) {
129         return new SortingRequest(this, comparator);
130     }
131
132     public static Request classWithoutSuiteMethod(Class JavaDoc<?> newTestClass) {
133         return new ClassRequest(newTestClass, false);
134     }
135 }
136
Popular Tags