KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.textui.TestRunner;
23
24 import org.openide.filesystems.*;
25 import org.openide.util.Lookup;
26 import java.io.IOException JavaDoc;
27 import java.util.*;
28 import org.netbeans.junit.*;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import org.openide.util.io.NbMarshalledObject;
32
33 /** Check how the default behaviour of DataLoader without overriden
34  * actionsContext works.
35  *
36  * @author Jaroslav Tulach
37  */

38 public class DataLoaderGetActionsCompatibilityTest extends NbTestCase {
39     /** sample data object */
40     private DataObject obj;
41     /** its node */
42     private org.openide.nodes.Node node;
43     /** monitor sfs */
44     private PCL sfs;
45
46     public DataLoaderGetActionsCompatibilityTest (String JavaDoc name) {
47         super(name);
48     }
49     
50     /**
51      * Sets up the testing environment by creating testing folders
52      * on the system file system.
53      */

54     protected void setUp () throws Exception JavaDoc {
55         System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.DataLoaderGetActionsCompatibilityTest$Lkp");
56         assertEquals ("Our lookup is installed", Lookup.getDefault ().getClass (), Lkp.class);
57         
58         MyDL loader = (MyDL)MyDL.getLoader (MyDL.class);
59
60         FileSystem dfs = Repository.getDefault().getDefaultFileSystem();
61         dfs.refresh (true);
62         
63         FileObject fo = FileUtil.createData (dfs.getRoot (), "a.txt");
64         obj = DataObject.find (fo);
65         
66         assertEquals ("The correct loader", loader, obj.getLoader ());
67         
68         node = obj.getNodeDelegate ();
69         
70         sfs = new PCL ();
71         dfs.addFileChangeListener (sfs);
72     }
73     
74     /**
75      * Deletes the folders created in method setUp().
76      */

77     protected void tearDown() throws Exception JavaDoc {
78         obj.getLoader ().setActions (new org.openide.util.actions.SystemAction[0]);
79         
80         int l = node.getActions (false).length;
81         if (l != 0) {
82             fail ("Not empty actions at the end!!!");
83         }
84         Repository.getDefault ().getDefaultFileSystem ().removeFileChangeListener (sfs);
85
86         // no suspicious activity on getDefaultFileSystem
87
sfs.assertEvent (0, null);
88     }
89     
90     public void testDefaultActionContextReturnsNull () {
91         assertNull (obj.getLoader ().actionsContext ());
92     }
93     
94     public void testDefaultActionsUsedWhenCreatedForTheFirstTime () throws Exception JavaDoc {
95         SndDL loader = (SndDL)SndDL.getLoader (SndDL.class);
96         
97         org.openide.util.actions.SystemAction[] arr = loader.getActions ();
98         
99         assertEquals (
100             "Arrays of actions are the same",
101             java.util.Arrays.asList (loader.defaultActions ()),
102             java.util.Arrays.asList (arr)
103         );
104     }
105
106     /** Test to check that the deserialization of actions works as expected.
107      */

108     public void testNoDeserializationOfActions () throws Exception JavaDoc {
109         assertEquals("No actions at the start", 0, node.getActions(false).length);
110         
111         PCL pcl = new PCL ();
112         obj.getLoader ().addPropertyChangeListener (pcl);
113         
114         obj.getLoader ().setActions (new org.openide.util.actions.SystemAction[] {
115                 org.openide.util.actions.SystemAction.get (org.openide.actions.PropertiesAction.class)
116         });
117         
118         pcl.assertEvent (1, "actions");
119         
120         Action JavaDoc [] res = node.getActions(false);
121         assertEquals("There should be exactly one action.", 1, res.length);
122         
123         NbMarshalledObject m = new NbMarshalledObject (obj.getLoader ());
124         
125         obj.getLoader ().setActions (new org.openide.util.actions.SystemAction[0]);
126
127         pcl.assertEvent (2, "actions");
128         assertEquals("No actions after setting empty array", 0, node.getActions(false).length);
129         
130         assertEquals ("Loader deserialized", obj.getLoader (), m.get ());
131         res = node.getActions(false);
132         assertEquals("One action", 1, res.length);
133         assertEquals (
134             "and that is the property action",
135             org.openide.util.actions.SystemAction.get (org.openide.actions.PropertiesAction.class),
136             res[0]
137         );
138         
139         obj.getLoader ().removePropertyChangeListener (pcl);
140     }
141     
142     /** Loader that does not override the actionsContext.
143      */

144     private static class MyDL extends UniFileLoader {
145         public MyDL () {
146             super ("org.openide.loaders.DataObject");
147             getExtensions ().addExtension ("txt");
148         }
149         
150         protected org.openide.loaders.MultiDataObject createMultiObject (FileObject primaryFile) throws org.openide.loaders.DataObjectExistsException, IOException JavaDoc {
151             
152             
153             
154             return new MultiDataObject (primaryFile, this);
155         }
156         
157         protected org.openide.util.actions.SystemAction[] defaultActions () {
158             return new org.openide.util.actions.SystemAction[0];
159         }
160         
161     } // end of MyDL
162

163     //
164
// Our fake lookup
165
//
166
public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
167         public Lkp () throws Exception JavaDoc {
168             this (new org.openide.util.lookup.InstanceContent ());
169         }
170         
171         private Lkp (org.openide.util.lookup.InstanceContent ic) throws Exception JavaDoc {
172             super (ic);
173             
174             TestUtilHid.destroyLocalFileSystem (Lkp.class.getName ());
175             ic.add (new Repository (TestUtilHid.createLocalFileSystem (Lkp.class.getName (), new String JavaDoc[0])));
176             ic.add (new Pool ());
177 // ic.add (new EM ());
178
}
179     }
180     
181     
182     private static final class Pool extends DataLoaderPool {
183         
184         protected java.util.Enumeration JavaDoc loaders () {
185             return org.openide.util.Enumerations.singleton (
186                 DataLoader.getLoader(MyDL.class)
187             );
188         }
189         
190     } // end of Pool
191

