001    package junit.extensions;
002    
003    import junit.framework.Test;
004    import junit.framework.TestCase;
005    import junit.framework.TestResult;
006    import junit.framework.TestSuite;
007    
008    /**
009     * A TestSuite for active Tests. It runs each
010     * test in a separate thread and waits until all
011     * threads have terminated.
012     * -- Aarhus Radisson Scandinavian Center 11th floor
013     */
014    public class ActiveTestSuite extends TestSuite {
015        private volatile int fActiveTestDeathCount;
016    
017        public ActiveTestSuite() {
018        }
019    
020        public ActiveTestSuite(Class<? extends TestCase> theClass) {
021            super(theClass);
022        }
023    
024        public ActiveTestSuite(String name) {
025            super(name);
026        }
027    
028        public ActiveTestSuite(Class<? extends TestCase> theClass, String name) {
029            super(theClass, name);
030        }
031    
032        @Override
033        public void run(TestResult result) {
034            fActiveTestDeathCount = 0;
035            super.run(result);
036            waitUntilFinished();
037        }
038    
039        @Override
040        public void runTest(final Test test, final TestResult result) {
041            Thread t = new Thread() {
042                @Override
043                public void run() {
044                    try {
045                        // inlined due to limitation in VA/Java
046                        //ActiveTestSuite.super.runTest(test, result);
047                        test.run(result);
048                    } finally {
049                        ActiveTestSuite.this.runFinished();
050                    }
051                }
052            };
053            t.start();
054        }
055    
056        synchronized void waitUntilFinished() {
057            while (fActiveTestDeathCount < testCount()) {
058                try {
059                    wait();
060                } catch (InterruptedException e) {
061                    return; // ignore
062                }
063            }
064        }
065    
066        synchronized public void runFinished() {
067            fActiveTestDeathCount++;
068            notifyAll();
069        }
070    }