KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > MultiThreadedTestCaseHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.filesystems;
21 import junit.framework.*;
22 import org.netbeans.junit.*;
23
24 /**
25  * A multi-threaded JUnit test case.
26  *
27 */

28 public class MultiThreadedTestCaseHid extends NbTestCase {
29     /**
30      * The threads that are executing.
31      */

32     private Thread JavaDoc threads[] = null;
33     /**
34      * The tests TestResult.*/

35     private TestResult testResult = null;
36     /**
37      * Simple constructor.
38      */

39
40     public MultiThreadedTestCaseHid(String JavaDoc s) {
41         super(s);
42     }
43     /**
44      * Interrupt the running threads.
45      */

46
47     public void interruptThreads() {
48         if(threads != null) {
49             for(int i = 0;i < threads.length;i++) {
50                 //System.out.println("interrupted");
51
threads[i].interrupt();
52             }
53         }
54     }
55     /**
56      * Override run so we can squirrel away the test result.*/

57     
58     public void run(final TestResult result) {
59         testResult = result;
60         super.run(result);
61         testResult = null;
62     }
63     /**
64      * Run the test case threads.*/

65     
66     protected void runTestCaseRunnables (final TestCaseRunnable[] runnables, int ms) {
67         if(runnables == null) {
68             throw new IllegalArgumentException JavaDoc("runnables is null");
69         }
70         threads = new Thread JavaDoc[runnables.length];
71         for(int i = 0;i < threads.length;i++) {
72             threads[i] = new Thread JavaDoc(runnables[i]);
73         }
74         for(int i = 0;i < threads.length;i++) {
75             threads[i].start();
76         }
77         try {
78             long start = System.currentTimeMillis();
79             for(int i = 0;i < threads.length;i++) {
80                 if (System.currentTimeMillis()-start > ms ) {
81                     interruptThreads();
82                     throw new InterruptedException JavaDoc ("Time ammount elapsed: probably deadlock.");
83                 }
84                 threads[i].join((ms - (System.currentTimeMillis()-start)),0);
85             }
86         }
87         catch(InterruptedException JavaDoc ignore) {
88             handleException(ignore);
89         }
90         threads = null;
91     }
92     /**
93      * Handle an exception. Since multiple threads won't have their
94      * exceptions caught the threads must manually catch them and call
95      * <code>handleException ()</code>.
96      * @param t Exception to handle.*/

97     
98     private void handleException(final Throwable JavaDoc t) {
99         synchronized(testResult) {
100             if(t instanceof AssertionFailedError) {
101                 testResult.addFailure(this, (AssertionFailedError)t);
102             }
103             else {
104                 testResult.addError(this, t);
105             }
106         }
107     }
108     /**
109      * A test case thread. Override runTestCase () and define
110      * behaviour of test in there.*/

111     protected abstract class TestCaseRunnable implements Runnable JavaDoc {
112         /**
113          * Override this to define the test*/

114         
115         public abstract void runTestCase()
116                               throws Throwable JavaDoc;
117         /**
118          * Run the test in an environment where
119          * we can handle the exceptions generated by the test method.*/

120         
121         public void run() {
122             try {
123                 runTestCase();
124             }
125             catch(Throwable JavaDoc t) /* Any other exception we handle and then we interrupt the other threads.*/ {
126                 handleException(t);
127                 interruptThreads();
128             }
129         }
130     }
131 }
132
Popular Tags