KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.util.Arrays JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.ActionMap JavaDoc;
26
27 import junit.textui.TestRunner;
28
29 import org.netbeans.junit.*;
30 import org.openide.actions.*;
31 import org.openide.util.Lookup;
32 import org.openide.util.actions.CallbackSystemAction;
33
34
35 /** Test behaviour of regular callback actions.
36  */

37 public abstract class AbstractCallbackActionTestHidden extends NbTestCase {
38     public AbstractCallbackActionTestHidden(String JavaDoc name) {
39         super(name);
40     }
41
42
43
44
45     /** global action */
46     protected org.openide.util.actions.CallbackSystemAction global;
47     
48     /** our action that is being added into the map */
49     protected OurAction action = new OurAction ();
50     
51     /** map that we lookup action in */
52     protected ActionMap JavaDoc map;
53     /** the clonned action */
54     protected Action JavaDoc clone;
55     
56     /** listener that is attached to the clone action and allows counting of prop events.*/
57     protected CntListener listener;
58     
59     /** that is the action being clonned to */
60     private Lookup lookup;
61     
62     /** Which action to test should be subclass of CallbackSystemAction.
63      */

64     protected abstract Class JavaDoc actionClass ();
65     
66     /** The key that is used in the action map
67      */

68     protected abstract String JavaDoc actionKey ();
69
70     
71     
72     protected boolean runInEQ () {
73         return true;
74     }
75     
76     protected void setUp() throws Exception JavaDoc {
77         global = (CallbackSystemAction)CallbackSystemAction.get (actionClass ());
78         map = new ActionMap JavaDoc ();
79         map.put (actionKey (), action);
80         lookup = org.openide.util.lookup.Lookups.singleton(map);
81         // Retrieve context sensitive action instance if possible.
82
clone = global.createContextAwareInstance(lookup);
83         
84         listener = new CntListener ();
85         clone.addPropertyChangeListener(listener);
86     }
87     
88     public void testThatDefaultEditorKitPasteActionIsTheCorrectKeyOfPasteAction () {
89         clone.actionPerformed (new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
90         action.assertCnt ("Clone correctly delegates to OurAction", 1);
91     }
92     
93     public void testChangesAreCorrectlyPropagatedToTheDelegate () {
94         action.setEnabled (true);
95         
96         assertTrue ("Clone is correctly enabled", clone.isEnabled ());
97         
98         action.setEnabled (false);
99         assertTrue ("Clone is disabled", !clone.isEnabled());
100         listener.assertCnt ("Change notified", 1);
101         
102         action.setEnabled (true);
103         listener.assertCnt ("Change notified again", 1);
104         assertTrue ("Clone is correctly enabled", clone.isEnabled ());
105     }
106     
107     protected static final class OurAction extends javax.swing.AbstractAction JavaDoc {
108         private int cnt;
109         private java.util.HashSet JavaDoc listeners = new java.util.HashSet JavaDoc ();
110         
111         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
112             cnt++;
113         }
114         
115         public void assertCnt (String JavaDoc msg, int count) {
116             assertEquals (msg, count, this.cnt);
117             this.cnt = 0;
118         }
119         
120         public void assertListeners (String JavaDoc msg, int count) throws Exception JavaDoc {
121             if (count == 0) {
122                 synchronized (this) {
123                     int c = 5;
124                     while (this.listeners.size () != 0 && c-- > 0) {
125                         System.gc ();
126                         wait (500);
127                     }
128                 }
129             }
130             
131             if (count != this.listeners.size ()) {
132                 fail (msg + " listeners expected: " + count + " but are " + this.listeners);
133             }
134         }
135         
136         public void addPropertyChangeListener (java.beans.PropertyChangeListener JavaDoc listener) {
137             super.addPropertyChangeListener (listener);
138             listeners.add (listener);
139         }
140         
141         public synchronized void removePropertyChangeListener (java.beans.PropertyChangeListener JavaDoc listener) {
142             super.removePropertyChangeListener (listener);
143             listeners.remove (listener);
144             notifyAll ();
145         }
146     } // end of OurAction
147

148     protected static final class CntListener extends Object JavaDoc
149     implements java.beans.PropertyChangeListener JavaDoc {
150         private int cnt;
151         
152         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
153             cnt++;
154         }
155         
156         public void assertCnt (String JavaDoc msg, int count) {
157             assertEquals (msg, count, this.cnt);
158             this.cnt = 0;
159         }
160     } // end of CntListener
161
}
162
Popular Tags