1 33 34 package edu.rice.cs.drjava.model; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 import edu.rice.cs.drjava.ui.DefinitionsPaneTest; 38 import edu.rice.cs.util.Log; 39 40 import junit.framework.AssertionFailedError; 41 42 45 public abstract class MultiThreadedTestCase extends DrJavaTestCase { 46 public MultiThreadedTestCase() { super(); } 47 public MultiThreadedTestCase(String name) { super(name); } 48 49 50 protected volatile static boolean _testFailed = false; 51 52 53 public void setUp() throws Exception { 54 super.setUp(); 55 _testFailed = false; 56 ExceptionHandler.ONLY.reset(); 57 Thread.setDefaultUncaughtExceptionHandler(ExceptionHandler.ONLY); 58 } 59 60 61 public void tearDown() throws Exception { 62 ExceptionHandler.ONLY.rethrow(); 63 if (_testFailed) fail("test failed in another thread"); 64 DefinitionsPaneTest._log.log("MultithreadedTestCase.tearDown() calling super.tearDown()"); 65 super.tearDown(); 66 } 67 68 71 protected static void listenerFail(String s) { 72 StackTraceElement [] trace = Thread.getAllStackTraces().get(Thread.currentThread()); 73 System.out.println("TEST FAILED in a listener thread"); 74 System.out.println("Failing thread stack trace:\n " + Log.traceToString(trace)); 75 _testFailed = true; 77 fail(s); 78 } 79 80 84 public static void join(Thread t) { 85 try { t.join(); } 86 catch(InterruptedException e) { 87 throw new edu.rice.cs.util.UnexpectedException(e, "Thread.join was unexpectedly interrupted."); 88 } 89 } 90 91 94 public static void wait(Object o) { 95 try { o.wait(); } 96 catch(InterruptedException e) { 97 e.printStackTrace(); 98 throw new edu.rice.cs.util.UnexpectedException(e, "Thread.wait was unexpectedly interrupted."); 99 } 100 } 101 102 106 private static class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler { 107 108 109 private volatile Throwable _e = null; 110 111 112 private volatile java.lang.Thread _t = null; 113 114 115 private volatile java.lang.Thread _mainThread = java.lang.Thread.currentThread(); 116 117 121 public void uncaughtException(java.lang.Thread t, Throwable e) { 122 _t = t; 123 _e = e; 124 if (_mainThread != null) { 125 System.out.println("Uncaught Exception in spawned thread within a MultiThreadedTestCase:"); 126 e.printStackTrace(System.out); 127 _mainThread.interrupt(); 128 } 129 } 130 131 132 public void reset() { 133 _t = null; 134 _e = null; 135 } 136 137 138 public void rethrow() { 139 if (exceptionOccurred()) { 140 if (_e instanceof Error ) throw (Error )_e; 141 if (_e instanceof RuntimeException ) throw (RuntimeException )_e; 142 else { 143 throw new AssertionFailedError("Exception in thread "+_t+": "+_e); 145 } 146 } 147 } 148 149 152 public boolean exceptionOccurred() { return (_e != null); } 153 154 public Throwable getException() { return _e; } 155 156 public java.lang.Thread getThread() { return _t; } 157 158 159 public void setMainThread(java.lang.Thread mainThread) { _mainThread = mainThread; } 160 161 162 private ExceptionHandler() { } 163 164 165 public static final ExceptionHandler ONLY = new ExceptionHandler(); 166 } 167 } 168 | Popular Tags |