1 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 ; 27 import java.util.*; 28 import org.netbeans.junit.*; 29 import java.beans.PropertyChangeListener ; 30 import javax.swing.Action ; 31 import org.openide.util.io.NbMarshalledObject; 32 33 38 public class DataLoaderGetActionsCompatibilityTest extends NbTestCase { 39 40 private DataObject obj; 41 42 private org.openide.nodes.Node node; 43 44 private PCL sfs; 45 46 public DataLoaderGetActionsCompatibilityTest (String name) { 47 super(name); 48 } 49 50 54 protected void setUp () throws Exception { 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 77 protected void tearDown() throws Exception { 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 sfs.assertEvent (0, null); 88 } 89 90 public void testDefaultActionContextReturnsNull () { 91 assertNull (obj.getLoader ().actionsContext ()); 92 } 93 94 public void testDefaultActionsUsedWhenCreatedForTheFirstTime () throws Exception { 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 108 public void testNoDeserializationOfActions () throws Exception { 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 [] 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 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 { 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 } 163 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 167 public Lkp () throws Exception { 168 this (new org.openide.util.lookup.InstanceContent ()); 169 } 170 171 private Lkp (org.openide.util.lookup.InstanceContent ic) throws Exception { 172 super (ic); 173 174 TestUtilHid.destroyLocalFileSystem (Lkp.class.getName ()); 175 ic.add (new Repository (TestUtilHid.createLocalFileSystem (Lkp.class.getName (), new String [0]))); 176 ic.add (new Pool ()); 177 } 179 } 180 181 182 private static final class Pool extends DataLoaderPool { 183 184 protected java.util.Enumeration loaders () { 185 return org.openide.util.Enumerations.singleton ( 186 DataLoader.getLoader(MyDL.class) 187 ); 188 } 189 190 } 192 private final class PCL implements org.openide.filesystems.FileChangeListener, java.beans.PropertyChangeListener { 193 int cnt; 194 String name; 195 196 public void propertyChange (java.beans.PropertyChangeEvent ev) { 197 name = ev.getPropertyName(); 198 cnt++; 199 } 200 201 public void assertEvent (int cnt, String 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 } 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 |