KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.junit.runner;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import junit.runner.Version;
7 import org.junit.internal.runners.OldTestClassRunner;
8 import org.junit.internal.runners.TextListener;
9 import org.junit.runner.notification.Failure;
10 import org.junit.runner.notification.RunListener;
11 import org.junit.runner.notification.RunNotifier;
12
13 /**
14  * <code>JUnitCore</code> is a facade for running tests. It supports running JUnit 4 tests,
15  * JUnit 3.8.x tests, and mixtures. To run tests from the command line, run
16  * <code>java org.junit.runner.JUnitCore TestClass1 TestClass2 ...</code>.
17  * For one-shot test runs, use the static method {@link #runClasses(Class[])}.
18  * If you want to add special listeners,
19  * create an instance of {@link org.junit.runner.JUnitCore} first and use it to run the tests.
20  *
21  * @see org.junit.runner.Result
22  * @see org.junit.runner.notification.RunListener
23  * @see org.junit.runner.Request
24  */

25 public class JUnitCore {
26     
27     private RunNotifier fNotifier;
28
29     /**
30      * Create a new <code>JUnitCore</code> to run tests.
31      */

32     public JUnitCore() {
33         fNotifier= new RunNotifier();
34     }
35
36     /**
37      * Run the tests contained in the classes named in the <code>args</code>.
38      * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
39      * Write feedback while tests are running and write
40      * stack traces for all failed tests after the tests all complete.
41      * @param args names of classes in which to find tests to run
42      */

43     public static void main(String JavaDoc... args) {
44         Result result= new JUnitCore().runMain(args);
45         killAllThreads(result);
46     }
47
48     private static void killAllThreads(Result result) {
49         System.exit(result.wasSuccessful() ? 0 : 1);
50     }
51     
52     /**
53      * Run the tests contained in <code>classes</code>. Write feedback while the tests
54      * are running and write stack traces for all failed tests after all tests complete. This is
55      * similar to {@link #main(String[])}, but intended to be used programmatically.
56      * @param classes Classes in which to find tests
57      * @return a {@link Result} describing the details of the test run and the failed tests.
58      */

59     public static Result runClasses(Class JavaDoc<?>... classes) {
60         return new JUnitCore().run(classes);
61     }
62     
63     /**
64      * Do not use. Testing purposes only.
65      */

66     public Result runMain(String JavaDoc... args) {
67         System.out.println("JUnit version " + Version.id());
68         List JavaDoc<Class JavaDoc<?>> classes= new ArrayList JavaDoc<Class JavaDoc<?>>();
69         List JavaDoc<Failure> missingClasses= new ArrayList JavaDoc<Failure>();
70         for (String JavaDoc each : args)
71             try {
72                 classes.add(Class.forName(each));
73             } catch (ClassNotFoundException JavaDoc e) {
74                 System.out.println("Could not find class: " + each);
75                 Description description= Description.createSuiteDescription(each);
76                 Failure failure= new Failure(description, e);
77                 missingClasses.add(failure);
78             }
79         RunListener listener= new TextListener();
80         addListener(listener);
81         Result result= run(classes.toArray(new Class JavaDoc[0]));
82         for (Failure each : missingClasses)
83             result.getFailures().add(each);
84         return result;
85     }
86
87     /**
88      * @return the version number of this release
89      */

90     public String JavaDoc getVersion() {
91         return Version.id();
92     }
93     
94     /**
95      * Run all the tests in <code>classes</code>.
96      * @param classes the classes containing tests
97      * @return a {@link Result} describing the details of the test run and the failed tests.
98      */

99     public Result run(Class JavaDoc<?>... classes) {
100         return run(Request.classes("All", classes));
101     }
102
103     /**
104      * Run all the tests contained in <code>request</code>.
105      * @param request the request describing tests
106      * @return a {@link Result} describing the details of the test run and the failed tests.
107      */

108     public Result run(Request request) {
109         return run(request.getRunner());
110     }
111
112     /**
113      * Run all the tests contained in JUnit 3.8.x <code>test</code>. Here for backward compatibility.
114      * @param test the old-style test
115      * @return a {@link Result} describing the details of the test run and the failed tests.
116      */

117     public Result run(junit.framework.Test test) {
118         return run(new OldTestClassRunner(test));
119     }
120     
121     /**
122      * Do not use. Testing purposes only.
123      */

124     public Result run(Runner runner) {
125         Result result= new Result();
126         RunListener listener= result.createListener();
127         addFirstListener(listener);
128         try {
129             fNotifier.fireTestRunStarted(runner.getDescription());
130             runner.run(fNotifier);
131             fNotifier.fireTestRunFinished(result);
132         } finally {
133             removeListener(listener);
134         }
135         return result;
136     }
137     
138     private void addFirstListener(RunListener listener) {
139         fNotifier.addFirstListener(listener);
140     }
141     
142
143     /**
144      * Add a listener to be notified as the tests run.
145      * @param listener the listener to add
146      * @see org.junit.runner.notification.RunListener
147      */

148     public void addListener(RunListener listener) {
149         fNotifier.addListener(listener);
150     }
151
152     /**
153      * Remove a listener.
154      * @param listener the listener to remove
155      */

156     public void removeListener(RunListener listener) {
157         fNotifier.removeListener(listener);
158     }
159 }
160
Popular Tags