KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jester > TestRunnerImpl


1 package jester;
2
3 import java.io.*;
4 import java.lang.reflect.Method JavaDoc;
5
6 import junit.framework.*;
7
8 /**
9  * Taken from JUnit 3.2 - see JUnit license.
10  * Modified by Ivan Moore for use by Jester.
11  *
12  * A command line based tool to run tests.
13  * <pre>
14  * java test.textui.TestRunner TestCaseClass
15  * </pre>
16  * TestRunner expects the name of a TestCase class as argument.
17  * If this class defines a static <code>suite</code> method it
18  * will be invoked and the returned test is run. Otherwise all
19  * the methods starting with "test" having no arguments are run.
20  * <p>
21  * TestRunner prints a trace as the tests are executed followed by a
22  * summary at the end.
23  */

24 public class TestRunnerImpl implements TestListener {
25     static String JavaDoc FAILED = "FAILED";
26     static String JavaDoc PASSED = "PASSED";
27     static long ONE_HOUR = 1 * 60 * 60 * 1000;
28     static long TEN_SECONDS = 10 * 1000;
29     static long MINIMUM_TEST_TIMEOUT = TEN_SECONDS;
30     static long TEST_TIMEOUT_IF_NONE_SET = ONE_HOUR;
31
32     public TestRunnerImpl() {
33     }
34
35     public synchronized void addError(Test test, Throwable JavaDoc t) {
36         print(FAILED);
37         System.exit(0);
38     }
39
40     public synchronized void addFailure(Test test, AssertionFailedError t) {
41         print(FAILED);
42         System.exit(0);
43     }
44     /**
45      * Creates the TestResult to be used for the test run.
46      */

47     protected TestResult createTestResult() {
48         return new TestResult();
49     }
50
51     public void endTest(Test test) {
52     }
53
54     /**
55      * main entry point.
56      */

57     public static void main(String JavaDoc args[]) {
58         final long startTime = System.currentTimeMillis();
59         boolean timeoutFileNeedsWriting = false;
60         long timeout = 0;
61         try {
62             timeout = Math.max(readTestsTimeout() * 2, MINIMUM_TEST_TIMEOUT);
63         } catch (IOException ex) {
64             timeoutFileNeedsWriting = true;
65             timeout = TEST_TIMEOUT_IF_NONE_SET;
66         }
67         final long testsTimeout = timeout;
68         final long delayBeforeCheckingIfTestsTakingTooLong = 1000;
69         Runnable JavaDoc timeoutChecker = new Runnable JavaDoc() {
70             public void run() {
71                 while (true) {
72                     if (System.currentTimeMillis() > startTime + testsTimeout) {
73                         System.exit(0);
74                     }
75                     try {
76                         Thread.sleep(delayBeforeCheckingIfTestsTakingTooLong);
77                     } catch (InterruptedException JavaDoc ex) {
78                     }
79                 }
80             }
81         };
82         new Thread JavaDoc(timeoutChecker).start();
83         TestRunnerImpl aTestRunner = new TestRunnerImpl();
84         aTestRunner.start(args);
85         if (timeoutFileNeedsWriting) {
86             writeTestsTimeout(System.currentTimeMillis() - startTime);
87         }
88         System.exit(0);
89     }
90     /**
91      * Prints failures to the standard output
92      */

93     public synchronized void print(TestResult result) {
94         //don't care about details for the moment - just the bottom line
95
print(result.wasSuccessful() ? PASSED : FAILED);
96     }
97
98     /**
99      * Starts a test run. Analyzes the command line arguments
100      * and runs the given test suite.
101      */

102     protected void start(String JavaDoc args[]) {
103         try {
104             Class JavaDoc testClass = getTestCaseClass(args);
105             Test suite = getTestSuite(testClass);
106             doRun(suite);
107         } catch (Exception JavaDoc e) {
108             println("Could not create and run test suite");
109             System.exit(-1);
110         }
111     }
112     
113     private Test getTestSuite(Class JavaDoc testClass) {
114         try {
115             Method JavaDoc suiteMethod = testClass.getMethod("suite", new Class JavaDoc[0]);
116             try {
117                 return (Test) suiteMethod.invoke(null, new Class JavaDoc[0]);
118                 // static method
119
} catch (Exception JavaDoc e) {
120                 println("Could not invoke the suite() method");
121                 System.exit(-1);
122                 return null;//can never happen because of System.exit(-1); but compiler doesn't realise that
123
}
124         } catch (Exception JavaDoc e) {
125             // try to extract a test suite automatically
126
return new TestSuite(testClass);
127         }
128     }
129
130     private Class JavaDoc getTestCaseClass(String JavaDoc[] args){
131         String JavaDoc testCaseName = getTestCaseName(args);
132         
133         try {
134             return Class.forName(testCaseName);
135         } catch (Exception JavaDoc e) {
136             println("Suite class \"" + testCaseName + "\" not found");
137             System.exit(-1);
138             return null;//can never happen because of System.exit(-1); but compiler doesn't realise that
139
}
140     }
141     
142     private String JavaDoc getTestCaseName(String JavaDoc[] args) {
143         String JavaDoc testCaseName = "";
144
145         for (int i = 0; i < args.length; i++) {
146             if (args[i].equals("-v"))
147                 println("JUnit 3.2 by Kent Beck and Erich Gamma");
148             else
149                 testCaseName = args[i];
150         }
151
152         if (testCaseName.equals("")) {
153             println("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class");
154             System.exit(-1);
155         }
156         
157         return testCaseName;
158     }
159
160     public synchronized void startTest(Test test) {
161         print(".");
162     }
163     
164     private void print(String JavaDoc string) {
165         System.out.print(string);
166     }
167     
168     private void println(String JavaDoc string) {
169         System.out.println(string);
170     }
171     
172     private void println() {
173         System.out.println();
174     }
175
176     protected void doRun(Test suite) {
177         TestResult result = createTestResult();
178         result.addListener(this);
179         long startTime = System.currentTimeMillis();
180         suite.run(result);
181         long endTime = System.currentTimeMillis();
182         long runTime = endTime - startTime;
183         println();
184         println("Time: " + runTime);
185         print(result);
186
187         println();
188     }
189
190     private static long readTestsTimeout() throws IOException {
191         FileReader timeoutFile = new FileReader(TestTester.TIMEOUT_FILENAME);
192         BufferedReader in = new BufferedReader(timeoutFile);
193         String JavaDoc firstLine = in.readLine();
194         timeoutFile.close();
195         return Long.parseLong(firstLine);
196     }
197
198     private static void writeTestsTimeout(long timeToRunTests) {
199         try {
200             FileWriter timeoutFile = new FileWriter(TestTester.TIMEOUT_FILENAME);
201             timeoutFile.write(Long.toString(timeToRunTests));
202             timeoutFile.close();
203         } catch (IOException notMuchYouCanDo) {
204         }
205     }
206 }
Popular Tags