KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > ActionsTest


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.ant.freeform;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.TreeSet JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.spi.project.ActionProvider;
36 import org.netbeans.spi.project.ui.LogicalViewProvider;
37 import org.netbeans.spi.project.ui.support.CommonProjectActions;
38 import org.openide.actions.FindAction;
39 import org.openide.actions.ToolsAction;
40 import org.openide.filesystems.FileObject;
41 import org.openide.loaders.DataObject;
42 import org.openide.util.ContextAwareAction;
43 import org.openide.util.Lookup;
44 import org.openide.util.NbBundle;
45 import org.openide.util.NbCollections;
46 import org.openide.util.Utilities;
47 import org.openide.util.actions.SystemAction;
48 import org.openide.util.lookup.Lookups;
49
50 /**
51  * Test functionality of actions in FreeformProject.
52  * This class just tests the basic functionality found in the "simple" project.
53  * @author Jesse Glick
54  */

55 public class ActionsTest extends TestBase {
56     
57     private static final class AntTargetInvocation {
58         public final FileObject scriptFile;
59         public final String JavaDoc[] targetNameArray;
60         public final Map JavaDoc<String JavaDoc,String JavaDoc> props;
61         public AntTargetInvocation(FileObject scriptFile, String JavaDoc[] targetNameArray, Map JavaDoc<String JavaDoc,String JavaDoc> props) {
62             assert scriptFile != null;
63             this.scriptFile = scriptFile;
64             this.targetNameArray = targetNameArray;
65             this.props = props != null ? new HashMap JavaDoc<String JavaDoc,String JavaDoc>(props) : Collections.<String JavaDoc,String JavaDoc>emptyMap();
66         }
67         public String JavaDoc toString() {
68             return "invocation<script=" + scriptFile + ",targets=" + (targetNameArray != null ? Arrays.toString(targetNameArray) : null) + ",props=" + props + ">";
69         }
70         public boolean equals(Object JavaDoc obj) {
71             if (!(obj instanceof AntTargetInvocation)) {
72                 return false;
73             }
74             AntTargetInvocation other = (AntTargetInvocation) obj;
75             return other.scriptFile == scriptFile &&
76                 Utilities.compareObjects(other.targetNameArray, targetNameArray) &&
77                 other.normalizedProps().equals(normalizedProps());
78         }
79         public int hashCode() {
80             int x = scriptFile.hashCode() ^ props.hashCode();
81             if (targetNameArray != null) {
82                 x ^= Arrays.asList(targetNameArray).hashCode();
83             }
84             return x;
85         }
86         private Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> normalizedProps() {
87             Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> m = new HashMap JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>>();
88             for (Map.Entry JavaDoc<String JavaDoc,String JavaDoc> e : props.entrySet()) {
89                 m.put(e.getKey(), new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(e.getValue().split(","))));
90             }
91             return m;
92         }
93     }
94     
95     private static final List JavaDoc<AntTargetInvocation> targetsRun = new ArrayList JavaDoc<AntTargetInvocation>();
96     
97     static {
98         Actions.TARGET_RUNNER = new Actions.TargetRunner() {
99             public void runTarget(FileObject scriptFile, String JavaDoc[] targetNameArray, Properties JavaDoc props) {
100                 targetsRun.add(new AntTargetInvocation(scriptFile, targetNameArray,
101                         NbCollections.checkedMapByFilter(props, String JavaDoc.class, String JavaDoc.class, true)));
102             }
103         };
104     }
105     
106     public ActionsTest(String JavaDoc name) {
107         super(name);
108     }
109     
110     private FileObject buildXml;
111     private ActionProvider ap;
112     private LogicalViewProvider lvp;
113     private DataObject myAppJavaDO, someFileJavaDO, someResourceTxtDO, specialTaskJavaDO;
114     
115     protected void setUp() throws Exception JavaDoc {
116         super.setUp();
117         targetsRun.clear();
118         buildXml = simple.getProjectDirectory().getFileObject("build.xml");
119         assertNotNull("found build.xml", buildXml);
120         ap = simple.getLookup().lookup(ActionProvider.class);
121         assertNotNull("have an action provider", ap);
122         FileObject myAppJava = simple.getProjectDirectory().getFileObject("src/org/foo/myapp/MyApp.java");
123         assertNotNull("have MyApp.java", myAppJava);
124         myAppJavaDO = DataObject.find(myAppJava);
125         FileObject someFileJava = simple.getProjectDirectory().getFileObject("src/org/foo/myapp/SomeFile.java");
126         assertNotNull("have SomeFile.java", someFileJava);
127         someFileJavaDO = DataObject.find(someFileJava);
128         FileObject someResourceTxt = simple.getProjectDirectory().getFileObject("src/org/foo/myapp/some-resource.txt");
129         assertNotNull("have some-resource.txt", someResourceTxt);
130         someResourceTxtDO = DataObject.find(someResourceTxt);
131         FileObject specialTaskJava = simple.getProjectDirectory().getFileObject("antsrc/org/foo/ant/SpecialTask.java");
132         assertNotNull("have SpecialTask.java", specialTaskJava);
133         specialTaskJavaDO = DataObject.find(specialTaskJava);
134         lvp = simple.getLookup().lookup(LogicalViewProvider.class);
135         assertNotNull("have a LogicalViewProvider", lvp);
136     }
137     
138     public boolean runInEQ () {
139         return true;
140     }
141     
142     public void testBasicActions() throws Exception JavaDoc {
143         List JavaDoc<String JavaDoc> actionNames = new ArrayList JavaDoc<String JavaDoc>(Arrays.asList(ap.getSupportedActions()));
144         Collections.sort(actionNames);
145         assertEquals("right action names", Arrays.asList(
146             "build",
147             "clean",
148             "compile.single",
149             "copy",
150             "delete",
151             "javadoc",
152             "move",
153             "rebuild",
154             // #46886: COMMON_NON_IDE_GLOBAL_ACTIONS are now also enabled
155
"redeploy",
156             "rename",
157             "run",
158             "run.single",
159             // #46886 again
160
"test"),
161             actionNames);
162         assertTrue("clean is enabled", ap.isActionEnabled("clean", Lookup.EMPTY));
163         try {
164             ap.isActionEnabled("frobnitz", Lookup.EMPTY);
165             fail("Should throw IAE for unrecognized commands");
166         } catch (IllegalArgumentException JavaDoc e) {
167             // Good.
168
}
169         try {
170             ap.invokeAction("goetterdaemmerung", Lookup.EMPTY);
171             fail("Should throw IAE for unrecognized commands");
172         } catch (IllegalArgumentException JavaDoc e) {
173             // Good.
174
}
175         ap.invokeAction("rebuild", Lookup.EMPTY);
176         AntTargetInvocation inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"clean", "jar"}, null);
177         assertEquals("ran right target", Collections.singletonList(inv), targetsRun);
178     }
179     
180     public void testLogicalViewActions() throws Exception JavaDoc {
181         Action JavaDoc[] actions = lvp.createLogicalView().getActions(false);
182         assertNotNull("have some context actions", actions);
183         ResourceBundle JavaDoc bundle = NbBundle.getBundle(Actions.class);
184         assertEquals("correct labels", Arrays.asList(
185             (String JavaDoc) CommonProjectActions.newFileAction().getValue(Action.NAME),
186             null,
187             bundle.getString("CMD_build"),
188             bundle.getString("CMD_clean"),
189             bundle.getString("CMD_rebuild"),
190             null,
191             bundle.getString("CMD_run"),
192             null,
193             bundle.getString("CMD_javadoc"),
194             "Generate XDocs",
195             null,
196             "Create Distribution",
197             null,
198             (String JavaDoc) CommonProjectActions.setAsMainProjectAction().getValue(Action.NAME),
199             (String JavaDoc) CommonProjectActions.openSubprojectsAction().getValue(Action.NAME),
200             (String JavaDoc) CommonProjectActions.closeProjectAction().getValue(Action.NAME),
201             null,
202             (String JavaDoc) CommonProjectActions.renameProjectAction().getValue(Action.NAME),
203             (String JavaDoc) CommonProjectActions.moveProjectAction().getValue(Action.NAME),
204             (String JavaDoc) CommonProjectActions.copyProjectAction().getValue(Action.NAME),
205             (String JavaDoc) CommonProjectActions.deleteProjectAction().getValue(Action.NAME),
206             null,
207             (String JavaDoc) SystemAction.get(FindAction.class).getValue(Action.NAME),
208             null,
209             (String JavaDoc) SystemAction.get(ToolsAction.class).getValue(Action.NAME),
210             null,
211             (String JavaDoc) CommonProjectActions.customizeProjectAction().getValue(Action.NAME)),
212             findActionLabels(actions));
213         Action JavaDoc javadocAction = actions[8];
214         assertEquals("this is Run Javadoc", bundle.getString("CMD_javadoc"), javadocAction.getValue(Action.NAME));
215         runContextMenuAction(javadocAction, simple);
216         AntTargetInvocation inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"build-javadoc"}, Collections.singletonMap("from-ide", "true"));
217         assertEquals("ran right target", Collections.singletonList(inv), targetsRun);
218         targetsRun.clear();
219         Action JavaDoc xdocsAction = actions[9];
220         assertEquals("this is Generate XDocs", "Generate XDocs", xdocsAction.getValue(Action.NAME));
221         runContextMenuAction(xdocsAction, simple);
222         inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"generate-xdocs"}, Collections.singletonMap("from-ide", "true"));
223         assertEquals("ran right target", Collections.singletonList(inv), targetsRun);
224     }
225     
226     private static List JavaDoc<String JavaDoc> findActionLabels(Action JavaDoc[] actions) {
227         String JavaDoc[] labels = new String JavaDoc[actions.length];
228         for (int i = 0; i < actions.length; i++) {
229             if (actions[i] != null) {
230                 String JavaDoc label = (String JavaDoc) actions[i].getValue(Action.NAME);
231                 if (label == null) {
232                     label = "???";
233                 }
234                 labels[i] = label;
235             } else {
236                 labels[i] = null;
237             }
238         }
239         return Arrays.asList(labels);
240     }
241     
242     /**
243      * Run an action as if it were in the context menu of a project.
244      */

