1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: JUnitTestRunner.java,v 1.3 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.Enumeration;
48
49 import org.apache.log4j.Category;
50
51 import junit.framework.AssertionFailedError;
52 import junit.framework.Test;
53 import junit.framework.TestFailure;
54 import junit.framework.TestResult;
55 import junit.framework.TestSuite;
56 import junit.runner.BaseTestRunner;
57 import junit.runner.StandardTestSuiteLoader;
58 import junit.runner.TestSuiteLoader;
59
60
61 /***
62 * JUnit test runner which logs a trace as the tests are executed,
63 * followed by a summary at the end.
64 *
65 * @version $Revision: 1.3 $ $Date: 2004/01/31 13:44:24 $
66 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67 */
68 public class JUnitTestRunner extends BaseTestRunner {
69
70 /***
71 * The summary logger
72 */
73 private static final Category SUMMARY_LOG = Category.getInstance(
74 JUnitTestRunner.class.getName() + ".Summary");
75
76 /***
77 * The trace logger
78 */
79 private static final Category TRACE_LOG = Category.getInstance(
80 JUnitTestRunner.class.getName() + ".Trace");
81
82 /***
83 * The detail logger
84 */
85 private static final Category DETAIL_LOG = Category.getInstance(
86 JUnitTestRunner.class.getName() + ".Detail");
87
88
89 /***
90 * Constructs a <code>JUnitTestRunner</code>
91 */
92 public JUnitTestRunner() {
93 }
94
95 /***
96 * Always use the StandardTestSuiteLoader. Overridden from
97 * BaseTestRunner.
98 *
99 * @return a new <code>StandardTestSuiteLoader</code>
100 */
101 public TestSuiteLoader getLoader() {
102 return new StandardTestSuiteLoader();
103 }
104
105 /***
106 * Notifies the start of a test
107 *
108 * @param testName the name of the test
109 */
110 public void testStarted(String testName) {
111 // no-op
112 }
113
114 /***
115 * Notifies the end of a test
116 *
117 * @param testName the name of the test
118 */
119 public void testEnded(String testName) {
120 // no-op
121 }
122
123 /***
124 * Notifies the failure of a test
125 *
126 * @param status the status
127 * @param test the test
128 * @param error the error
129 */
130 public void testFailed(int status, Test test, Throwable error) {
131 // no-op
132 }
133
134 /***
135 * Run a test suite
136 *
137 * @param suite the suite to run
138 * @return the result of the run
139 */
140 public TestResult doRun(Test suite) {
141 TestResult result = createTestResult();
142 result.addListener(this);
143 long startTime = System.currentTimeMillis();
144 suite.run(result);
145 long endTime = System.currentTimeMillis();
146 long runTime = endTime - startTime;
147 SUMMARY_LOG.info("Time: " + elapsedTimeAsString(runTime));
148 log(result);
149 return result;
150 }
151
152 /***
153 * Logs the start of a test
154 *
155 * @param test the test
156 */
157 public void startTest(Test test) {
158 TRACE_LOG.info("Running: " + test);
159 }
160
161 /***
162 * Logs a test error
163 *
164 * @param test the test
165 * @param error the error
166 */
167 public void addError(Test test, Throwable error) {
168 TRACE_LOG.info("Error: " + test);
169 }
170
171 /***
172 * Logs a test failure
173 *
174 * @param test the test
175 * @param error the error
176 */
177 public void addFailure(Test test, AssertionFailedError error) {
178 TRACE_LOG.info("Failed: " + test);
179 }
180
181 /***
182 * Logs the result of the test
183 *
184 * @param result the test result
185 */
186 public synchronized void log(TestResult result) {
187 logErrors(result);
188 logFailures(result);
189 logHeader(result);
190 }
191
192 /***
193 * Logs any errors
194 *
195 * @param result the test result
196 */
197 public void logErrors(TestResult result) {
198 if (result.errorCount() != 0) {
199 if (result.errorCount() == 1) {
200 DETAIL_LOG.error("There was " + result.errorCount()
201 + " error:");
202 } else {
203 DETAIL_LOG.error("There were " + result.errorCount()
204 + " errors:");
205 }
206 int i = 1;
207 for (Enumeration e = result.errors(); e.hasMoreElements(); i++) {
208 TestFailure failure = (TestFailure) e.nextElement();
209 DETAIL_LOG.error(i + ") " + failure.failedTest(),
210 failure.thrownException());
211 }
212 }
213 }
214
215 /***
216 * Log any failures
217 *
218 * @param result the test result
219 */
220 public void logFailures(TestResult result) {
221 if (result.failureCount() != 0) {
222 if (result.failureCount() == 1) {
223 DETAIL_LOG.error("There was " + result.failureCount()
224 + " failure:");
225 } else {
226 DETAIL_LOG.error("There were " + result.failureCount()
227 + " failures:");
228 int i = 1;
229 for (Enumeration e = result.failures(); e.hasMoreElements();
230 ++i) {
231 TestFailure failure = (TestFailure) e.nextElement();
232 DETAIL_LOG.error(i + ") " + failure.failedTest(),
233 failure.thrownException());
234 }
235 }
236 }
237 }
238
239 /***
240 * Logs the header of the report
241 *
242 * @param result the test result
243 */
244 public void logHeader(TestResult result) {
245 if (result.wasSuccessful()) {
246 SUMMARY_LOG.info("OK (" + result.runCount() + " tests)");
247 } else {
248 SUMMARY_LOG.info("FAILURES!!!");
249 SUMMARY_LOG.info("Tests run: " + result.runCount()
250 + ", Failures: " + result.failureCount()
251 + ", Errors: " + result.errorCount());
252 }
253 }
254
255 /***
256 * Runs a suite extracted from a TestCase subclass.
257 *
258 * @param testClass the class of the test case
259 */
260 public static void run(Class testClass) {
261 run(new TestSuite(testClass));
262 }
263
264 /***
265 * Runs a suite
266 *
267 * @param suite the suite to run
268 */
269 public static void run(Test suite) {
270 JUnitTestRunner runner = new JUnitTestRunner();
271 runner.doRun(suite);
272 }
273
274 /***
275 * Creates the TestResult to be used for the test run.
276 *
277 * @return a new <code>TestResult</code>
278 */
279 protected TestResult createTestResult() {
280 return new TestResult();
281 }
282
283 /***
284 * Notifies of overall test failure, and exits
285 *
286 * @param message the failure message
287 */
288 protected void runFailed(String message) {
289 DETAIL_LOG.error(message);
290 System.exit(-1);
291 }
292
293 }
This page was automatically generated by Maven