KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > extensions > ActiveTestSuite


1 package junit.extensions;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestResult;
6 import junit.framework.TestSuite;
7
8 /**
9  * 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 JavaDoc<? extends TestCase> theClass) {
21         super(theClass);
22     }
23     
24     public ActiveTestSuite(String JavaDoc name) {
25         super (name);
26     }
27     
28     public ActiveTestSuite(Class JavaDoc<? extends TestCase> theClass, String JavaDoc name) {
29         super(theClass, name);
30     }
31     
32     @Override JavaDoc
33     public void run(TestResult result) {
34         fActiveTestDeathCount= 0;
35         super.run(result);
36         waitUntilFinished();
37     }
38     
39     @Override JavaDoc
40     public void runTest(final Test test, final TestResult result) {
41         Thread JavaDoc t= new Thread JavaDoc() {
42             @Override JavaDoc
43             public void run() {
44                 try {
45                     // inlined due to limitation in VA/Java
46
//ActiveTestSuite.super.runTest(test, result);
47
test.run(result);
48                 } finally {
49                     ActiveTestSuite.this.runFinished();
50                 }
51             }
52         };
53         t.start();
54     }
55
56     synchronized void waitUntilFinished() {
57         while (fActiveTestDeathCount < testCount()) {
58             try {
59                 wait();
60             } catch (InterruptedException JavaDoc e) {
61                 return; // ignore
62
}
63         }
64     }
65     
66     synchronized public void runFinished() {
67         fActiveTestDeathCount++;
68         notifyAll();
69     }
70 }
Popular Tags