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