1 19 20 package org.openide.filesystems; 21 import junit.framework.*; 22 import org.netbeans.junit.*; 23 24 28 public class MultiThreadedTestCaseHid extends NbTestCase { 29 32 private Thread threads[] = null; 33 35 private TestResult testResult = null; 36 39 40 public MultiThreadedTestCaseHid(String s) { 41 super(s); 42 } 43 46 47 public void interruptThreads() { 48 if(threads != null) { 49 for(int i = 0;i < threads.length;i++) { 50 threads[i].interrupt(); 52 } 53 } 54 } 55 57 58 public void run(final TestResult result) { 59 testResult = result; 60 super.run(result); 61 testResult = null; 62 } 63 65 66 protected void runTestCaseRunnables (final TestCaseRunnable[] runnables, int ms) { 67 if(runnables == null) { 68 throw new IllegalArgumentException ("runnables is null"); 69 } 70 threads = new Thread [runnables.length]; 71 for(int i = 0;i < threads.length;i++) { 72 threads[i] = new Thread (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 ("Time ammount elapsed: probably deadlock."); 83 } 84 threads[i].join((ms - (System.currentTimeMillis()-start)),0); 85 } 86 } 87 catch(InterruptedException ignore) { 88 handleException(ignore); 89 } 90 threads = null; 91 } 92 97 98 private void handleException(final Throwable 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 111 protected abstract class TestCaseRunnable implements Runnable { 112 114 115 public abstract void runTestCase() 116 throws Throwable ; 117 120 121 public void run() { 122 try { 123 runTestCase(); 124 } 125 catch(Throwable t) { 126 handleException(t); 127 interruptThreads(); 128 } 129 } 130 } 131 } 132 | Popular Tags |