01: package junit.extensions;
02:
03: import junit.framework.Test;
04: import junit.framework.TestCase;
05: import junit.framework.TestResult;
06: import junit.framework.TestSuite;
07:
08: /**
09: * A TestSuite for active Tests. It runs each
10: * test in a separate thread and waits until all
11: * threads have terminated.
12: * -- Aarhus Radisson Scandinavian Center 11th floor
13: */
14: public class ActiveTestSuite extends TestSuite {
15: private volatile int fActiveTestDeathCount;
16:
17: public ActiveTestSuite() {
18: }
19:
20: public ActiveTestSuite(Class<? extends TestCase> theClass) {
21: super (theClass);
22: }
23:
24: public ActiveTestSuite(String name) {
25: super (name);
26: }
27:
28: public ActiveTestSuite(Class<? extends TestCase> theClass,
29: String name) {
30: super (theClass, name);
31: }
32:
33: @Override
34: public void run(TestResult result) {
35: fActiveTestDeathCount = 0;
36: super .run(result);
37: waitUntilFinished();
38: }
39:
40: @Override
41: public void runTest(final Test test, final TestResult result) {
42: Thread t = new Thread() {
43: @Override
44: public void run() {
45: try {
46: // inlined due to limitation in VA/Java
47: //ActiveTestSuite.super.runTest(test, result);
48: test.run(result);
49: } finally {
50: ActiveTestSuite.this .runFinished();
51: }
52: }
53: };
54: t.start();
55: }
56:
57: synchronized void waitUntilFinished() {
58: while (fActiveTestDeathCount < testCount()) {
59: try {
60: wait();
61: } catch (InterruptedException e) {
62: return; // ignore
63: }
64: }
65: }
66:
67: synchronized public void runFinished() {
68: fActiveTestDeathCount++;
69: notifyAll();
70: }
71: }
|