192     private final class PCL implements org.openide.filesystems.FileChangeListener, java.beans.PropertyChangeListener JavaDoc {
193         int cnt;
194         String JavaDoc name;
195
196         public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
197             name = ev.getPropertyName();
198             cnt++;
199         }
200         
201         public void assertEvent (int cnt, String JavaDoc name) {
202             obj.getLoader ().waitForActions ();
203
204             if (cnt != this.cnt) {
205                 fail ("Excepted more changes then we got: expected: " + cnt + " we got: " + this.cnt + " with name: " + this.name);
206             }
207         }
208
209         public void fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fe) {
210             cnt++;
211             name = "fileAttributeChanged";
212         }
213
214         public void fileChanged(org.openide.filesystems.FileEvent fe) {
215             cnt++;
216             name = "fileChanged";
217         }
218
219         public void fileDataCreated(org.openide.filesystems.FileEvent fe) {
220             cnt++;
221             name = "fileDataCreated";
222         }
223
224         public void fileDeleted(org.openide.filesystems.FileEvent fe) {
225             cnt++;
226             name = "fileDeleted";
227         }
228
229         public void fileFolderCreated(org.openide.filesystems.FileEvent fe) {
230             cnt++;
231             name = "fileFolderCreated";
232         }
233
234         public void fileRenamed(org.openide.filesystems.FileRenameEvent fe) {
235             cnt++;
236             name = "fileRenamed";
237         }
238     } // end of PCL
239

240     public static final class SndDL extends MyDL {
241         public SndDL () {
242             getExtensions ().addExtension ("bla");
243         }
244         
245         protected org.openide.util.actions.SystemAction[] defaultActions () {
246             return new org.openide.util.actions.SystemAction[] {
247                 org.openide.util.actions.SystemAction.get (org.openide.actions.CutAction.class),
248                 null,
249                 org.openide.util.actions.SystemAction.get (org.openide.actions.CopyAction.class),
250                 null,
251                 org.openide.util.actions.SystemAction.get (org.openide.actions.DeleteAction.class),
252                 
253             };
254         }
255         
256     }
257     
258 }
259
Popular Tags