1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.io.Serializable ; 24 import java.util.*; 25 import java.util.logging.Logger ; 26 import javax.swing.SwingUtilities ; 27 import junit.framework.AssertionFailedError; 28 29 import org.openide.filesystems.*; 30 import org.openide.loaders.*; 31 import org.openide.cookies.*; 32 import org.openide.loaders.InstanceSupport.Instance; 33 import org.openide.util.*; 34 35 import org.netbeans.junit.*; 36 import java.util.Enumeration ; 37 38 public class FolderInstanceTest extends LoggingTestCaseHid { 39 private Logger err; 40 41 public FolderInstanceTest() { 42 super(""); 43 } 44 45 public FolderInstanceTest(java.lang.String testName) { 46 super(testName); 47 } 48 49 private static void setSystemProp(String key, String value) { 50 java.util.Properties prop = System.getProperties(); 51 if (prop.get(key) != null) return; 52 prop.put(key, value); 53 } 54 55 protected void setUp () throws Exception { 56 registerIntoLookup(new Pool()); 57 58 DataLoaderPool pool = DataLoaderPool.getDefault (); 59 assertNotNull (pool); 60 assertEquals (Pool.class, pool.getClass ()); 61 62 Pool.setExtra(null); 63 64 clearWorkDir (); 65 66 FileObject[] arr = Repository.getDefault ().getDefaultFileSystem ().getRoot ().getChildren (); 67 for (int i = 0; i < arr.length; i++) { 68 arr[i].delete (); 69 } 70 71 72 err = Logger.getLogger("TEST-" + getName()); 73 } 74 75 78 public void testListenersCountNoCookie () throws Exception { 79 doTestListenersCount (false); 80 } 81 82 85 public void testListenersCountWithCookie () throws Exception { 86 doTestListenersCount (true); 87 } 88 89 94 private void doTestListenersCount (boolean cookie) throws Exception { 95 FileSystem lfs = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem(); 96 97 FileObject bb = lfs.findResource("/AA"); 98 err.info("Found resource: " + bb); 99 if (bb != null) { 100 bb.delete (); 101 } 102 err.info("Resource deleted"); 103 FileObject theFile = FileUtil.createData (lfs.getRoot (), "/AA/A.simple"); 104 err.info("Found the file: " + theFile); 105 bb = FileUtil.createFolder(lfs.getRoot (), "/AA"); 106 err.info("Found the folder: " + bb); 107 assertTrue("Is file", theFile.isData()); 108 err.info("Confirmed, its the data: " + theFile); 109 110 111 DataFolder folder = DataFolder.findFolder (bb); 112 113 114 DataLoader l = DataLoader.getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class); 115 err.info("Add loader: " + l); 116 Pool.setExtra(l); 117 err.info("Loader added"); 118 try { 119 FileObject aa = lfs.findResource("/AA/A.simple"); 120 DataObject tmp = DataObject.find (aa); 121 assertEquals ("Is of the right type", DataLoaderOrigTest.SimpleDataObject.class, tmp.getClass ()); 122 DataLoaderOrigTest.SimpleDataObject obj = (DataLoaderOrigTest.SimpleDataObject)tmp; 123 124 err.info("simple object found: " + obj); 125 126 if (cookie) { 127 err.info("Adding cookie"); 128 obj.cookieSet().add (new InstanceSupport.Instance (new Integer (100))); 129 err.info("Cookie added"); 130 } 131 132 133 F instance = new F (folder); 134 err.info("Instance for " + folder + " created"); 135 Object result = instance.instanceCreate (); 136 err.info("instanceCreate called. Result: " + result); 137 138 Enumeration en = obj.listeners (); 139 140 err.info("Asking for listeners of " + obj); 141 142 assertTrue ("Folder instance should have add one listener", en.hasMoreElements ()); 143 en.nextElement (); 144 assertTrue ("But there should be just one", !en.hasMoreElements ()); 145 146 err.info("Successfully tested for one listener, creating B.simple"); 147 148 folder.getPrimaryFile().createData("B.simple"); 149 err.info("B.simple created"); 150 assertEquals ("DO created", folder.getChildren ().length, 2); 151 err.info("Children obtained correctly"); 152 153 result = instance.instanceCreate (); 155 err.info("instanceCreate finished, with result: " + result); 156 157 en = obj.listeners (); 158 err.info("Asking for listeners once again"); 159 assertTrue ("Folder instance should not change the amount of listeners", en.hasMoreElements ()); 160 en.nextElement (); 161 assertTrue ("And there still should be just one", !en.hasMoreElements ()); 162 err.info("Successfully tested for listeners"); 163 } finally { 164 err.info("Clearing data loader"); 165 Pool.setExtra(null); 166 err.info("Loader cleared"); 167 } 168 } 169 170 172 public void testChangeCookie () throws Exception { 173 String fsstruct [] = new String [] { 174 "AA/A.simple" 175 }; 176 177 TestUtilHid.destroyLocalFileSystem (getName()); 178 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 179 180 FileObject bb = lfs.findResource("/AA"); 181 182 DataFolder folder = DataFolder.findFolder (bb); 183 184 185 DataLoader l = DataLoader.getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class); 186 Pool.setExtra(l); 187 try { 188 FileObject aa = lfs.findResource("/AA/A.simple"); 189 DataObject obj = DataObject.find (aa); 190 191 if (! (obj instanceof DataLoaderOrigTest.SimpleDataObject)) { 192 fail ("Not instance of desired object"); 193 } 194 195 F instance = new F (folder); 196 197 org.openide.nodes.CookieSet set = ((DataLoaderOrigTest.SimpleDataObject)obj).cookieSet (); 198 199 List list; 200 list = (List)instance.instanceCreate (); 201 if (!list.isEmpty ()) { 202 fail ("Should be empty with object with no cookies"); 203 } 204 205 InstanceSupport.Instance is = new InstanceSupport.Instance (new Integer (100)); 206 set.add (is); 207 208 list = (List)instance.instanceCreate (); 209 if (list.isEmpty ()) { 210 fail ("Cookie added, should return instance"); 211 } 212 213 set.remove (is); 214 215 list = (List)instance.instanceCreate (); 216 if (!list.isEmpty ()) { 217 fail ("Cookie removed should be empty"); 218 } 219 220 set.add (is); 221 list = (List)instance.instanceCreate (); 222 if (list.isEmpty ()) { 223 fail ("Cookie added again, should return instance"); 224 } 225 } finally { 226 Pool.setExtra(null); 227 } 228 } 229 230 232 public void testChangeOfOrder () throws Exception { 233 String fsstruct [] = new String [] { 234 "AA/A.simple", 235 "AA/B.simple" 236 }; 237 238 TestUtilHid.destroyLocalFileSystem (getName()); 239 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 240 241 FileObject f = lfs.findResource("/AA"); 242 243 DataFolder folder = DataFolder.findFolder (f); 244 245 246 DataLoader l = DataLoader.getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class); 247 Pool.setExtra(l); 248 try { 249 FileObject aa = lfs.findResource("/AA/A.simple"); 250 DataObject objA = DataObject.find (aa); 251 FileObject bb = lfs.findResource("/AA/B.simple"); 252 DataObject objB = DataObject.find (bb); 253 254 if (! (objA instanceof DataLoaderOrigTest.SimpleDataObject)) { 255 fail ("Not instance of desired object: " + objA); 256 } 257 if (! (objB instanceof DataLoaderOrigTest.SimpleDataObject)) { 258 fail ("Not instance of desired object: " + objB); 259 } 260 261 folder.setOrder (new DataObject[] { objA, objB }); 262 263 F instance = new F (folder); 264 265 { 266 org.openide.nodes.CookieSet set = ((DataLoaderOrigTest.SimpleDataObject)objA).cookieSet (); 267 InstanceSupport.Instance is = new InstanceSupport.Instance (new Integer (1)); 268 set.add (is); 269 } 270 { 271 org.openide.nodes.CookieSet set = ((DataLoaderOrigTest.SimpleDataObject)objB).cookieSet (); 272 InstanceSupport.Instance is = new InstanceSupport.Instance (new Integer (2)); 273 set.add (is); 274 } 275 276 List list; 277 list = (List)instance.instanceCreate (); 278 assertEquals ("Two integer", 2, list.size ()); 279 assertEquals ("1 is first", new Integer (1), list.get (0)); 280 assertEquals ("2 is next", new Integer (2), list.get (1)); 281 282 folder.setOrder (new DataObject[] { objB, objA }); 283 284 list = (List)instance.instanceCreate (); 285 assertEquals ("Two integer", 2, list.size ()); 286 assertEquals ("2 is first", new Integer (2), list.get (0)); 287 assertEquals ("1 is next", new Integer (1), list.get (1)); 288 289 } finally { 290 Pool.setExtra(null); 291 } 292 } 293 294 297 public void testModification () throws Exception { 298 String fsstruct [] = new String [] { 299 "AA/" 300 }; 301 302 TestUtilHid.destroyLocalFileSystem (getName()); 303 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 304 305 FileObject bb = lfs.findResource("/AA"); 306 307 DataFolder folder = DataFolder.findFolder (bb); 308 DataFolder subfolder = DataFolder.create (folder, "BB"); 309 310 modification (new F (folder), folder); 311 } 312 313 314 317 public void testModificationOnSubfolder () throws Exception { 318 String fsstruct [] = new String [] { 319 "AA/BB/" 320 }; 321 322 TestUtilHid.destroyLocalFileSystem (getName()); 323 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 324 325 FileObject bb = lfs.findResource("/AA"); 326 327 DataFolder folder = DataFolder.findFolder (bb); 328 DataFolder subfolder = DataFolder.create (folder, "BB"); 329 330 modification (new F (folder), subfolder); 331 } 332 333 336 public void testModificationOnSubSubfolder () throws Exception { 337 String fsstruct [] = new String [] { 338 "/AA/BB/CC/DD/EE/FF/GG/HH/II/JJ/KK" 339 }; 340 341 TestUtilHid.destroyLocalFileSystem (getName()); 342 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 343 344 FileObject bb = lfs.findResource("/AA"); 345 346 DataFolder folder = DataFolder.findFolder (bb); 347 348 Enumeration en = lfs.getRoot ().getChildren (true); 349 FileObject fo = null; 350 while (en.hasMoreElements ()) { 351 FileObject f = (FileObject)en.nextElement (); 352 if (f.isFolder ()) { 353 fo = f; 354 } 355 } 356 357 DataFolder subfolder = DataFolder.findFolder (fo); 358 359 modification (new F (folder), subfolder); 360 } 361 362 public void testWhetherRenameTriggersRevalidationOfTheFolderInstance() throws Exception { 363 String fsstruct [] = new String [] { 364 "/AAXX/OldName.shadow" 365 }; 366 367 TestUtilHid.destroyLocalFileSystem (getName()); 368 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 369 370 FileObject bb = lfs.findResource("/AAXX"); 371 assertEquals ("One child", 1, bb.getChildren ().length); 372 373 class NamedF extends F { 374 public NamedF (DataFolder f) { 375 super (f); 376 } 377 378 protected InstanceCookie acceptDataObject (DataObject obj) { 379 return new InstanceSupport.Instance (obj.getName ()); 380 } 381 } 382 383 DataFolder f = DataFolder.findFolder (bb); 384 NamedF namedf = new NamedF (f); 385 386 List result; 387 result = (List)namedf.instanceCreate (); 388 if (1 != result.size ()) { 389 fail ("One item expected, but: " + result); 390 } 391 assertEquals ("It is the name of data object", "OldName", result.get (0)); 392 393 FileObject aa = lfs.findResource (fsstruct[0]); 394 DataObject.find (aa).rename ("NewName"); 395 396 result = (List)namedf.instanceCreate (); 397 assertEquals ("One item", 1, result.size ()); 398 assertEquals ("It is the name of data object", "NewName", result.get (0)); 399 } 400 401 403 private static void modification (F instance, DataFolder folder) 404 throws Exception { 405 List list; 406 int cnt; 407 list = (List)instance.instanceCreate (); 408 409 if (list.size () != 0) { 410 fail ("List should be empty: " + list); 411 } 412 413 cnt = instance.getCount (); 414 if (cnt != 1) { 415 fail ("Too many calls to createInstance during initialization: " + cnt); 416 } 417 418 419 InstanceDataObject obj = InstanceDataObject.create (folder, null, Numb.class); 420 421 list = (List)instance.instanceCreate (); 422 423 assertEquals ("One item", 1, list.size ()); 424 assertEquals ("The item is of the right class", Numb.class, list.get (0).getClass ()); 425 426 cnt = instance.getCount (); 427 if (cnt != 1) { 428 fail ("Too many calls to createInstance after create: " + cnt); 429 } 430 431 obj.delete (); 432 433 list = (List)instance.instanceCreate (); 434 435 if (list.size () != 0) { 436 fail ("List should be empty again: " + list); 437 } 438 439 cnt = instance.getCount (); 440 if (cnt != 1) { 441 fail ("Too many calls to createInstance after delete: " + cnt); 442 } 443 444 } 445 446 447 448 private static class F extends FolderInstance { 449 450 private int count; 451 452 public F (DataFolder f) { 453 super (f); 454 } 455 456 458 public synchronized int getCount () { 459 int c = count; 460 count = 0; 461 return c; 462 } 463 464 465 467 protected InstanceCookie acceptFolder (DataFolder f) { 468 return new F (f); 469 } 470 471 protected Object createInstance (InstanceCookie[] arr) 472 throws java.io.IOException , ClassNotFoundException { 473 synchronized (this) { 474 count++; 475 } 476 LinkedList ll = new LinkedList (); 477 for (int i = 0; i < arr.length; i++) { 478 Object obj = arr[i].instanceCreate (); 479 if (obj instanceof Collection) { 480 ll.addAll ((Collection)obj); 481 } else { 482 ll.add (obj); 483 } 484 } 485 return ll; 486 } 487 protected Task postCreationTask (Runnable run) { 488 490 run.run (); 491 return null; 492 } 493 } 494 495 500 public void testFolderInstanceNeverPassesInvObjects() throws Exception { 501 doFolderInstanceNeverPassesInvObjects (100, 1000); 502 } 503 private void doFolderInstanceNeverPassesInvObjects (int cnt, int sleep) throws Exception { 504 String [] names = new String [cnt]; 505 for (int i = 0; i < names.length; i++) { 506 names[i] = "folder/file" + i + ".simple"; 507 } 508 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), names); 509 Repository.getDefault().addFileSystem(lfs); 510 try { 511 FileObject folder = lfs.findResource("folder"); 512 DataLoader l = DataLoader.getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class); 513 DataFolder f = DataFolder.findFolder(folder); 514 InvCheckFolderInstance icfi = new InvCheckFolderInstance(f, false); 515 assertTrue(icfi.ok); 516 assertEquals(new Integer (0), icfi.instanceCreate()); 517 err.info("sample1: " + DataObject.find(lfs.findResource(names[0]))); 518 Pool.setExtra(l); 519 try { 520 err.info("sample2: " + DataObject.find(lfs.findResource(names[0]))); 521 assertTrue(icfi.ok); 522 532 Thread.sleep(sleep); 533 assertEquals(new Integer (cnt), icfi.instanceCreate()); 534 assertTrue(icfi.ok); 536 } finally { 539 err.info("begining to clear the pool"); 540 Pool.setExtra(null); 541 err.info("clearing pool is finished"); 542 } 543 err.info("sample3: " + DataObject.find(lfs.findResource(names[0]))); 544 err.info("sample4: " + DataFolder.findFolder(lfs.findResource(names[0]).getParent ()).getChildren()[0]); 545 assertTrue(icfi.ok); 546 Object instance = null; 547 for (int i = 0; i < 1; i++) { 548 Thread.sleep(sleep); 549 err.info("getting the instance: " + i); 550 instance = icfi.instanceCreate(); 551 err.info("instance is here (" + i + "): " + instance); 552 553 if (new Integer (0).equals (instance)) { 554 break; 555 } 556 } 557 assertEquals(new Integer (0), instance); 558 err.info("passed the usual failing point"); 559 assertTrue(icfi.ok); 561 Pool.setExtra(l); 562 try { 563 assertTrue(icfi.ok); 564 Thread.sleep(sleep); 565 assertTrue(icfi.ok); 566 } finally { 567 Pool.setExtra(null); 568 } 569 assertTrue(icfi.ok); 570 Pool.setExtra(l); 571 try { 572 assertTrue(icfi.ok); 573 } finally { 574 Pool.setExtra(null); 575 } 576 assertTrue(icfi.ok); 577 Thread.sleep(sleep); 578 assertTrue(icfi.ok); 579 } finally { 580 Repository.getDefault().removeFileSystem(lfs); 581 } 582 } 583 584 585 public void testFolderInstanceNeverPassesInvFolders() throws Exception { 586 String [] names = { 587 "folder/sub/" 588 }; 589 FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), names); 590 Repository.getDefault().addFileSystem(lfs); 591 try { 592 FileObject folder = lfs.findResource("folder"); 593 DataFolder f = DataFolder.findFolder(folder); 594 595 DataObject[] arr = f.getChildren(); 596 assertEquals("One child", 1, arr.length); 597 assertEquals("It is folder", DataFolder.class, arr[0].getClass()); 598 599 err.info("Creating InvCheckFolderInstance"); 600 InvCheckFolderInstance icfi = new InvCheckFolderInstance(f, true); 601 err.info("Computing result"); 602 List computed = (List)icfi.instanceCreate(); 603 err.info("Result is here: " + computed); 604 assertEquals("One from folder instance", 1, computed.size()); 605 assertEquals("The same data object", arr[0], computed.get(0)); 606 607 arr[0].setValid(false); 608 609 List newComputed = (List)icfi.instanceCreate(); 610 assertEquals("Still one", 1, newComputed.size()); 611 612 DataObject[] arr2 = f.getChildren(); 613 assertEquals("Still one", 1, arr2.length); 614 if (arr[0] == arr2[0]) { 615 fail("They should not be the same: " + arr2[0]); 616 } 617 618 assertEquals("The same new object", arr2[0], newComputed.get(0)); 619 620 621 } finally { 622 Repository.getDefault().removeFileSystem(lfs); 623 } 624 } 625 626 private final class InvCheckFolderInstance extends FolderInstance { 627 public boolean ok = true; 628 private boolean acceptF; 629 public InvCheckFolderInstance(DataFolder f, boolean folders) { 630 super(f); 631 this.acceptF = folders; 632 } 633 634 protected Object createInstance(InstanceCookie[] cookies) throws IOException , ClassNotFoundException { 635 err.info("new createInstance: " + cookies.length); 637 638 if (acceptF) { 639 ArrayList list = new ArrayList(); 640 for (int i = 0; i < cookies.length; i++) { 641 list.add(cookies[i].instanceCreate()); 642 } 643 return list; 644 } 645 646 return new Integer (cookies.length); 647 } 648 649 protected InstanceCookie acceptDataObject(DataObject o) { 650 if (! o.isValid()) { 651 ok = false; 652 Thread.dumpStack(); 653 return null; 654 } 655 if (o instanceof DataLoaderOrigTest.SimpleDataObject) { 656 err.info("got a simpledataobject"); 657 try { 659 Thread.sleep(10); 660 } catch (InterruptedException ie) {} 661 return new InstanceSupport.Instance("ignore"); 662 } else { 663 if (acceptF && o instanceof DataFolder) { 664 err.info("Recognized folder: " + o); 665 return new InstanceSupport.Instance(o); 666 } 667 err.info("got a " + o); 668 return null; 669 } 670 } 671 protected Task postCreationTask (Runnable run) { 673 err.info("postCreationTask"); 674 return new AWTTask (run); 675 } 676 } 677 private final class AWTTask extends Task { 678 private boolean executed; 679 public AWTTask (Runnable r) { 680 super (r); 681 Mutex.EVENT.readAccess (this); 682 } 683 public void run () { 684 if (!executed) { 685 super.run (); 686 executed = true; 687 err.info("AWTTask executed"); 688 } 689 } 690 public void waitFinished () { 691 err.info("AWTTask waitFinished"); 692 if (SwingUtilities.isEventDispatchThread ()) { 693 err.info("AWTTask waitFinished on AWT thread"); 694 run (); 695 err.info("AWTTask waitFinished on AWT thread done"); 696 } else { 697 super.waitFinished (); 698 err.info("AWTTask waitFinished done"); 699 } 700 } 701 } 702 703 public static final class Numb extends Object implements Serializable { 704 public Numb () { 705 } 706 } 707 708 709 710 public static final class Lkp extends org.openide.util.lookup.AbstractLookup { 711 public Lkp() { 712 this(new org.openide.util.lookup.InstanceContent()); 713 } 714 715 private Lkp(org.openide.util.lookup.InstanceContent ic) { 716 super(ic); 717 ic.add(new Pool ()); 718 } 719 } 720 721 static final class Pool extends org.openide.loaders.DataLoaderPool { 722 private static DataLoader extra; 723 724 725 protected Enumeration loaders () { 726 if (getExtra() == null) { 727 return Enumerations.empty (); 728 } else { 729 return Enumerations.singleton (getExtra()); 730 } 731 } 732 733 public static DataLoader getExtra () { 734 return extra; 735 } 736 737 public static void setExtra (DataLoader aExtra) { 738 if (extra != null && aExtra != null) { 739 fail ("Both are not null: " + extra + " aExtra: " + aExtra); 740 } 741 extra = aExtra; 742 Pool p = (Pool)DataLoaderPool.getDefault (); 743 p.fireChangeEvent (new javax.swing.event.ChangeEvent (p)); 744 } 745 } 746 } 747
| Popular Tags
|