KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > test > EventPumpTest


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 java.lang.reflect.Method JavaDoc;
12
13 import javax.swing.SwingUtilities JavaDoc;
14
15 import foxtrot.EventPump;
16 import foxtrot.Task;
17 import foxtrot.pumps.QueueEventPump;
18 import foxtrot.pumps.SunJDK14ConditionalEventPump;
19
20 /**
21  * @version $Revision: 1.6 $
22  */

23 public class EventPumpTest extends FoxtrotTestCase
24 {
25    public EventPumpTest(String JavaDoc s)
26    {
27       super(s);
28    }
29
30    public void testJDK13QueueEventPump() throws Exception JavaDoc
31    {
32       if (!isJRE14())
33       {
34          QueueEventPump pump = new QueueEventPump();
35          testEventPump(pump);
36       }
37    }
38
39    public void testSunJDK14ConditionalEventPump() throws Exception JavaDoc
40    {
41       if (isJRE14())
42       {
43          SunJDK14ConditionalEventPump pump = new SunJDK14ConditionalEventPump();
44          testEventPump(pump);
45       }
46    }
47
48    private void testEventPump(EventPump pump) throws Exception JavaDoc
49    {
50       testPumpEventsBlocks(pump);
51       testPumpEventsDequeues(pump);
52       tesPumpEventsOnThrowException(pump);
53       tesPumpEventsOnThrowError(pump);
54    }
55
56    /**
57     * Verifies that EventPump.pumpEvents(Task) blocks until the Task is completed
58     */

59    private void testPumpEventsBlocks(final EventPump pump) throws Exception JavaDoc
60    {
61       invokeTest(null, new Runnable JavaDoc()
62       {
63          public void run()
64          {
65             final Task task = new Task()
66             {
67                public Object JavaDoc run() throws Exception JavaDoc
68                {
69                   return null;
70                }
71             };
72
73             final long delay = 5000;
74
75             // I enqueue another event to stop the task
76
SwingUtilities.invokeLater(new Runnable JavaDoc()
77             {
78                public void run()
79                {
80                   sleep(delay);
81                   setTaskCompleted(task);
82                }
83             });
84
85             // Now I start the event pump, the events above must be dequeued.
86
long start = System.currentTimeMillis();
87             pump.pumpEvents(task);
88             long stop = System.currentTimeMillis();
89
90             long elapsed = stop - start;
91             if (elapsed <= delay) fail("Blocking is not effective: expecting " + delay + ", blocked for only " + elapsed);
92          }
93       }, null);
94    }
95
96    /**
97     * Verifies that AWT events are dequeued by EventPump.pumpEvents(Task)
98     */

99    private void testPumpEventsDequeues(final EventPump pump) throws Exception JavaDoc
100    {
101       invokeTest(null, new Runnable JavaDoc()
102       {
103          public void run()
104          {
105             final MutableInteger count = new MutableInteger(0);
106             final int value = 13;
107
108             SwingUtilities.invokeLater(new Runnable JavaDoc()
109             {
110                public void run()
111                {
112                   count.set(value);
113                }
114             });
115
116             final Task task = new Task()
117             {
118                public Object JavaDoc run() throws Exception JavaDoc
119                {
120                   return null;
121                }
122             };
123
124             // I enqueue another event to stop the task
125
SwingUtilities.invokeLater(new Runnable JavaDoc()
126             {
127                public void run()
128                {
129                   setTaskCompleted(task);
130                }
131             });
132
133             // Now I start the event pump, the events above must be dequeued.
134
pump.pumpEvents(task);
135
136             if (count.get() != value) fail("Event pump does not dequeue events");
137          }
138       }, null);
139    }
140
141    /**
142     * Verifies that EventPump.pumpEvents(Task) does not return in case of runtime exceptions
143     */

144    private void tesPumpEventsOnThrowException(final EventPump pump) throws Exception JavaDoc
145    {
146       invokeTest(null, new Runnable JavaDoc()
147       {
148          public void run()
149          {
150             SwingUtilities.invokeLater(new Runnable JavaDoc()
151             {
152                public void run()
153                {
154                   throw new RuntimeException JavaDoc();
155                }
156             });
157
158             final Task task = new Task()
159             {
160                public Object JavaDoc run() throws Exception JavaDoc
161                {
162                   return null;
163                }
164             };
165
166             final long delay = 3000;
167
168             // I enqueue another event to stop the task
169
SwingUtilities.invokeLater(new Runnable JavaDoc()
170             {
171                public void run()
172                {
173                   sleep(delay);
174                   setTaskCompleted(task);
175                }
176             });
177
178             try
179             {
180                // Now I start the event pump, the events above must be dequeued.
181
long start = System.currentTimeMillis();
182                pump.pumpEvents(task);
183                long stop = System.currentTimeMillis();
184                long elapsed = stop - start;
185                if (elapsed <= delay) fail("Blocking is not effective when events throw exceptions: expecting " + delay + ", blocked for only " + elapsed);
186             }
187             catch (RuntimeException JavaDoc x)
188             {
189                fail("Event pump must not throw runtime exceptions thrown by events");
190             }
191          }
192       }, null);
193    }
194
195    /**
196     * Verifies that EventPump.pumpEvents(Task) does not return in case of errors
197     */

198    private void tesPumpEventsOnThrowError(final EventPump pump) throws Exception JavaDoc
199    {
200       invokeTest(null, new Runnable JavaDoc()
201       {
202          public void run()
203          {
204             SwingUtilities.invokeLater(new Runnable JavaDoc()
205             {
206                public void run()
207                {
208                   throw new Error JavaDoc();
209                }
210             });
211
212             final Task task = new Task()
213             {
214                public Object JavaDoc run() throws Exception JavaDoc
215                {
216                   return null;
217                }
218             };
219
220             final long delay = 3000;
221
222             // I enqueue another event to stop the task
223
SwingUtilities.invokeLater(new Runnable JavaDoc()
224             {
225                public void run()
226                {
227                   sleep(delay);
228                   setTaskCompleted(task);
229                }
230             });
231
232             try
233             {
234                // Now I start the event pump, the events above must be dequeued.
235
long start = System.currentTimeMillis();
236                pump.pumpEvents(task);
237                long stop = System.currentTimeMillis();
238                long elapsed = stop - start;
239                if (elapsed <= delay) fail("Blocking is not effective when events throw errors: expecting " + delay + ", blocked for only " + elapsed);
240             }
241             catch (Error JavaDoc x)
242             {
243                fail("Event pump must not throw errors thrown by events");
244             }
245          }
246       }, null);
247    }
248
249    private void setTaskCompleted(Task task)
250    {
251       try
252       {
253          Method JavaDoc completed = Task.class.getDeclaredMethod("setCompleted", new Class JavaDoc[] {boolean.class});
254          completed.setAccessible(true);
255          completed.invoke(task, new Object JavaDoc[]{Boolean.TRUE});
256
257          // In case no other events are posted to the event queue, here we wake it up,
258
// see AbstractWorkerThread.runTask (in the finally block)
259
SwingUtilities.invokeLater(new Runnable JavaDoc()
260          {
261             public void run()
262             {
263             }
264          });
265       }
266       catch (Throwable JavaDoc x)
267       {
268          x.printStackTrace();
269          fail();
270       }
271    }
272 }
273
Popular Tags