001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui.actions;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Iterator;
047: import java.util.List;
048: import javax.swing.Icon;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.junit.MockServices;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.modules.project.ui.actions.ProjectActionTest.ActionCreator;
054: import org.netbeans.spi.project.ActionProvider;
055: import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.loaders.DataObject;
059: import org.openide.util.Lookup;
060: import org.openide.util.lookup.Lookups;
061:
062: public class FileCommandActionTest extends NbTestCase {
063:
064: public FileCommandActionTest(String name) {
065: super (name);
066: }
067:
068: private FileObject p1;
069: private FileObject p2;
070: private FileObject f1_1;
071: private FileObject f1_2;
072: private FileObject f2_1;
073: private FileObject f2_2;
074: private DataObject d1_1;
075: private DataObject d1_2;
076: private DataObject d2_1;
077: private DataObject d2_2;
078: private TestSupport.TestProject project1;
079: private TestSupport.TestProject project2;
080:
081: protected void setUp() throws Exception {
082: super .setUp();
083: MockServices.setServices(TestSupport.TestProjectFactory.class);
084: clearWorkDir();
085: FileObject workDir = FileUtil.toFileObject(getWorkDir());
086:
087: p1 = TestSupport.createTestProject(workDir, "project1");
088: f1_1 = p1.createData("f1_1.java");
089: f1_2 = p1.createData("f1_2.krava");
090: d1_1 = DataObject.find(f1_1);
091: d1_2 = DataObject.find(f1_2);
092:
093: project1 = (TestSupport.TestProject) ProjectManager
094: .getDefault().findProject(p1);
095: project1.setLookup(Lookups
096: .fixed(new Object[] { new TestActionProvider() }));
097:
098: p2 = TestSupport.createTestProject(workDir, "project2");
099: f2_1 = p2.createData("f2_1.java");
100: f2_2 = p2.createData("f2_2.krava");
101: d2_1 = DataObject.find(f2_1);
102: d2_2 = DataObject.find(f2_2);
103:
104: project2 = (TestSupport.TestProject) ProjectManager
105: .getDefault().findProject(p2);
106:
107: }
108:
109: protected void tearDown() throws Exception {
110: clearWorkDir();
111: super .tearDown();
112: }
113:
114: public boolean runInEQ() {
115: return true;
116: }
117:
118: public void testCommandEnablement() {
119: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
120: FileCommandAction action = new FileCommandAction("COMMAND",
121: "TestFileCommandAction", (Icon) null, lookup);
122:
123: assertFalse("Action should NOT be enabled", action.isEnabled());
124:
125: lookup.change(d1_1);
126: assertTrue("Action should be enabled", action.isEnabled());
127:
128: lookup.change(d1_1, d1_2);
129: assertFalse("Action should NOT be enabled", action.isEnabled());
130:
131: lookup.change(d1_1, d2_1);
132: assertFalse("Action should NOT be enabled", action.isEnabled());
133:
134: }
135:
136: public void testAcceleratorsPropagated() {
137: ProjectActionTest.doTestAcceleratorsPropagated(
138: new ActionCreator() {
139: public ProjectAction create(Lookup l) {
140: return new FileCommandAction("command",
141: "TestProjectAction", (Icon) null, l);
142: }
143: }, true);
144: }
145:
146: private static class TestActionProvider implements ActionProvider {
147:
148: public String COMMAND = "COMMAND";
149:
150: private String[] ACTIONS = new String[] { COMMAND };
151:
152: private List<String> invocations = new ArrayList<String>();
153:
154: public String[] getSupportedActions() {
155: return ACTIONS;
156: }
157:
158: public void invokeAction(String command, Lookup context)
159: throws IllegalArgumentException {
160:
161: if (COMMAND.equals(command)) {
162: invocations.add(command);
163: } else {
164: throw new IllegalArgumentException();
165: }
166:
167: }
168:
169: public boolean isActionEnabled(String command, Lookup context)
170: throws IllegalArgumentException {
171:
172: if (COMMAND.equals(command)) {
173: Collection dobjs = context.lookupAll(DataObject.class);
174: for (Iterator it = dobjs.iterator(); it.hasNext();) {
175: DataObject dobj = (DataObject) it.next();
176: if (!dobj.getPrimaryFile().getNameExt().endsWith(
177: ".java")) {
178: return false;
179: }
180: }
181: return true;
182: } else {
183: throw new IllegalArgumentException();
184: }
185:
186: }
187:
188: }
189:
190: private static class TestActionPerformer implements
191: ProjectActionPerformer {
192:
193: private int enableCount;
194: private Project project;
195: private boolean on;
196:
197: public boolean enable(Project project) {
198: enableCount++;
199: this .project = project;
200: return on;
201: }
202:
203: public void perform(Project project) {
204: this .project = project;
205: }
206:
207: public void on(boolean on) {
208: this .on = on;
209: }
210:
211: public void clear() {
212: this .project = null;
213: enableCount = 0;
214: }
215:
216: }
217:
218: }
|