Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.openide.loaders; |
15 |
|
16 |
import junit.textui.TestRunner; |
17 |
|
18 |
import org.openide.filesystems.*; |
19 |
import org.openide.util.Lookup; |
20 |
import java.io.IOException; |
21 |
import java.util.*; |
22 |
import org.netbeans.junit.*; |
23 |
import java.beans.PropertyChangeListener; |
24 |
import javax.swing.Action; |
25 |
import org.openide.util.io.NbMarshalledObject; |
26 |
|
27 |
/** Tests the proposed behaviour of DataLoader.getActions that is going |
28 |
* to read its values from layer. |
29 |
* |
30 |
* @author Jaroslav Tulach (taken from openidex/enode by David Strupl) |
31 |
*/ |
32 |
public class DataLoaderGetActionsTest extends NbTestCase { |
33 |
/** root folder FileObject */ |
34 |
private FileObject root; |
35 |
/** sample data object */ |
36 |
private DataObject obj; |
37 |
/** its node */ |
38 |
private org.openide.nodes.Node node; |
39 |
|
40 |
public DataLoaderGetActionsTest (String name) { |
41 |
super(name); |
42 |
} |
43 |
|
44 |
public static void main(String[] args) { |
45 |
if (args.length == 1) { |
46 |
TestRunner.run (new DataLoaderGetActionsTest (args[0])); |
47 |
} else { |
48 |
TestRunner.run(new NbTestSuite(DataLoaderGetActionsTest.class)); |
49 |
} |
50 |
} |
51 |
|
52 |
/** |
53 |
* Sets up the testing environment by creating testing folders |
54 |
* on the system file system. |
55 |
*/ |
56 |
protected void setUp () throws Exception { |
57 |
System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.DataLoaderGetActionsTest$Lkp"); |
58 |
assertEquals ("Our lookup is installed", Lookup.getDefault ().getClass (), Lkp.class); |
59 |
|
60 |
MyDL loader = (MyDL)MyDL.getLoader (MyDL.class); |
61 |
|
62 |
FileSystem dfs = Repository.getDefault().getDefaultFileSystem(); |
63 |
dfs.refresh (true); |
64 |
root = FileUtil.createFolder (dfs.getRoot (), loader.actionsContext ()); |
65 |
|
66 |
|
67 |
FileObject fo = FileUtil.createData (dfs.getRoot (), "a.txt"); |
68 |
obj = DataObject.find (fo); |
69 |
|
70 |
assertEquals ("The correct loader", loader, obj.getLoader ()); |
71 |
|
72 |
node = obj.getNodeDelegate (); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Deletes the folders created in method setUp(). |
77 |
*/ |
78 |
protected void tearDown() throws Exception { |
79 |
FileObject[] arr = root.getChildren (); |
80 |
for (int i = 0; i < arr.length; i++) { |
81 |
arr[i].delete(); |
82 |
} |
83 |
int l = node.getActions (false).length; |
84 |
if (l != 0) { |
85 |
System.err.println("Not empty actions at the end!!!"); |
86 |
} |
87 |
} |
88 |
|
89 |
/** |
90 |
* This test tests the presence of declarative actions from |
91 |
* system file system without the hierarchical flag set (the ExtensibleNode |
92 |
* instance is created with constructor ExtensibleNode("test", false). |
93 |
* The tests performs following steps: |
94 |
* <OL><LI> Create an instance of ExtensibleNode with folder set to "test" |
95 |
* <LI> No actions should be returned by getActions since the "test" folder |
96 |
* is not there |
97 |
* <LI> Create one action in the testing folder |
98 |
* <LI> The action should be visible in the result of getActions |
99 |
* <LI> After deleting the action from the folder the action should |
100 |
* not be returned from getActions(). |
101 |
* </OL> |
102 |
*/ |
103 |
public void testCreateAndDeleteAction() throws Exception { |
104 |
assertEquals("No actions at the start", 0, node.getActions(false).length); |
105 |
FileObject test = root; |
106 |
|
107 |
PCL pcl = new PCL (); |
108 |
obj.getLoader ().addPropertyChangeListener (pcl); |
109 |
|
110 |
FileObject a1 = test.createData("org-openide-actions-PropertiesAction.instance"); |
111 |
|
112 |
pcl.assertEvent (1, "actions"); |
113 |
|
114 |
Action [] res = node.getActions(false); |
115 |
assertEquals("There should be exactly one action.", 1, res.length); |
116 |
a1.delete(); |
117 |
|
118 |
pcl.assertEvent (2, "actions"); |
119 |
assertEquals("No actions after deleting", 0, node.getActions(false).length); |
120 |
|
121 |
obj.getLoader ().removePropertyChangeListener (pcl); |
122 |
} |
123 |
|
124 |
/** |
125 |
* An attempt to create a simple stress test. Just calls |
126 |
* the <code>testCreateAndDeleteAction</code> 100 times. |
127 |
*/ |
128 |
public void testRepetitiveDeleting() throws Exception { |
129 |
for (int i = 0; i < 10; i++) { |
130 |
testCreateAndDeleteAction(); |
131 |
} |
132 |
} |
133 |
|
134 |
/** |
135 |
* This test should test behaviour of the getActions method when |
136 |
* there is some alien object specified in the configuration folder. |
137 |
* The testing object is of type Integer (instead of javax.swing.Action). |
138 |
*/ |
139 |
public void testWrongActionObjectInConfig() throws Exception { |
140 |
assertEquals("No actions at the start", 0, node.getActions(false).length); |
141 |
FileObject test = root; |
142 |
FileObject a1 = test.createData("java-lang-String.instance"); |
143 |
Action [] res = node.getActions(false); |
144 |
assertEquals("There should be zero actions.", 0, res.length); |
145 |
} |
146 |
|
147 |
/** |
148 |
* This test checks whether the JSeparator added from the configuration |
149 |
* file is reflected in the resulting popup. |
150 |
* The tests performs following steps: |
151 |
* <OL><LI> Create an instance of ExtensibleNode with folder set to "test" |
152 |
* <LI> No actions should be returned by getActions since the "test" folder |
153 |
* is not there |
154 |
* <LI> Create two actions in the testing folder separated by JSeparator |
155 |
* <LI> getActions should return 3 elements - null element for the separator |
156 |
* <LI> Popup is created from the actions array - the null element |
157 |
* should be replaced by a JSeparator again |
158 |
* </OL> |
159 |
*/ |
160 |
public void testAddingSeparators() throws Exception { |
161 |
org.openide.nodes.Node en1 = node; |
162 |
assertEquals("No actions at the start", 0, en1.getActions(false).length); |
163 |
FileObject test = root; |
164 |
FileObject a1 = test.createData("1[org-openide-actions-PropertiesAction].instance"); |
165 |
FileObject sep = test.createData("2[javax-swing-JSeparator].instance"); |
166 |
FileObject a2 = test.createData("3[org-openide-actions-CutAction].instance"); |
167 |
javax.swing.Action[] actions = en1.getActions(false); |
168 |
assertEquals("Actions array should contain 3 elements", 3, actions.length); |
169 |
assertNull("separator should create null element in the array", actions[1]); |
170 |
javax.swing.JPopupMenu jp = org.openide.util.Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); |
171 |
assertEquals("Popup should contain 3 components", 3, jp.getComponentCount()); |
172 |
assertTrue("Separator should be second", jp.getComponent(1) instanceof javax.swing.JSeparator); |
173 |
} |
174 |
|
175 |
/** Test to see whether a compatibility behaviour is still kept. E.g. |
176 |
* if one adds actions using DataLoader.setActions they really will be |
177 |
* there. |
178 |
*/ |
179 |
public void testCompatibilityIsPropagatedToDisk () throws Exception { |
180 |
assertEquals("No actions at the start", 0, node.getActions(false).length); |
181 |
FileObject test = root; |
182 |
|
183 |
PCL pcl = new PCL (); |
184 |
obj.getLoader ().addPropertyChangeListener (pcl); |
185 |
|
186 |
obj.getLoader ().setActions (new org.openide.util.actions.SystemAction[] { |
187 |
org.openide.util.actions.SystemAction.get (org.openide.actions.PropertiesAction.class) |
188 |
}); |
189 |
|
190 |
pcl.assertEvent (1, "actions"); |
191 |
|
192 |
Action [] res = node.getActions(false); |
193 |
assertEquals("There should be exactly one action.", 1, res.length); |
194 |
assertEquals("One file created", 1, test.getChildren ().length); |
195 |
|
196 |
obj.getLoader ().setActions (new org.openide.util.actions.SystemAction[0]); |
197 |
|
198 |
pcl.assertEvent (2, "actions"); |
199 |
assertEquals("No actions after deleting", 0, node.getActions(false).length); |
200 |
|
201 |
assertEquals("file disappeared", 0, test.getChildren ().length); |
202 |
obj.getLoader ().removePropertyChangeListener (pcl); |
203 |
} |
204 |
|
205 |
/** Test to check that the deserialization of actions is completely ignored. |
206 |
*/ |
207 |
public void testNoDeserializationOfActions () throws Exception { |
208 |
assertEquals("No actions at the start", 0, node.getActions(false).length); |
209 |
FileObject test = root; |
210 |
|
211 |
PCL pcl = new PCL (); |
212 |
obj.getLoader ().addPropertyChangeListener (pcl); |
213 |
|
214 |
obj.getLoader ().setActions (new org.openide.util.actions.SystemAction[] { |
215 |
org.openide.util.actions.SystemAction.get (org.openide.actions.PropertiesAction.class) |
216 |
}); |
217 |
|
218 |
pcl.assertEvent (1, "actions"); |
219 |
|
220 |
Action [] res = node.getActions(false); |
221 |
assertEquals("There should be exactly one action.", 1, res.length); |
222 |
assertEquals("One file created", 1, test.getChildren ().length); |
223 |
|
224 |
NbMarshalledObject m = new NbMarshalledObject (obj.getLoader ()); |
225 |
|
226 |
obj.getLoader ().setActions (new org.openide.util.actions.SystemAction[0]); |
227 |
|
228 |
pcl.assertEvent (2, "actions"); |
229 |
assertEquals("No actions after deleting", 0, node.getActions(false).length); |
230 |
|
231 |
assertEquals("file disappeared", 0, test.getChildren ().length); |
232 |
|
233 |
assertEquals ("Loader deserialized", obj.getLoader (), m.get ()); |
234 |
assertEquals("Still no actions", 0, node.getActions(false).length); |
235 |
|
236 |
obj.getLoader ().removePropertyChangeListener (pcl); |
237 |
} |
238 |
|
239 |
public void testDefaultActionsUsedWhenCreatedForTheFirstTime () throws Exception { |
240 |
SndDL loader = (SndDL)SndDL.getLoader (SndDL.class); |
241 |
|
242 |
org.openide.util.actions.SystemAction[] arr = loader.getActions (); |
243 |
|
244 |
assertEquals ( |
245 |
"Arrays of actions are the same", |
246 |
java.util.Arrays.asList (loader.defaultActions ()), |
247 |
java.util.Arrays.asList (arr) |
248 |
); |
249 |
} |
250 |
|
251 |
private static class MyDL extends UniFileLoader { |
252 |
public MyDL () { |
253 |
super ("org.openide.loaders.DataObject"); |
254 |
getExtensions ().addExtension ("txt"); |
255 |
} |
256 |
|
257 |
/** Returns the name of the folder to read the actions from |
258 |
*/ |
259 |
protected String actionsContext () { |
260 |
String sup = super.actionsContext (); |
261 |
assertEquals ("Just verify that the default is really the one", |
262 |
"org-openide-loaders/org-openide-loaders-DataLoaderGetActionsTest-MyDL/Actions", |
263 |
sup |
264 |
); |
265 |
|
266 |
|
267 |
return "test"; |
268 |
} |
269 |
|
270 |
protected org.openide.loaders.MultiDataObject createMultiObject (FileObject primaryFile) throws org.openide.loaders.DataObjectExistsException, IOException { |
271 |
|
272 |
|
273 |
|
274 |
return new MultiDataObject (primaryFile, this); |
275 |
} |
276 |
|
277 |
protected org.openide.util.actions.SystemAction[] defaultActions () { |
278 |
return new org.openide.util.actions.SystemAction[0]; |
279 |
} |
280 |
|
281 |
} // end of MyDL |
282 |
|
283 |
// |
284 |
// Our fake lookup |
285 |
// |
286 |
public static final class Lkp extends org.openide.util.lookup.AbstractLookup { |
287 |
public Lkp () throws Exception { |
288 |
this (new org.openide.util.lookup.InstanceContent ()); |
289 |
} |
290 |
|
291 |
private Lkp (org.openide.util.lookup.InstanceContent ic) throws Exception { |
292 |
super (ic); |
293 |
|
294 |
TestUtilHid.destroyLocalFileSystem (Lkp.class.getName ()); |
295 |
ic.add (new Repository (TestUtilHid.createLocalFileSystem (Lkp.class.getName (), new String[0]))); |
296 |
ic.add (new Pool ()); |
297 |
// ic.add (new EM ()); |
298 |
} |
299 |
} |
300 |
|
301 |
|
302 |
private static final class Pool extends DataLoaderPool { |
303 |
|
304 |
protected java.util.Enumeration loaders () { |
305 |
return new org.openide.util.enum.SingletonEnumeration ( |
306 |
DataLoader.getLoader(MyDL.class) |
307 |
); |
308 |
} |
309 |
|
310 |
} // end of Pool |
311 |
|
312 |
private final class PCL |
313 |
implements java.beans.PropertyChangeListener { |
314 |
int cnt; |
315 |
String name; |
316 |
|
317 |
public void propertyChange (java.beans.PropertyChangeEvent ev) { |
318 |
name = ev.getPropertyName(); |
319 |
cnt++; |
320 |
} |
321 |
|
322 |
public void assertEvent (int cnt, String name) { |
323 |
obj.getLoader ().waitForActions (); |
324 |
|
325 |
if (cnt > this.cnt) { |
326 |
fail ("Excepted more changes then we got: expected: " + cnt + " we got: " + this.cnt); |
327 |
} |
328 |
assertEquals ("same name", name, this.name); |
329 |
} |
330 |
} // end of PCL |
331 |
|
332 |
public static final class SndDL extends MyDL { |
333 |
public SndDL () { |
334 |
getExtensions ().addExtension ("bla"); |
335 |
} |
336 |
|
337 |
protected org.openide.util.actions.SystemAction[] defaultActions () { |
338 |
return new org.openide.util.actions.SystemAction[] { |
339 |
org.openide.util.actions.SystemAction.get (org.openide.actions.CutAction.class), |
340 |
null, |
341 |
org.openide.util.actions.SystemAction.get (org.openide.actions.CopyAction.class), |
342 |
null, |
343 |
org.openide.util.actions.SystemAction.get (org.openide.actions.DeleteAction.class), |
344 |
|
345 |
}; |
346 |
} |
347 |
|
348 |
protected String actionsContext () { |
349 |
return "2ndtestdir"; |
350 |
} |
351 |
|
352 |
} |
353 |
|
354 |
} |