KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > SaveAllActionTest


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.openide.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import org.netbeans.junit.MockServices;
24 import org.netbeans.junit.NbTestCase;
25 import org.openide.LifecycleManager;
26
27 /**
28  *
29  * @author Jaroslav Tulach
30  */

31 public class SaveAllActionTest extends NbTestCase {
32
33     public SaveAllActionTest (String JavaDoc testName) {
34         super (testName);
35     }
36
37     protected boolean runInEQ () {
38         return true;
39     }
40
41
42     protected void setUp () {
43         MockServices.setServices(new Class JavaDoc[] {Life.class});
44         Life.max = 0;
45         Life.cnt = 0;
46         Life.executed = 0;
47     }
48
49     public void testThatTheActionIsInvokeOutsideOfAWTThreadAndOnlyOnceAtATime () throws Exception JavaDoc {
50         SaveAllAction a = (SaveAllAction)SaveAllAction.get (SaveAllAction.class);
51         a.setEnabled (true);
52         assertTrue ("Is enabled", a.isEnabled ());
53         
54         ActionEvent JavaDoc ev = new ActionEvent JavaDoc (this, 0, "");
55         a.actionPerformed (ev);
56         a.actionPerformed (ev);
57         a.actionPerformed (ev);
58         
59         Object JavaDoc life = org.openide.util.Lookup.getDefault ().lookup (LifecycleManager.class);
60         synchronized (life) {
61             while (Life.executed != 3) {
62                 life.wait ();
63             }
64         }
65         
66         assertEquals ("Maximum is one invocation of saveAll at one time", 1, Life.max);
67     }
68
69     public static final class Life extends LifecycleManager {
70         static int max;
71         static int cnt;
72         static int executed;
73         
74         public synchronized void saveAll () {
75             cnt++;
76             if (cnt > max) {
77                 max = cnt;
78             }
79             try {
80                 wait (500);
81             } catch (Exception JavaDoc ex) {
82                 
83             }
84             executed++;
85             notifyAll ();
86             
87             cnt--;
88             assertFalse ("No AWT thread: ", javax.swing.SwingUtilities.isEventDispatchThread ());
89         }
90
91         public void exit () {
92             fail ("Not supported");
93         }
94         
95     }
96 }
97
Popular Tags