KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > EventRequestProcessorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.common;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.JRootPane JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.util.Mutex;
29 import org.openide.util.RequestProcessor;
30
31 /**
32  *
33  * @author Andrei Badea
34  */

35 public class EventRequestProcessorTest extends NbTestCase {
36
37     // this class uses Mutex.EVENT.readAccess(Mutex.Action) in several places
38
// instead of the more obvious SwingUtilities.invokeAndWait()
39
// just because the former method has simpler exception handling
40

41     public EventRequestProcessorTest(String JavaDoc testName) {
42         super(testName);
43     }
44
45     protected boolean runInEQ() {
46         return true;
47     }
48
49     public void testInvoke() {
50         final EventRequestProcessor erp = new EventRequestProcessor();
51         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
52
53         // incremented each time an action is invoked
54
final int[] invokeCount = new int[1];
55         // incremented each time an action is invoked in the correct thread
56
// (EDT for sync actions, !EDT for async actions)
57
final int[] correctThreadCount = new int[1];
58         // incremented each time the progress panel state is correct during an action invocation
59
// (closed for sync actions, open for async actions)
60
final int[] correctPanelStateCount = new int[1];
61
62         actions.add(new EventRequestProcessor.SynchronousAction() {
63             public void run(final EventRequestProcessor.Context actionContext) {
64                 correctThreadCount[0] += isEventDispatchThread();
65                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
66                     public Object JavaDoc run() {
67                         correctPanelStateCount[0] += isClosed(actionContext.getProgress().getPanel());
68                         return null;
69                     }
70                 });
71                 invokeCount[0]++;
72             }
73         });
74
75         actions.add(new EventRequestProcessor.SynchronousAction() {
76             public void run(final EventRequestProcessor.Context actionContext) {
77                 correctThreadCount[0] += isEventDispatchThread();
78                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
79                     public Object JavaDoc run() {
80                         correctPanelStateCount[0] += isClosed(actionContext.getProgress().getPanel());
81                         return null;
82                     }
83                 });
84                 invokeCount[0]++;
85             }
86         });
87
88         actions.add(new EventRequestProcessor.AsynchronousAction() {
89             public void run(final EventRequestProcessor.Context actionContext) {
90                 actionContext.getProgress().progress("Testing an asynchronous action.");
91
92                 correctThreadCount[0] += isNotEventDispatchThread();
93                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
94                     public Object JavaDoc run() {
95                         correctPanelStateCount[0] += isOpen(actionContext.getProgress().getPanel());
96                         return null;
97                     }
98                 });
99                 invokeCount[0]++;
100                 try {
101                     Thread.sleep(300);
102                 } catch (InterruptedException JavaDoc e) {
103                 }
104             }
105         });
106
107         actions.add(new EventRequestProcessor.AsynchronousAction() {
108             public void run(final EventRequestProcessor.Context actionContext) {
109                 actionContext.getProgress().progress("Testing the second asynchronous action.");
110
111                 correctThreadCount[0] += isNotEventDispatchThread();
112                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
113                     public Object JavaDoc run() {
114                         correctPanelStateCount[0] += isOpen(actionContext.getProgress().getPanel());
115                         return null;
116                     }
117                 });
118                 invokeCount[0]++;
119                 try {
120                     Thread.sleep(300);
121                 } catch (InterruptedException JavaDoc e) {
122                 }
123             }
124         });
125
126         actions.add(new EventRequestProcessor.SynchronousAction() {
127             public void run(final EventRequestProcessor.Context actionContext) {
128                 correctThreadCount[0] += isEventDispatchThread();
129                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
130                     public Object JavaDoc run() {
131                         correctPanelStateCount[0] += isClosed(actionContext.getProgress().getPanel());
132                         return null;
133                     }
134                 });
135                 invokeCount[0]++;
136             }
137         });
138
139         actions.add(new EventRequestProcessor.AsynchronousAction() {
140             public void run(final EventRequestProcessor.Context actionContext) {
141                 actionContext.getProgress().progress("Testing the third asynchronous action.");
142
143                 correctThreadCount[0] += isNotEventDispatchThread();
144                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
145                     public Object JavaDoc run() {
146                         correctPanelStateCount[0] += isOpen(actionContext.getProgress().getPanel());
147                         return null;
148                     }
149                 });
150                 invokeCount[0]++;
151                 try {
152                     Thread.sleep(300);
153                 } catch (InterruptedException JavaDoc e) {
154                 }
155             }
156         });
157
158         erp.invoke(actions);
159         assertEquals(6, invokeCount[0]);
160         assertEquals(6, correctThreadCount[0]);
161         assertEquals(6, correctPanelStateCount[0]);
162
163         erp.invoke(actions);
164         assertEquals(12, invokeCount[0]);
165         assertEquals(12, correctThreadCount[0]);
166         assertEquals(12, correctPanelStateCount[0]);
167     }
168
169     private int isEventDispatchThread() {
170         return SwingUtilities.isEventDispatchThread() ? 1 : 0;
171     }
172
173     private int isNotEventDispatchThread() {
174         return SwingUtilities.isEventDispatchThread() ? 0 : 1;
175     }
176
177     private int isOpen(ProgressPanel panel) {
178         return panel.isOpen() ? 1 : 0;
179     }
180
181     private int isClosed(ProgressPanel panel) {
182         return panel.isOpen() ? 0 : 1;
183     }
184
185     public void testProgressMessage() {
186         EventRequestProcessor erp = new EventRequestProcessor();
187         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
188
189         final String JavaDoc[] progressMessage = new String JavaDoc[1];
190
191         actions.add(new EventRequestProcessor.AsynchronousAction() {
192             public void run(final EventRequestProcessor.Context actionContext) {
193                 actionContext.getProgress().progress("Progress message");
194
195                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
196                     public Object JavaDoc run() {
197                         ProgressPanel progressPanel = actionContext.getProgress().getPanel();
198                         progressMessage[0] = progressPanel.getText();
199                         return null;
200                     }
201                 });
202             }
203         });
204
205         erp.invoke(actions);
206         assertEquals("Progress message", progressMessage[0]);
207     }
208
209     public void testDisabledActionDoesNotCauseAnInfiniteLoop() {
210         EventRequestProcessor erp = new EventRequestProcessor();
211         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
212
213         final Object JavaDoc sync = new Object JavaDoc();
214         final boolean[] ran = new boolean[1];
215
216         actions.add(new EventRequestProcessor.SynchronousAction() {
217             public void run(EventRequestProcessor.Context actionContext) {
218                 synchronized (sync) {
219                     ran[0] = true;
220                     sync.notifyAll();
221                 }
222             }
223
224             public boolean isEnabled() {
225                 return false;
226             }
227         });
228
229         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
230             public void run() {
231                 long startTime = System.currentTimeMillis();
232                 synchronized (sync) {
233                     while (!ran[0]) {
234                         if (System.currentTimeMillis() - startTime >= 5 * 1000) {
235                             // hmm, anything better?
236
System.exit(1);
237                         }
238                         try {
239                             sync.wait(500);
240                         } catch (InterruptedException JavaDoc e) { }
241                     }
242                 }
243             }
244         });
245
246         erp.invoke(actions);
247     }
248
249     public void testExceptionInSyncActionPropagates() {
250         EventRequestProcessor erp = new EventRequestProcessor();
251         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
252
253         actions.add(new EventRequestProcessor.SynchronousAction() {
254             public void run(final EventRequestProcessor.Context actionContext) {
255                 throw new RuntimeException JavaDoc("Error");
256             }
257         });
258
259         try {
260             erp.invoke(actions);
261             fail("Should throw RuntimeException");
262         } catch (RuntimeException JavaDoc e) {
263         }
264
265         assertNull(erp.actionInvoker);
266     }
267
268     public void testExceptionInAsyncActionPropagatesAndProgressPanelCloses() {
269         final EventRequestProcessor erp = new EventRequestProcessor();
270         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
271
272         final ProgressPanel progressPanel[] = new ProgressPanel[1];
273         final Boolean JavaDoc panelOpen[] = new Boolean JavaDoc[1];
274
275         actions.add(new EventRequestProcessor.AsynchronousAction() {
276             public void run(final EventRequestProcessor.Context actionContext) {
277                 actionContext.getProgress().progress("Asynchronous");
278
279                 // get the panel
280
progressPanel[0] = actionContext.getProgress().getPanel();
281
282                 // and simulate an error
283
throw new AssertionError JavaDoc("Error");
284             }
285         });
286
287         try {
288             erp.invoke(actions);
289             fail("Should throw RuntimeException");
290         } catch (RuntimeException JavaDoc e) {
291             assertTrue(e.getCause() instanceof AssertionError JavaDoc);
292         }
293
294         assertFalse(progressPanel[0].isOpen());
295
296         assertNull(erp.actionInvoker);
297     }
298
299     public void testNoCancelButtonWhenNonCancellableInvocation() {
300         EventRequestProcessor erp = new EventRequestProcessor();
301         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
302
303         final boolean[] cancelVisible = new boolean[1];
304
305         actions.add(new EventRequestProcessor.AsynchronousAction() {
306             public void run(final EventRequestProcessor.Context actionContext) {
307                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
308                     public Object JavaDoc run() {
309                         ProgressPanel progressPanel = actionContext.getProgress().getPanel();
310                         cancelVisible[0] = progressPanel.isCancelVisible();
311                         return null;
312                     }
313                 });
314             }
315         });
316
317         erp.invoke(actions);
318         assertFalse(cancelVisible[0]);
319     }
320
321     public void testCancelButtonWhenCancellableInvocation() {
322         EventRequestProcessor erp = new EventRequestProcessor();
323         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
324
325         final boolean[] cancelVisible = new boolean[2];
326         final boolean[] cancelEnabled = new boolean[2];
327
328         actions.add(new EventRequestProcessor.AsynchronousAction() {
329             public void run(final EventRequestProcessor.Context actionContext) {
330                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
331                     public Object JavaDoc run() {
332                         ProgressPanel progressPanel = actionContext.getProgress().getPanel();
333                         cancelVisible[0] = progressPanel.isCancelVisible();
334                         cancelEnabled[0] = progressPanel.isCancelEnabled();
335                         return null;
336                     }
337                 });
338             }
339         });
340
341         actions.add(new EventRequestProcessor.CancellableAction() {
342             public void run(final EventRequestProcessor.Context actionContext) {
343                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
344                     public Object JavaDoc run() {
345                         ProgressPanel progressPanel = actionContext.getProgress().getPanel();
346                         cancelVisible[1] = progressPanel.isCancelVisible();
347                         cancelEnabled[1] = progressPanel.isCancelEnabled();
348                         return null;
349                     }
350                 });
351             }
352
353             public boolean isEnabled() {
354                 return true;
355             }
356
357             public boolean getRunInEventThread() {
358                 return false;
359             }
360
361             public boolean cancel() {
362                 return true;
363             }
364         });
365
366         // the actions are cancellable, thus the cancel button is always visible
367
// invoke() should return true, meaning all actions were invoked
368
assertTrue(erp.invoke(actions, true));
369
370         // the first action was not cancellable
371
assertTrue(cancelVisible[0]);
372         assertFalse(cancelEnabled[0]);
373
374         // the second action was cancellable
375
assertTrue(cancelVisible[1]);
376         assertTrue(cancelEnabled[1]);
377     }
378
379     public void testCancelWorks() {
380         EventRequestProcessor erp = new EventRequestProcessor();
381         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
382
383         final boolean[] secondActionInvoked = new boolean[1];
384         final boolean[] cancelInvoked = new boolean[1];
385         final boolean[] cancelInvokedInEDT = new boolean[1];
386
387         actions.add(new EventRequestProcessor.CancellableAction() {
388             public void run(final EventRequestProcessor.Context actionContext) {
389                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
390                     public Object JavaDoc run() {
391                         actionContext.getProgress().getPanel().getCancelButton().doClick();
392                         return null;
393                     }
394                 });
395                 // at this point cancel() should already have been called
396
}
397
398             public boolean isEnabled() {
399                 return true;
400             }
401
402             public boolean getRunInEventThread() {
403                 return false;
404             }
405
406             public boolean cancel() {
407                 cancelInvokedInEDT[0] = SwingUtilities.isEventDispatchThread();
408                 cancelInvoked[0] = true;
409                 return true;
410             }
411         });
412
413         actions.add(new EventRequestProcessor.AsynchronousAction() {
414             public void run(EventRequestProcessor.Context actionContext) {
415                 secondActionInvoked[0] = true;
416             }
417         });
418
419         assertFalse(erp.invoke(actions, true));
420         assertFalse(secondActionInvoked[0]);
421         assertTrue(cancelInvokedInEDT[0]);
422         assertTrue(cancelInvoked[0]);
423     }
424
425     public void testNoCancelWhenTheCancelMethodReturnsFalse() {
426         EventRequestProcessor erp = new EventRequestProcessor();
427         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
428
429         final boolean[] secondActionInvoked = new boolean[1];
430
431         actions.add(new EventRequestProcessor.CancellableAction() {
432             public void run(final EventRequestProcessor.Context actionContext) {
433                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
434                     public Object JavaDoc run() {
435                         actionContext.getProgress().getPanel().getCancelButton().doClick();
436                         return null;
437                     }
438                 });
439                 // at this point cancel() should already have been called
440
}
441
442             public boolean isEnabled() {
443                 return true;
444             }
445
446             public boolean getRunInEventThread() {
447                 return false;
448             }
449
450             public boolean cancel() {
451                 return false;
452             }
453         });
454
455         actions.add(new EventRequestProcessor.AsynchronousAction() {
456             public void run(EventRequestProcessor.Context actionContext) {
457                 secondActionInvoked[0] = true;
458             }
459         });
460
461         assertTrue(erp.invoke(actions, true));
462         assertTrue(secondActionInvoked[0]);
463     }
464
465     public void testEscapeDoesNotCloseDialogForAsyncNonCancellableActions() {
466         EventRequestProcessor erp = new EventRequestProcessor();
467         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
468
469         final boolean[] panelOpen = new boolean[1];
470
471         actions.add(new EventRequestProcessor.AsynchronousAction() {
472             public void run(final EventRequestProcessor.Context actionContext) {
473                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
474                     public Object JavaDoc run() {
475                         // fake an escape key press
476
JRootPane JavaDoc rootPane = actionContext.getProgress().getPanel().getRootPane();
477                         KeyEvent JavaDoc event = new KeyEvent JavaDoc(rootPane, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, KeyEvent.CHAR_UNDEFINED);
478                         rootPane.dispatchEvent(event);
479
480                         panelOpen[0] = actionContext.getProgress().getPanel().isOpen();
481                         return null;
482                     }
483                 });
484             }
485         });
486
487         erp.invoke(actions);
488         assertTrue(panelOpen[0]);
489     }
490
491     public void testEscapeCancelsCancellableActions() {
492         EventRequestProcessor erp = new EventRequestProcessor();
493         List JavaDoc<EventRequestProcessor.Action> actions = new ArrayList JavaDoc<EventRequestProcessor.Action>();
494
495         final boolean[] panelOpen = new boolean[1];
496         final boolean[] cancelEnabled = new boolean[1];
497
498         actions.add(new EventRequestProcessor.CancellableAction() {
499             public void run(final EventRequestProcessor.Context actionContext) {
500                 Mutex.EVENT.readAccess(new Mutex.Action<Object JavaDoc>() {
501                     public Object JavaDoc run() {
502                         // fake an escape key press
503
JRootPane JavaDoc rootPane = actionContext.getProgress().getPanel().getRootPane();
504                         KeyEvent JavaDoc event = new KeyEvent JavaDoc(rootPane, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, KeyEvent.CHAR_UNDEFINED);
505                         rootPane.dispatchEvent(event);
506
507                         panelOpen[0] = actionContext.getProgress().getPanel().isOpen();
508                         cancelEnabled[0] = actionContext.getProgress().getPanel().getCancelButton().isEnabled();
509                         return null;
510                     }
511                 });
512             }
513
514             public boolean isEnabled() {
515                 return true;
516             }
517
518             public boolean getRunInEventThread() {
519                 return false;
520             }
521
522             public boolean cancel() {
523                 return true;
524             }
525         });
526
527         erp.invoke(actions, true);
528         assertTrue(panelOpen[0]);
529         assertFalse(cancelEnabled[0]);
530     }
531 }
532
Popular Tags