1 8 9 package foxtrot.test; 10 11 import javax.swing.SwingUtilities ; 12 13 import foxtrot.AsyncTask; 14 import foxtrot.AsyncWorker; 15 import foxtrot.Task; 16 import foxtrot.WorkerThread; 17 18 21 public class AsyncWorkerTest extends FoxtrotTestCase 22 { 23 public AsyncWorkerTest(String s) 24 { 25 super(s); 26 } 27 28 public void testPostAndForget() throws Exception 29 { 30 final MutableHolder result = new MutableHolder(null); 31 final WorkerThread workerThread = AsyncWorker.getWorkerThread(); 32 invokeTest(workerThread, new Runnable () 33 { 34 public void run() 35 { 36 workerThread.postTask(new Task() 38 { 39 public Object run() throws Exception 40 { 41 sleep(1000); 42 result.set(Boolean.TRUE); 43 return null; 44 } 45 }); 46 47 sleep(500); 48 } 49 }, new Runnable () 50 { 51 public void run() 52 { 53 if (result.get() != Boolean.TRUE) fail("AsyncTask is not executed"); 54 } 55 }); 56 } 57 58 public void testUsage() throws Exception 59 { 60 final MutableHolder result = new MutableHolder(null); 61 invokeTest(AsyncWorker.getWorkerThread(), new Runnable () 62 { 63 public void run() 64 { 65 AsyncWorker.post(new AsyncTask() 66 { 67 private static final String VALUE = "1000"; 68 69 public Object run() throws Exception 70 { 71 Thread.sleep(1000); 72 return VALUE; 73 } 74 75 public void finish() 76 { 77 try 78 { 79 String value = (String )getResultOrThrow(); 80 if (!VALUE.equals(value)) result.set("AsyncTask.run() does not return the result"); 81 } 82 catch (Exception x) 83 { 84 result.set(x.toString()); 85 } 86 } 87 }); 88 89 sleep(500); 90 } 91 }, new Runnable () 92 { 93 public void run() 94 { 95 if (result.get() != null) fail((String )result.get()); 96 } 97 }); 98 } 99 100 public void testThreads() throws Exception 101 { 102 final MutableHolder result = new MutableHolder(null); 103 invokeTest(AsyncWorker.getWorkerThread(), new Runnable () 104 { 105 public void run() 106 { 107 AsyncWorker.post(new AsyncTask() 108 { 109 public Object run() throws Exception 110 { 111 if (SwingUtilities.isEventDispatchThread()) 113 { 114 return "Must not be in the Event Dispatch Thread"; 115 } 116 else 117 { 118 if (Thread.currentThread().getName().indexOf("Foxtrot") < 0) return "Must be in the Foxtrot Worker Thread"; 120 } 121 return null; 122 } 123 124 public void finish() 125 { 126 try 127 { 128 String failure = (String )getResultOrThrow(); 129 if (failure == null) 130 { 131 if (!SwingUtilities.isEventDispatchThread()) result.set("Must be in the Event Dispatch Thread"); 133 } 134 else 135 { 136 result.set(failure); 137 } 138 } 139 catch (Exception x) 140 { 141 result.set(x.toString()); 142 } 143 } 144 }); 145 146 sleep(500); 147 } 148 }, new Runnable () 149 { 150 public void run() 151 { 152 if (result.get() != null) fail((String )result.get()); 153 } 154 }); 155 } 156 157 public void testTaskException() throws Exception 158 { 159 final MutableHolder result = new MutableHolder(null); 160 invokeTest(AsyncWorker.getWorkerThread(), new Runnable () 161 { 162 public void run() 163 { 164 final IndexOutOfBoundsException ex = new IndexOutOfBoundsException (); 165 AsyncWorker.post(new AsyncTask() 166 { 167 public Object run() throws Exception 168 { 169 throw ex; 170 } 171 172 public void finish() 173 { 174 try 175 { 176 getResultOrThrow(); 177 result.set("Expected exception"); 178 } 179 catch (IndexOutOfBoundsException x) 180 { 181 if (x != ex) result.set("Expected same exception"); 182 } 183 catch (Exception x) 184 { 185 result.set("Did not expect checked exception"); 186 } 187 } 188 }); 189 190 sleep(500); 191 } 192 }, new Runnable () 193 { 194 public void run() 195 { 196 if (result.get() != null) fail((String )result.get()); 197 } 198 }); 199 } 200 201 public void testTaskError() throws Exception 202 { 203 final MutableHolder result = new MutableHolder(null); 204 invokeTest(AsyncWorker.getWorkerThread(), new Runnable () 205 { 206 public void run() 207 { 208 final Error ex = new Error (); 209 AsyncWorker.post(new AsyncTask() 210 { 211 public Object run() throws Exception 212 { 213 throw ex; 214 } 215 216 public void finish() 217 { 218 try 219 { 220 getResultOrThrow(); 221 result.set("Expected error"); 222 } 223 catch (Error x) 224 { 225 if (x != ex) result.set("Expected same error"); 226 } 227 catch (Exception x) 228 { 229 result.set("Did not expect exception"); 230 } 231 } 232 }); 233 234 sleep(500); 235 } 236 }, new Runnable () 237 { 238 public void run() 239 { 240 if (result.get() != null) fail((String )result.get()); 241 } 242 }); 243 } 244 245 public void testPostFromTask() throws Exception 246 { 247 final MutableHolder result = new MutableHolder(null); 248 invokeTest(AsyncWorker.getWorkerThread(), new Runnable () 249 { 250 public void run() 251 { 252 AsyncWorker.post(new AsyncTask() 253 { 254 public Object run() 255 { 256 AsyncWorker.post(new AsyncTask() 260 { 261 public Object run() 262 { 263 return null; 264 } 265 266 public void finish() 267 { 268 } 269 }); 270 return null; 271 } 272 273 public void finish() 274 { 275 try 276 { 277 getResultOrThrow(); 278 result.set("Expected exception"); 279 } 280 catch (IllegalStateException x) 281 { 282 } 283 catch (Exception x) 284 { 285 result.set("Did not expect exception"); 286 } 287 } 288 }); 289 } 290 }, new Runnable () 291 { 292 public void run() 293 { 294 if (result.get() != null) fail((String )result.get()); 295 } 296 }); 297 } 298 } 299 | Popular Tags |