KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > test > AsyncWorkerTest


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot.test;
10
11 import javax.swing.SwingUtilities JavaDoc;
12
13 import foxtrot.AsyncTask;
14 import foxtrot.AsyncWorker;
15 import foxtrot.Task;
16 import foxtrot.WorkerThread;
17
18 /**
19  * @version $Revision: 1.2 $
20  */

21 public class AsyncWorkerTest extends FoxtrotTestCase
22 {
23    public AsyncWorkerTest(String JavaDoc s)
24    {
25       super(s);
26    }
27
28    public void testPostAndForget() throws Exception JavaDoc
29    {
30       final MutableHolder result = new MutableHolder(null);
31       final WorkerThread workerThread = AsyncWorker.getWorkerThread();
32       invokeTest(workerThread, new Runnable JavaDoc()
33       {
34          public void run()
35          {
36             // This avoids to use AsyncTask and implement finish()
37
workerThread.postTask(new Task()
38             {
39                public Object JavaDoc run() throws Exception JavaDoc
40                {
41                   sleep(1000);
42                   result.set(Boolean.TRUE);
43                   return null;
44                }
45             });
46
47             sleep(500);
48          }
49       }, new Runnable JavaDoc()
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 JavaDoc
59    {
60       final MutableHolder result = new MutableHolder(null);
61       invokeTest(AsyncWorker.getWorkerThread(), new Runnable JavaDoc()
62       {
63          public void run()
64          {
65             AsyncWorker.post(new AsyncTask()
66             {
67                private static final String JavaDoc VALUE = "1000";
68
69                public Object JavaDoc run() throws Exception JavaDoc
70                {
71                   Thread.sleep(1000);
72                   return VALUE;
73                }
74
75                public void finish()
76                {
77                   try
78                   {
79                      String JavaDoc value = (String JavaDoc)getResultOrThrow();
80                      if (!VALUE.equals(value)) result.set("AsyncTask.run() does not return the result");
81                   }
82                   catch (Exception JavaDoc x)
83                   {
84                      result.set(x.toString());
85                   }
86                }
87             });
88
89             sleep(500);
90          }
91       }, new Runnable JavaDoc()
92       {
93          public void run()
94          {
95             if (result.get() != null) fail((String JavaDoc)result.get());
96          }
97       });
98    }
99
100    public void testThreads() throws Exception JavaDoc
101    {
102       final MutableHolder result = new MutableHolder(null);
103       invokeTest(AsyncWorker.getWorkerThread(), new Runnable JavaDoc()
104       {
105          public void run()
106          {
107             AsyncWorker.post(new AsyncTask()
108             {
109                public Object JavaDoc run() throws Exception JavaDoc
110                {
111                   // Check that I'm NOT in the AWT Event Dispatch Thread
112
if (SwingUtilities.isEventDispatchThread())
113                   {
114                      return "Must not be in the Event Dispatch Thread";
115                   }
116                   else
117                   {
118                      // Check that I'm really in the Foxtrot Worker Thread
119
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 JavaDoc failure = (String JavaDoc)getResultOrThrow();
129                      if (failure == null)
130                      {
131                         // Check that I'm in the AWT Event Dispatch Thread
132
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 JavaDoc x)
140                   {
141                      result.set(x.toString());
142                   }
143                }
144             });
145
146             sleep(500);
147          }
148       }, new Runnable JavaDoc()
149       {
150          public void run()
151          {
152             if (result.get() != null) fail((String JavaDoc)result.get());
153          }
154       });
155    }
156
157    public void testTaskException() throws Exception JavaDoc
158    {
159       final MutableHolder result = new MutableHolder(null);
160       invokeTest(AsyncWorker.getWorkerThread(), new Runnable JavaDoc()
161       {
162          public void run()
163          {
164             final IndexOutOfBoundsException JavaDoc ex = new IndexOutOfBoundsException JavaDoc();
165             AsyncWorker.post(new AsyncTask()
166             {
167                public Object JavaDoc run() throws Exception JavaDoc
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 JavaDoc x)
180                   {
181                      if (x != ex) result.set("Expected same exception");
182                   }
183                   catch (Exception JavaDoc x)
184                   {
185                      result.set("Did not expect checked exception");
186                   }
187                }
188             });
189
190             sleep(500);
191          }
192       }, new Runnable JavaDoc()
193       {
194          public void run()
195          {
196             if (result.get() != null) fail((String JavaDoc)result.get());
197          }
198       });
199    }
200
201    public void testTaskError() throws Exception JavaDoc
202    {
203       final MutableHolder result = new MutableHolder(null);
204       invokeTest(AsyncWorker.getWorkerThread(), new Runnable JavaDoc()
205       {
206          public void run()
207          {
208             final Error JavaDoc ex = new Error JavaDoc();
209             AsyncWorker.post(new AsyncTask()
210             {
211                public Object JavaDoc run() throws Exception JavaDoc
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 JavaDoc x)
224                   {
225                      if (x != ex) result.set("Expected same error");
226                   }
227                   catch (Exception JavaDoc x)
228                   {
229                      result.set("Did not expect exception");
230                   }
231                }
232             });
233
234             sleep(500);
235          }
236       }, new Runnable JavaDoc()
237       {
238          public void run()
239          {
240             if (result.get() != null) fail((String JavaDoc)result.get());
241          }
242       });
243    }
244
245    public void testPostFromTask() throws Exception JavaDoc
246    {
247       final MutableHolder result = new MutableHolder(null);
248       invokeTest(AsyncWorker.getWorkerThread(), new Runnable JavaDoc()
249       {
250          public void run()
251          {
252             AsyncWorker.post(new AsyncTask()
253             {
254                public Object JavaDoc run()
255                {
256                   // Nested AsyncWorker.post(): this is invalid as
257
// it has the same effect of 2 consecutive AsyncWorker.post()
258
// calls, so there is no need to nest
259
AsyncWorker.post(new AsyncTask()
260                   {
261                      public Object JavaDoc 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 JavaDoc x)
281                   {
282                   }
283                   catch (Exception JavaDoc x)
284                   {
285                      result.set("Did not expect exception");
286                   }
287                }
288             });
289          }
290       }, new Runnable JavaDoc()
291       {
292          public void run()
293          {
294             if (result.get() != null) fail((String JavaDoc)result.get());
295          }
296       });
297    }
298 }
299
Popular Tags