1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 import java.util.ResourceBundle ; 31 import java.util.Set ; 32 import java.util.TreeSet ; 33 import javax.swing.Action ; 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 55 public class ActionsTest extends TestBase { 56 57 private static final class AntTargetInvocation { 58 public final FileObject scriptFile; 59 public final String [] targetNameArray; 60 public final Map <String ,String > props; 61 public AntTargetInvocation(FileObject scriptFile, String [] targetNameArray, Map <String ,String > props) { 62 assert scriptFile != null; 63 this.scriptFile = scriptFile; 64 this.targetNameArray = targetNameArray; 65 this.props = props != null ? new HashMap <String ,String >(props) : Collections.<String ,String >emptyMap(); 66 } 67 public String toString() { 68 return "invocation<script=" + scriptFile + ",targets=" + (targetNameArray != null ? Arrays.toString(targetNameArray) : null) + ",props=" + props + ">"; 69 } 70 public boolean equals(Object 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 <String ,Set <String >> normalizedProps() { 87 Map <String ,Set <String >> m = new HashMap <String ,Set <String >>(); 88 for (Map.Entry <String ,String > e : props.entrySet()) { 89 m.put(e.getKey(), new TreeSet <String >(Arrays.asList(e.getValue().split(",")))); 90 } 91 return m; 92 } 93 } 94 95 private static final List <AntTargetInvocation> targetsRun = new ArrayList <AntTargetInvocation>(); 96 97 static { 98 Actions.TARGET_RUNNER = new Actions.TargetRunner() { 99 public void runTarget(FileObject scriptFile, String [] targetNameArray, Properties props) { 100 targetsRun.add(new AntTargetInvocation(scriptFile, targetNameArray, 101 NbCollections.checkedMapByFilter(props, String .class, String .class, true))); 102 } 103 }; 104 } 105 106 public ActionsTest(String 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 { 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 { 143 List <String > actionNames = new ArrayList <String >(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 "redeploy", 156 "rename", 157 "run", 158 "run.single", 159 "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 e) { 167 } 169 try { 170 ap.invokeAction("goetterdaemmerung", Lookup.EMPTY); 171 fail("Should throw IAE for unrecognized commands"); 172 } catch (IllegalArgumentException e) { 173 } 175 ap.invokeAction("rebuild", Lookup.EMPTY); 176 AntTargetInvocation inv = new AntTargetInvocation(buildXml, new String [] {"clean", "jar"}, null); 177 assertEquals("ran right target", Collections.singletonList(inv), targetsRun); 178 } 179 180 public void testLogicalViewActions() throws Exception { 181 Action [] actions = lvp.createLogicalView().getActions(false); 182 assertNotNull("have some context actions", actions); 183 ResourceBundle bundle = NbBundle.getBundle(Actions.class); 184 assertEquals("correct labels", Arrays.asList( 185 (String ) 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 ) CommonProjectActions.setAsMainProjectAction().getValue(Action.NAME), 199 (String ) CommonProjectActions.openSubprojectsAction().getValue(Action.NAME), 200 (String ) CommonProjectActions.closeProjectAction().getValue(Action.NAME), 201 null, 202 (String ) CommonProjectActions.renameProjectAction().getValue(Action.NAME), 203 (String ) CommonProjectActions.moveProjectAction().getValue(Action.NAME), 204 (String ) CommonProjectActions.copyProjectAction().getValue(Action.NAME), 205 (String ) CommonProjectActions.deleteProjectAction().getValue(Action.NAME), 206 null, 207 (String ) SystemAction.get(FindAction.class).getValue(Action.NAME), 208 null, 209 (String ) SystemAction.get(ToolsAction.class).getValue(Action.NAME), 210 null, 211 (String ) CommonProjectActions.customizeProjectAction().getValue(Action.NAME)), 212 findActionLabels(actions)); 213 Action 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 [] {"build-javadoc"}, Collections.singletonMap("from-ide", "true")); 217 assertEquals("ran right target", Collections.singletonList(inv), targetsRun); 218 targetsRun.clear(); 219 Action 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 [] {"generate-xdocs"}, Collections.singletonMap("from-ide", "true")); 223 assertEquals("ran right target", Collections.singletonList(inv), targetsRun); 224 } 225 226 private static List <String > findActionLabels(Action [] actions) { 227 String [] labels = new String [actions.length]; 228 for (int i = 0; i < actions.length; i++) { 229 if (actions[i] != null) { 230 String label = (String ) 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 245 private void runContextMenuAction(Action 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 { 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 [] {"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 [] {"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 [] {"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 [] {"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 [] {"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 |