245     private void runContextMenuAction(Action JavaDoc a, Project p) {
246         if (a instanceof ContextAwareAction) {
247             Lookup l = Lookups.singleton(p);
248             a = ((ContextAwareAction) a).createContextAwareInstance(l);
249         }
250         a.actionPerformed(null);
251     }
252     
253     public void testContextSensitiveActions() throws Exception JavaDoc {
254         assertFalse("c.s disabled on empty selection", ap.isActionEnabled("compile.single", Lookup.EMPTY));
255         assertTrue("c.s enabled on SomeFile.java", ap.isActionEnabled("compile.single", Lookups.singleton(someFileJavaDO)));
256         assertTrue("c.s enabled on SomeFile.java (FileObject)", ap.isActionEnabled("compile.single", Lookups.singleton(someFileJavaDO.getPrimaryFile())));
257         assertTrue("c.s enabled on SpecialTask.java", ap.isActionEnabled("compile.single", Lookups.singleton(specialTaskJavaDO)));
258         assertFalse("c.s disabled on some-resource.txt", ap.isActionEnabled("compile.single", Lookups.singleton(someResourceTxtDO)));
259         assertTrue("c.s enabled on similar *.java", ap.isActionEnabled("compile.single", Lookups.fixed(someFileJavaDO, myAppJavaDO)));
260         assertFalse("c.s disabled on mixed *.java", ap.isActionEnabled("compile.single", Lookups.fixed(someFileJavaDO, specialTaskJavaDO)));
261         assertFalse("c.s disabled on mixed types", ap.isActionEnabled("compile.single", Lookups.fixed(someFileJavaDO, someResourceTxtDO)));
262         assertFalse("r.s disabled on empty selection", ap.isActionEnabled("run.single", Lookup.EMPTY));
263         assertTrue("r.s enabled on SomeFile.java", ap.isActionEnabled("run.single", Lookups.singleton(someFileJavaDO)));
264         assertFalse("r.s disabled on SpecialTask.java", ap.isActionEnabled("run.single", Lookups.singleton(specialTaskJavaDO)));
265         assertFalse("r.s disabled on some-resource.txt", ap.isActionEnabled("run.single", Lookups.singleton(someResourceTxtDO)));
266         assertFalse("r.s disabled on multiple files", ap.isActionEnabled("run.single", Lookups.fixed(someFileJavaDO, myAppJavaDO)));
267         ap.invokeAction("compile.single", Lookups.singleton(someFileJavaDO));
268         AntTargetInvocation inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"compile-some-files"}, Collections.singletonMap("files", "org/foo/myapp/SomeFile.java"));
269         assertEquals("compiled one file in src", Collections.singletonList(inv), targetsRun);
270         targetsRun.clear();
271         ap.invokeAction("compile.single", Lookups.singleton(someFileJavaDO.getPrimaryFile()));
272         inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"compile-some-files"}, Collections.singletonMap("files", "org/foo/myapp/SomeFile.java"));
273         assertEquals("compiled one file in src (FileObject)", Collections.singletonList(inv), targetsRun);
274         targetsRun.clear();
275         ap.invokeAction("compile.single", Lookups.singleton(specialTaskJavaDO));
276         inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"ant-compile-some-files"}, Collections.singletonMap("files", "org/foo/ant/SpecialTask.java"));
277         assertEquals("compiled one file in antsrc", Collections.singletonList(inv), targetsRun);
278         targetsRun.clear();
279         ap.invokeAction("compile.single", Lookups.fixed(someFileJavaDO, myAppJavaDO));
280         inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"compile-some-files"}, Collections.singletonMap("files", "org/foo/myapp/SomeFile.java,org/foo/myapp/MyApp.java"));
281         assertEquals("compiled two files in src", Collections.singletonList(inv), targetsRun);
282         targetsRun.clear();
283         ap.invokeAction("run.single", Lookups.singleton(someFileJavaDO));
284         inv = new AntTargetInvocation(buildXml, new String JavaDoc[] {"start-with-specified-class"}, Collections.singletonMap("class", "org.foo.myapp.SomeFile"));
285         assertEquals("ran one file in src", Collections.singletonList(inv), targetsRun);
286     }
287     
288 }
289
Popular Tags