KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > ProjectActionTest


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.project.ui.actions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.KeyStroke JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.api.project.ProjectManager;
28 import org.netbeans.junit.MockServices;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.spi.project.ActionProvider;
31 import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.loaders.DataObject;
35 import org.openide.util.Lookup;
36 import org.openide.util.lookup.Lookups;
37
38 public class ProjectActionTest extends NbTestCase {
39     
40     public ProjectActionTest(String JavaDoc name) {
41         super( name );
42     }
43
44     private FileObject p1;
45     private FileObject p2;
46     private FileObject f1_1;
47     private FileObject f1_2;
48     private FileObject f2_1;
49     private FileObject f2_2;
50     private DataObject d1_1;
51     private DataObject d1_2;
52     private DataObject d2_1;
53     private DataObject d2_2;
54     private TestSupport.TestProject project1;
55     private TestSupport.TestProject project2;
56
57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59         MockServices.setServices(TestSupport.TestProjectFactory.class);
60         clearWorkDir();
61         FileObject workDir = FileUtil.toFileObject(getWorkDir());
62     
63         
64         p1 = TestSupport.createTestProject( workDir, "project1" );
65         f1_1 = p1.createData("f1_1.java");
66         f1_2 = p1.createData("f1_2.krava");
67         d1_1 = DataObject.find(f1_1);
68         d1_2 = DataObject.find(f1_2);
69                
70         project1 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p1 );
71         project1.setLookup( Lookups.fixed( new Object JavaDoc[] { new TestActionProvider() } ) );
72         
73         p2 = TestSupport.createTestProject( workDir, "project2" );
74         f2_1 = p2.createData("f2_1.java");
75         f2_2 = p2.createData("f2_2.krava");
76         d2_1 = DataObject.find(f2_1);
77         d2_2 = DataObject.find(f2_2);
78                
79         project2 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p2 );
80     }
81     
82     protected void tearDown() throws Exception JavaDoc {
83         clearWorkDir();
84         super.tearDown();
85     }
86     
87     public boolean runInEQ () {
88         return true;
89     }
90     
91     public void testCommandEnablement() {
92         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
93         ProjectAction action = new ProjectAction( "COMMAND", "TestProjectAction", null, lookup );
94         
95         assertFalse( "Action should NOT be enabled", action.isEnabled() );
96         
97         lookup.change(d1_1);
98         assertTrue( "Action should be enabled", action.isEnabled() );
99         
100         lookup.change(d1_1, d1_2);
101         assertFalse( "Action should NOT be enabled", action.isEnabled() );
102         
103         lookup.change(d1_1, d2_1);
104         assertFalse( "Action should NOT be enabled", action.isEnabled() );
105         
106     }
107     
108     public void testProviderEnablement() {
109         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
110         TestActionPerformer tap = new TestActionPerformer();
111         ProjectAction action = new ProjectAction( tap, "TestProjectAction", null,lookup );
112      
113         assertFalse( "Action should NOT be enabled", action.isEnabled() );
114         assertEquals( "enable() should NOT be called: ", 0, tap.enableCount );
115         
116         tap.clear();
117         tap.on( true );
118         assertFalse( "Action should NOT be enabled", action.isEnabled() );
119         assertEquals( "enable() should NOT be called: ", 0, tap.enableCount );
120         
121         tap.clear();
122         tap.on( false );
123         lookup.change(d1_1);
124         assertFalse( "Action should NOT be enabled", action.isEnabled() );
125         assertEquals( "enable() should be called once: ", 1, tap.enableCount );
126         assertEquals( "enable() should be called on right project: ", project1.toString(), tap.project.toString() );
127         
128         tap.clear();
129         tap.on( true );
130         lookup.change(d1_2);
131         assertTrue( "Action should be enabled", action.isEnabled() );
132         assertEquals( "enable() should be called once: ", 1, tap.enableCount );
133         assertEquals( "enable() should be called on right project: ", project1.toString(), tap.project.toString() );
134                 
135         tap.clear();
136         tap.on( false );
137         lookup.change(d1_1, d2_1);
138         assertFalse( "Action should NOT be enabled", action.isEnabled() );
139         assertEquals( "enable() should NOT be called: ", 0, tap.enableCount );
140         
141     }
142     
143     public void testAcceleratorsPropagated() {
144         doTestAcceleratorsPropagated(new ActionCreator() {
145             public ProjectAction create(Lookup l) {
146                 return new ProjectAction("command", "TestProjectAction", null, l);
147             }
148         }, true);
149     }
150     
151     public static void doTestAcceleratorsPropagated(ActionCreator creator, boolean testMenus) {
152         Lookup l1 = Lookups.fixed(new Object JavaDoc[] {"1"});
153         Lookup l2 = Lookups.fixed(new Object JavaDoc[] {"2"});
154         
155         ProjectAction a1 = creator.create(l1);
156         
157         KeyStroke JavaDoc k1 = KeyStroke.getKeyStroke("shift pressed A");
158         KeyStroke JavaDoc k2 = KeyStroke.getKeyStroke("shift pressed A");
159         
160         assertNotNull(k1);
161         assertNotNull(k2);
162         
163         a1.putValue(Action.ACCELERATOR_KEY, k1);
164         
165         ProjectAction a2 = creator.create(l2);
166         
167         assertEquals(k1, a2.getValue(Action.ACCELERATOR_KEY));
168         
169         a2.putValue(Action.ACCELERATOR_KEY, k2);
170         
171         assertEquals(k2, a1.getValue(Action.ACCELERATOR_KEY));
172         
173         if (testMenus) {
174             assertEquals(k2, a2.getMenuPresenter().getAccelerator());
175         }
176
177         a1.putValue(Action.ACCELERATOR_KEY, k1);
178         assertEquals(k1, a2.getValue(Action.ACCELERATOR_KEY));
179         
180         if (testMenus) {
181             assertEquals(k1, a2.getMenuPresenter().getAccelerator());
182         }
183     }
184     
185     public static interface ActionCreator {
186         public ProjectAction create(Lookup l);
187     }
188     
189     private static class TestActionProvider implements ActionProvider {
190         
191         public String JavaDoc COMMAND = "COMMAND";
192         
193         private String JavaDoc[] ACTIONS = new String JavaDoc[] { COMMAND };
194         
195         private List JavaDoc<String JavaDoc> invocations = new ArrayList JavaDoc<String JavaDoc>();
196         
197         public String JavaDoc[] getSupportedActions() {
198             return ACTIONS;
199         }
200                 
201         public void invokeAction( String JavaDoc command, Lookup context ) throws IllegalArgumentException JavaDoc {
202             
203             if ( COMMAND.equals( command ) ) {
204                 invocations.add( command );
205             }
206             else {
207                 throw new IllegalArgumentException JavaDoc();
208             }
209             
210         }
211
212         public boolean isActionEnabled( String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
213             
214             if ( COMMAND.equals( command ) ) {
215                 for (DataObject dobj : context.lookupAll(DataObject.class)) {
216                     if ( !dobj.getPrimaryFile().getNameExt().endsWith( ".java" ) ) {
217                         return false;
218                     }
219                 }
220                 return true;
221             }
222             else {
223                 throw new IllegalArgumentException JavaDoc();
224             }
225             
226         }
227
228         
229     }
230     
231     private static class TestActionPerformer implements ProjectActionPerformer {
232         
233         private int enableCount;
234         private Project project;
235         private boolean on;
236         
237         public boolean enable( Project project ) {
238             enableCount ++;
239             this.project = project;
240             return on;
241         }
242
243         public void perform( Project project ) {
244             this.project = project;
245         }
246         
247         public void on( boolean on ) {
248             this.on = on;
249         }
250         
251         public void clear() {
252             this.project = null;
253             enableCount = 0;
254         }
255         
256         
257     }
258         
259 }
260
Popular Tags