KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > ShortcutsFolderTest


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 package org.netbeans.core;
20
21 import java.awt.Toolkit JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import javax.swing.AbstractAction JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.KeyStroke JavaDoc;
32 import javax.swing.text.Keymap JavaDoc;
33 import org.netbeans.core.startup.Main;
34 import org.netbeans.junit.*;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileLock;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileSystem;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.filesystems.Repository;
41 import org.openide.loaders.DataObject;
42 import org.openide.util.Lookup;
43
44 public class ShortcutsFolderTest extends NbTestCase {
45     private ErrorManager err;
46     private Keymap JavaDoc keymap;
47     
48     /** Constructor required by JUnit.
49      * @param testName method name to be used as testcase
50      */

51     public ShortcutsFolderTest(String JavaDoc s) {
52         super(s);
53     }
54     
55     protected Level JavaDoc logLevel() {
56         return Level.ALL;
57     }
58     
59     protected void setUp() throws Exception JavaDoc {
60         MockServices.setServices(ENV.class);
61
62         Main.initializeURLFactory ();
63         keymap = Lookup.getDefault().lookup(Keymap JavaDoc.class);
64         
65         assertNotNull("There is a keymap", keymap);
66         ShortcutsFolder.initShortcuts ();
67         
68         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
69     }
70     
71     public void testApplyChangeToFactoryActionIssue49597 () throws Exception JavaDoc {
72         final FileSystem fs = Repository.getDefault ().getDefaultFileSystem ();
73         final FileObject shortcuts = fs.getRoot ().getFileObject ("Shortcuts");
74         FileObject inst = FileUtil.createData (fs.getRoot (), "/Actions/Tools/TestAction.instance");
75         TestAction action = new TestAction ();
76         inst.setAttribute ("instanceCreate", action);
77         
78         WeakReference JavaDoc ref = new WeakReference JavaDoc (inst);
79         inst = null;
80         assertGC ("File can disappear", ref);
81
82 // ShortcutsFolder.waitFinished ();
83

84         assertEquals ("Nothing registered", Collections.EMPTY_LIST, Arrays.asList (keymap.getBoundActions ()));
85         
86         final KeyStroke JavaDoc stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F9, KeyEvent.ALT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
87
88         class R implements FileSystem.AtomicAction {
89             FileObject inst2;
90             
91             public void run() throws IOException JavaDoc {
92                 inst2 = FileUtil.createData (fs.getRoot (), "/Shortcuts/CA-F9.shadow");
93                 inst2.setAttribute ("originalFile", "/Actions/Tools/TestAction.instance");
94             }
95         }
96         R run = new R();
97         fs.runAtomicAction(run);
98
99         ShortcutsFolder.waitFinished ();
100         err.log("ShortcutsFolder.waitFinished");
101
102         FileObject[] arr = shortcuts.getChildren ();
103         err.log("children are here");
104         
105         assertEquals ("One element is there", 1, arr.length);
106         org.openide.loaders.DataObject obj = org.openide.loaders.DataObject.find (arr[0]);
107         err.log("Object is here" + obj);
108         
109         assertEquals ("It is DataShadow", org.openide.loaders.DataShadow.class, obj.getClass ());
110
111         Object JavaDoc a = keymap.getAction (stroke);
112         assertNotNull ("There is an action", a);
113         assertEquals ("It is test action", TestAction.class, a.getClass ());
114     }
115     
116     public void testShortcutsForDifferentFilesThanInstanceOrShadows () throws Exception JavaDoc {
117         FileSystem fs = Repository.getDefault ().getDefaultFileSystem ();
118         FileObject shortcuts = fs.getRoot ().getFileObject ("Shortcuts");
119         FileObject inst = FileUtil.createData (fs.getRoot (), "/Shortcuts/C-F11.xml");
120
121         FileLock lock = inst.lock ();
122         java.io.PrintStream JavaDoc ps = new java.io.PrintStream JavaDoc (inst.getOutputStream (lock));
123         ps.println ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
124         ps.println ("<project default=\"run\" name=\"Build everything.\">");
125         ps.println ("<target name=\"run\">");
126         ps.println ("<ant antfile=\"SampleProject.xml\" inheritall=\"false\" target=\"all\"/>");
127         ps.println ("</target>");
128         ps.println ("</project>");
129         ps.close();
130         lock.releaseLock ();
131         
132         DataObject obj = DataObject.find (inst);
133         assertEquals ("XML Data object", org.openide.loaders.XMLDataObject.class, obj.getClass());
134         org.openide.cookies.InstanceCookie ic = (org.openide.cookies.InstanceCookie)obj.getCookie(org.openide.cookies.InstanceCookie.class);
135         assertNotNull ("Has cookie", ic);
136
137         final KeyStroke JavaDoc stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F11, KeyEvent.CTRL_MASK);
138         
139         ShortcutsFolder.waitFinished ();
140
141         Action JavaDoc action = keymap.getAction(stroke);
142         if (action == null) {
143             fail("There should be some action for " + stroke + " in:\n" + keymap);
144         }
145         
146         inst.delete ();
147         ShortcutsFolder.waitFinished ();
148         action = keymap.getAction (stroke);
149         assertNull ("Action removed", action);
150     }
151     
152     public static class TestAction extends AbstractAction JavaDoc {
153         public void actionPerformed (ActionEvent JavaDoc ae) {}
154     }
155     
156     public static class ENV extends Object JavaDoc implements org.openide.loaders.Environment.Provider {
157         public Lookup getEnvironment(DataObject obj) {
158             if (obj instanceof org.openide.loaders.XMLDataObject) {
159                 try {
160                     org.w3c.dom.Document JavaDoc doc = ((org.openide.loaders.XMLDataObject)obj).getDocument();
161                     if (doc.getDocumentElement().getNodeName().equals ("project")) {
162                         return org.openide.util.lookup.Lookups.singleton (
163                             new org.openide.loaders.InstanceSupport.Instance (
164                                 new TestAction ()
165                             )
166                         );
167                     }
168                 } catch (Exception JavaDoc ex) {
169                     ex.printStackTrace();
170                     fail ("No exception: " + ex.getMessage());
171                 }
172             }
173             return org.openide.util.Lookup.EMPTY;
174         }
175     }
176     
177 }
178
Popular Tags