1 8 9 package foxtrot.test; 10 11 import java.lang.reflect.Method ; 12 13 import javax.swing.SwingUtilities ; 14 15 import junit.framework.TestCase; 16 import foxtrot.WorkerThread; 17 import foxtrot.workers.SingleWorkerThread; 18 19 24 public class FoxtrotTestCase extends TestCase 25 { 26 protected boolean debug = false; 27 28 protected FoxtrotTestCase(String s) 29 { 30 super(s); 31 } 32 33 39 protected void invokeTest(final WorkerThread workerThread, final Runnable test, final Runnable callback) throws Exception 40 { 41 if (SwingUtilities.isEventDispatchThread()) fail("Tests cannot be invoked from the Event Dispatch Thread"); 42 43 final Object lock = new Object (); 44 final MutableInteger barrier = new MutableInteger(0); 45 final MutableHolder throwable = new MutableHolder(null); 46 47 SwingUtilities.invokeLater(new Runnable () 50 { 51 public void run() 52 { 53 synchronized (lock) 55 { 56 while (barrier.get() < 1) 57 { 58 try 59 { 60 lock.wait(); 61 } 62 catch (InterruptedException ignored) 63 { 64 Thread.currentThread().interrupt(); 65 break; 66 } 67 } 68 } 69 70 if (debug) System.out.println("Running test " + this); 71 try 73 { 74 test.run(); 75 } 76 catch (Throwable x) 77 { 78 synchronized (lock) 79 { 80 throwable.set(x); 81 } 82 } 83 84 if (debug) System.out.println("Test method completed, waiting for test completion for " + this); 85 try 90 { 91 while (hasPendingTasks(workerThread)) 92 { 93 if (debug) System.out.println("Pending tasks for test " + this); 94 sleep(100); 95 } 96 } 97 catch (Exception x) 98 { 99 throwable.set(x); 100 } 101 if (debug) System.out.println("No more tasks running for test " + this); 102 SwingUtilities.invokeLater(new Runnable () 103 { 104 public void run() 105 { 106 synchronized (lock) 107 { 108 barrier.set(0); 109 lock.notifyAll(); 110 } 111 } 112 }); 113 } 114 }); 115 116 if (debug) System.out.println("Test " + this + " launched"); 119 synchronized (lock) 120 { 121 barrier.set(1); 122 lock.notifyAll(); 123 124 while (barrier.get() > 0) lock.wait(); 125 } 126 127 if (debug) System.out.println("Test " + this + " consumed all AWT events"); 128 129 try 130 { 131 if (callback != null) callback.run(); 133 134 synchronized (lock) 135 { 136 Throwable t = (Throwable )throwable.get(); 137 if (t instanceof Error ) throw (Error )t; 138 if (t instanceof Exception ) throw (Exception )t; 139 } 140 } 141 finally 142 { 143 if (debug) System.out.println("Test " + this + " completed"); 144 if (debug) System.out.println(); 145 } 146 } 147 148 protected void sleep(long ms) 149 { 150 try 151 { 152 Thread.sleep(ms); 153 } 154 catch (InterruptedException ignored) 155 { 156 } 157 } 158 159 protected boolean isJRE14() 160 { 161 return loadClass("java.awt.SequencedEvent") != null; 162 } 163 164 private Class loadClass(String className) 165 { 166 try 168 { 169 return Class.forName(className, false, null); 170 } 171 catch (ClassNotFoundException ignored) 172 { 173 } 174 return null; 175 } 176 177 private boolean hasPendingTasks(WorkerThread workerThread) throws Exception 178 { 179 if (workerThread == null) return false; 180 if (workerThread instanceof SingleWorkerThread) 181 { 182 Method hasPendingTasks = SingleWorkerThread.class.getDeclaredMethod("hasPendingTasks", null); 183 hasPendingTasks.setAccessible(true); 184 Boolean result = (Boolean )hasPendingTasks.invoke(workerThread, null); 185 return result.booleanValue(); 186 } 187 throw new IllegalArgumentException ("Invalid WorkerThread " + workerThread); 188 } 189 } 190 | Popular Tags |