1 19 20 package org.openide.loaders; 21 22 import java.io.File ; 23 import java.lang.ref.WeakReference ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 import junit.framework.AssertionFailedError; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileStateInvalidException; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.LocalFileSystem; 35 import org.openide.filesystems.Repository; 36 import org.openide.nodes.Node; 37 import org.openide.util.Utilities; 38 39 42 public class DataShadowTest extends NbTestCase 43 implements java.net.URLStreamHandlerFactory { 44 45 private DataObject original; 46 47 private DataFolder folder; 48 49 private FileSystem lfs; 50 51 private Logger err; 52 53 static { 54 java.net.URL.setURLStreamHandlerFactory (new DataShadowTest (null)); 56 } 57 58 public DataShadowTest (String name) { 59 super(name); 60 } 61 62 protected Level logLevel() { 63 return Level.FINER; 64 } 65 66 protected void setUp() throws Exception { 67 68 lfs = Repository.getDefault ().getDefaultFileSystem (); 69 70 FileObject[] delete = lfs.getRoot().getChildren(); 71 for (int i = 0; i < delete.length; i++) { 72 delete[i].delete(); 73 } 74 75 FileObject fo = FileUtil.createData (lfs.getRoot (), getName () + "/folder/original.txt"); 76 assertNotNull(fo); 77 original = DataObject.find (fo); 78 assertFalse ("Just to be sure that this is not shadow", original instanceof DataShadow); 79 assertFalse ("And is some kind of subclass of DO", original.getClass () == DataObject.class); 80 fo = FileUtil.createFolder (lfs.getRoot (), getName () + "/modify"); 81 assertNotNull(fo); 82 assertTrue (fo.isFolder ()); 83 folder = DataFolder.findFolder (fo); 84 85 Repository.getDefault ().addFileSystem (lfs); 86 87 err = Logger.getLogger(getName()); 88 } 89 90 public java.net.URLStreamHandler createURLStreamHandler(String protocol) { 91 if (protocol.equals ("nbfs")) { 92 return FileUtil.nbfsURLStreamHandler (); 93 } 94 return null; 95 } 96 97 public void testCreateTheShadow61175() throws Exception { 98 if (!Utilities.isUnix()) { 99 return; 100 } 101 102 final LocalFileSystem lfs = new LocalFileSystem() { 103 public Status getStatus() { 104 return new TestStatus(this); 105 } 106 107 class TestStatus implements FileSystem.Status { 108 FileSystem lfs; 109 TestStatus(FileSystem lfs) { 110 this.lfs = lfs; 111 } 112 public String annotateName(String name, java.util.Set files) { 113 if (files.size() > 0) { 114 try { 115 FileSystem fs = ((FileObject)files.toArray()[0]).getFileSystem(); 116 assertEquals(fs, lfs); 117 } catch(FileStateInvalidException fsx) {} 118 119 } 120 return name; 121 } 122 123 public java.awt.Image annotateIcon(java.awt.Image icon, int iconType, java.util.Set files) { 124 return icon; 125 } 126 } 127 }; 128 129 130 lfs.setRootDirectory(new File ("/")); 131 Repository.getDefault().addFileSystem(lfs); 132 133 134 DataFolder what = DataFolder.findFolder(lfs.getRoot()); 135 assertNotNull(what); 136 FileObject whereFo = Repository.getDefault().getDefaultFileSystem().getRoot(); 137 assertNotNull(whereFo); 138 139 DataFolder where = DataFolder.findFolder(whereFo); 140 assertNotNull(where); 141 142 DataShadow shade = what.createShadow(where); 143 assertNotNull(shade); 144 145 Node node= shade.getNodeDelegate(); 146 assertNotNull(node); 147 node.getIcon (java.beans.BeanInfo.ICON_COLOR_16x16); 148 149 } 150 151 public void testBrokenShadow55115 () throws Exception { 152 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 153 FileObject brokenShadow = FileUtil.createData(sfs.getRoot(),"brokenshadows/brokon.shadow"); 154 assertNotNull (brokenShadow); 155 BrokenDataShadow bds = (BrokenDataShadow)DataObject.find(brokenShadow); 158 assertNotNull (bds); 159 URL url = bds.getUrl(); 160 bds.refresh(); 163 164 if ("file".equals(url.getProtocol())) { 167 new File (URI.create(url.toExternalForm())); 168 } 169 } 170 171 public void testCreateTheShadow () throws Exception { 172 DataShadow shade = original.createShadow (folder); 173 174 assertEquals ("Shadow's original is the one", original, shade.getOriginal ()); 175 176 Object cookie = shade.getCookie (DataObject.class); 177 assertEquals ("The shadow is own data object", shade, cookie); 178 179 cookie = shade.getCookie (original.getClass ()); 180 assertEquals ("But it also returns the original when requested", original, cookie); 181 182 URL u = DataShadow.readURL(shade.getPrimaryFile()); 183 assertEquals("DataShadow's URL must point to the Original", original.getPrimaryFile().getURL(), u); 184 } 185 186 public void testDeleteInvalidatesCreateCreates () throws Exception { 187 doDeleteInvalidatesCreateCreates (true); 188 } 189 190 194 public void testDeleteInvalidatesCreateCreatesJustOnFileSystemLevel () throws Exception { 195 doDeleteInvalidatesCreateCreates (false); 196 } 197 198 private void doDeleteInvalidatesCreateCreates (boolean createDataObjectOrNot) throws Exception { 199 DataShadow shade = original.createShadow (folder); 200 FileObject primary = shade.getPrimaryFile (); 201 202 assertTrue ("Is valid now", shade.isValid ()); 203 err.info("Before delete"); 204 original.delete (); 205 err.info("After delete"); 206 207 assertFalse ("Shadow is not valid anymore", shade.isValid ()); 208 assertFalse ("Original is gone", original.isValid ()); 209 210 err.info("Going to find new shadow"); 211 DataObject shade2 = DataObject.find (primary); 212 err.info("Found: " + shade2); 213 assertEquals ("Represents broken shadow (a bit implemetnation detail, but useful test)", BrokenDataShadow.class, shade2.getClass ()); 214 assertFalse ("Is not data shadow", shade2 instanceof DataShadow); 215 216 err.info("Before recreation of the original"); 218 FileObject original2 = FileUtil.createData (lfs.getRoot (), original.getPrimaryFile ().getPath ()); 219 err.info("Original is there: " + original2); 220 DataObject obj2; 221 222 if (createDataObjectOrNot) { 223 err.info("Now get the data object"); 224 obj2 = DataObject.find (original2); 225 err.info("Object is there: " + obj2); 226 } 227 228 assertFalse ("Previous is not valid anymore", shade2.isValid ()); 229 230 DataObject shade3 = DataObject.find (primary); 231 err.info("Shade3 is here: " + shade3); 232 assertTrue ("it is a data shadow again", shade3 instanceof DataShadow); 233 assertEquals ("Points to the same filename", original.getPrimaryFile ().getPath (), ((DataShadow)shade3).getOriginal ().getPrimaryFile ().getPath ()); 234 235 err.info("Before find for " + original2); 236 assertEquals ("But of course the original is newly created", DataObject.find (original2), ((DataShadow)shade3).getOriginal ()); 237 238 err.info("Before shade.getOriginal()"); 239 assertEquals ("The old shadow is updated & getOriginal() waits for that to happen", original2, shade.getOriginal().getPrimaryFile()); 240 } 241 242 public void testDeleteInvalidatesCreateCreatesWhenChangeHappensInAtomicAction () throws Exception { 243 DataShadow shade = original.createShadow (folder); 244 FileObject primary = shade.getPrimaryFile (); 245 246 assertTrue ("Is valid now", shade.isValid ()); 247 248 class DeleteCreate implements FileSystem.AtomicAction { 249 public FileObject fo; 250 251 public void run () throws java.io.IOException { 252 FileSystem fs = original.getPrimaryFile ().getFileSystem (); 253 String create = original.getPrimaryFile ().getPath (); 254 original.getPrimaryFile ().delete (); 255 256 fo = FileUtil.createData (fs.getRoot (), create); 257 } 258 } 259 DeleteCreate deleteCreate = new DeleteCreate (); 260 original.getPrimaryFile ().getFileSystem ().runAtomicAction (deleteCreate); 261 262 assertTrue ("Shadow is valid (again)", shade.isValid ()); 263 assertFalse ("Original is gone", original.isValid ()); 264 DataObject orig = DataObject.find (deleteCreate.fo); 265 if (orig == original) { 266 fail ("new original shall be created"); 267 } 268 assertTrue ("New orig is valid", orig.isValid ()); 269 270 Thread.sleep (2000); 275 276 assertEquals ("Shadow's original is updated", orig, shade.getOriginal ()); 277 } 278 279 public void testRenameUpdatesTheShadowIfItExists () throws Exception { 280 DataShadow shade = original.createShadow (folder); 281 FileObject primary = shade.getPrimaryFile (); 282 283 original.rename ("newname.txt"); 284 285 WeakReference ref = new WeakReference (shade); 286 shade = null; 287 assertGC ("Shadow can disappear", ref); 288 289 DataObject obj = DataObject.find (primary); 290 assertEquals ("It is shadow", DataShadow.class, obj.getClass ()); 291 shade = (DataShadow)obj; 292 293 assertEquals ("And points to original with updated name", original, shade.getOriginal ()); 294 } 295 296 public void testRenameDoesNotUpdateTheShadowIfItDoesNotExist () throws Exception { 297 302 DataShadow shade = original.createShadow (folder); 303 FileObject primary = shade.getPrimaryFile (); 304 305 WeakReference ref = new WeakReference (shade); 306 shade = null; 307 assertGC ("Shadow can disappear", ref); 308 309 original.rename ("newname"); 310 311 312 DataObject obj = DataObject.find (primary); 313 assertEquals ("It is broken shadow", BrokenDataShadow.class, obj.getClass ()); 314 } 315 316 public void testBrokenShadowNodeProperties() throws Exception { 317 DataShadow shade = original.createShadow (folder); 318 FileObject primary = shade.getPrimaryFile (); 319 320 assertTrue ("Is valid now", shade.isValid ()); 321 original.delete (); 322 323 DataObject obj = DataObject.find (primary); 324 assertEquals ("Instance class must be BrokenDataShadow", BrokenDataShadow.class, obj.getClass ()); 325 326 Node node = obj.getNodeDelegate (); 327 328 Node.Property link = findProperty (node, "BrokenLink"); 329 assertNotNull ("Link must be non null string", (String )link.getValue ()); 330 331 assertTrue ("Is writeable", link.canWrite ()); 332 FileObject fo = FileUtil.createData (lfs.getRoot (), getName () + "/folder/orig.txt"); 334 link.setValue (fo.getURL().toExternalForm()); 335 336 assertFalse ("The change of link should turn the shadow to valid one and invalidate this broken shadow", obj.isValid ()); 337 338 DataObject newObj = DataObject.find (primary); 339 assertEquals ("This is a shadow", DataShadow.class, newObj.getClass ()); 340 shade = (DataShadow)newObj; 341 342 assertEquals ("Points to the new file", getName () + "/folder/orig.txt", shade.getOriginal ().getPrimaryFile ().getPath ()); 343 } 344 345 public void testFindOriginalFromAnonymousFilesystem() throws Exception { 346 FileSystem fs = FileUtil.createMemoryFileSystem(); 348 FileObject orig = FileUtil.createData(fs.getRoot(), "path/to/orig"); 349 FileObject shadow = FileUtil.createData(fs.getRoot(), "link.shadow"); 350 shadow.setAttribute("originalFile", "path/to/orig"); 351 assertEquals("found the right original file", DataObject.find(orig), DataShadow.deserialize(shadow)); 352 orig = FileUtil.createData(fs.getRoot(), "path to orig"); 353 shadow = FileUtil.createData(fs.getRoot(), "link2.shadow"); 354 shadow.setAttribute("originalFile", "path to orig"); 355 assertEquals("found the right original file", DataObject.find(orig), DataShadow.deserialize(shadow)); 356 } 357 358 private static Node.Property findProperty (Node n, String name) { 359 Node.PropertySet[] arr = n.getPropertySets (); 360 StringBuffer names = new StringBuffer (); 361 362 String prefix = ""; 363 for (int i = 0; i < arr.length; i++) { 364 Node.PropertySet set = arr[i]; 365 Node.Property[] properties = set.getProperties (); 366 for (int j = 0; j < properties.length; j++) { 367 Node.Property p = properties[j]; 368 if (name.equals (p.getName ())) { 369 return p; 370 } 371 names.append (prefix); 372 names.append (p.getName ()); 373 prefix = ", "; 374 } 375 } 376 377 fail ("Cannot find property \"" + name + "\" in node " + n + " it has only " + names + " propeties."); 378 return null; 379 } 380 } 381 | Popular Tags |