01: package com.clarkware.junitperf;
02:
03: import junit.framework.AssertionFailedError;
04: import junit.framework.Test;
05: import junit.framework.TestResult;
06:
07: /**
08: * The <code>ThreadedTestGroup</code> is a <code>ThreadGroup</code>
09: * that catches and handles exceptions thrown by threads created
10: * and started by <code>ThreadedTest</code> instances.
11: * <p>
12: * If a thread managed by a <code>ThreadedTestGroup</code> throws
13: * an uncaught exception, then the exception is added to the current
14: * test's results and all other threads are immediately interrupted.
15: * </p>
16: *
17: * @author Ervin Varga
18: * @author <b>Mike Clark</b>
19: * @author Clarkware Consulting, Inc.
20: */
21:
22: public class ThreadedTestGroup extends ThreadGroup {
23:
24: private final Test test;
25: private TestResult testResult;
26:
27: /**
28: * Constructs a <code>ThreadedTestGroup</code> for the
29: * specified test.
30: *
31: * @param test Current test.
32: */
33: public ThreadedTestGroup(Test test) {
34: super ("ThreadedTestGroup");
35: this .test = test;
36: }
37:
38: /**
39: * Sets the current test result.
40: *
41: * @param result Test result.
42: */
43: public void setTestResult(TestResult result) {
44: testResult = result;
45: }
46:
47: /**
48: * Called when a thread in this thread group stops because of
49: * an uncaught exception.
50: * <p>
51: * If the uncaught exception is a <code>ThreadDeath</code>,
52: * then it is ignored. If the uncaught exception is an
53: * <code>AssertionFailedError</code>, then a failure
54: * is added to the current test's result. Otherwise, an
55: * error is added to the current test's result.
56: *
57: * @param t Originating thread.
58: * @param e Uncaught exception.
59: */
60: public void uncaughtException(Thread t, Throwable e) {
61:
62: if (e instanceof ThreadDeath) {
63: return;
64: }
65:
66: if (e instanceof AssertionFailedError) {
67: testResult.addFailure(test, (AssertionFailedError) e);
68: } else {
69: testResult.addError(test, e);
70: }
71:
72: super.interrupt();
73: }
74: }
|