KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DataLoaderGetActionsTest


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.openide.loaders;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JSeparator JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.actions.CopyAction;
31 import org.openide.actions.CutAction;
32 import org.openide.actions.DeleteAction;
33 import org.openide.actions.PropertiesAction;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.Repository;
38 import org.openide.nodes.Node;
39 import org.openide.util.Enumerations;
40 import org.openide.util.Lookup;
41 import org.openide.util.Utilities;
42 import org.openide.util.actions.SystemAction;
43 import org.openide.util.io.NbMarshalledObject;
44 import org.openide.util.lookup.AbstractLookup;
45 import org.openide.util.lookup.InstanceContent;
46 import org.openide.util.lookup.Lookups;
47
48 /** Tests the proposed behaviour of DataLoader.getActions that is going
49  * to read its values from layer.
50  *
51  * @author Jaroslav Tulach (taken from openidex/enode by David Strupl)
52  */

53 public class DataLoaderGetActionsTest extends NbTestCase {
54     /** root folder FileObject */
55     private FileObject root;
56     /** sample data object */
57     private DataObject obj;
58     /** its node */
59     private Node node;
60
61     public DataLoaderGetActionsTest (String JavaDoc name) {
62         super(name);
63     }
64     
65     /**
66      * Sets up the testing environment by creating testing folders
67      * on the system file system.
68      */

69     protected void setUp () throws Exception JavaDoc {
70         System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.DataLoaderGetActionsTest$Lkp");
71         assertEquals ("Our lookup is installed", Lookup.getDefault ().getClass (), Lkp.class);
72         
73         MyDL loader = (MyDL)MyDL.getLoader (MyDL.class);
74
75         FileSystem dfs = Repository.getDefault().getDefaultFileSystem();
76         dfs.refresh (true);
77         root = FileUtil.createFolder (dfs.getRoot (), loader.actionsContext ());
78         
79         
80         FileObject fo = FileUtil.createData (dfs.getRoot (), "a.txt");
81         obj = DataObject.find (fo);
82         
83         assertEquals ("The correct loader", loader, obj.getLoader ());
84         
85         node = obj.getNodeDelegate ();
86     }
87     
88     /**
89      * Deletes the folders created in method setUp().
90      */

91     protected void tearDown() throws Exception JavaDoc {
92         FileObject[] arr = root.getChildren ();
93         for (int i = 0; i < arr.length; i++) {
94             arr[i].delete();
95         }
96         int l = node.getActions (false).length;
97         if (l != 0) {
98             System.err.println("Not empty actions at the end!!!");
99         }
100     }
101     
102     /**
103      * This test tests the presence of declarative actions from
104      * system file system without the hierarchical flag set (the ExtensibleNode
105      * instance is created with constructor ExtensibleNode("test", false).
106      * The tests performs following steps:
107      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test"
108      * <LI> No actions should be returned by getActions since the "test" folder
109      * is not there
110      * <LI> Create one action in the testing folder
111      * <LI> The action should be visible in the result of getActions
112      * <LI> After deleting the action from the folder the action should
113      * not be returned from getActions().
114      * </OL>
115      */

116     public void testCreateAndDeleteAction() throws Exception JavaDoc {
117         assertEquals("No actions at the start", 0, node.getActions(false).length);
118         FileObject test = root;
119         
120         PCL pcl = new PCL ();
121         obj.getLoader ().addPropertyChangeListener (pcl);
122         
123         FileObject a1 = test.createData("org-openide-actions-PropertiesAction.instance");
124         
125         pcl.assertEvent (1, "actions");
126         
127         Action JavaDoc [] res = node.getActions(false);
128         assertEquals("There should be exactly one action.", 1, res.length);
129         a1.delete();
130
131         pcl.assertEvent (2, "actions");
132         assertEquals("No actions after deleting", 0, node.getActions(false).length);
133         
134         obj.getLoader ().removePropertyChangeListener (pcl);
135     }
136     
137     /**
138      * An attempt to create a simple stress test. Just calls
139      * the <code>testCreateAndDeleteAction</code> 100 times.
140      */

141     public void testRepetitiveDeleting() throws Exception JavaDoc {
142         for (int i = 0; i < 10; i++) {
143             testCreateAndDeleteAction();
144         }
145     }
146     
147     /**
148      * This test should test behaviour of the getActions method when
149      * there is some alien object specified in the configuration folder.
150      * The testing object is of type Integer (instead of javax.swing.Action).
151      */

152     public void testWrongActionObjectInConfig() throws Exception JavaDoc {
153         assertEquals("No actions at the start", 0, node.getActions(false).length);
154         FileObject test = root;
155         FileObject a1 = test.createData("java-lang-String.instance");
156         Action JavaDoc [] res = node.getActions(false);
157         assertEquals("There should be zero actions.", 0, res.length);
158     }
159     
160     /**
161      * This test checks whether the JSeparator added from the configuration
162      * file is reflected in the resulting popup.
163      * The tests performs following steps:
164      * <OL><LI> Create an instance of ExtensibleNode with folder set to "test"
165      * <LI> No actions should be returned by getActions since the "test" folder
166      * is not there
167      * <LI> Create two actions in the testing folder separated by JSeparator
168      * <LI> getActions should return 3 elements - null element for the separator
169      * <LI> Popup is created from the actions array - the null element
170      * should be replaced by a JSeparator again
171      * </OL>
172      */

173     public void testAddingSeparators() throws Exception JavaDoc {
174         Node en1 = node;
175         assertEquals("No actions at the start", 0, en1.getActions(false).length);
176         FileObject test = root;
177         FileObject a1 = test.createData("1[org-openide-actions-PropertiesAction].instance");
178         FileObject sep = test.createData("2[javax-swing-JSeparator].instance");
179         FileObject a2 = test.createData("3[org-openide-actions-CutAction].instance");
180         Action JavaDoc[] actions = en1.getActions(false);
181         assertEquals("Actions array should contain 3 elements: "+Arrays.asList(actions), 3, actions.length);
182         assertNull("separator should create null element in the array", actions[1]);
183         javax.swing.JPopupMenu JavaDoc jp = Utilities.actionsToPopup(actions, Lookups.singleton(en1));
184         assertEquals("Popup should contain 3 components", 3, jp.getComponentCount());
185         assertTrue("Separator should be second", jp.getComponent(1) instanceof JSeparator JavaDoc);
186     }
187
188     /** Test to see whether a compatibility behaviour is still kept. E.g.
189      * if one adds actions using DataLoader.setActions they really will be
190      * there.
191      */

192     public void testCompatibilityIsPropagatedToDisk () throws Exception JavaDoc {
193         assertEquals("No actions at the start", 0, node.getActions(false).length);
194         FileObject test = root;
195         
196         PCL pcl = new PCL ();
197         obj.getLoader ().addPropertyChangeListener (pcl);
198         
199         obj.getLoader().setActions(new SystemAction[] {
200             SystemAction.get(PropertiesAction.class)
201         });
202         
203         pcl.assertEvent (1, "actions");
204         
205         Action JavaDoc [] res = node.getActions(false);
206         assertEquals("There should be exactly one action.", 1, res.length);
207         assertEquals("One file created", 1, test.getChildren ().length);
208         
209         obj.getLoader().setActions(new SystemAction[0]);
210
211         pcl.assertEvent (2, "actions");
212         assertEquals("No actions after deleting", 0, node.getActions(false).length);
213         
214         assertEquals("file disappeared", 0, test.getChildren ().length);
215         obj.getLoader ().removePropertyChangeListener (pcl);
216     }
217     
218     /** Test to check that the deserialization of actions is completely ignored.
219      */

220     public void testNoDeserializationOfActions () throws Exception JavaDoc {
221         assertEquals("No actions at the start", 0, node.getActions(false).length);
222         FileObject test = root;
223         
224         PCL pcl = new PCL ();
225         obj.getLoader ().addPropertyChangeListener (pcl);
226         
227         obj.getLoader().setActions(new SystemAction[] {
228             SystemAction.get(PropertiesAction.class)
229         });
230         
231         pcl.assertEvent (1, "actions");
232         
233         Action JavaDoc [] res = node.getActions(false);
234         assertEquals("There should be exactly one action.", 1, res.length);
235         assertEquals("One file created", 1, test.getChildren ().length);
236         
237         NbMarshalledObject m = new NbMarshalledObject (obj.getLoader ());
238         
239         obj.getLoader().setActions(new SystemAction[0]);
240
241         pcl.assertEvent (2, "actions");
242         assertEquals("No actions after deleting", 0, node.getActions(false).length);
243         
244         assertEquals("file disappeared", 0, test.getChildren ().length);
245         
246         assertEquals ("Loader deserialized", obj.getLoader (), m.get ());
247         assertEquals("Still no actions", 0, node.getActions(false).length);
248         
249         obj.getLoader ().removePropertyChangeListener (pcl);
250     }
251     
252     public void testDefaultActionsUsedWhenCreatedForTheFirstTime () throws Exception JavaDoc {
253         SndDL loader = (SndDL)SndDL.getLoader (SndDL.class);
254         
255         SystemAction[] arr = loader.getActions();
256         
257         assertEquals (
258             "Arrays of actions are the same",
259             Arrays.asList(loader.defaultActions()),
260             Arrays.asList(arr)
261         );
262     }
263     
264     private static class MyDL extends UniFileLoader {
265         public MyDL () {
266             super ("org.openide.loaders.DataObject");
267             getExtensions ().addExtension ("txt");
268         }
269         
270         /** Returns the name of the folder to read the actions from
271          */

272         protected String JavaDoc actionsContext () {
273             return "test";
274         }
275         
276         protected MultiDataObject createMultiObject (FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
277             return new MultiDataObject (primaryFile, this);
278         }
279         
280         protected SystemAction[] defaultActions() {
281             return new SystemAction[0];
282         }
283         
284     } // end of MyDL
285

286     //
287
// Our fake lookup
288
//
289
public static final class Lkp extends AbstractLookup {
290         public Lkp () throws Exception JavaDoc {
291             this(new InstanceContent());
292         }
293         
294         private Lkp(InstanceContent ic) throws Exception JavaDoc {
295             super (ic);
296
297             FileSystem fs = TestUtilHid.createLocalFileSystem (Lkp.class.getName (), new String JavaDoc[0]);
298             for (FileObject fo : fs.getRoot().getChildren()) {
299                 fo.delete();
300             }
301             assertEquals("No children", 0, fs.getRoot().getChildren().length);
302             
303             ic.add (new Repository (fs));
304             ic.add (new Pool ());
305 // ic.add (new EM ());
306
}
307     }
308     
309     
310     private static final class Pool extends DataLoaderPool {
311         
312         protected Enumeration JavaDoc loaders() {
313             return Enumerations.singleton(DataLoader.getLoader(MyDL.class));
314         }
315         
316     } // end of Pool
317

318     private final class PCL implements PropertyChangeListener JavaDoc {
319         int cnt;
320         String JavaDoc name;
321
322         public void propertyChange(PropertyChangeEvent JavaDoc ev) {
323             name = ev.getPropertyName();
324             cnt++;
325         }
326         
327         public void assertEvent (int cnt, String JavaDoc name) {
328             obj.getLoader ().waitForActions ();
329
330             if (cnt > this.cnt) {
331                 fail ("Excepted more changes then we got: expected: " + cnt + " we got: " + this.cnt);
332             }
333             assertEquals ("same name", name, this.name);
334         }
335     } // end of PCL
336

337     public static final class SndDL extends MyDL {
338         public SndDL () {
339             getExtensions ().addExtension ("bla");
340         }
341         
342         protected SystemAction[] defaultActions() {
343             return new SystemAction[] {
344                 SystemAction.get(CutAction.class),
345                 null,
346                 SystemAction.get(CopyAction.class),
347                 null,
348                 SystemAction.get(DeleteAction.class),
349             };
350         }
351         
352         protected String JavaDoc actionsContext () {
353             return "2ndtestdir";
354         }
355         
356     }
357     
358 }
359
Popular Tags