1 19 20 package org.openide.loaders; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import java.util.Arrays ; 26 import java.util.Enumeration ; 27 import javax.swing.Action ; 28 import javax.swing.JSeparator ; 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 53 public class DataLoaderGetActionsTest extends NbTestCase { 54 55 private FileObject root; 56 57 private DataObject obj; 58 59 private Node node; 60 61 public DataLoaderGetActionsTest (String name) { 62 super(name); 63 } 64 65 69 protected void setUp () throws Exception { 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 91 protected void tearDown() throws Exception { 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 116 public void testCreateAndDeleteAction() throws Exception { 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 [] 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 141 public void testRepetitiveDeleting() throws Exception { 142 for (int i = 0; i < 10; i++) { 143 testCreateAndDeleteAction(); 144 } 145 } 146 147 152 public void testWrongActionObjectInConfig() throws Exception { 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 [] res = node.getActions(false); 157 assertEquals("There should be zero actions.", 0, res.length); 158 } 159 160 173 public void testAddingSeparators() throws Exception { 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 [] 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 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 ); 186 } 187 188 192 public void testCompatibilityIsPropagatedToDisk () throws Exception { 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 [] 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 220 public void testNoDeserializationOfActions () throws Exception { 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 [] 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 { 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 272 protected String actionsContext () { 273 return "test"; 274 } 275 276 protected MultiDataObject createMultiObject (FileObject primaryFile) throws DataObjectExistsException, IOException { 277 return new MultiDataObject (primaryFile, this); 278 } 279 280 protected SystemAction[] defaultActions() { 281 return new SystemAction[0]; 282 } 283 284 } 286 public static final class Lkp extends AbstractLookup { 290 public Lkp () throws Exception { 291 this(new InstanceContent()); 292 } 293 294 private Lkp(InstanceContent ic) throws Exception { 295 super (ic); 296 297 FileSystem fs = TestUtilHid.createLocalFileSystem (Lkp.class.getName (), new String [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 } 307 } 308 309 310 private static final class Pool extends DataLoaderPool { 311 312 protected Enumeration loaders() { 313 return Enumerations.singleton(DataLoader.getLoader(MyDL.class)); 314 } 315 316 } 318 private final class PCL implements PropertyChangeListener { 319 int cnt; 320 String name; 321 322 public void propertyChange(PropertyChangeEvent ev) { 323 name = ev.getPropertyName(); 324 cnt++; 325 } 326 327 public void assertEvent (int cnt, String 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 } 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 actionsContext () { 353 return "2ndtestdir"; 354 } 355 356 } 357 358 } 359 | Popular Tags |