1 19 20 package org.netbeans.modules.masterfs; 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.lang.ref.WeakReference ; 24 import java.util.*; 25 import org.netbeans.modules.masterfs.filebasedfs.FileBasedFileSystem; 26 import org.openide.filesystems.*; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.zip.ZipEntry ; 30 import java.util.jar.JarOutputStream ; 31 import java.io.File ; 32 import java.io.FileOutputStream ; 33 import org.openide.util.RequestProcessor; 34 35 import org.openide.util.io.NbMarshalledObject; 36 import org.openide.util.Utilities; 37 38 import javax.swing.filechooser.FileSystemView ; 39 import org.netbeans.modules.masterfs.filebasedfs.fileobjects.WriteLockUtils; 40 import org.netbeans.modules.masterfs.providers.ProvidedExtensionsTest; 41 42 public class MasterFileObjectTestHid extends TestBaseHid{ 43 private FileObject root; 44 45 public MasterFileObjectTestHid(String name) { 46 super(name); 47 } 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 root = testedFS.findResource(getResourcePrefix()); 52 } 53 54 protected String [] getResources(String testName) { 55 return new String [] {"testdir/ignoredir/nextdir/", 56 "testdir/mountdir/nextdir/", 57 "testdir/mountdir2/nextdir/", 58 "testdir/mountdir2/fname/notestfile", 59 "testdir/mountdir2/fname2/testfile", 60 "testdir/mountdir4/file.ext", 61 "testdir/mountdir5/file.ext", 62 "testdir/mountdir6/file.ext", 63 "testdir/mountdir6/file2.ext", 64 "testdir/mountdir7/file2.ext", 65 "testdir/mountdir8/", 66 "testdir/mountdir9/", 67 }; 68 } 69 70 public void testCreateNotExistingFolderOrDataFile() throws IOException { 71 final File wDir = getWorkDir(); 72 final File fold = new File (wDir,"a/b/c"); 73 final File data = new File (fold,"c.data"); 74 implCreateFolderOrDataFile(fold, data); 75 } 76 77 public void testCreateExistingFolderOrDataFile() throws IOException { 78 final File wDir = getWorkDir(); 79 final File fold = new File (wDir,"a/b/c"); 80 final File data = new File (fold,"c.data"); 81 82 assertTrue(fold.mkdirs()); 83 assertTrue(data.createNewFile()); 84 85 implCreateFolderOrDataFile(fold, data); 86 } 87 88 public void testCreateFolderOrDataFileExternalChange() throws IOException { 89 final File wDir = getWorkDir(); 90 final File fold = new File (wDir,"a/b/c"); 91 final File data = new File (fold,"c.data"); 92 createFiles(data, fold); 93 94 implCreateFolderOrDataFile(fold, data); 95 96 FileObject foldFo = FileUtil.toFileObject(fold); 97 FileObject dataFo = FileUtil.toFileObject(data); 98 assertNotNull(foldFo); 99 assertNotNull(dataFo); 100 deleteFiles(data, wDir); 101 102 implCreateFolderOrDataFile(fold, data); 103 104 deleteFiles(data, wDir); 105 foldFo.getFileSystem().refresh(false); 106 foldFo = FileUtil.toFileObject(fold); 107 dataFo = FileUtil.toFileObject(data); 108 assertNull(foldFo); 109 assertNull(dataFo); 110 createFiles(data, fold); 111 112 implCreateFolderOrDataFile(fold, data); 113 } 114 131 132 146 147 private static void createFiles(final File data, final File fold) throws IOException { 148 assertTrue(fold.mkdirs()); 149 assertTrue(data.createNewFile()); 150 } 151 152 private static void deleteFiles(final File data, final File wDir) { 153 File tmp = data; 154 while(!tmp.equals(wDir)) { 155 assertTrue(tmp.delete()); 156 tmp = tmp.getParentFile(); 157 } 158 } 159 160 private void implCreateFolderOrDataFile(final File fold, final File data) throws IOException { 161 FileObject foldFo = FileUtil.createFolder(fold); 162 assertNotNull(foldFo); 163 assertTrue(foldFo.isValid()); 164 assertNotNull(FileUtil.toFile(foldFo)); 165 assertEquals(FileUtil.toFile(foldFo),fold); 166 assertTrue(foldFo.isFolder()); 167 168 FileObject dataFo = FileUtil.createData(data); 169 assertNotNull(dataFo); 170 assertTrue(dataFo.isValid()); 171 assertNotNull(FileUtil.toFile(dataFo)); 172 assertEquals(FileUtil.toFile(dataFo),data); 173 assertTrue(dataFo.isData()); 174 } 175 176 public void testGetNameExt2() throws IOException { 177 FileObject fold1 = FileUtil.createFolder( 178 FileBasedFileSystem.getFileObject(getWorkDir()),getName()); 179 assertNotNull(fold1); 180 testComposeNameImpl(fold1.createData("a.b")); 181 testComposeNameImpl(fold1.createData(".b")); 182 if (!Utilities.isWindows()) { 183 testComposeNameImpl(fold1.createData("a.")); 184 } 185 } 186 187 private void testComposeNameImpl(FileObject fo) throws IOException { 188 assertTrue(fo.isValid() && fo.isData()); 189 String fullName = fo.getNameExt(); 190 String ext = fo.getExt(); 191 String name = fo.getName(); 192 FileObject parent = fo.getParent(); 193 fo.delete(); 194 FileObject fo2 = parent.createData(name, ext); 195 assertEquals(fullName, fo2.getNameExt()); 196 assertEquals(name, fo2.getName()); 197 assertEquals(ext, fo2.getExt()); 198 } 199 200 public void testFileUtilToFileObjectIsValid() throws Exception { 201 char SEP = File.separatorChar; 202 final File fileF = new File (FileUtil.toFile(root).getAbsolutePath() + SEP + "dir" + SEP + "file2"); 203 File dirF = fileF.getParentFile(); 204 205 for (int cntr = 0; cntr < 10; cntr++) { 206 dirF.mkdir(); 207 new FileOutputStream (fileF).close(); 208 root.getFileSystem().refresh(false); 209 final List valid = new ArrayList (); 210 FileUtil.toFileObject(fileF).addFileChangeListener(new FileChangeListener() { 211 public void fileAttributeChanged(FileAttributeEvent fe) { 212 update(); 213 } 214 public void fileChanged(FileEvent fe) { 215 update(); 216 } 217 public void fileDataCreated(FileEvent fe) { 218 update(); 219 } 220 public void fileDeleted(FileEvent fe) { 221 update(); 222 } 223 public void fileFolderCreated(FileEvent fe) { 224 update(); 225 } 226 public void fileRenamed(FileRenameEvent fe) { 227 update(); 228 } 229 230 private void update() { 231 FileObject fo; 232 File f = fileF; 233 234 while ((fo = FileUtil.toFileObject(f)) == null) { 235 f = f.getParentFile(); 236 } 237 238 valid.add(Boolean.valueOf(fo.isValid())); 239 } 240 }); 241 fileF.delete(); 242 dirF.delete(); 243 root.getFileSystem().refresh(false); 244 245 assertTrue("at least one event", valid.size() > 0); 246 247 for (Iterator i = valid.iterator(); i.hasNext(); ) { 248 assertTrue("valid=" + valid + ", count=" + cntr, ((Boolean ) i.next()).booleanValue()); 249 } 250 } 251 } 252 253 public void testRefresh69744() throws Exception { 254 File thisTest = new File (getWorkDir(),"thisFolder/thisFolder"); 255 thisTest.mkdirs(); 256 thisTest = new File (thisTest,"thisTest"); 257 thisTest.createNewFile(); 258 FileObject testf = FileUtil.toFileObject(thisTest); 259 assertNotNull(testf); 260 assertGC("",new WeakReference (testf.getParent())); 261 modifyFileObject(testf, "abc"); 262 FileSystem fs = testf.getFileSystem(); 263 final List l = new ArrayList (); 264 FileChangeListener fcl = new FileChangeAdapter() { 265 public void fileChanged(FileEvent fe) { 266 l.add(fe); 267 } 268 }; 269 Thread.sleep(1500); 270 fs.addFileChangeListener(fcl); 271 try { 272 modifyFileObject(testf, "def"); 273 assertFalse(l.isEmpty()); 274 } finally { 275 fs.removeFileChangeListener(fcl); 276 } 277 } 278 279 private void modifyFileObject(final FileObject testf, String content) throws IOException { 280 FileLock lock = null; 281 OutputStream os = null; 282 try { 283 lock = testf.lock(); 284 os = testf.getOutputStream(lock); 285 os.write(content.getBytes()); 286 } finally { 287 if (os != null) os.close(); 288 if (lock != null) lock.releaseLock(); 289 } 290 } 291 292 public void testCaseInsensitivity() throws Exception { 293 if (!Utilities.isWindows()) return; 294 295 File testa = new File (getWorkDir(), "a"); 296 File testA = new File (getWorkDir(), "A"); 297 298 if (testA.exists()) { 299 assertTrue(testA.delete()); 300 } 301 if (!testa.exists()) { 302 assertTrue(testa.createNewFile()); 303 } 304 305 FileObject A = root.getFileObject("A"); 308 assertNotNull(A); 309 assertNotNull(root.getFileObject("a")); 310 assertSame(root.getFileObject("A"), root.getFileObject("a")); 311 assertSame(URLMapper.findFileObject(testa.toURI().toURL()), 312 URLMapper.findFileObject(testA.toURI().toURL())); 313 314 root.getChildren(); 316 assertEquals("A",root.getFileObject("A").getName()); 317 assertEquals("A",root.getFileObject("a").getName()); 318 } 319 320 private class TestListener extends FileChangeAdapter { 321 private List fileObjects; 322 TestListener(List fileObjects) { 323 this.fileObjects = fileObjects; 324 } 325 public void fileFolderCreated(FileEvent fe) { 326 assertTrue(fileObjects.remove(fe.getFile())); 327 } 328 329 public void fileDeleted(FileEvent fe) { 330 assertTrue(fileObjects.remove(fe.getFile())); 331 } 332 333 public void fileDataCreated(FileEvent fe) { 334 assertTrue(fileObjects.remove(fe.getFile())); 335 } 336 } 337 338 public void testSimulatesRefactoringRename() throws Exception { 339 assertNotNull(root); 340 FileSystem fs = root.getFileSystem(); 341 assertNotNull(fs); 342 FileObject main = root.createData("Main.java"); 343 FileUtil.createData(root,"subpackage/newclass.java"); 344 final List fileObjects = new ArrayList (); 345 final Set allSubPackages = new HashSet(); 346 final TestListener tl = new TestListener(fileObjects); 347 fs.addFileChangeListener(tl); 348 try { 349 fs.runAtomicAction(new FileSystem.AtomicAction(){ 350 public void run() throws IOException { 351 FileObject subpackage = root.getFileObject("subpackage"); 352 allSubPackages.add(subpackage); 353 FileObject newclass = subpackage.getFileObject("newclass.java"); 354 FileObject subpackage1 = root.createFolder("subpackage1"); 355 fileObjects.add(subpackage1); 356 allSubPackages.add(subpackage1); 357 FileObject newclass1 = subpackage1.createData("newclass.java"); 358 fileObjects.add(newclass1); 359 subpackage.delete(); 360 fileObjects.add(subpackage); 361 fileObjects.add(newclass); 362 } 363 }); 364 } finally { 365 fs.removeFileChangeListener(tl); 366 } 367 assertTrue(fileObjects.isEmpty()); 368 assertNotNull(root.getFileObject("Main.java")); 369 assertNotNull(root.getFileObject("subpackage1")); 370 assertNotNull(root.getFileObject("subpackage1/newclass.java")); 371 FileObjectTestHid.implOfTestGetFileObjectForSubversion(root, "subpackage"); 372 final String subpackageName = (ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal() && Utilities.isWindows()) ? 373 "subpackage2" : "Subpackage"; 374 fs.addFileChangeListener(tl); 375 try { 376 fs.runAtomicAction(new FileSystem.AtomicAction(){ 377 public void run() throws IOException { 378 FileObject subpackage1 = root.getFileObject("subpackage1"); 379 FileObject newclass = root.getFileObject("subpackage1/newclass.java"); 380 FileObject Subpackage = root.createFolder(subpackageName); 381 allSubPackages.add(Subpackage); 382 assertEquals(3,allSubPackages.size()); 383 384 fileObjects.add(Subpackage); 385 FileObject newclass1 = Subpackage.createData("newclass.java"); 386 fileObjects.add(newclass1); 387 subpackage1.delete(); 388 fileObjects.add(subpackage1); 389 fileObjects.add(newclass); 390 } 391 }); 392 } finally { 393 fs.removeFileChangeListener(tl); 394 } 395 assertTrue(fileObjects.isEmpty()); 396 assertNotNull(root.getFileObject("Main.java")); 397 assertNotNull(root.getFileObject(subpackageName+"/newclass.java")); 398 FileObjectTestHid.implOfTestGetFileObjectForSubversion(root, "subpackage1"); 399 assertEquals(3,allSubPackages.size()); 400 } 401 402 public void testRefresh60479 () throws Exception { 403 final List l = new ArrayList (); 404 File rootFile = FileUtil.toFile(root); 405 assertTrue(rootFile.exists()); 406 407 File testFile = new File (rootFile, "testRefresh60479.txt"); 408 assertTrue(testFile.createNewFile()); 409 assertTrue(testFile.exists()); 410 411 FileObject testFo = FileUtil.toFileObject(testFile); 412 assertNotNull(testFo); 413 FileLock lock = testFo.lock(); 414 OutputStream os = null; 415 416 try { 417 os = testFo.getOutputStream(lock); 418 os.write("abcdefgh".getBytes()); 419 lock.releaseLock(); 420 os.close(); 421 Thread.sleep(3000); 422 os = new FileOutputStream (testFile); 423 os.write("ijkl".getBytes()); 424 os.close(); 425 } finally { 426 if (lock != null && lock.isValid()) { 427 lock.releaseLock(); 428 } 429 if (os != null) { 430 os.close(); 431 } 432 } 433 434 testFo.addFileChangeListener(new FileChangeAdapter(){ 435 public void fileChanged(FileEvent fe) { 436 l.add(fe); 437 } 438 439 }); 440 441 testFo.refresh(true); 442 assertEquals(1,l.size()); 443 } 444 445 public void testNormalization51910 () throws Exception { 446 if (!Utilities.isWindows()) return; 447 448 File rootFile = FileUtil.toFile(root); 449 assertTrue(rootFile.exists()); 450 451 File testFile = new File (rootFile, "abc.txt"); 452 assertTrue(testFile.createNewFile()); 453 assertTrue(testFile.exists()); 454 455 File testFile2 = new File (rootFile, "ABC.TXT"); 456 assertTrue(testFile2.exists()); 457 458 459 assertEquals(FileUtil.normalizeFile(testFile).toURI().toURL(), FileUtil.normalizeFile(testFile2).toURI().toURL()); 460 } 461 462 public void testEventsAfterCreatedFiles55550() throws Exception { 463 FileObject parent = root.getFileObject("testdir/mountdir8"); 464 assertNotNull(parent); 465 assertTrue(parent.isFolder()); 466 parent.getChildren(); 467 468 File parentFile = FileUtil.toFile(parent); 469 assertNotNull(parentFile); 470 assertTrue(parentFile.getAbsolutePath(),parentFile.exists()); 471 File newFile = new File (parentFile, "sun-web.xml"); 472 assertFalse(newFile.getAbsolutePath(),newFile.exists()); 473 474 class FCLImpl extends FileChangeAdapter { 475 boolean created; 476 public void fileDataCreated(FileEvent e) { 477 created = true; 478 } 479 } 480 FCLImpl fl = new FCLImpl(); 481 parent.addFileChangeListener(fl); 482 483 assertTrue(newFile.getAbsolutePath(), newFile.createNewFile()); 484 assertTrue(newFile.exists()); 485 486 FileUtil.toFileObject(newFile); 490 491 492 parent.refresh(); 493 parent.removeFileChangeListener(fl); 494 assertTrue("Didn't receive a FileEvent on the parent.", fl.created); 495 } 496 497 public void testIssue49037 () throws Exception { 498 assertNotNull(root); 499 FileObject fo = root.getFileObject("testdir/"); 500 assertNotNull(fo); 501 502 File f = FileUtil.toFile (fo); 503 assertNotNull(f); 504 505 File newFile = new File (f, "absolutelyNewFile"); 506 assertFalse(newFile.exists()); 507 508 new FileOutputStream (newFile).close(); 509 assertTrue(newFile.exists()); 510 assertNotNull(FileUtil.toFileObject(newFile)); 511 } 512 513 public void testDeadlock55547 () throws Exception { 514 assertNotNull(root); 515 File rootFile = new File (FileUtil.toFile(root), "testdir/mountdir9/"); 516 assertNotNull(rootFile); 517 assertTrue(rootFile.exists()); 518 519 IgnoreDirFileSystem fs1 = new IgnoreDirFileSystem(); 520 521 fs1.setRootDirectory(rootFile); 522 MountTable.getDefault().mount(rootFile.getAbsolutePath(), fs1); 523 524 FileObject fo = root.getFileObject("testdir/mountdir9/"); 525 assertNotNull(fo); 526 527 File f = FileUtil.toFile (fo); 528 assertNotNull(f); 529 530 File nFile = new File (f, "newchild.txt"); 531 assertFalse(nFile.exists()); 532 assertTrue(nFile.createNewFile()); 533 534 final Object wait = new Object (); 535 536 FileChangeListener fcl = null; 537 final List list = new ArrayList (); 538 fo.addFileChangeListener(fcl = new FileChangeAdapter () { 539 public void fileDataCreated(final FileEvent fe) { 540 RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable () { 541 public void run() { 542 synchronized (wait) { 543 assertNotNull(root.getFileObject("testdir/")); 544 list.add(fe); 545 } 546 } 547 }); 548 549 task.schedule(0); 550 task.waitFinished(); 551 } 552 }); 553 554 if (!ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal()) { 555 assertNotNull(fo.getFileObject("newchild.txt")); 556 assertTrue(list.size() == 1); 557 } 558 fo.removeFileChangeListener(fcl); 559 } 560 561 public void testToFileObject () throws Exception { 562 assertNotNull(root); 563 564 File rootFile = FileUtil.toFile (root); 565 File mnt1 = new File (rootFile, "testdir/mountdir2/fname"); 566 567 IgnoreDirFileSystem fs1 = new IgnoreDirFileSystem(); 568 569 fs1.setRootDirectory(mnt1); 570 MountTable.getDefault().mount(mnt1.getAbsolutePath(), fs1); 571 572 File test = new File (rootFile, "testdir/mountdir2/fname2/testfile"); 573 assertNotNull (FileUtil.toFileObject(test)); 574 } 575 576 public void testFileUtilFromFile () throws Exception { 577 assertNotNull(root); 578 579 File f = FileUtil.normalizeFile(getWorkDir()); 580 IgnoreDirFileSystem ifs = new IgnoreDirFileSystem(); 581 ifs.setRootDirectory(f); 582 583 Repository.getDefault().addFileSystem(ifs); 584 Repository.getDefault().addFileSystem(testedFS); 585 586 FileObject[] fos = FileUtil.fromFile(f); 587 assertTrue(fos.length > 0); 588 assertEquals(fos[0].getFileSystem(), testedFS ); 589 590 } 591 592 public void testFileSystemStatus () throws Exception { 593 assertNotNull(root); 594 595 File rootFile = FileUtil.toFile (root); 596 File mnt1 = new File (rootFile, "testdir/mountdir6"); 597 598 IgnoreDirFileSystem fs1 = new IgnoreDirFileSystem(); 599 600 fs1.setRootDirectory(mnt1); 601 MountTable.getDefault().mount(mnt1.getAbsolutePath(), fs1); 602 603 assertNotNull (FileUtil.toFileObject(mnt1)); 604 605 java.util.Set files = new java.util.HashSet (); 606 java.util.Enumeration en = FileUtil.toFileObject(mnt1).getChildren(true); 607 while (en.hasMoreElements()) { 608 files.add(en.nextElement()); 609 } 610 FileSystem.Status status = root.getFileSystem().getStatus(); 611 String name = status.annotateName("name", files); 612 613 Iterator it = files.iterator(); 614 while (it.hasNext()) { 615 FileObject fo = (FileObject)it.next(); 616 assertTrue (name.indexOf(fo.getNameExt()) != -1); 617 } 618 619 } 620 621 public void testIssue45485 () { 622 assertNotNull(root); 623 final FileObject testdir = root.getFileObject("testdir."); 624 assertNull(testdir); 625 } 626 627 public void testMount1 () throws Exception { 628 final List events = new ArrayList (); 629 assertNotNull(root); 630 final FileObject ignore = root.getFileObject("testdir/ignoredir"); 631 assertNotNull(ignore); 632 assertTrue(ignore.isFolder()); 633 MountTable mt = MountTable.getDefault(); 634 FileObject fo = root.getFileObject("testdir").getParent(); 635 File f = FileUtil.normalizeFile(FileUtil.toFile(fo)); 636 IgnoreDirFileSystem ifs = new IgnoreDirFileSystem(); 637 ifs.setRootDirectory(f); 638 639 mt.mount(f.getAbsolutePath(), ifs); 640 641 assertNotNull(root.getFileObject("testdir/ignoredir")); 642 mt.unmount(ifs); 643 FileObject file = root.getFileObject("testdir/ignoredir"); 644 assertNotNull(file); 645 assertTrue(file.isFolder()); 646 assertNotNull(file.getParent()); 647 assertTrue(FileUtil.isParentOf(root, file)); 648 } 649 650 public void testDeleteMountPoint () throws Exception { 651 assertNotNull(root); 652 FileObject mountDir = root.getFileObject("testdir/mountdir"); 653 assertNotNull(mountDir); 654 File mountFile = FileUtil.normalizeFile(FileUtil.toFile(mountDir)); 655 IgnoreDirFileSystem ifs = new IgnoreDirFileSystem(); 656 ifs.setRootDirectory(mountFile); 657 658 MountTable mt = MountTable.getDefault(); 659 mt.mount(mountFile.getAbsolutePath(), ifs); 660 661 if (!ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal()) { 662 mountDir.delete(); 663 assertTrue(!mountDir.isValid()); 664 assertNull(root.getFileObject("testdir/mountdir")); 665 } 666 } 667 668 public void testDeleteCaptureExteralChanges () throws Exception { 669 String externalName = "newfile.external"; 670 assertNotNull(root); 671 FileObject fileObject = root.getFileObject("testdir/mountdir5/file.ext"); 672 assertNotNull(fileObject); 673 File f = FileUtil.toFile(fileObject); 674 675 assertNotNull(f); 676 f = FileUtil.normalizeFile(f); 677 assertTrue(f.exists()); 678 assertTrue (f.delete()); 679 680 if (!ProvidedExtensionsTest.ProvidedExtensionsImpl.isImplsDeleteRetVal()) { 681 assertFalse(fileObject.isValid()); 682 } 683 } 684 685 public void testFindResourceCaptureExteralChanges () throws Exception { 686 String externalName = "newfile.external"; 687 assertNotNull(root); 688 FileObject fileObject = root.getFileObject("testdir"); 689 assertNotNull(fileObject); 690 File f = FileUtil.toFile(fileObject); 691 692 assertNull(testedFS.findResource(new File (f, externalName).getAbsolutePath().replace('\\',File.separatorChar))); 693 assertNull(fileObject.getFileObject(externalName)); 694 695 assertNotNull(f); 696 f = FileUtil.normalizeFile(f); 697 assertTrue(f.exists()); 698 f = new File (f, externalName); 699 assertTrue(!f.exists()); 700 assertTrue(f.getAbsolutePath(),f.createNewFile()); 701 assertNotNull(testedFS.findResource(f.getAbsolutePath().replace('\\',File.separatorChar))); 702 } 703 704 public void testGetFileObjectCaptureExteralChanges () throws Exception { 705 String externalName = "newfile.external2"; 706 assertNotNull(root); 707 FileObject fileObject = root.getFileObject("testdir"); 708 assertNotNull(fileObject); 709 File f = FileUtil.toFile(fileObject); 710 711 assertNull(testedFS.findResource(new File (f, externalName).getAbsolutePath().replace('\\',File.separatorChar))); 712 assertNull(fileObject.getFileObject(externalName)); 713 714 assertNotNull(f); 715 f = FileUtil.normalizeFile(f); 716 assertTrue(f.exists()); 717 f = new File (f, externalName); 718 assertTrue(!f.exists()); 719 assertTrue(f.getAbsolutePath(),f.createNewFile()); 720 assertNotNull(fileObject.getFileObject(externalName)); 721 } 722 723 public void testGetFileObject47885 () throws Exception { 724 assertNotNull(root); 725 726 FileObject fileObject = root.getFileObject("testdir/mountdir4/file.ext"); 727 assertNotNull(fileObject); 728 729 fileObject = root.getFileObject("testdir/mountdir4/file", "ext"); 730 assertNull(fileObject); 731 732 fileObject = root.getFileObject("testdir\\mountdir4\\file.ext"); 733 assertNull(fileObject); 734 } 735 736 737 public void testValidRoots () throws Exception { 738 FileSystemView fsv = FileSystemView.getFileSystemView(); 739 740 File [] roots = File.listRoots(); 741 for (int i = 0; i < roots.length; i++) { 742 FileObject root = FileUtil.toFileObject(roots[i]); 743 if (fsv.isFloppyDrive(roots[i]) || !roots[i].exists()) { 744 assertNull(root); 745 continue; 746 } 747 748 assertNotNull(roots[i].getAbsolutePath (),root); 749 assertEquals(testedFS, root.getFileSystem()); 750 assertTrue(root.isValid()); 751 } 752 assertNotNull(testedFS.getRoot()); 753 assertTrue(testedFS.getRoot().isValid()); 754 } 755 756 public void testDeserializationOfMasterFSLeadsToTheSameFileSystem () throws Exception { 757 NbMarshalledObject stream = new NbMarshalledObject (testedFS); 758 Object obj = stream.get (); 759 assertSame ("After deserialization it is still the same", testedFS, obj); 760 } 761 762 763 public void testNormalizeDrivesOnWindows48681 () { 764 if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) { 765 File [] roots = File.listRoots(); 766 for (int i = 0; i < roots.length; i++) { 767 File file = roots[i]; 768 if (FileSystemView.getFileSystemView().isFloppyDrive(file) || !file.exists()) { 769 continue; 770 } 771 File normalizedFile = FileUtil.normalizeFile(file); 772 File normalizedFile2 = FileUtil.normalizeFile(new File (file, ".")); 773 774 assertEquals (normalizedFile.getPath(), normalizedFile2.getPath()); 775 } 776 777 } 778 } 779 780 public void testJarFileSystemDelete () throws Exception { 781 assertNotNull(root); 782 FileObject folderFo = root.getFileObject("testdir/mountdir7"); 783 File folder = FileUtil.toFile(folderFo); 784 assertNotNull(folder); 785 786 File f = new File (folder,"jfstest.jar"); 787 if (!f.exists()) { 788 f.getParentFile().mkdirs(); 789 f.createNewFile(); 790 } 791 JarOutputStream jos = new JarOutputStream (new FileOutputStream (f)); 792 jos.putNextEntry(new ZipEntry ("a/b/c/c.txt")); 793 jos.putNextEntry(new ZipEntry ("a/b/d/d.txt")); 794 795 jos.close(); 796 797 FileObject parent = FileUtil.toFileObject(f.getParentFile()); 798 parent.getChildren(); 799 JarFileSystem jfs = new JarFileSystem (); 800 try { 801 jfs.setJarFile(f); 802 } catch (Exception ex) { 803 fail (); 804 } 805 806 807 ArrayList all = new ArrayList (); 808 FileObject jfsRoot = jfs.getRoot(); 809 Enumeration en = jfsRoot.getChildren(true); 810 while (en.hasMoreElements()) { 811 all.add ((FileObject) en.nextElement()); 812 } 813 814 assertTrue (all.size() > 0); 815 816 final ArrayList deleted = new ArrayList (); 817 jfs.addFileChangeListener(new FileChangeAdapter() { 818 public void fileDeleted(FileEvent fe) { 819 super.fileDeleted(fe); 820 deleted.add (fe.getFile()); 821 } 822 }); 823 824 Thread.sleep(1000); 825 assertTrue (f.getAbsolutePath(), f.delete()); 826 parent.refresh(); 827 assertEquals (deleted.size(), all.size()); 828 829 for (int i = 0; i < all.size(); i++) { 830 FileObject fileObject = (FileObject) all.get(i); 831 assertFalse (fileObject.isValid()); 832 } 833 834 835 assertFalse (jfsRoot.isValid()); 836 } 837 838 839 public void testInitialization () throws Exception { 840 assertTrue (MasterFileSystemTest.TestFSProvider.DEFAULT.isInitailized()); 841 } 842 843 844 public void testLockFileAfterCrash() throws Exception { 845 FileObject testFo = FileUtil.createData(root,"/testAfterCrash/testfile.data"); 846 File testFile = FileUtil.toFile(testFo); 847 848 849 File lockFile = WriteLockUtils.getAssociatedLockFile(testFile); 850 if (!lockFile.exists()) { 851 assertTrue(lockFile.createNewFile()); 852 } 853 854 assertTrue(lockFile.exists()); 855 856 FileObject lockFo = FileUtil.toFileObject(lockFile); 857 assertNull(lockFo); 858 testFo.delete(); 859 860 861 lockFo = FileUtil.toFileObject(lockFile); 862 String msg = (lockFo != null) ? lockFo.toString() : ""; 863 assertNull(msg,lockFo); 864 } 865 866 private class IgnoreDirFileSystem extends LocalFileSystem { 867 org.openide.filesystems.FileSystem.Status status = new org.openide.filesystems.FileSystem.HtmlStatus() { 868 public String annotateName (String name, java.util.Set files) { 869 java.lang.StringBuffer sb = new StringBuffer (name); 870 Iterator it = files.iterator (); 871 while (it.hasNext()) { 872 FileObject fo = (FileObject)it.next(); 873 try { 874 if (fo.getFileSystem() instanceof IgnoreDirFileSystem) { 875 sb.append ("," +fo.getNameExt()); } 877 } catch (Exception ex) { 878 fail (); 879 } 880 } 881 882 return sb.toString () ; 883 } 884 885 public java.awt.Image annotateIcon (java.awt.Image icon, int iconType, java.util.Set files) { 886 return icon; 887 } 888 889 public String annotateNameHtml(String name, Set files) { 890 return annotateName (name, files); 891 } 892 893 }; 894 895 public org.openide.filesystems.FileSystem.Status getStatus() { 896 return status; 897 } 898 899 protected String [] children(String name) { 900 String [] strings = super.children(name); 901 return strings; 902 } 903 } 904 } 905 | Popular Tags |