1 19 20 package org.openide.loaders; 21 22 import java.io.ObjectOutputStream ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Hashtable ; 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.filesystems.FileLock; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileSystem; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.filesystems.Repository; 32 import org.openide.util.Lookup; 33 import org.openide.util.Lookup.Result; 34 import org.openide.util.lookup.AbstractLookup; 35 import org.openide.util.lookup.InstanceContent; 36 import org.openide.util.lookup.Lookups; 37 import org.openide.util.lookup.ProxyLookup; 38 39 public class FolderLookupTest extends NbTestCase { 40 41 public FolderLookupTest(java.lang.String testName) { 42 super(testName); 43 } 44 45 static { 46 System.setProperty ("org.openide.util.Lookup", GLkp.class.getName()); 47 } 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 clearWorkDir(); 52 } 53 54 58 public void testFolderLookupIsUpdatedQuickly () throws Exception { 59 String fsstruct [] = new String [] { 60 "AA/", 61 }; 62 63 TestUtilHid.destroyLocalFileSystem (getName()); 64 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 65 66 FileObject bb = lfs.findResource("/AA"); 67 68 DataFolder folder = DataFolder.findFolder (bb); 69 70 71 Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup (); 72 try { 73 checkTheLookup (lookup, folder); 74 } finally { 75 folder.delete (); 76 } 77 } 78 79 80 private void checkTheLookup (Lookup lookup, DataFolder folder) throws Exception { 81 Class toFind = java.util.Dictionary .class; 82 Class toCreate = java.util.Hashtable .class; 83 84 Object wrongResult = lookup.lookup (toFind); 85 DataObject obj = InstanceDataObject.create (folder, "Test", toCreate); 86 87 if (lookup.lookup (toFind) == null) { 88 fail ("Lookup has not found the class"); 89 } 90 obj.delete (); 91 92 if (lookup.lookup (toFind) != null) { 93 fail ("Still it is possible to find the class"); 94 } 95 if (wrongResult != null) { 96 fail ("There is uncleaned environment: " + wrongResult); 99 } 100 101 } 102 103 107 public void testFolderLookupIsUpdatedQuicklyForSubfolders () throws Exception { 108 String fsstruct [] = new String [] { 109 "AA/", 110 }; 111 112 TestUtilHid.destroyLocalFileSystem (getName()); 113 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 114 115 FileObject bb = lfs.findResource("/AA"); 116 assertNotNull(bb + " not found", bb); 117 118 DataFolder folder = DataFolder.findFolder (bb); 119 120 121 Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup (); 122 checkTheLookupForSubfolders (lookup, folder); 123 } 124 125 private void checkTheLookupForSubfolders (Lookup lookup, DataFolder folder) throws Exception { 126 Class toFind = java.awt.Component .class; 127 Class toCreate = javax.swing.JButton .class; 128 129 Lookup.Result res = lookup.lookupResult(toFind); 130 assertEquals("no Component's in " + res.allInstances(), 0, res.allInstances().size()); 131 132 DataObject obj = InstanceDataObject.create (folder, "Test", toCreate); 133 assertNotNull(obj.getPrimaryFile() + " not found", 134 folder.getPrimaryFile().getFileSystem().findResource(obj.getPrimaryFile().getPath())); 135 assertEquals("just one Component in " + res.allInstances(), 1, res.allInstances().size()); 136 137 DataFolder subfolder = DataFolder.create(folder, "BB"); 138 assertNotNull(subfolder.getPrimaryFile() + " not found", 139 folder.getPrimaryFile().getFileSystem().findResource(subfolder.getPrimaryFile().getPath())); 140 141 obj = InstanceDataObject.create (subfolder, "Test", toCreate); 142 assertNotNull(obj.getPrimaryFile() + " not found", 143 folder.getPrimaryFile().getFileSystem().findResource(obj.getPrimaryFile().getPath())); 144 assertEquals("now two Component's in " + res.allInstances(), 2, res.allInstances().size()); 145 } 146 147 149 public void testProxyLookups () throws Exception { 150 String fsstruct [] = new String [] { 151 "AA/", 152 }; 153 154 TestUtilHid.destroyLocalFileSystem (getName()); 155 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 156 157 FileObject bb = lfs.findResource("/AA"); 158 159 DataFolder folder = DataFolder.findFolder (bb); 160 161 InstanceDataObject obj = InstanceDataObject.create (folder, null, ALkp.class); 162 163 Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup (); 164 165 if (lookup.lookup (Integer .class) == null) { 166 fail ("Integer not found in delegating lookup"); 167 } 168 169 } 170 171 public void testFindInstanceNotCreatedByYouIssue24986 () throws Exception { 172 String fsstruct [] = new String [] { 173 "AA/", 174 }; 175 TestUtilHid.destroyLocalFileSystem (getName()); 176 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 177 178 FileObject bb = lfs.findResource("/AA"); 179 180 String inst = "My instnace"; 181 182 DataFolder folder = DataFolder.findFolder (bb); 183 FileObject fo = FileUtil.createData (folder.getPrimaryFile (), "test.ser"); 184 FileLock lock = fo.lock (); 185 ObjectOutputStream oss = new ObjectOutputStream (fo.getOutputStream (lock)); 186 oss.writeObject (inst); 187 oss.close (); 188 lock.releaseLock (); 189 DataObject o = DataObject.find (fo); 190 assertTrue ("Is IDO: " + o, o instanceof InstanceDataObject); 191 InstanceDataObject obj = (InstanceDataObject)o; 192 193 assertEquals ("The instance is created", inst, obj.instanceCreate ()); 194 assertNotSame ("But is not the same", inst, obj.instanceCreate ()); 195 inst = (String )obj.instanceCreate (); 196 197 Lookup lookup = new org.openide.loaders.FolderLookup (folder).getLookup (); 198 199 Lookup.Template t = new Lookup.Template (null, null, inst); 200 Collection found = lookup.lookup (t).allInstances (); 201 202 assertEquals ("Lookup finds it as well", 1, found.size ()); 203 assertEquals ("Lookup finds it as well", inst, found.iterator ().next()); 204 } 205 206 public void testDeadlockWhileWaitingForFolderRecognizerToFinish50768 () throws Exception { 207 272 273 274 String fsstruct [] = new String [] { 275 "AA/X/Y/Z//java-lang-StringBuffer.instance", 276 }; 277 278 TestUtilHid.destroyLocalFileSystem (getName()); 279 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 280 Repository.getDefault ().addFileSystem (lfs); 281 282 final FileObject bb = lfs.findResource("/AA"); 283 final FileObject file = lfs.findResource ("/AA/X/Y/Z/java-lang-StringBuffer.instance"); 284 final DataFolder folder = DataFolder.findFolder (bb); 285 final Thread t = Thread.currentThread (); 286 DataLoaderPool pool = DataLoaderPool.getDefault (); 287 assertNotNull (pool); 288 289 290 291 class R implements OperationListener, FolderListListener { 292 public int cnt; 293 public int created; 294 public Exception ex; 295 public Lookup lookup; 296 public boolean doWrongThing; 297 298 299 public void operationPostCreate (OperationEvent ev) { 300 if (doWrongThing && file.equals (ev.getObject ().getPrimaryFile ())) { 301 assertSame ("The right thread", t, Thread.currentThread ()); 302 303 doWrongThing = false; 304 cnt++; 305 306 307 try { 308 synchronized (this) { 309 if (!alreadyBlocked) { 310 wait (); 311 } 312 } 313 FileUtil.createData (bb, "java-lang-Object.instance"); 315 } catch (Exception ex) { 316 this.ex = ex; 317 } 318 lookup.lookup (String .class); 320 } 321 } 322 323 324 public void operationCopy (OperationEvent.Copy ev) { 325 } 326 327 328 public void operationMove (OperationEvent.Move ev) { 329 } 330 331 332 public void operationDelete (OperationEvent ev) { 333 } 334 335 336 public void operationRename (OperationEvent.Rename ev) { 337 } 338 339 340 public void operationCreateShadow (OperationEvent.Copy ev) { 341 } 342 343 344 public void operationCreateFromTemplate (OperationEvent.Copy ev) { 345 } 346 347 private boolean alreadyBlocked; 348 public synchronized void process (DataObject obj, java.util.List arr) { 349 if (!alreadyBlocked) { 350 try { 351 wait (1000); 352 } catch (InterruptedException ex) { 353 fail ("No exceptions"); 354 } 355 alreadyBlocked = true; 356 } 357 } 358 359 public synchronized void finished (java.util.List arr) { 360 notifyAll (); 361 } 362 } 363 364 R r = new R (); 365 pool.addOperationListener (r); 366 367 r.lookup = new org.openide.loaders.FolderLookup (folder).getLookup (); 368 369 Object o = r.lookup.lookup (StringBuffer .class); 370 assertNotNull ("StringBuffer found", o); 371 372 org.openide.util.io.NbMarshalledObject mar = new org.openide.util.io.NbMarshalledObject (r.lookup); 373 374 DataObject obj = DataObject.find (file); 375 assertEquals ("IDO", InstanceDataObject.class, obj.getClass ()); 376 java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (obj); 377 obj = null; 378 r.lookup = null; 379 assertGC ("Make sure the object goes away", ref); 380 381 FolderList l = FolderList.find (bb, true); 383 org.openide.util.RequestProcessor.Task task = l.computeChildrenList (r); 384 385 r.doWrongThing = true; 386 r.lookup = (Lookup)mar.get (); 387 388 o = r.lookup.lookup (StringBuffer .class); 390 o = r.lookup.lookup (Runnable .class); 391 392 assertEquals ("Called once", 1, r.cnt); 393 394 if (r.ex != null) { 395 throw r.ex; 396 } 397 398 pool.removeOperationListener (r); 399 } 400 401 402 public void testDeserializationOnFolder () throws Exception { 403 doDeser (true); 404 } 405 406 public void testDeserializationOnSubFolder () throws Exception { 407 doDeser (false); 408 } 409 410 public void testGcWhenHoldingOnlyResult () throws Exception { 411 String fsstruct [] = new String [] { 412 "AA/BB/A.simple" 413 }; 414 415 TestUtilHid.destroyLocalFileSystem (getName()); 416 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 417 Repository.getDefault ().addFileSystem (lfs); 418 419 DataFolder folder = DataFolder.findFolder (lfs.findResource("/AA")); 420 DataFolder subfolder = DataFolder.findFolder (lfs.findResource("/AA/BB/")); 421 DataObject tmp = InstanceDataObject.create (subfolder, null, Hashtable .class); 422 423 FolderLookup lkp = new FolderLookup (folder); 424 Lookup.Result res = lkp.getLookup ().lookup (new Lookup.Template(Hashtable .class)); 425 java.lang.ref.WeakReference ref2 = new java.lang.ref.WeakReference (lkp); 426 427 lkp = null; 428 folder = null; 429 subfolder = null; 430 tmp = null; 431 boolean collected; 432 try { 433 assertGC("XXX", ref2); 434 collected = true; 435 } catch (junit.framework.AssertionFailedError x) { 436 collected = false; 437 } 438 assertEquals(res.allInstances().size(), 1); 439 if (collected) { 440 fail("Lookup got GCed when holding only result.."); 441 } 442 } 443 444 private void doDeser (boolean root) throws Exception { 445 String fsstruct [] = new String [] { 446 "AA/BB/A.simple" 447 }; 448 449 TestUtilHid.destroyLocalFileSystem (getName()); 450 FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct); 451 Repository.getDefault ().addFileSystem (lfs); 452 453 DataFolder folder = DataFolder.findFolder (lfs.findResource("/AA")); 454 DataFolder subfolder = DataFolder.findFolder (lfs.findResource("/AA/BB/")); 455 DataObject tmp = InstanceDataObject.create (subfolder, null, Hashtable .class); 456 457 FolderLookup lkp = new FolderLookup (folder); 458 Object res = lkp.getLookup ().lookup (Hashtable .class); 459 assertNotNull ("The table is obtained", res); 460 461 org.openide.util.io.NbMarshalledObject mar = new org.openide.util.io.NbMarshalledObject (lkp.getLookup ()); 462 463 java.lang.ref.WeakReference ref1 = new java.lang.ref.WeakReference (subfolder); 464 java.lang.ref.WeakReference ref2 = new java.lang.ref.WeakReference (lkp); 465 subfolder = null; 466 tmp = null; 467 folder = null; 468 lkp = null; 469 assertGC ("Lookup can disappear", ref1); 470 assertGC ("Folder can disappear", ref2); 471 472 473 Lookup lookup = (Lookup)mar.get (); 474 ((FolderLookup.ProxyLkp)lookup).waitFinished (); 475 res = lookup.lookup (Hashtable .class); 476 assertNotNull ("A table is there", res); 477 res = lookup.lookup (ArrayList .class); 478 assertNull ("No array list", res); 479 480 DataFolder my = DataFolder.findFolder (lfs.findResource(root ? "/AA/" : "/AA/BB/")); 481 tmp = InstanceDataObject.create (my, null, ArrayList .class); 482 res = lookup.lookup (ArrayList .class); 483 assertNotNull ("array list is there", res); 484 485 Repository.getDefault ().removeFileSystem (lfs); 486 } 487 488 489 490 491 492 public static class ALkp extends AbstractLookup { 493 public InstanceContent ic; 494 495 public ALkp () { 496 this (new InstanceContent ()); 497 } 498 499 private ALkp (InstanceContent ic) { 500 super (ic); 501 ic.add (new Integer (1)); 502 this.ic = ic; 503 } 504 505 } 506 507 public static final class GLkp extends ProxyLookup { 508 public GLkp() { 509 super(new Lookup[] { 510 new ALkp(), 511 Lookups.metaInfServices(GLkp.class.getClassLoader()), 512 }); 513 } 514 } 515 516 } 517 | Popular Tags |