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