1 19 20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.lang.ref.Reference ; 28 import java.lang.ref.WeakReference ; 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 import java.util.Enumeration ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.logging.Handler ; 36 import java.util.logging.Level ; 37 import java.util.logging.LogRecord ; 38 import java.util.logging.Logger ; 39 import org.netbeans.junit.NbTestCase; 40 import org.netbeans.modules.masterfs.filebasedfs.Statistics; 41 import org.netbeans.modules.masterfs.filebasedfs.FileBasedFileSystem; 42 import org.netbeans.modules.masterfs.filebasedfs.naming.NamingFactory; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileSystem; 45 import org.openide.filesystems.FileUtil; 46 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo; 47 import org.netbeans.modules.masterfs.providers.ProvidedExtensions; 48 import org.netbeans.modules.masterfs.providers.ProvidedExtensionsTest; 49 import org.openide.filesystems.FileChangeAdapter; 50 import org.openide.filesystems.FileChangeListener; 51 import org.openide.filesystems.FileEvent; 52 import org.openide.filesystems.FileLock; 53 import org.openide.filesystems.FileRenameEvent; 54 import org.openide.filesystems.URLMapper; 55 import org.openide.util.Utilities; 56 57 61 public class FolderObjTest extends NbTestCase { 62 File testFile; 63 64 public FolderObjTest(String testName) { 65 super(testName); 66 } 67 68 protected void setUp() throws Exception { 69 clearWorkDir(); 70 testFile = getWorkDir(); 71 } 72 73 protected Level logLevel() { 74 String [] testsWithEnabledLogger = new String [] { 75 "testCreateFolder72617", 76 "testCreateData72617" 77 }; 78 return (Arrays.asList(testsWithEnabledLogger).contains(getName())) ? 79 Level.FINEST : Level.OFF; 80 } 81 82 static abstract class IntrusiveLogHandler extends Handler { 83 private String message; 84 IntrusiveLogHandler(final String message) { 85 this.message = message; 86 } 87 public final void publish(LogRecord record) { 88 String recordMsg = record.getMessage(); 89 if (recordMsg != null && recordMsg.indexOf(this.message) != -1) { 90 processLogRecord(record); 91 } 92 } 93 94 abstract protected void processLogRecord(final LogRecord record); 95 public void flush() {} 96 public void close() { flush(); } 97 } 98 99 public void testMove85336() throws Exception { 100 final FileObject workDirFo = FileBasedFileSystem.getFileObject(getWorkDir()); 101 FolderObj to = (FolderObj)FileUtil.createFolder(workDirFo, "a/b/c"); 102 FolderObj from = (FolderObj)FileUtil.createFolder(workDirFo, "aa/b/c"); 103 assertNotNull(to); 104 assertNotNull(from); 105 BaseFileObj what = (BaseFileObj)FileUtil.createData(from, "hello.txt"); 106 assertNotNull(what); 107 FileLock lck = what.lock(); 108 ProvidedExtensions.IOHandler io = new ProvidedExtensionsTest.ProvidedExtensionsImpl(). 109 getMoveHandler(what.getFileName().getFile(), new File (to.getFileName().getFile(),what.getNameExt())); 110 to.getChildren(); 111 try { 112 what.move(lck, to, what.getName(), what.getExt(), io); 113 } finally { 114 lck.releaseLock(); 115 } 116 } 117 118 public void testCreateFolder72617() throws IOException { 119 Handler handler = new IntrusiveLogHandler("FolderCreated:") { protected void processLogRecord(final LogRecord record) { 121 Object [] params = record.getParameters(); 122 if (params != null && params.length > 0 && params[0] instanceof File ) { 123 File f = (File )params[0]; 124 assertTrue(f.exists()); 125 assertTrue(f.delete()); 126 } 127 } 128 }; 129 Logger.getLogger("org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj").addHandler(handler); 130 try { 131 File f = testFile; 132 FileSystem fs = FileBasedFileSystem.getInstance(f); 133 assertNotNull(fs); 134 135 FileObject fo = fs.findResource(TestUtils.getFileObjectPath(testFile)); 136 assertNotNull(fo); 137 File f2 = new File (testFile, "newfoldercreated"); 138 try { 139 fo.createFolder (f2.getName()); 140 fail(); 141 } catch(IOException iex) {} 142 } finally { 143 Logger.getLogger("org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj").removeHandler(handler); 144 } 145 } 146 147 public void testCreateData72617() throws IOException { 148 Handler handler = new IntrusiveLogHandler("DataCreated:") { protected void processLogRecord(final LogRecord record) { 150 Object [] params = record.getParameters(); 151 if (params != null && params.length > 0 && params[0] instanceof File ) { 152 File f = (File )params[0]; 153 assertTrue(f.exists()); 154 assertTrue(f.delete()); 155 } 156 } 157 }; 158 Logger.getLogger("org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj").addHandler(handler); 159 try { 160 File f = testFile; 161 FileSystem fs = FileBasedFileSystem.getInstance(f); 162 assertNotNull(fs); 163 164 FileObject fo = fs.findResource(TestUtils.getFileObjectPath(testFile)); 165 assertNotNull(fo); 166 File f2 = new File (testFile, "newdatacreated"); 167 try { 168 fo.createData (f2.getName()); 169 fail(); 170 } catch(IOException iex) {} 171 } finally { 172 Logger.getLogger("org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj").removeHandler(handler); 173 } 174 } 175 176 177 public void testFileObjectDistributionWorksAccuratelyAccordingToChildrenCache() throws IOException { 178 final FileObject workDirFo = FileBasedFileSystem.getFileObject(getWorkDir()); 179 assertNotNull(workDirFo); 180 File fold = new File (getWorkDir(),"fold"); assertNull(FileUtil.toFileObject(fold)); 182 FileObject foldFo = workDirFo.createFolder(fold.getName()); 183 assertNotNull(foldFo); 184 185 foldFo.delete(); 186 assertNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 187 assertNull(FileBasedFileSystem.getFileObject(fold)); 188 assertNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 189 assertNull(workDirFo.getFileObject(fold.getName())); 190 assertFalse(existsChild(workDirFo, fold.getName())); 191 fold.mkdir(); 192 assertNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 193 assertNull(FileBasedFileSystem.getFileObject(fold)); 194 assertNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 195 assertNull(workDirFo.getFileObject(fold.getName())); 196 assertFalse(existsChild(workDirFo, fold.getName())); 197 workDirFo.refresh(); 198 assertNotNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 199 assertNotNull(FileBasedFileSystem.getFileObject(fold)); 200 assertNotNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 201 assertNotNull(workDirFo.getFileObject(fold.getName())); 202 assertTrue(existsChild(workDirFo, fold.getName())); 203 fold.delete(); 204 assertNotNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 205 assertNotNull(FileBasedFileSystem.getFileObject(fold)); 206 assertNotNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 207 assertNotNull(workDirFo.getFileObject(fold.getName())); 208 assertTrue(existsChild(workDirFo, fold.getName())); 209 workDirFo.refresh(); 210 assertNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 211 assertNull(FileBasedFileSystem.getFileObject(fold)); 212 assertNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 213 assertNull(workDirFo.getFileObject(fold.getName())); 214 assertFalse(existsChild(workDirFo, fold.getName())); 215 fold.mkdir(); 216 assertNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 217 assertNull(FileBasedFileSystem.getFileObject(fold)); 218 assertNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 219 assertNull(workDirFo.getFileObject(fold.getName())); 220 assertFalse(existsChild(workDirFo, fold.getName())); 221 workDirFo.getFileSystem().refresh(false); 222 assertNotNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 223 assertNotNull(FileBasedFileSystem.getFileObject(fold)); 224 assertNotNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 225 assertNotNull(workDirFo.getFileObject(fold.getName())); 226 assertTrue(existsChild(workDirFo, fold.getName())); 227 foldFo.delete(); 228 assertNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 229 assertNull(FileBasedFileSystem.getFileObject(fold)); 230 assertNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 231 assertNull(workDirFo.getFileObject(fold.getName())); 232 assertFalse(existsChild(workDirFo, fold.getName())); 233 fold.mkdir(); 234 assertNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 235 assertNull(FileBasedFileSystem.getFileObject(fold)); 236 assertNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 237 assertNull(workDirFo.getFileObject(fold.getName())); 238 assertFalse(existsChild(workDirFo, fold.getName())); 239 workDirFo.getFileSystem().refresh(false); 240 assertNotNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 241 assertNotNull(FileBasedFileSystem.getFileObject(fold)); 242 assertNotNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 243 assertNotNull(workDirFo.getFileObject(fold.getName())); 244 assertTrue(existsChild(workDirFo, fold.getName())); 245 fold.delete(); 246 assertNotNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 247 assertNotNull(FileBasedFileSystem.getFileObject(fold)); 248 assertNotNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 249 assertNotNull(workDirFo.getFileObject(fold.getName())); 250 assertTrue(existsChild(workDirFo, fold.getName())); 251 workDirFo.getFileSystem().refresh(false); 252 assertNull(((FileBasedFileSystem)workDirFo.getFileSystem()).findFileObject(fold)); 253 assertNull(FileBasedFileSystem.getFileObject(fold)); 254 assertNull((workDirFo.getFileSystem()).findResource(fold.getAbsolutePath())); 255 assertNull(workDirFo.getFileObject(fold.getName())); 256 assertFalse(existsChild(workDirFo, fold.getName())); 257 } 258 259 private static boolean existsChild(final FileObject folder, final String childName) { 260 FileObject[] childs = folder.getChildren(); 261 for (int i = 0; i < childs.length; i++) { 262 if (childs[i].getNameExt().equals(childName)) { 263 return true; 264 } 265 } 266 return false; 267 } 268 269 public void testChildren() throws Exception { 270 final FolderObj testRoot = (FolderObj)FileBasedFileSystem.getFileObject(getWorkDir()); 271 assertNotNull(testRoot); 272 for (int i = 0; i < 5; i++) { 273 assertTrue(new File (getWorkDir(), "file"+String.valueOf(i)).createNewFile()); 274 } 275 assertEquals(5,testRoot.getChildren().length); 276 } 277 278 public void testSize() throws Exception { 279 final FolderObj testRoot = (FolderObj)FileBasedFileSystem.getFileObject(getWorkDir()); 280 assertNotNull(testRoot); 281 FileObj bfo = (FileObj)testRoot.createData(getName()); 282 283 assertSize("",Collections.singleton(bfo),32 ,new Object [] {bfo.getFileName()}); 284 assertSize("",Collections.singleton(testRoot),32 ,new Object [] {testRoot.getFileName(), 285 testRoot.getChildrenCache()}); 286 } 287 288 public void testCreateNbAttrs() throws IOException { 289 final FileObject testRoot = FileBasedFileSystem.getFileObject(getWorkDir()); 290 assertNotNull(testRoot); 291 try { 292 testRoot.createData(".nbattrs"); 293 fail(); 294 } catch (IOException ex) {} 295 } 296 297 public void testCreateWriteLock() throws IOException { 298 final FileObject testRoot = FileBasedFileSystem.getFileObject(getWorkDir()); 299 assertNotNull(testRoot); 300 File file2lock = new File (getWorkDir(),"aa.txt"); 301 File associatedLock = WriteLockUtils.getAssociatedLockFile(file2lock); 303 try { 304 testRoot.createData(associatedLock.getName()); 305 fail(); 306 } catch (IOException ex) {} 307 } 308 309 310 public void testCaseInsensitivity() throws Exception { 311 if (!Utilities.isWindows()) return; 312 final FileObject testRoot = FileBasedFileSystem.getFileObject(getWorkDir()); 313 assertNotNull(testRoot); 314 315 File testa = new File (getWorkDir(), "a"); 316 File testA = new File (getWorkDir(), "A"); 317 318 if (testA.exists()) { 319 assertTrue(testA.delete()); 320 } 321 if (!testa.exists()) { 322 assertTrue(testa.createNewFile()); 323 } 324 325 FileObject A = testRoot.getFileObject("A"); 328 assertNotNull(A); 329 assertNotNull(testRoot.getFileObject("a")); 330 assertSame(testRoot.getFileObject("A"), testRoot.getFileObject("a")); 331 assertSame(URLMapper.findFileObject(testa.toURI().toURL()), 332 URLMapper.findFileObject(testA.toURI().toURL())); 333 334 testRoot.getChildren(); 336 assertEquals("A",testRoot.getFileObject("A").getName()); 337 assertEquals("A",testRoot.getFileObject("a").getName()); 338 BaseFileObj bobj = (BaseFileObj)testRoot.getFileObject("a"); 339 NamingFactory.checkCaseSensitivity(bobj.getFileName(),testa); 340 assertEquals("a",testRoot.getFileObject("a").getName()); 341 assertEquals("a",testRoot.getFileObject("A").getName()); 342 } 343 344 private class TestListener extends FileChangeAdapter { 345 private List fileObjects; 346 TestListener(List fileObjects) { 347 this.fileObjects = fileObjects; 348 } 349 public void fileFolderCreated(FileEvent fe) { 350 assertTrue(fileObjects.remove(fe.getFile())); 351 } 352 353 public void fileDeleted(FileEvent fe) { 354 assertTrue(fileObjects.remove(fe.getFile())); 355 } 356 357 public void fileDataCreated(FileEvent fe) { 358 assertTrue(fileObjects.remove(fe.getFile())); 359 } 360 } 361 362 public void testSimulatesRefactoringRename() throws Exception { 363 if (!Utilities.isWindows()) return; 364 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(getWorkDir()); 365 assertNotNull(fs); 366 final FileObject root = fs.findFileObject(getWorkDir()); 367 assertNotNull(root); 368 FileObject main = root.createData("Main.java"); 369 FileUtil.createData(root,"subpackage/newclass.java"); 370 final List fileObjects = new ArrayList () { 371 public boolean add(Object o) { 372 assertNotNull(o); 373 return super.add(o); 374 } 375 376 }; 377 final TestListener tl = new TestListener(fileObjects); 378 fs.addFileChangeListener(tl); 379 try { 380 fs.runAtomicAction(new FileSystem.AtomicAction(){ 381 public void run() throws IOException { 382 FileObject subpackage = root.getFileObject("subpackage"); 383 FileObject newclass = subpackage.getFileObject("newclass.java"); 384 FileObject subpackage1 = root.createFolder("subpackage1"); 385 fileObjects.add(subpackage1); 386 FileObject newclass1 = subpackage1.createData("newclass.java"); 387 fileObjects.add(newclass1); 388 subpackage.delete(); 389 fileObjects.add(subpackage); 390 fileObjects.add(newclass); 391 } 392 }); 393 } finally { 394 fs.removeFileChangeListener(tl); 395 } 396 assertTrue(fileObjects.toString(),fileObjects.isEmpty()); 397 assertNotNull(root.getFileObject("Main.java")); 398 assertNotNull(root.getFileObject("subpackage1")); 399 assertNotNull(root.getFileObject("subpackage1/newclass.java")); 400 assertNull(root.getFileObject("subpackage")); 401 fs.addFileChangeListener(tl); 402 try { 403 fs.runAtomicAction(new FileSystem.AtomicAction(){ 404 public void run() throws IOException { 405 FileObject subpackage1 = root.getFileObject("subpackage1"); 406 FileObject newclass = root.getFileObject("subpackage1/newclass.java"); 407 FileObject Subpackage = root.createFolder("Subpackage"); 408 fileObjects.add(Subpackage); 409 FileObject newclass1 = Subpackage.createData("newclass.java"); 410 fileObjects.add(newclass1); 411 subpackage1.delete(); 412 fileObjects.add(subpackage1); 413 fileObjects.add(newclass); 414 } 415 }); 416 } finally { 417 fs.removeFileChangeListener(tl); 418 } 419 assertTrue(fileObjects.toString(), fileObjects.isEmpty()); 420 assertNotNull(root.getFileObject("Main.java")); 421 assertNotNull(root.getFileObject("Subpackage/newclass.java")); 422 assertNull(root.getFileObject("subpackage1")); 423 } 424 425 public void testRename() throws Exception { 426 File f = testFile; 427 FileSystem fs = FileBasedFileSystem.getInstance(f); 428 assertNotNull(fs); 429 430 final List l = new ArrayList (); 431 FileChangeListener fcl = new FileChangeAdapter () { 432 public void fileRenamed(FileRenameEvent fe) { 433 FileObject fold = (FileObject)fe.getFile(); 434 assertTrue(fold.getChildren().length > 0); 435 l.add(fe); 436 } 437 }; 438 439 FileObject fo = fs.findResource(f.getAbsolutePath().replace('\\','/')); 440 assertNotNull(fo); 441 FileObject folder =fo.createFolder("testRename"); 442 assertNotNull(folder); 443 444 FileObject file =folder.createData("test.txt"); 445 assertNotNull(file); 446 folder.addFileChangeListener(fcl); 447 assertTrue(folder.getChildren().length > 0); 448 FileLock lock = folder.lock(); 449 try { 450 folder.rename(lock,"renfolder",""); 451 assertTrue(folder.getChildren().length > 0); 452 assertTrue(!l.isEmpty()); 453 454 l.clear(); 455 456 folder.rename(lock,"testRename",""); 457 assertTrue(folder.getChildren().length > 0); 458 assertTrue(!l.isEmpty()); 459 460 } finally { 461 lock.releaseLock(); 462 } 463 464 465 } 466 467 public void testRename2 () throws Exception { 468 File test = new File (getWorkDir(), "testrename.txt"); 469 if (!test.exists()) { 470 assertTrue(test.createNewFile()); 471 } 472 473 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(test); 474 assertNotNull(fs); 475 476 FileObject testFo = fs.findFileObject(test); 477 assertNotNull (testFo); 478 479 FileLock lock = testFo.lock(); 480 assertNotNull (lock); 481 try { 482 testFo.rename(lock, "TESTRENAME", "TXT"); 483 } finally { 484 lock.releaseLock(); 485 } 486 } 487 488 491 public void testGetChildren() throws Exception { 492 File f = testFile; 493 FileSystem fs = FileBasedFileSystem.getInstance(f); 494 assertNotNull(fs); 495 assertNotNull(f.getAbsolutePath(),fs.findResource(f.getAbsolutePath())); 496 497 while (f != null) { 498 if (new FileInfo (f).isWindowsFloppy()) continue; 499 FileObject fo0 = fs.findResource(f.getAbsolutePath()); 500 assertNotNull(f.getAbsolutePath(),fo0); 501 502 FileObject fo = fs.getRoot().getFileObject(f.getAbsolutePath().replace('\\','/')); 503 assertNotNull(f.getAbsolutePath(),fo); 504 if (fo0 != fo) { 505 fs.getRoot().getFileObject(f.getAbsolutePath().replace('\\','/')); 506 } 507 assertSame(fo.toString(), fo0, fo); 508 509 if (f.getParentFile() != null) { 510 FileObject parent = fo.getParent(); 511 assertNotNull(parent); 512 String nameExt = fo.getNameExt(); 513 FileObject fo2 = parent.getFileObject(nameExt); 514 assertNotNull(fo2); 515 assertSame(fo, fo2); 516 FileObject[] fos = parent.getChildren(); 517 List list = Arrays.asList(fos); 518 assertTrue((fo.toString()+ " " + System.identityHashCode(fo)),list.contains(fo)); 519 520 521 WeakReference ref = new WeakReference (fo); 522 String msg = fo.toString(); 523 fo = null; fo0 = null; fo2 = null; parent = null;fos = null; list = null; 524 assertGC(msg, ref); 525 } else { 526 WeakReference ref = new WeakReference (fo); 528 String msg = fo.toString(); 529 fo = null; fo0 = null; 530 assertNotNull(msg, ref.get()); 531 } 532 533 f = f.getParentFile(); 534 } 535 } 536 537 public void testGetChildrenStackOwerflow() throws Exception { 538 File f = testFile; 539 assertTrue(f.exists()); 540 assertTrue(f.isDirectory()); 541 FileSystem fs = FileBasedFileSystem.getInstance(f); 542 assertNotNull(fs); 543 final FileObject fo = fs.findResource(f.getAbsolutePath()); 544 assertNotNull(f.getAbsolutePath(),fo); 545 assertTrue(fo.isFolder()); 546 assertEquals(0,fo.getChildren().length); 547 assertTrue(new File (f,"child1").createNewFile()); 548 assertTrue(new File (f,"child2").createNewFile()); 549 final File child3 = new File (f,"child3"); 550 assertTrue(child3.createNewFile()); 551 final List keepThem = new ArrayList (); 552 fo.addFileChangeListener(new FileChangeAdapter(){ 553 public void fileDeleted(FileEvent fe) { 554 for (Iterator it = keepThem.iterator(); it.hasNext();) { 555 FileObject fodel = (FileObject) it.next(); 556 FileObject[] all = fo.getChildren(); 557 for (int i = 0; i < all.length; i++) { 558 all[i].refresh(); 559 } 560 561 } 562 } 563 564 public void fileDataCreated(FileEvent fe) { 565 FileObject ffoo = fe.getFile(); 566 keepThem.add(ffoo); 567 ((BaseFileObj)ffoo).getFileName().getFile().delete(); 568 ffoo.refresh(); 569 } 570 } ); 571 572 fo.refresh(); 573 assertEquals(0,fo.getChildren().length); 574 } 575 576 579 public void testIsData() { 580 File f = testFile; 581 FileSystem fs = FileBasedFileSystem.getInstance(f); 582 assertNotNull(fs); 583 584 while (f != null) { 585 FileObject fo = fs.findResource(f.getAbsolutePath()); 586 assertNotNull(f.getAbsolutePath(),fo); 587 assertEquals(f.isFile(), fo.isData()); 588 if (fo.isData ()) { 589 assertTrue(fo instanceof FileObj); 590 } 591 592 f = f.getParentFile(); 593 } 594 } 595 596 599 public void testIsFolder() { 600 File f = testFile; 601 FileSystem fs = FileBasedFileSystem.getInstance(f); 602 assertNotNull(fs); 603 604 while (f != null) { 605 FileObject fo = fs.findResource(f.getAbsolutePath()); 606 assertNotNull(f.getAbsolutePath(),fo); 607 if (fo.isFolder() && !fo.isRoot()) { 608 assertTrue(fo instanceof FolderObj); 609 } 610 611 f = f.getParentFile(); 612 } 613 } 614 615 618 public void testIsRoot() { 619 File f = testFile; 620 FileSystem fs = FileBasedFileSystem.getInstance(f); 621 assertNotNull(fs); 622 FileObject fo = null; 623 while (f != null) { 624 fo = fs.findResource(f.getAbsolutePath()); 625 assertNotNull(f.getAbsolutePath(),fo); 626 f = f.getParentFile(); 627 } 628 assertNotNull(fo.toString(), fo); 629 FileObject root = fo.getParent (); 630 631 assertNotNull(root.toString(), root); 632 assertTrue(root.isRoot()); 633 assertTrue(root instanceof RootObj); 634 assertSame(root, fs.getRoot()); 635 assertSame(fo, root.getFileObject(fo.getNameExt())); 636 } 637 638 641 public void testGetFileObject() { 642 File f = testFile; 643 FileSystem fs = FileBasedFileSystem.getInstance(f); 644 assertNotNull(fs); 645 646 while (f != null) { 647 FileObject fo = fs.findResource(f.getAbsolutePath()); 648 assertNotNull(f.getAbsolutePath(),fo); 649 FileObject parent = fo.getParent(); 650 while (parent != null && parent != fo) { 651 assertNotNull(parent); 652 String relativePath = FileUtil.getRelativePath(parent, fo); 653 assertNotNull(relativePath); 654 FileObject fo2 = parent.getFileObject(relativePath); 655 assertNotNull((relativePath + " not found in " + parent.toString()), fo2); 656 assertSame (fo, fo2); 657 parent = parent.getParent(); 658 } 659 660 assertNotNull(fs.getRoot().getFileObject(TestUtils.getFileObjectPath(((BaseFileObj)fo).getFileName().getFile ()))); 661 f = f.getParentFile(); 662 } 663 } 664 665 666 669 public void testCreateFolder() throws Exception { 670 File f = testFile; 671 FileSystem fs = FileBasedFileSystem.getInstance(f); 672 assertNotNull(fs); 673 674 FileObject fo = fs.findResource(TestUtils.getFileObjectPath(testFile)); 675 676 File f2 = new File (testFile, "newfoldercreated"); 677 FileObject nfo = fo.createFolder (f2.getName()); 678 assertNotNull(nfo); 679 assertTrue(nfo.isFolder()); 680 File nfile = ((BaseFileObj)nfo).getFileName().getFile (); 681 assertSame(nfo, fs.findResource(TestUtils.getFileObjectPath(nfile))); 682 assertSame(fo, nfo.getParent()); 683 684 try { 685 FileObject nfo2 = fo.createFolder (f2.getName()); 686 fail (); 687 } catch (IOException iox) { 688 689 } 690 691 } 692 693 696 public void testCreateData() throws Exception { 697 File f = testFile; 698 FileSystem fs = FileBasedFileSystem.getInstance(f); 699 assertNotNull(fs); 700 701 final FileObject fo = fs.findResource(TestUtils.getFileObjectPath(testFile)); 702 703 File f2 = new File (testFile, "newdatacreated.txt"); 704 final FileObject nfo = fo.createData (f2.getName()); 705 assertNotNull(nfo); 706 assertTrue(nfo.isData()); 707 File nfile = ((BaseFileObj)nfo).getFileName().getFile (); 708 assertEquals(nfo.getClass(), fs.findResource(TestUtils.getFileObjectPath(nfile)).getClass()); 709 assertSame(nfo, fs.findResource(TestUtils.getFileObjectPath(nfile))); 710 713 System.gc();System.gc();System.gc();System.gc();System.gc();System.gc(); 714 FileObject pp = nfo.getParent(); 715 assertEquals(((BaseFileObj)pp).getFileName().getFile(), ((BaseFileObj)fo).getFileName().getFile()); 716 assertEquals(((BaseFileObj)pp).getFileName().getId(), ((BaseFileObj)fo).getFileName().getId()); 717 718 assertSame(((BaseFileObj)fo).getFileName().getId() + " | " + ((BaseFileObj)nfo.getParent()).getFileName().getId(), fo, pp); 719 720 try { 721 FileObject nfo2 = fo.createData (f2.getName()); 722 fail (); 723 } catch (IOException iox) { 724 725 } 726 } 727 728 729 732 public void testDelete() throws IOException { 733 File f = testFile; 734 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 735 736 FileObject testFo = fs.findFileObject(testFile); 737 assertNotNull(testFo); 738 739 final List l = new ArrayList (); 740 FileChangeListener fcl = new FileChangeAdapter () { 741 public void fileDeleted(FileEvent fe) { 742 l.add(fe); 743 } 744 }; 745 FileObject fo = FileUtil.createData(testFo, "delete/the/whole/structure/in/depth/todelete.txt"); 746 fo.addFileChangeListener(fcl); 747 748 FileObject toDelete = testFo.getFileObject("delete"); 749 assertNotNull(toDelete); 750 toDelete.addFileChangeListener(fcl); 751 752 FileObject toGC = testFo.getFileObject("delete/the/whole/structure"); 753 assertNotNull(toGC); 754 Reference toGCRef = new WeakReference (toGC); 755 toGC.addFileChangeListener(fcl); 756 toGC = null; 757 758 assertGC("", toGCRef); 759 toDelete.delete(); 760 assertEquals(0,testFo.getChildren().length); 761 toDelete = testFo.getFileObject("delete"); 762 assertNull(toDelete); 763 assertEquals(2, l.size()); 764 } 765 766 public void testDelete2() throws IOException { 767 File f = testFile; 768 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 769 770 FileObject testFo = fs.findFileObject(testFile); 771 assertNotNull(testFo); 772 assertEquals(0,testFo.getChildren().length); 773 774 FileObject newFolder = testFo.createFolder("testDelete2"); 775 assertNotNull(newFolder); 776 assertEquals(1,testFo.getChildren().length); 777 778 newFolder.delete(); 779 assertFalse(newFolder.isValid()); 780 assertEquals(0,testFo.getChildren().length); 781 assertNull(testFo.getFileObject(newFolder.getNameExt())); 782 } 783 784 public void testExternalDelete2() throws IOException { 785 File f = new File (testFile, "testDelete2/testForExternalRefresh/"); 786 assert !f.exists() : f.getAbsolutePath(); 787 assert f.mkdirs() : f.getAbsolutePath(); 788 assert f.exists() : f.getAbsolutePath(); 789 790 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 791 792 FileObject testFo = fs.findFileObject(f); 793 assertNotNull(testFo); 794 assertTrue(testFo.isFolder()); 795 796 final List l = new ArrayList (); 797 FileChangeListener fcl = new FileChangeAdapter () { 798 public void fileDeleted(FileEvent fe) { 799 l.add(fe); 800 fe.getFile().refresh(); 801 } 802 public void fileChanged(FileEvent fe) { 803 fail(); 804 } 805 806 }; 807 808 testFo.addFileChangeListener(fcl); 809 assertEquals(0, l.size()); 810 811 f.delete(); 812 testFo.refresh(); 813 assertEquals(1, l.size()); 814 } 815 816 public void testExternalDelete2_1() throws IOException { 817 File f = new File (testFile, "testDelete2/testForExternalRefresh/"); 818 assert !f.exists() : f.getAbsolutePath(); 819 assert f.mkdirs() : f.getAbsolutePath(); 820 assert f.exists() : f.getAbsolutePath(); 821 822 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 823 824 final FileObject testFo = fs.findFileObject(f); 825 assertNotNull(testFo); 826 assertTrue(testFo.isFolder()); 827 828 final List l = new ArrayList (); 829 FileChangeListener fcl = new FileChangeAdapter () { 830 public void fileDeleted(FileEvent fe) { 831 if (fe.getFile().equals(testFo)) { 832 l.add(fe); 833 } 834 fe.getFile().refresh(); 835 } 836 public void fileChanged(FileEvent fe) { 837 fail(); 838 } 839 840 }; 841 842 testFo.getFileSystem().addFileChangeListener(fcl); 843 assertEquals(0, l.size()); 844 845 f.delete(); 846 testFo.getFileSystem().refresh(true); 847 assertEquals(1, l.size()); 848 testFo.getFileSystem().removeFileChangeListener(fcl); 849 } 850 851 public void testExternalDelete2_2() throws IOException { 852 File f = new File (testFile, "testDelete2/testForExternalRefresh/"); 853 assert !f.exists() : f.getAbsolutePath(); 854 assert f.mkdirs() : f.getAbsolutePath(); 855 assert f.exists() : f.getAbsolutePath(); 856 857 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 858 859 FileObject testFo = fs.findFileObject(f); 860 assertNotNull(testFo); 861 assertTrue(testFo.isFolder()); 862 863 final List l = new ArrayList (); 864 FileChangeListener fcl = new FileChangeAdapter () { 865 public void fileDeleted(FileEvent fe) { 866 l.add(fe); 867 fe.getFile().refresh(); 868 } 869 public void fileChanged(FileEvent fe) { 870 fail(); 871 } 872 }; 873 874 testFo.addFileChangeListener(fcl); 875 assertEquals(0, l.size()); 876 877 f.delete(); 878 testFo.getFileSystem().refresh(true); 879 assertEquals(1, l.size()); 880 } 881 882 public void testExternalDelete3() throws IOException { 883 File f = new File (testFile, "testDelete2/testForExternalRefresh3/"); 884 assert !f.exists() : f.getAbsolutePath(); 885 assert f.mkdirs() : f.getAbsolutePath(); 886 assert f.exists() : f.getAbsolutePath(); 887 f = new File (f, "f.txt"); 888 assert !f.exists() : f.getAbsolutePath(); 889 assert f.createNewFile() : f.getAbsolutePath(); 890 assert f.exists() : f.getAbsolutePath(); 891 892 893 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 894 895 FileObject testFo = fs.findFileObject(f); 896 assertNotNull(testFo); 897 assertTrue(testFo.isData()); 898 899 final List l = new ArrayList (); 900 FileChangeListener fcl = new FileChangeAdapter () { 901 public void fileDeleted(FileEvent fe) { 902 l.add(fe); 903 fe.getFile().refresh(); 904 } 905 906 public void fileChanged(FileEvent fe) { 907 fail(); 908 } 909 910 }; 911 912 testFo.addFileChangeListener(fcl); 913 assertEquals(0, l.size()); 914 915 f.delete(); 916 917 testFo.refresh(); 920 921 assertEquals(1, l.size()); 922 } 923 924 public void testExternalDelete3_1() throws IOException { 925 File f = new File (testFile, "testDelete2/testForExternalRefresh3/"); 926 assert !f.exists() : f.getAbsolutePath(); 927 assert f.mkdirs() : f.getAbsolutePath(); 928 assert f.exists() : f.getAbsolutePath(); 929 f = new File (f, "f.txt"); 930 assert !f.exists() : f.getAbsolutePath(); 931 assert f.createNewFile() : f.getAbsolutePath(); 932 assert f.exists() : f.getAbsolutePath(); 933 934 935 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 936 937 FileObject testFo = fs.findFileObject(f); 938 assertNotNull(testFo); 939 assertTrue(testFo.isData()); 940 941 final List l = new ArrayList (); 942 FileChangeListener fcl = new FileChangeAdapter () { 943 public void fileDeleted(FileEvent fe) { 944 l.add(fe); 945 fe.getFile().refresh(); 946 } 947 public void fileChanged(FileEvent fe) { 948 fail(); 949 } 950 951 }; 952 953 testFo.getFileSystem().addFileChangeListener(fcl); 954 assertEquals(0, l.size()); 955 956 f.delete(); 957 testFo.getFileSystem().refresh(true); 958 assertEquals(1, l.size()); 959 testFo.getFileSystem().removeFileChangeListener(fcl); 960 961 } 962 963 public void testExternalDelete3_2() throws IOException { 964 File f = new File (testFile, "testDelete2/testForExternalRefresh3/"); 965 assert !f.exists() : f.getAbsolutePath(); 966 assert f.mkdirs() : f.getAbsolutePath(); 967 assert f.exists() : f.getAbsolutePath(); 968 f = new File (f, "f.txt"); 969 assert !f.exists() : f.getAbsolutePath(); 970 assert f.createNewFile() : f.getAbsolutePath(); 971 assert f.exists() : f.getAbsolutePath(); 972 973 974 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 975 976 FileObject testFo = fs.findFileObject(f); 977 assertNotNull(testFo); 978 assertTrue(testFo.isData()); 979 980 final List l = new ArrayList (); 981 FileChangeListener fcl = new FileChangeAdapter () { 982 public void fileDeleted(FileEvent fe) { 983 l.add(fe); 984 fe.getFile().refresh(); 985 } 986 public void fileChanged(FileEvent fe) { 987 fail(); 988 } 989 990 }; 991 992 testFo.addFileChangeListener(fcl); 993 assertEquals(0, l.size()); 994 995 f.delete(); 996 testFo.getFileSystem().refresh(true); 997 assertEquals(1, l.size()); 998 } 999 1000 1001 public void testExternalDelete4() throws IOException { 1002 File f = new File (testFile, "testDelete2/testForExternalRefresh3/"); 1003 assert !f.exists() : f.getAbsolutePath(); 1004 assert f.mkdirs() : f.getAbsolutePath(); 1005 assert f.exists() : f.getAbsolutePath(); 1006 1007 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 1008 FileObject testFolder = fs.findFileObject(f); 1009 assertNotNull(testFolder); 1010 assertTrue(testFolder.isFolder()); 1011 1012 f = new File (f, "f.txt"); 1013 assert !f.exists() : f.getAbsolutePath(); 1014 assert f.createNewFile() : f.getAbsolutePath(); 1015 assert f.exists() : f.getAbsolutePath(); 1016 1017 FileObject testFile = testFolder.getFileObject(f.getName()); 1018 assertNotNull(testFile); 1019 assertTrue(testFile.isData()); 1020 1021 1022 final List l = new ArrayList (); 1023 FileChangeListener fcl = new FileChangeAdapter () { 1024 public void fileDeleted(FileEvent fe) { 1025 l.add(fe); 1026 fe.getFile().refresh(); 1027 } 1028 public void fileChanged(FileEvent fe) { 1029 fail(); 1030 } 1031 1032 }; 1033 1034 testFolder.addFileChangeListener(fcl); 1035 assertEquals(0, l.size()); 1036 1037 assertTrue(f.delete()); 1038 testFolder.refresh(); 1039 assertEquals(1, l.size()); 1040 } 1041 1042 public void testExternalDelete4_1() throws IOException { 1043 File f = new File (testFile, "testDelete2/testForExternalRefresh3/"); 1044 assert !f.exists() : f.getAbsolutePath(); 1045 assert f.mkdirs() : f.getAbsolutePath(); 1046 assert f.exists() : f.getAbsolutePath(); 1047 1048 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 1049 FileObject testFolder = fs.findFileObject(f); 1050 assertNotNull(testFolder); 1051 assertTrue(testFolder.isFolder()); 1052 1053 f = new File (f, "f.txt"); 1054 assert !f.exists() : f.getAbsolutePath(); 1055 assert f.createNewFile() : f.getAbsolutePath(); 1056 assert f.exists() : f.getAbsolutePath(); 1057 1058 FileObject testFile = testFolder.getFileObject(f.getName()); 1059 assertNotNull(testFile); 1060 assertTrue(testFile.isData()); 1061 1062 1063 final List l = new ArrayList (); 1064 FileChangeListener fcl = new FileChangeAdapter () { 1065 public void fileDeleted(FileEvent fe) { 1066 l.add(fe); 1067 fe.getFile().refresh(); 1068 } 1069 public void fileChanged(FileEvent fe) { 1070 fail(); 1071 } 1072 1073 }; 1074 1075 testFolder.getFileSystem().addFileChangeListener(fcl); 1076 assertEquals(0, l.size()); 1077 1078 f.delete(); 1079 testFolder.getFileSystem().refresh(true); 1080 testFolder.getFileSystem().removeFileChangeListener(fcl); 1081 assertEquals(1, l.size()); 1082 1083 } 1084 1085 public void testExternalDelete4_1_1() throws IOException { 1086 File f = new File (testFile, "testDelete2/testForExternalRefresh3/"); 1087 assert !f.exists() : f.getAbsolutePath(); 1088 assert f.mkdirs() : f.getAbsolutePath(); 1089 assert f.exists() : f.getAbsolutePath(); 1090 1091 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 1092 FileObject testFolder = fs.findFileObject(f); 1093 assertNotNull(testFolder); 1094 assertTrue(testFolder.isFolder()); 1095 1096 f = new File (f, "f.txt"); 1097 assert !f.exists() : f.getAbsolutePath(); 1098 assert f.createNewFile() : f.getAbsolutePath(); 1099 assert f.exists() : f.getAbsolutePath(); 1100 1101 FileObject testFile = fs.findFileObject(f); assertNotNull(testFile); 1103 assertTrue(testFile.isData()); 1104 1105 1106 final List l = new ArrayList (); 1107 FileChangeListener fcl = new FileChangeAdapter () { 1108 public void fileDeleted(FileEvent fe) { 1109 l.add(fe); 1110 fe.getFile().refresh(); 1111 } 1112 public void fileChanged(FileEvent fe) { 1113 fail(); 1114 } 1115 1116 }; 1117 1118 testFolder.getFileSystem().addFileChangeListener(fcl); 1119 assertEquals(0, l.size()); 1120 1121 f.delete(); 1122 testFolder.getFileSystem().refresh(true); 1123 testFolder.getFileSystem().removeFileChangeListener(fcl); 1124 assertEquals(1, l.size()); 1125 1126 } 1127 1128 public void testExternalDelete4_2() throws IOException { 1129 File f = new File (testFile, "testDelete2/testForExternalRefresh3/"); 1130 assert !f.exists() : f.getAbsolutePath(); 1131 assert f.mkdirs() : f.getAbsolutePath(); 1132 assert f.exists() : f.getAbsolutePath(); 1133 1134 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(f); 1135 FileObject testFolder = fs.findFileObject(f); 1136 assertNotNull(testFolder); 1137 assertTrue(testFolder.isFolder()); 1138 1139 f = new File (f, "f.txt"); 1140 assert !f.exists() : f.getAbsolutePath(); 1141 assert f.createNewFile() : f.getAbsolutePath(); 1142 assert f.exists() : f.getAbsolutePath(); 1143 1144 FileObject testFile = testFolder.getFileObject(f.getName()); 1145 assertNotNull(testFile); 1146 assertTrue(testFile.isData()); 1147 1148 1149 final List l = new ArrayList (); 1150 FileChangeListener fcl = new FileChangeAdapter () { 1151 public void fileDeleted(FileEvent fe) { 1152 l.add(fe); 1153 fe.getFile().refresh(); 1154 } 1155 public void fileChanged(FileEvent fe) { 1156 fail(); 1157 } 1158 1159 }; 1160 1161 testFolder.getFileSystem().addFileChangeListener(fcl); 1162 assertEquals(0, l.size()); 1163 1164 f.delete(); 1165 testFolder.refresh(true); 1166 assertEquals(1, l.size()); 1167 testFolder.getFileSystem().removeFileChangeListener(fcl); 1168 1169 } 1170 1171 1172 1175 public void testGetInputStream() { 1176 File f = testFile; 1177 FileSystem fs = FileBasedFileSystem.getInstance(f); 1178 1179 FileObject root = fs.getRoot(); 1180 assertNotNull(root); 1181 1182 Enumeration en = root.getFolders(true); 1183 for (int i = 0; i < 10 && en.hasMoreElements(); i++) { 1184 FileObject fo = (FileObject) en.nextElement(); 1185 assertTrue(fo.isFolder()); 1186 assertFalse(fo.isData()); 1187 try { 1188 fo.getInputStream(); 1189 fail (); 1190 } catch (FileNotFoundException e) { 1191 1192 } 1193 1194 } 1195 } 1196 1197 1235 1236 public void testRefresh43231() throws Exception { 1238 File thisTest = new File (getWorkDir(),getName()); 1239 thisTest.createNewFile(); 1240 FileObject testf = FileBasedFileSystem.getFileObject(thisTest); 1241 final List l = new ArrayList (); 1242 testf.addFileChangeListener(new FileChangeAdapter(){ 1243 public void fileChanged(FileEvent fe) { 1244 if (l.isEmpty()) { 1245 fail(); 1246 } 1247 l.clear(); 1248 } 1249 }); 1250 thisTest.setLastModified(1000000); 1254 testf.refresh(); 1255 assertTrue(l.isEmpty()); 1256 1257 l.add("not empty"); 1261 thisTest.setLastModified(10000); 1262 testf.refresh(); 1263 assertTrue(l.isEmpty()); 1264 } 1265 1266 public void testRefresh69744() throws Exception { 1267 File thisTest = new File (getWorkDir(),"thisTest"); 1268 thisTest.createNewFile(); 1269 FileObject testf = FileBasedFileSystem.getFileObject(thisTest); 1270 assertNotNull(testf); 1271 assertGC("",new WeakReference (testf.getParent())); 1272 modifyFileObject(testf, "abc"); 1273 FileSystem fs = testf.getFileSystem(); 1274 final List l = new ArrayList (); 1275 FileChangeListener fcl = new FileChangeAdapter() { 1276 public void fileChanged(FileEvent fe) { 1277 l.add(fe); 1278 } 1279 }; 1280 Thread.sleep(1500); 1281 fs.addFileChangeListener(fcl); 1282 try { 1283 modifyFileObject(testf, "def"); 1284 assertFalse(l.isEmpty()); 1285 } finally { 1286 fs.removeFileChangeListener(fcl); 1287 } 1288 } 1289 1290 private void modifyFileObject(final FileObject testf, String content) throws IOException { 1291 FileLock lock = null; 1292 OutputStream os = null; 1293 try { 1294 lock = testf.lock(); 1295 os = testf.getOutputStream(lock); 1296 os.write(content.getBytes()); 1297 } finally { 1298 if (os != null) os.close(); 1299 if (lock != null) lock.releaseLock(); 1300 } 1301 } 1302 1303 public void testFileTypeChanged() throws Exception { 1304 String newFileName = "test"; 1305 File f = new File (testFile, "testFileTypeNotRemembered/"); 1306 assert !f.exists() : f.getAbsolutePath(); 1307 assert f.mkdirs() : f.getAbsolutePath(); 1308 assert f.exists() : f.getAbsolutePath(); 1309 1310 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(testFile); 1311 FileObject parent = fs.findFileObject(testFile); 1312 1313 assertNotNull(parent); 1314 assertTrue(parent.isFolder()); 1315 FileObject fo = parent.getFileObject("testFileTypeNotRemembered"); 1316 assertTrue(fo.isFolder()); 1317 1318 fo.delete(); 1319 FileObject fo2 = parent.createFolder("testFileTypeNotRemembered"); 1320 assertNotNull(fo); 1321 assertTrue(fo != fo2); 1322 1323 fo2 = fs.findFileObject(f); 1324 assertNotNull(fo); 1325 assertTrue(fo != fo2); 1326 } 1327 1328 public void testRefresh2 () throws Exception { 1329 String childName = "refreshtest.txt"; 1330 FileSystem fs = FileBasedFileSystem.getInstance(testFile); 1331 final File file = new File (testFile, childName); 1332 FileObject parent = fs.findResource(testFile.getAbsolutePath().replace('\\', '/')); 1333 assertNotNull(parent); 1334 1335 file.createNewFile(); 1336 parent.getFileObject(childName); 1337 parent.getChildren(); 1338 fs.refresh(true); 1339 1340 final ArrayList deleted = new ArrayList (); 1341 final ArrayList created = new ArrayList (); 1342 1343 FileChangeListener fcl = new FileChangeAdapter () { 1344 public void fileDeleted(FileEvent fe) { 1345 BaseFileObj fo = (BaseFileObj)fe.getFile(); 1346 if (file.equals(fo.getFileName().getFile())) { 1347 String p = fo.toString(); 1348 deleted.add(fo); 1349 } 1350 } 1351 1352 public void fileDataCreated(FileEvent fe) { 1353 BaseFileObj fo = (BaseFileObj)fe.getFile(); 1354 if (file.equals(fo.getFileName().getFile())) { 1355 String p = fo.toString(); 1356 created.add(fo); 1357 } 1358 } 1359 1360 }; 1361 fs.addFileChangeListener(fcl); 1362 int stepsCount = 10; 1363 for (int i = 0; i < stepsCount; i++) { 1364 assertTrue(file.delete()); 1365 fs.refresh(true); 1366 1367 assertTrue(file.createNewFile()); 1368 fs.refresh(true); 1369 } 1370 1371 fs.removeFileChangeListener(fcl); 1372 assertEquals(stepsCount,deleted.size()); 1373 assertEquals(stepsCount,created.size()); 1374 1375 } 1376 1377 public void testRefresh3 () throws Exception { 1378 String childName = "refreshtest2.txt"; 1379 FileBasedFileSystem fs = FileBasedFileSystem.getInstance(testFile); 1380 final File file = new File (testFile, childName); 1381 FileObject parent = fs.findResource(testFile.getAbsolutePath().replace('\\', '/')); 1382 assertNotNull(parent); 1383 1384 1385 final ArrayList events = new ArrayList (); 1386 1387 final ArrayList deletedIncrement = new ArrayList (); 1388 final ArrayList createdIncrement = new ArrayList (); 1389 1390 final ArrayList hardRef = new ArrayList (); 1391 final FileChangeListener fcl = new FileChangeAdapter () { 1392 public void fileDeleted(FileEvent fe) { 1393 BaseFileObj fo = (BaseFileObj)fe.getFile(); 1394 if (file.equals(fo.getFileName().getFile())) 1395 { 1396 String p = fo.toString(); 1397 assertEquals(0, events.size()); 1398 assertTrue(!fo.isValid()); 1399 events.add("fo"); 1400 deletedIncrement.add("fo"); 1401 fo.removeFileChangeListener(this); 1402 fo.getParent().addFileChangeListener(this); 1403 hardRef.clear(); 1404 hardRef.add(fo.getParent()); 1405 fo.getParent().getChildren (); 1406 } 1407 } 1408 1409 1410 public void fileDataCreated(FileEvent fe) { 1411 BaseFileObj fo = (BaseFileObj)fe.getFile(); 1412 if (file.equals(fo.getFileName().getFile())) 1413 { 1414 String p = fo.toString(); 1415 assertEquals(1,events.size()); 1416 assertTrue(fo.isValid()); 1417 assertTrue(events.remove("fo")); 1418 createdIncrement.add("fo"); 1419 fo.getParent().removeFileChangeListener(this); 1420 fo.addFileChangeListener(this); 1421 hardRef.clear(); 1422 hardRef.add(fo); 1423 1424 } 1425 } 1426 1427 1428 }; 1429 fs.refresh(true); 1430 file.createNewFile(); 1431 hardRef.add(parent.getFileObject(childName)); 1432 parent.getFileObject(childName).addFileChangeListener(fcl); 1433 parent = null; 1434 int stepsCount = 10; 1435 Reference ref2 = new WeakReference (fs.findFileObject(file.getParentFile())); 1436 assertGC("", ref2); 1437 1438 for (int i = 0; i < stepsCount; i++) { 1439 assertTrue(file.delete()); 1440 fs.refresh(true); 1441 Reference ref = new WeakReference (fs.findFileObject(file)); 1442 assertGC("", ref); 1443 1444 1445 assertTrue(file.createNewFile()); 1446 fs.refresh(true); 1447 1448 ref = new WeakReference (fs.findFileObject(file.getParentFile())); 1449 assertGC(file.getParentFile().getAbsolutePath(), ref); 1450 } 1451 1452 fs.removeFileChangeListener(fcl); 1453 assertEquals(0,events.size()); 1454 assertEquals(stepsCount,createdIncrement.size()); 1455 assertEquals(stepsCount,deletedIncrement.size()); 1456 1457 } 1458 1459 public void testRefreshDoesNotMultiplyFileObjects_89059 () throws Exception { 1460 FileObject fo = FileBasedFileSystem.getFileObject(testFile); 1461 fo.getChildren(); 1462 FileSystem fs = fo.getFileSystem(); 1463 FileChangeListener fcl = new FileChangeAdapter(); 1464 OutputStream os = null; 1465 fs.addFileChangeListener(fcl); 1466 fo.addFileChangeListener(fcl); 1467 try { 1468 int foInstancesInCache = Statistics.fileObjects(); 1470 fs.refresh(false); 1471 assertTrue(foInstancesInCache >= Statistics.fileObjects()); 1472 1473 File ff = new File (testFile,"a/b/c/d/aa.txt"); FileUtil.createData(ff); 1476 foInstancesInCache = Statistics.fileObjects(); 1477 fs.refresh(false); 1478 assertTrue(foInstancesInCache >= Statistics.fileObjects()); 1479 1480 FileObject ffObject = FileBasedFileSystem.getFileObject(ff); 1482 foInstancesInCache = Statistics.fileObjects(); 1483 os = new java.io.FileOutputStream (ff); 1484 os.write("dsdopsdsd".getBytes()); os.close(); 1486 fs.refresh(false); 1487 assertTrue(foInstancesInCache >= Statistics.fileObjects()); 1488 1489 assertTrue(new File (testFile,"nfile").createNewFile()); fs.refresh(false); 1491 fo.refresh(false); 1492 assertTrue(foInstancesInCache+1 >= Statistics.fileObjects()); 1493 1494 foInstancesInCache = Statistics.fileObjects(); 1495 assertTrue(new File (testFile,"aa/bb/cc").mkdirs()); fs.refresh(false); 1497 fo.refresh(false); 1498 assertTrue(foInstancesInCache+3 >= Statistics.fileObjects()); 1499 } finally { 1500 if (os != null) { 1501 os.close(); 1502 } 1503 fs.removeFileChangeListener(fcl); 1504 fo.removeFileChangeListener(fcl); 1505 } 1506 } 1507 1508 1511 public void testGetOutputStream() { 1512 File f = testFile; 1513 FileSystem fs = FileBasedFileSystem.getInstance(f); 1514 1515 FileObject root = fs.getRoot(); 1516 assertNotNull(root); 1517 1518 Enumeration en = root.getFolders(true); 1519 for (int i = 0; i < 10 && en.hasMoreElements(); i++) { 1520 FileObject fo = (FileObject) en.nextElement(); 1521 assertTrue(fo.isFolder()); 1522 assertFalse(fo.isData()); 1523 try { 1524 fo.getOutputStream(fo.lock()); 1525 fail (); 1526 } catch (IOException e) { 1527 1528 } finally { 1529 1530 } 1531 1532 } 1533 } 1534 1535 public void testReadWrite ( ) throws Exception { 1536 String content = "content of data file"; 1537 File f = testFile; 1538 FileSystem fs = FileBasedFileSystem.getInstance(f); 1539 1540 BaseFileObj fo = (BaseFileObj)fs.findResource(testFile.getAbsolutePath().replace('\\','/')); 1541 assertNotNull(fo); 1542 File dFile = new File (fo.getFileName().getFile(),"newreadwrite.txt"); 1543 BaseFileObj data = (BaseFileObj)fo.createData(dFile.getName()); 1544 1545 FileLock lock = data.lock(); 1546 try { 1547 OutputStream os = data.getOutputStream(lock); 1548 os.write(content.getBytes()); 1549 os.close(); 1550 } finally { 1551 lock.releaseLock(); 1552 } 1553 1554 InputStream is = data.getInputStream(); 1555 byte[] b = new byte [content.length()]; 1556 assertEquals(content.length(), is.read(b)); 1557 assertEquals(new String (b),new String (b), content); 1558 } 1559 1560 public void testIsLightWeightLockRequiredRequired() throws Exception { 1561 File f = getWorkDir(); 1562 FileSystem fs = FileBasedFileSystem.getInstance(f); 1563 assertNotNull(fs); 1564 FolderObj fo = (FolderObj)fs.findResource(f.getAbsolutePath()); 1565 assertNotNull(fo); 1566 1567 assertFalse(fo.isLightWeightLockRequired()); 1568 FileLock fakeLock = (FileLock)fo.getAttribute(FolderObj.LIGHTWEIGHT_LOCK_SET); 1569 assertNotNull(fakeLock); 1570 assertTrue(fo.isLightWeightLockRequired()); 1571 fakeLock.releaseLock(); 1572 assertFalse(fo.isLightWeightLockRequired()); 1573 fakeLock = (FileLock)fo.getAttribute(FolderObj.LIGHTWEIGHT_LOCK_SET); 1574 assertNotNull(fakeLock); assertTrue(fo.isLightWeightLockRequired()); 1575 fakeLock.releaseLock(); 1576 assertFalse(fo.isLightWeightLockRequired()); 1577 } 1578 1579 public void testLightWeigtLock() throws Exception { 1580 File f = new File (getWorkDir(), "testFile.txt"); 1581 if (!f.exists()) { 1582 assertTrue(f.createNewFile()); 1583 } 1584 File lckFile = WriteLockUtils.getAssociatedLockFile(f); 1585 1586 FileSystem fs = FileBasedFileSystem.getInstance(f); 1587 assertNotNull(fs); 1588 FileObject foChild = fs.findResource(f.getAbsolutePath()); 1589 assertNotNull(foChild); 1590 1591 FileObject folder = foChild.getParent(); 1592 assertNotNull(folder); 1593 1594 FileLock lock = foChild.lock(); 1595 assertTrue(lckFile.exists()); 1596 lock.releaseLock(); 1597 1598 FileLock fakeLock = (FileLock)folder.getAttribute(FolderObj.LIGHTWEIGHT_LOCK_SET); 1599 assertNotNull(fakeLock); 1600 lock = foChild.lock(); 1601 assertFalse(lckFile.exists()); 1602 lock.releaseLock(); 1603 1604 fakeLock.releaseLock(); 1605 lock = foChild.lock(); 1606 assertTrue(lckFile.exists()); 1607 lock.releaseLock(); 1608 } 1609 1610 1611 public File getWorkDir() throws IOException { 1612 return super.getWorkDir(); 1613 } 1614} 1615 | Popular Tags |