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: package org.netbeans.core;
042:
043: import java.awt.Toolkit;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.KeyEvent;
046: import java.io.IOException;
047: import java.lang.ref.Reference;
048: import java.lang.ref.WeakReference;
049: import java.util.Arrays;
050: import java.util.Collections;
051: import java.util.logging.Level;
052: import javax.swing.AbstractAction;
053: import javax.swing.Action;
054: import javax.swing.KeyStroke;
055: import javax.swing.text.Keymap;
056: import org.netbeans.core.startup.Main;
057: import org.netbeans.junit.*;
058: import org.openide.ErrorManager;
059: import org.openide.cookies.InstanceCookie;
060: import org.openide.filesystems.FileLock;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileSystem;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.filesystems.Repository;
065: import org.openide.loaders.DataObject;
066: import org.openide.util.Lookup;
067:
068: public class ShortcutsFolderTest extends NbTestCase {
069: private ErrorManager err;
070: private Keymap keymap;
071:
072: /** Constructor required by JUnit.
073: * @param testName method name to be used as testcase
074: */
075: public ShortcutsFolderTest(String s) {
076: super (s);
077: }
078:
079: protected Level logLevel() {
080: return Level.ALL;
081: }
082:
083: protected void setUp() throws Exception {
084: MockServices.setServices(ENV.class);
085:
086: Main.initializeURLFactory();
087: keymap = Lookup.getDefault().lookup(Keymap.class);
088:
089: assertNotNull("There is a keymap", keymap);
090: ShortcutsFolder.initShortcuts();
091:
092: err = ErrorManager.getDefault()
093: .getInstance("TEST-" + getName());
094: }
095:
096: public void testApplyChangeToFactoryActionIssue49597()
097: throws Exception {
098: final FileSystem fs = Repository.getDefault()
099: .getDefaultFileSystem();
100: final FileObject shortcuts = fs.getRoot().getFileObject(
101: "Shortcuts");
102: FileObject inst = FileUtil.createData(fs.getRoot(),
103: "/Actions/Tools/TestAction.instance");
104: TestAction action = new TestAction();
105: inst.setAttribute("instanceCreate", action);
106:
107: Reference<?> ref = new WeakReference<Object>(inst);
108: inst = null;
109: assertGC("File can disappear", ref);
110:
111: // ShortcutsFolder.waitFinished ();
112:
113: assertEquals("Nothing registered", Collections.EMPTY_LIST,
114: Arrays.asList(keymap.getBoundActions()));
115:
116: final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F9,
117: KeyEvent.ALT_MASK
118: | Toolkit.getDefaultToolkit()
119: .getMenuShortcutKeyMask());
120:
121: class R implements FileSystem.AtomicAction {
122: FileObject inst2;
123:
124: public void run() throws IOException {
125: inst2 = FileUtil.createData(fs.getRoot(),
126: "/Shortcuts/CA-F9.shadow");
127: inst2.setAttribute("originalFile",
128: "/Actions/Tools/TestAction.instance");
129: }
130: }
131: R run = new R();
132: fs.runAtomicAction(run);
133:
134: ShortcutsFolder.waitFinished();
135: err.log("ShortcutsFolder.waitFinished");
136:
137: FileObject[] arr = shortcuts.getChildren();
138: err.log("children are here");
139:
140: assertEquals("One element is there", 1, arr.length);
141: org.openide.loaders.DataObject obj = org.openide.loaders.DataObject
142: .find(arr[0]);
143: err.log("Object is here" + obj);
144:
145: assertEquals("It is DataShadow",
146: org.openide.loaders.DataShadow.class, obj.getClass());
147:
148: Object a = keymap.getAction(stroke);
149: assertNotNull("There is an action", a);
150: assertEquals("It is test action", TestAction.class, a
151: .getClass());
152: }
153:
154: public void testShortcutsForDifferentFilesThanInstanceOrShadows()
155: throws Exception {
156: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
157: FileObject shortcuts = fs.getRoot().getFileObject("Shortcuts");
158: FileObject inst = FileUtil.createData(fs.getRoot(),
159: "/Shortcuts/C-F11.xml");
160:
161: FileLock lock = inst.lock();
162: java.io.PrintStream ps = new java.io.PrintStream(inst
163: .getOutputStream(lock));
164: ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
165: ps
166: .println("<project default=\"run\" name=\"Build everything.\">");
167: ps.println("<target name=\"run\">");
168: ps
169: .println("<ant antfile=\"SampleProject.xml\" inheritall=\"false\" target=\"all\"/>");
170: ps.println("</target>");
171: ps.println("</project>");
172: ps.close();
173: lock.releaseLock();
174:
175: DataObject obj = DataObject.find(inst);
176: assertEquals("XML Data object",
177: org.openide.loaders.XMLDataObject.class, obj.getClass());
178: InstanceCookie ic = obj.getCookie(InstanceCookie.class);
179: assertNotNull("Has cookie", ic);
180:
181: final KeyStroke stroke = KeyStroke.getKeyStroke(
182: KeyEvent.VK_F11, KeyEvent.CTRL_MASK);
183:
184: ShortcutsFolder.waitFinished();
185:
186: Action action = keymap.getAction(stroke);
187: if (action == null) {
188: fail("There should be some action for " + stroke + " in:\n"
189: + keymap);
190: }
191:
192: inst.delete();
193: ShortcutsFolder.waitFinished();
194: action = keymap.getAction(stroke);
195: assertNull("Action removed", action);
196: }
197:
198: public static class TestAction extends AbstractAction {
199: public void actionPerformed(ActionEvent ae) {
200: }
201: }
202:
203: public static class ENV extends Object implements
204: org.openide.loaders.Environment.Provider {
205: public Lookup getEnvironment(DataObject obj) {
206: if (obj instanceof org.openide.loaders.XMLDataObject) {
207: try {
208: org.w3c.dom.Document doc = ((org.openide.loaders.XMLDataObject) obj)
209: .getDocument();
210: if (doc.getDocumentElement().getNodeName().equals(
211: "project")) {
212: return org.openide.util.lookup.Lookups
213: .singleton(new org.openide.loaders.InstanceSupport.Instance(
214: new TestAction()));
215: }
216: } catch (Exception ex) {
217: ex.printStackTrace();
218: fail("No exception: " + ex.getMessage());
219: }
220: }
221: return org.openide.util.Lookup.EMPTY;
222: }
223: }
224:
225: }
|