KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.junit.MockServices;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.modules.project.ui.actions.ProjectActionTest.ActionCreator;
32 import org.netbeans.spi.project.ActionProvider;
33 import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.loaders.DataObject;
37 import org.openide.util.Lookup;
38 import org.openide.util.lookup.Lookups;
39
40 public class FileCommandActionTest extends NbTestCase {
41     
42     public FileCommandActionTest(String JavaDoc name) {
43         super( name );
44     }
45
46     private FileObject p1;
47     private FileObject p2;
48     private FileObject f1_1;
49     private FileObject f1_2;
50     private FileObject f2_1;
51     private FileObject f2_2;
52     private DataObject d1_1;
53     private DataObject d1_2;
54     private DataObject d2_1;
55     private DataObject d2_2;
56     private TestSupport.TestProject project1;
57     private TestSupport.TestProject project2;
58
59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         MockServices.setServices(TestSupport.TestProjectFactory.class);
62         clearWorkDir();
63         FileObject workDir = FileUtil.toFileObject(getWorkDir());
64     
65         
66         p1 = TestSupport.createTestProject( workDir, "project1" );
67         f1_1 = p1.createData("f1_1.java");
68         f1_2 = p1.createData("f1_2.krava");
69         d1_1 = DataObject.find(f1_1);
70         d1_2 = DataObject.find(f1_2);
71                
72         project1 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p1 );
73         project1.setLookup( Lookups.fixed( new Object JavaDoc[] { new TestActionProvider() } ) );
74         
75         p2 = TestSupport.createTestProject( workDir, "project2" );
76         f2_1 = p2.createData("f2_1.java");
77         f2_2 = p2.createData("f2_2.krava");
78         d2_1 = DataObject.find(f2_1);
79         d2_2 = DataObject.find(f2_2);
80                
81         project2 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p2 );
82         
83     }
84     
85     protected void tearDown() throws Exception JavaDoc {
86         clearWorkDir();
87         super.tearDown();
88     }
89     
90     public boolean runInEQ () {
91         return true;
92     }
93     
94     public void testCommandEnablement() {
95         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
96         FileCommandAction action = new FileCommandAction( "COMMAND", "TestFileCommandAction", (Icon JavaDoc)null, lookup );
97         
98         assertFalse( "Action should NOT be enabled", action.isEnabled() );
99         
100         lookup.change(d1_1);
101         assertTrue( "Action should be enabled", action.isEnabled() );
102         
103         lookup.change(d1_1, d1_2);
104         assertFalse( "Action should NOT be enabled", action.isEnabled() );
105         
106         lookup.change(d1_1, d2_1);
107         assertFalse( "Action should NOT be enabled", action.isEnabled() );
108         
109     }
110     
111     public void testAcceleratorsPropagated() {
112         ProjectActionTest.doTestAcceleratorsPropagated(new ActionCreator() {
113             public ProjectAction create(Lookup l) {
114                 return new FileCommandAction("command", "TestProjectAction", (Icon JavaDoc) null, l);
115             }
116         }, true);
117     }
118     
119     private static class TestActionProvider implements ActionProvider {
120         
121         public String JavaDoc COMMAND = "COMMAND";
122         
123         private String JavaDoc[] ACTIONS = new String JavaDoc[] { COMMAND };
124         
125         private List JavaDoc<String JavaDoc> invocations = new ArrayList JavaDoc<String JavaDoc>();
126         
127         public String JavaDoc[] getSupportedActions() {
128             return ACTIONS;
129         }
130                 
131         public void invokeAction( String JavaDoc command, Lookup context ) throws IllegalArgumentException JavaDoc {
132             
133             if ( COMMAND.equals( command ) ) {
134                 invocations.add( command );
135             }
136             else {
137                 throw new IllegalArgumentException JavaDoc();
138             }
139             
140         }
141
142         public boolean isActionEnabled( String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
143             
144             if ( COMMAND.equals( command ) ) {
145                 Collection JavaDoc dobjs = context.lookupAll(DataObject.class);
146                 for ( Iterator JavaDoc it = dobjs.iterator(); it.hasNext(); ) {
147                     DataObject dobj = (DataObject)it.next();
148                     if ( !dobj.getPrimaryFile().getNameExt().endsWith( ".java" ) ) {
149                         return false;
150                     }
151                 }
152                 return true;
153             }
154             else {
155                 throw new IllegalArgumentException JavaDoc();
156             }
157             
158         }
159
160         
161     }
162     
163     private static class TestActionPerformer implements ProjectActionPerformer {
164         
165         private int enableCount;
166         private Project project;
167         private boolean on;
168         
169         public boolean enable( Project project ) {
170             enableCount ++;
171             this.project = project;
172             return on;
173         }
174
175         public void perform( Project project ) {
176             this.project = project;
177         }
178         
179         public void on( boolean on ) {
180             this.on = on;
181         }
182         
183         public void clear() {
184             this.project = null;
185             enableCount = 0;
186         }
187         
188         
189     }
190     
191 }
192
Popular Tags