1 19 20 package org.openide.filesystems; 21 22 import org.xml.sax.*; 23 import org.openide.util.*; 24 import javax.xml.parsers.SAXParserFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import java.io.*; 27 import java.util.*; 28 import java.net.*; 29 30 34 public class FileObjectTestHid extends TestBaseHid { 35 36 private final static String FOLDER_CHILD_NAME= "testFolder"; 37 private final static String FOLDER_CHILD= "/"+FOLDER_CHILD_NAME; 38 39 private final static String FILE_CHILD_NAME= "test"; 40 private final static String FILE_CHILD_EXT= "txt"; 41 private final static String FILE_CHILD= "/"+FILE_CHILD_NAME+"." +FILE_CHILD_EXT; 42 43 44 private FileSystem fs; 45 46 private FileObject root; 47 private static Set res = null; 48 49 50 53 private static String [] resources = new String [] { 54 }; 55 56 public FileObjectTestHid(java.lang.String testName) { 57 super(testName); 58 } 59 60 protected void setUp() throws java.lang.Exception { 61 super.setUp(); 62 63 64 fs = this.testedFS; 65 root = fs.findResource(getResourcePrefix()); 66 } 67 68 protected void tearDown() throws Exception { 69 super.tearDown(); 70 fs = this.testedFS = null; 71 allTestedFS = null; 72 root = null; 74 } 76 77 public void testEventsDelivery81746() throws Exception { 78 doEventsDelivery81746(1); 79 } 80 81 public void testEventsDeliveryInInnerAtomicActions82459() throws Exception { 82 doEventsDelivery81746(2); 83 } 84 85 private void doEventsDelivery81746(final int howDeep) throws Exception { 86 checkSetUp(); 87 final FileObject fold = getTestFolder1(root); 88 if (fold.getFileSystem().isReadOnly()) { 89 return; 90 } 91 class L extends FileChangeAdapter { 92 public int cnt; 93 94 public void fileDataCreated(FileEvent fe) { 95 cnt++; 96 } 97 } 98 99 final FileChangeListener noFileDataCreatedListener = new FileChangeAdapter(){ 100 public void fileDataCreated(FileEvent fe) { 101 fail(); 102 } 103 }; 104 final FileChangeListener listener1 = new FileChangeAdapter(){ 105 public void fileDataCreated(FileEvent fe) { 106 try { 107 fold.getFileSystem().removeFileChangeListener(noFileDataCreatedListener); 108 fold.getFileSystem().addFileChangeListener(noFileDataCreatedListener); 109 } catch (FileStateInvalidException ex) { 110 FileObjectTestHid.this.fsFail(""); 111 } 112 } 113 }; 114 115 final L countingL = new L(); 116 try { 117 fold.getFileSystem().addFileChangeListener(listener1); 118 fold.addFileChangeListener(countingL); 119 fold.getFileSystem().addFileChangeListener(countingL); 120 fold.getFileSystem().runAtomicAction(new FileSystem.AtomicAction(){ 121 private int stillDeep = howDeep; 122 123 public void run() throws java.io.IOException { 124 if (--stillDeep > 0) { 125 fold.getFileSystem().runAtomicAction(this); 126 assertEquals("No events in inner actions", 0, countingL.cnt); 127 return; 128 } 129 130 131 fold.createData("file1"); 132 fold.createData("file2"); 133 } 134 }); 135 } finally { 136 fold.getFileSystem().removeFileChangeListener(listener1); 137 fold.getFileSystem().removeFileChangeListener(noFileDataCreatedListener); 138 } 139 } 140 141 public void testEventsDelivery81746_2() throws Exception { 142 checkSetUp(); 143 final FileObject fold = getTestFolder1(root); 144 if (fold.getFileSystem().isReadOnly()) { 145 return; 146 } 147 final FileChangeListener noFileDataCreatedListener = new FileChangeAdapter(){ 148 public void fileDataCreated(FileEvent fe) { 149 fail(); 150 } 151 }; 152 final FileChangeListener listener1 = new FileChangeAdapter(){ 153 public void fileDataCreated(FileEvent fe) { 154 fold.removeFileChangeListener(noFileDataCreatedListener); 155 fold.addFileChangeListener(noFileDataCreatedListener); 156 } 157 }; 158 159 try { 160 fold.getFileSystem().addFileChangeListener(listener1); 161 fold.getFileSystem().runAtomicAction(new FileSystem.AtomicAction(){ 162 public void run() throws java.io.IOException { 163 fold.createData("file1"); 164 fold.createData("file2"); 165 } 166 }); 167 } finally { 168 fold.getFileSystem().removeFileChangeListener(listener1); 169 fold.removeFileChangeListener(noFileDataCreatedListener); 170 } 171 } 172 173 174 public void testCopy() { 175 checkSetUp(); 176 FileObject fold = getTestFolder1(root); 177 FileObject fo1 = getTestFile1(fold); 178 FileObject fo2 = getTestFile2(fold); 179 180 try { 181 fo1.copy(fold,fo2.getName(),fo2.getExt()); 182 } catch (IOException iex) { 183 184 return; 185 } 186 fsFail ("copy should fire exception if file already exists"); 187 } 188 189 190 191 public void testCopy1() throws IOException { 192 checkSetUp(); 193 FileObject fold = getTestFolder1(root); 194 FileObject fo1 = getTestFile1(fold); 195 FileObject fo2 = getTestFile2(fold); 196 FileObject fo3 = null; 197 198 String testStr = "text..."; 199 String attrName = "attrName"; 200 String value = "value"; 201 202 registerDefaultListener(fold); 203 204 try { 205 writeStr(fo1, testStr); 206 fo1.setAttribute(attrName,value); 207 fo3 = fo1.copy(fold,fo2.getExt(),fo2.getName()); 208 } catch (IOException iex) { 209 fsAssert("expected copy will success on writable FS", 210 fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); 211 return; 212 } 213 fsAssert("no exception fired but copy returned null",fo3 != null); 214 fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); 215 fsAssert("attributes should be copied too", 216 value.equals((String )fo3.getAttribute(attrName)) ); 217 fileDataCreatedAssert("parent should fire fileDataCreated",1); 218 } 219 220 221 public void testCopy1_FS() throws IOException { 222 checkSetUp(); 223 FileObject fold = getTestFolder1(root); 224 FileObject fo1 = getTestFile1(fold); 225 FileObject fo2 = getTestFile2(fold); 226 FileObject fo3 = null; 227 228 String testStr = "text..."; 229 String attrName = "attrName"; 230 String value = "value"; 231 232 registerDefaultListener(testedFS); 233 234 try { 235 writeStr(fo1, testStr); 236 fo1.setAttribute(attrName,value); 237 fo3 = fo1.copy(fold,fo2.getExt(),fo2.getName()); 238 } catch (IOException iex) { 239 fsAssert("expected copy will success on writable FS", 240 fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); 241 return; 242 } 243 fsAssert("no exception fired but copy returned null",fo3 != null); 244 fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); 245 fsAssert("attributes should be copied too", 246 value.equals((String )fo3.getAttribute(attrName)) ); 247 fileDataCreatedAssert("parent should fire fileDataCreated",1); 248 } 249 250 251 252 253 public void testMove() { 254 checkSetUp(); 255 FileObject fold = getTestFolder1(root); 256 FileObject fo1 = getTestFile1(fold); 257 FileObject fo2 = getTestFile2(fold); 258 FileLock lock = null; 259 260 try { 261 lock = fo1.lock(); 262 fo1.move(lock, fold,fo2.getName(),fo2.getExt()); 263 } catch (IOException iex) { 264 265 return; 266 } finally { 267 if (lock != null) lock.releaseLock(); 268 } 269 fsFail ("move should fire exception if file already exists"); 270 } 271 272 273 public void testMove1() throws IOException { 274 checkSetUp(); 275 FileObject fold = getTestFolder1(root); 276 FileObject fo1 = getTestFile1(fold); 277 FileObject fo2 = getTestFile2(fold); 278 FileObject fo3 = null; 279 280 String testStr = "text..."; 281 String attrName = "attrName"; 282 String value = "value"; 283 284 registerDefaultListener(fold); 285 286 try { 287 writeStr(fo1, testStr); 288 fo1.setAttribute(attrName,value); 289 FileLock lock = fo1.lock(); 290 fold.getChildren(); 291 fo3 = fo1.move(lock,fold,fo2.getExt(),fo2.getName()); 292 lock.releaseLock(); 293 } catch (IOException iex) { 294 fsAssert("expected move will success on writable FS", 295 fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); 296 return; 297 } 298 fsAssert("no exception fired but copy returned null",fo3 != null); 299 fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); 300 fsAssert("attributes should be copied too", 301 value.equals((String )fo3.getAttribute(attrName)) ); 302 fsAssert ("",fold.getFileObject(fo3.getName(),fo3.getExt()) != null); 303 if (fo1.equals(fo3)) 304 this.fileRenamedAssert("File was actually renamed.",1); 305 else { 306 fileDeletedAssert("parent should fire fileDeleted",1); 307 fileDataCreatedAssert("parent should fire fileDataCreated",1); 308 } 309 } 310 311 312 public void testMove2() throws IOException { 313 checkSetUp(); 314 if (fs.isReadOnly()) return; 315 FileObject fold = getTestFolder1(root); 316 317 FileObject fold1 = fold.createFolder("A"); 318 FileObject fold2 = fold.createFolder("B"); 319 320 FileObject toMove = fold1.createData("something"); 321 FileLock lock = toMove.lock(); 322 try { 323 FileObject toMove2 = null; 324 assertNotNull (toMove2 = toMove.move(lock, fold2, toMove.getName(), toMove.getExt())); 325 lock.releaseLock(); 326 lock = toMove2.lock(); 327 assertNotNull (toMove2.move(lock, fold1, toMove.getName(), toMove.getExt())); 328 329 } finally { 330 lock.releaseLock(); 331 } 332 } 333 334 335 public void testMove1_Fs() throws IOException { 336 checkSetUp(); 337 FileObject fold = getTestFolder1(root); 338 FileObject fo1 = getTestFile1(fold); 339 FileObject fo2 = getTestFile2(fold); 340 FileObject fo3 = null; 341 342 String testStr = "text..."; 343 String attrName = "attrName"; 344 String value = "value"; 345 346 registerDefaultListener(testedFS); 347 348 try { 349 writeStr(fo1, testStr); 350 fo1.setAttribute(attrName,value); 351 FileLock lock = fo1.lock(); 352 fold.getChildren(); 353 fo3 = fo1.move(lock,fold,fo2.getExt(),fo2.getName()); 354 lock.releaseLock(); 355 } catch (IOException iex) { 356 fsAssert("expected move will success on writable FS", 357 fs.isReadOnly() || fo3.isReadOnly() || fo1.isReadOnly()); 358 return; 359 } 360 fsAssert("no exception fired but copy returned null",fo3 != null); 361 fsAssert("content of source and target should equal",testStr.equals(readStr(fo3))); 362 fsAssert("attributes should be copied too", 363 value.equals((String )fo3.getAttribute(attrName)) ); 364 fsAssert ("",fold.getFileObject(fo3.getName(),fo3.getExt()) != null); 365 if (fo1.equals(fo3)) 366 this.fileRenamedAssert("File was actually renamed.",1); 367 else { 368 fileDeletedAssert("parent should fire fileDeleted",1); 369 fileDataCreatedAssert("parent should fire fileDataCreated",1); 370 } 371 } 372 373 374 376 public void testWriteReadExclusion() throws Exception { 377 testWriteReadExclusion(false); 378 } 379 380 public void testWriteReadExclusionDeadlock() throws Exception { 381 testWriteReadExclusion(true); 382 } 383 384 private void testWriteReadExclusion(final boolean deadlockInWrite) throws Exception { 385 checkSetUp(); 386 FileObject fold = getTestFolder1(root); 387 final FileObject fo1 = getTestFile1(fold); 388 389 try { 390 firstThreadImpl1(fo1, deadlockInWrite); 391 firstThreadImpl2(fo1, deadlockInWrite); 392 } catch (IOException iex) { 393 fsAssert( 394 "expected move will success on writable FS", 395 fs.isReadOnly() || fo1.isReadOnly() 396 ); 397 } 398 } 399 400 private static void firstThreadImpl1(final FileObject fo1, final boolean deadlockInWrite) throws IOException, InterruptedException { 401 RequestProcessor.Task secondThread; 402 writeStr(fo1, "text"); 403 404 secondThread = startSecondThreadAndWait(fo1, deadlockInWrite); 405 406 InputStream is = null; 407 try { 408 is = fo1.getInputStream(); 409 byte[] arr = new byte[200]; 410 int len = is.read(arr); 411 assertEquals("Read all four bytes", 4, len); 412 for (int i = 1; i <= 4; i++) { 413 assertEquals(i + " th byte is " + i, i, arr[i - 1]); 414 } 415 } catch (IOException ex) { 416 if (!deadlockInWrite) { 418 throw ex; 419 } 420 assertTrue("FileAlreadyLockedException is fine here but just for dedlock in write", deadlockInWrite); 421 } finally { 422 try { 423 if (is != null) 424 is.close(); 425 } catch (IOException e) { 426 } 427 synchronized (FileObjectTestHid.class) { 428 FileObjectTestHid.class.notifyAll(); 430 } 431 secondThread.waitFinished(); 432 } 433 } 434 435 private static void firstThreadImpl2(final FileObject fo1, final boolean deadlockInWrite) throws IOException ,InterruptedException { 436 RequestProcessor.Task secondThread = null; 437 writeStr(fo1, "text"); 438 439 InputStream is = null; 440 try { 441 is = fo1.getInputStream(); 442 secondThread = startSecondThreadAndWait(fo1, deadlockInWrite); 443 444 byte[] arr = new byte[200]; 445 int len = is.read(arr); 446 assertEquals("Initial data only" , "text".length(), len); 447 } finally { 448 try { 449 if (is != null) 450 is.close(); 451 } catch (IOException e) { 452 } 453 synchronized (FileObjectTestHid.class) { 454 FileObjectTestHid.class.notifyAll(); 456 } 457 if (secondThread != null) secondThread.waitFinished(); 458 } 459 } 460 461 private static void secondThreadImpl(final FileObject fo1, final boolean deadlockInWrite) { 462 OutputStream os = null; 463 FileLock lock = null; 464 try { 465 try { 466 lock = fo1.lock(); 467 os = fo1.getOutputStream(lock); 468 os.write(1); 469 os.write(2); 470 os.flush(); 471 472 synchronized (FileObjectTestHid.class) { 473 FileObjectTestHid.class.notify(); 474 FileObjectTestHid.class.wait(deadlockInWrite ? 0 : 1000); 477 } 478 479 os.write(3); 480 os.write(4); 481 } finally { 482 if (lock != null) lock.releaseLock(); 483 if (os != null) os.close(); 484 synchronized (FileObjectTestHid.class) { 485 FileObjectTestHid.class.notify(); 486 } 487 } 488 } catch (Exception e) { 489 } 490 } 491 492 private static RequestProcessor.Task startSecondThreadAndWait(final FileObject fo1, final boolean deadlockInWrite) throws InterruptedException { 493 RequestProcessor.Task secondThread; 494 synchronized (FileObjectTestHid.class) { 495 secondThread = new RequestProcessor("Writes with delay").post(new Runnable () { 496 public void run() { 497 secondThreadImpl(fo1, deadlockInWrite); 498 } 499 }); 500 FileObjectTestHid.class.wait(); 501 } 502 return secondThread; 503 } 504 505 public void testGetPath1() { 506 checkSetUp(); 507 FileObject fold1 = getTestFolder1(root); 508 FileObject fold2 = getTestFolder1(fold1); 509 FileObject fo1 = getTestFile1(fold2); 510 511 FileObject result = fs.findResource(fo1.getPath()); 512 fsAssert("findResource problem", result != null); 513 fsAssert("findResource problem",fo1.equals(result)); 514 } 515 516 public void testGetPath2() { 517 checkSetUp(); 518 FileObject fold1 = getTestFolder1(root); 519 FileObject fold2 = getTestFolder1(fold1); 520 FileObject fo1 = null; 521 try { 522 fo1 = fold2.createFolder("a.b.c"); 523 } catch (IOException iex) { 524 fsAssert("There is not possible to create folder a.b.c", 525 fs.isReadOnly() || fold2.isReadOnly()); 526 return; 527 } 528 529 FileObject result = fs.findResource(fo1.getPath()); 530 fsAssert("findResource problem", result != null); 531 fsAssert("findResource problem",fo1.equals(result)); 532 } 533 534 public void testGetPath3() throws IOException{ 535 536 if ((org.openide.util.Utilities.isWindows () || 537 (org.openide.util.Utilities.getOperatingSystem () == org.openide.util.Utilities.OS_OS2))) { 538 return; 539 } 540 checkSetUp(); 541 FileObject fold1 = getTestFolder1(root); 542 FileObject fold2 = getTestFolder1(fold1); 543 544 FileObject fo1 = null; 545 try { 546 fo1 = fold2.createFolder("a.b.c."); 547 } catch (IOException iex) { 548 fsAssert("There is not possible to create folder a.b.c", 549 fs.isReadOnly() || fold2.isReadOnly()); 550 return; 551 } 552 553 554 FileObject result = fs.findResource(fo1.getPath()); 555 fsAssert("findResource problem", result != null); 556 fsAssert("findResource problem",fo1.equals(result)); 557 } 558 559 public void testGetPath4() throws IOException{ 560 checkSetUp(); 561 FileObject fold1 = getTestFolder1(root); 562 FileObject fold2 = getTestFolder1(fold1); 563 564 FileObject fo1 = null; 565 try { 566 fo1 = fold2.createData("a.b.c.java"); 567 } catch (IOException iex) { 568 fsAssert("There is not possible to create folder a.b.c", 569 fs.isReadOnly() || fold2.isReadOnly()); 570 return; 571 } 572 573 FileObject result = fs.findResource(fo1.getPath()); 574 fsAssert("findResource problem", result != null); 575 fsAssert("findResource problem",fo1.equals(result)); 576 } 577 578 public void testGetPath5() throws IOException{ 579 checkSetUp(); 580 FileObject fold1 = getTestFolder1(root); 581 FileObject fold2 = getTestFolder1(fold1); 582 583 584 FileObject fo1 = null; 585 try { 586 fo1 = fold2.createData("a.b.c","java"); 587 } catch (IOException iex) { 588 fsAssert("There is not possible to create folder a.b.c", 589 fs.isReadOnly() || fold2.isReadOnly()); 590 return; 591 } 592 593 FileObject result = fs.findResource(fo1.getPath()); 594 fsAssert("findResource problem", result != null); 595 fsAssert("findResource problem",fo1.equals(result)); 596 } 597 598 613 614 637 638 639 public void testGetNameExt() { 640 checkSetUp(); 641 FileObject fold1 = getTestFolder1(root); 642 FileObject fold2 = getTestFolder1(fold1); 643 FileObject fo1 = getTestFile1(fold2); 644 645 fsAssert("getNameExt problem",fo1.getNameExt().equals(fo1.getName() + "." +fo1.getExt())); 646 } 647 648 649 public void testExistsExt() { 650 checkSetUp(); 651 FileObject fo1 = getTestFile1(root); 652 FileObject fo2 = getTestFile2(root); 653 654 fsAssert("Expected both file`s names are equal. But extension differ.",fo1.existsExt(fo2.getExt())); 655 } 656 657 658 public void testHasExt() { 659 checkSetUp(); 660 FileObject fo1 = getTestFile1(root); 661 FileObject fo2 = getTestFile2(root); 662 FileObject fo11 = getTestFile1(getTestFolder1(root)); 663 664 fsTestFrameworkErrorAssert("",fo1.getExt().equals(fo11.getExt()) && 665 !fo1.getExt().equals(fo2.getExt())); 666 fsAssert("Unexpected ",fo1.hasExt(fo11.getExt()) && !fo1.hasExt(fo2.getExt())); 667 } 668 669 670 public void testFireFileDataCreatedEvent() throws IOException { 671 checkSetUp(); 672 registerDefaultListener(root); 673 root.getChildren(); 674 675 FileObject fo; 676 try { 677 fo = root.createData("name","ext"); 678 } catch (IOException iex) { 679 fsAssert("createData fired IOException. So there was expected fs or fo are read-only: " + iex.toString() , 680 fs.isReadOnly() || root.isReadOnly()); 681 fileDataCreatedAssert("fs or fo is read-only. So no event should be fired",0); 682 return; 683 } 684 685 fileDataCreatedAssert("createData should fire event fileDataCreated",1); 686 fsAssert("createData returned null",fo != null); 687 688 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 689 690 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 691 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 692 fileChangedAssert("fireFileChangedEvent should not be fired ",0); 693 } 694 695 696 public void testFireFileDataCreatedEvent_FS() throws IOException { 697 checkSetUp(); 698 registerDefaultListener(testedFS); 699 root.getChildren(); 700 701 FileObject fo; 702 try { 703 fo = root.createData("name","ext"); 704 } catch (IOException iex) { 705 fsAssert("createData fired IOException. So there was expected fs or fo are read-only: " + iex.toString() , 706 fs.isReadOnly() || root.isReadOnly()); 707 fileDataCreatedAssert("fs or fo is read-only. So no event should be fired",0); 708 return; 709 } 710 711 fileDataCreatedAssert("createData should fire event fileDataCreated",1); 712 fsAssert("createData returned null",fo != null); 713 714 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 715 716 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 717 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 718 fileChangedAssert("fireFileChangedEvent should not be fired ",0); 719 } 720 721 public void testFireFileDataCreatedEvent_FS2() throws IOException { 722 checkSetUp(); 723 registerDefaultListener(testedFS); 724 root.getChildren(); 725 726 File rootFile = FileUtil.toFile(root); 727 if (rootFile == null) return; 728 (new File (rootFile, "testfile.test")).createNewFile(); 729 root.refresh(); 730 731 fileDataCreatedAssert("createData should fire event fileDataCreated",1); 732 733 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 734 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 735 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 736 fileChangedAssert("fireFileChangedEvent should not be fired ",0); 737 } 738 739 740 public void testFireFileFolderCreatedEvent() { 741 checkSetUp(); 742 root.getChildren(); 743 registerDefaultListener(root); 744 745 FileObject fo; 746 try { 747 fo = root.createFolder("name"); 748 } catch (IOException iex) { 749 fsAssert("createFolder fired IOException. So there was expected fs or fo are read-only", 750 fs.isReadOnly() || root.isReadOnly()); 751 fileFolderCreatedAssert("fs or fo is read-only. So no event should be fired",0); 752 return; 753 } 754 fileFolderCreatedAssert("createFolder should fire event fileFolderCreated",1); 755 fsAssert("createFolder returned null",fo != null); 756 757 fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); 758 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 759 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 760 fileChangedAssert("fireFileChangedEvent should not be fired ",0); 761 } 762 763 764 public void testFireFileFolderCreatedEvent_FS() { 765 checkSetUp(); 766 root.getChildren(); 767 registerDefaultListener(testedFS); 768 769 FileObject fo; 770 try { 771 fo = root.createFolder("name"); 772 } catch (IOException iex) { 773 fsAssert("createFolder fired IOException. So there was expected fs or fo are read-only", 774 fs.isReadOnly() || root.isReadOnly()); 775 fileFolderCreatedAssert("fs or fo is read-only. So no event should be fired",0); 776 return; 777 } 778 fileFolderCreatedAssert("createFolder should fire event fileFolderCreated",1); 779 fsAssert("createFolder returned null",fo != null); 780 781 fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); 782 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 783 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 784 fileChangedAssert("fireFileChangedEvent should not be fired ",0); 785 } 786 787 788 public void testFireFileChangedEvent() throws IOException{ 789 checkSetUp(); 790 FileObject fo = getTestFile1(root); 791 registerDefaultListener(fo); 792 try { 793 writeStr(fo,"Text ..."); 794 } catch (IOException iex) { 795 fsAssert("FileObject could not be modified. So there was expected fs or fo are read-only", 796 fs.isReadOnly() || root.isReadOnly()); 797 fileChangedAssert("fs or fo is read-only. So no event should be fired",0); 798 return; 799 } 800 801 fileChangedAssert("FileObject was modified. fireFileChangedEvent should be fired",1); 802 803 fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); 804 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 805 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 806 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 807 } 808 809 810 public void testFireFileChangedEvent_FS() throws IOException{ 811 checkSetUp(); 812 FileObject fo = getTestFile1(root); 813 registerDefaultListener(testedFS); 814 try { 815 writeStr(fo,"Text ..."); 816 } catch (IOException iex) { 817 fsAssert("FileObject could not be modified. So there was expected fs or fo are read-only", 818 fs.isReadOnly() || root.isReadOnly()); 819 fileChangedAssert("fs or fo is read-only. So no event should be fired",0); 820 return; 821 } 822 823 fileChangedAssert("FileObject was modified. fireFileChangedEvent should be fired",1); 824 825 fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); 826 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 827 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 828 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 829 } 830 831 832 public void testFireFileDeletedEvent() throws IOException { 833 checkSetUp(); 834 FileObject fo1 = getTestFile1(root); 835 FileObject fo2 = getTestFile2(root); 836 837 registerDefaultListener(root); 838 registerDefaultListener(fo1); 839 registerDefaultListener(fo2); 840 841 FileLock lock1 = null; 842 FileLock lock2 = null; 843 try { 844 lock1 = fo1.lock(); 845 lock2 = fo2.lock(); 846 fo1.delete(lock1); 847 fileDeletedAssert("FileObject was deleted. fireFileDeletedEvent should be fired",2); 848 fo2.delete(lock2); 849 fileDeletedAssert("FileObject was deleted. fireFileDeletedEvent should be fired",4); 850 } catch (IOException iex) { 851 fsAssert("FileObject could not be deleted. So there was expected fs or fo are read-only", 852 fs.isReadOnly() || root.isReadOnly()); 853 fileDeletedAssert("fs or fo is read-only. So no event should be fired",0); 854 return; 855 } finally { 856 if (lock1 != null) lock1.releaseLock(); 857 if (lock2 != null) lock2.releaseLock(); 858 } 859 860 fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); 861 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 862 fileRenamedAssert("fireFileRenamedEvent should not be fired ",0); 863 fileChangedAssert("fireFileChangedEvent should not be fired ",0); 864 865 fsAssert("FileObject should be invalid after delete",!fo1.isValid()); 866 fsAssert("FileObject should be invalid after delete",!fo2.isValid()); 867 } 868 869 public void testFireFileDeletedEvent2() throws IOException { 870 checkSetUp(); 871 FileObject fo1 = getTestFolder1(root); 872 FileObject fo2 = getTestFolder1(fo1); 873 FileObject fo3 = getTestFile1(fo2); 874 875 registerDefaultListener(fo3); 876 FileLock lock1 = null; 877 try { 878 lock1 = fo1.lock(); 879 fo1.delete(lock1); 880 fileDeletedAssert("FileObject was deleted. fireFileDeletedEvent should be fired",1); 881 fsAssert("FileObject should be invalid after delete",!fo1.isValid()); 882 fsAssert("FileObject should be invalid after delete",!fo2.isValid()); 883 fsAssert("FileObject should be invalid after delete",!fo3.isValid()); 884 885 } catch (IOException iex) { 886 fsAssert("FileObject could not be deleted. So there was expected fs or fo are read-only", 887 fs.isReadOnly() || root.isReadOnly()); 888 fileDeletedAssert("fs or fo is read-only. So no event should be fired",0); 889 } finally { 890 if (lock1 != null) lock1.releaseLock(); 891 } 892 893 } 894 895 896 897 public void testFireFileAttributeChangedEvent() { 898 checkSetUp(); 899 FileObject fo1 = getTestFile1(root); 900 String value = "value"; 901 902 registerDefaultListener(fo1); 903 try { 904 fo1.setAttribute("attrName",value); 905 fileAttributeChangedAssert("",1); 906 fsAssert("",((String )fo1.getAttribute("attrName")).equals(value)); 907 } catch (IOException iex) { 908 fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", 909 fs.isReadOnly() || root.isReadOnly()); 910 fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); 911 return; 912 } 913 } 914 915 916 public void testFireFileAttributeChangedEvent_FS() { 917 checkSetUp(); 918 FileObject fo1 = getTestFile1(root); 919 String value = "value"; 920 921 registerDefaultListener(testedFS); 922 try { 923 fo1.setAttribute("attrName",value); 924 fileAttributeChangedAssert("",1); 925 fsAssert("",((String )fo1.getAttribute("attrName")).equals(value)); 926 } catch (IOException iex) { 927 fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", 928 fs.isReadOnly() || root.isReadOnly()); 929 fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); 930 return; 931 } 932 } 933 934 935 public void testFireFileRenamedEvent() { 936 checkSetUp(); 937 FileObject fo = getTestFile1(root); 938 registerDefaultListener(fo); 939 FileLock lock = null; 940 941 try { 942 lock = fo.lock(); 943 fo.rename(lock,fo.getName()+"X",fo.getExt()+"X"); 944 } catch (IOException iex) { 945 fsAssert("FileObject could not be renamed. So there was expected fs or fo are read-only", 946 fs.isReadOnly() || root.isReadOnly()); 947 fileRenamedAssert("fs or fo is read-only. So no event should be fired",0); 948 return; 949 } finally { 950 if (lock != null) lock.releaseLock(); 951 } 952 953 fileRenamedAssert("",1); 954 fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); 955 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 956 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 957 } 958 959 960 public void testFireFileRenamedEvent_FS() { 961 checkSetUp(); 962 FileObject fo = getTestFile1(root); 963 registerDefaultListener(testedFS); 964 FileLock lock = null; 965 966 try { 967 lock = fo.lock(); 968 fo.rename(lock,fo.getName()+"X",fo.getExt()+"X"); 969 } catch (IOException iex) { 970 fsAssert("FileObject could not be renamed. So there was expected fs or fo are read-only", 971 fs.isReadOnly() || root.isReadOnly()); 972 fileRenamedAssert("fs or fo is read-only. So no event should be fired",0); 973 return; 974 } finally { 975 if (lock != null) lock.releaseLock(); 976 } 977 978 fileRenamedAssert("",1); 979 fileDataCreatedAssert("fireFileDataCreatedEvent should not be fired ",0); 980 fileFolderCreatedAssert("fireFolderDataCreatedEvent should not be fired ",0); 981 fileDeletedAssert("fireFileDeletedEvent should not be fired ",0); 982 } 983 984 985 public void testGetMIMEType() { 986 checkSetUp(); 987 FileObject fo = getTestFile1(root); 988 String mimeType = "mimeType"; 989 990 FileUtil.setMIMEType(fo.getExt(),mimeType); 991 String actualMT = fo.getMIMEType(); 992 fsAssert("mimeType for this fo was registered; was really " + actualMT, actualMT.equals(mimeType)); 993 } 994 995 1077 public void testDefaultMimeTypeForBinaryFiles () throws Exception { 1078 checkSetUp(); 1079 FileObject fo; 1080 try { 1081 fo = FileUtil.createData(root, "file.jarda"); } catch (IOException iex) { 1083 fsAssert( 1084 "Does not seem to be writeable. So there was expected.", 1085 !root.canWrite () && fs.isReadOnly() 1086 ); 1087 return; 1088 } 1089 FileLock lock = fo.lock(); 1090 1091 InputStream is = getClass ().getResourceAsStream("FileObjectTestHid.class"); 1092 OutputStream os = fo.getOutputStream (lock); 1093 FileUtil.copy (is, os); 1094 is.close (); 1095 os.close (); 1096 lock.releaseLock (); 1097 1098 assertEquals ("File is recognized as unknown", "content/unknown", fo.getMIMEType()); 1099 } 1100 1101 1103 public void testFolderMimeTypeIsUnknown () throws Exception { 1104 checkSetUp(); 1105 FileObject fo; 1106 try { 1107 fo = FileUtil.createFolder (root, "SomeStrangeFolder"); 1108 } catch (IOException iex) { 1109 fsAssert( 1110 "Does not seem to be writeable. So there was expected.", 1111 !root.canWrite () && fs.isReadOnly() 1112 ); 1113 return; 1114 } 1115 1116 assertEquals ("File is recognized as unknown", "content/unknown", fo.getMIMEType()); 1117 } 1118 1119 1146 1147 1148 public void testGetChildren() { 1149 checkSetUp(); 1150 FileObject[] childs = root.getChildren(); 1151 FileObject fo1 = getTestFile1(root); 1152 FileObject fo2 = getTestFile2(root); 1153 FileObject fold1 = getTestFolder1(root); 1154 FileObject fold2 = getTestFolder2(root); 1155 1156 1157 fsAssert("Expected 2 folders and 2 files under root",childs.length >= 4); 1158 fsAssert(fo1.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fo1) ); 1159 fsAssert(fo2.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fo2) ); 1160 fsAssert(fold1.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fold1)); 1161 fsAssert(fold2.getNameExt() + " should be child of root" ,Arrays.asList(childs).contains(fold2)); 1162 } 1163 1164 1165 public void testGetFolders() { 1166 checkSetUp(); 1167 FileObject fold1 = getTestFolder1(root); 1168 FileObject fold2 = getTestFolder2(root); 1169 1170 List list= makeList(root.getFolders(false)); 1171 fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(fold1)); 1172 fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(fold2)); 1173 } 1174 1175 1176 public void testGetFolders1() { 1177 checkSetUp(); 1178 1179 List childs = makeList(root.getChildren(false)); 1180 List folders = makeList(root.getFolders(false)); 1181 List datas = makeList(root.getData(false)); 1182 1183 fsAssert("Expected that numbre of children equals number of folders + number of datas ", 1184 childs.size() == (folders.size() + datas.size())); 1185 } 1186 1187 1188 public void testGetFolders2() { 1189 checkSetUp(); 1190 1191 List childs = makeList(root.getChildren(true)); 1192 List folders = makeList(root.getFolders(true)); 1193 List datas = makeList(root.getData(true)); 1194 1195 fsAssert("Expected that numbre of children equals number of folders + number of datas ", 1196 childs.size() == (folders.size() + datas.size())); 1197 } 1198 1199 1200 public void testGetFolders3() { 1201 checkSetUp(); 1202 1203 List folders = makeList(root.getFolders(true)); 1204 Iterator it = folders.iterator(); 1205 1206 while (it.hasNext()) { 1207 fsAssert("getData should return FileObjects that return isFolder () == true and isData () == false", 1208 ((FileObject)it.next()).isFolder() && !((FileObject)it.next()).isData()); 1209 } 1210 } 1211 1212 1213 public void testGetData() { 1214 checkSetUp(); 1215 FileObject file1 = getTestFile1(root); 1216 FileObject file2 = getTestFile2(root); 1217 1218 List list= makeList(root.getData(false)); 1219 fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(file1)); 1220 fsAssert("Expected that enumeration will include FileObjects according to TestSetup",list.contains(file2)); 1221 } 1222 1223 1224 public void testGetData1() { 1225 checkSetUp(); 1226 1227 List folders = makeList(root.getData(true)); 1228 Iterator it = folders.iterator(); 1229 1230 while (it.hasNext()) { 1231 fsAssert("getData should return FileObjects that return isFolder () == false and isData () == true", 1232 !((FileObject)it.next()).isFolder() && ((FileObject)it.next()).isData()); 1233 } 1234 } 1235 1236 1237 public void testNbfsTransformation () throws FileStateInvalidException{ 1238 checkSetUp(); 1239 String sysName = fs.getSystemName(); 1241 if (sysName == null || sysName.length() == 0) 1242 return; 1243 if (Repository.getDefault().findFileSystem(fs.getSystemName()) == null) 1244 Repository.getDefault().addFileSystem(fs); 1245 1246 try { 1247 FileObject file1 = getTestFile1(root); 1248 URL u = FileURL.encodeFileObject(file1); 1249 FileObject file2 = FileURL.decodeURL(u); 1250 fsAssert("Nbfs check: both files should be equal: " + file1 + " | " + file2 + " url: " + u + " fs: " + fs.getSystemName(), file1 == file2); 1251 } finally { 1252 Repository.getDefault().removeFileSystem(fs); 1253 } 1254 } 1255 1256 public void testNbfsTransformation2() throws FileStateInvalidException, IOException, SAXException, ParserConfigurationException { 1257 checkSetUp(); 1258 if (fs.isReadOnly() || root.isReadOnly()) return; 1260 String sysName = fs.getSystemName(); 1261 if (sysName == null || sysName.length() == 0) 1262 return; 1263 if (Repository.getDefault().findFileSystem(fs.getSystemName()) == null) 1264 Repository.getDefault().addFileSystem(fs); 1265 1266 1267 InputStream inputStream = null; 1268 try { 1269 FileObject test = getTestFolder1(root); 1270 FileObject f = test.createData("layer.xml"); 1271 createSimpleXML(f); 1272 SAXParserFactory pFactory = SAXParserFactory.newInstance(); 1273 pFactory.setValidating (false); 1274 Parser p = pFactory.newSAXParser().getParser(); 1275 p.setDocumentHandler(new HandlerBase()); 1276 URL u = f.getURL(); 1277 p.parse(u.toExternalForm()); 1278 byte[] b = new byte[10]; 1280 inputStream = u.openConnection().getInputStream(); 1281 inputStream.read(b); 1282 fsAssert("Nbfs check: unexpected content ", (new String (b)).startsWith("<?xml")); 1283 } finally { 1284 Repository.getDefault().removeFileSystem(fs); 1285 if (inputStream != null) inputStream.close(); 1286 } 1287 } 1288 1289 private void createSimpleXML(FileObject f) throws IOException { 1290 FileLock fLock = f.lock(); 1291 OutputStream os = f.getOutputStream(fLock); 1292 try { 1293 StringBuffer sb = new StringBuffer (); 1294 sb.append("<?xml version=\"1.0\"?>"); 1295 sb.append("<filesystem>"); 1296 sb.append("</filesystem>"); 1297 os.write(sb.toString().getBytes()); 1298 os.close(); 1299 fLock.releaseLock(); 1300 } finally { 1301 if (os != null) os.close(); 1302 } 1303 } 1304 1305 public void testSetAttrWithNullValue () throws IOException { 1306 checkSetUp(); 1307 if (fs instanceof MultiFileSystem) return; 1310 FileObject file1 = getTestFile1(root); 1311 File f1 = getNbAttrs(file1); 1312 1313 if (f1 != null) { 1314 file1.setAttribute("key", "value"); 1315 file1.setAttribute("key", null); 1316 1317 fsAssert(".nbattrs shouldn't exist: " + f1.getAbsolutePath(), !f1.exists() ); 1318 } 1319 } 1320 1321 private File getNbAttrs(FileObject file1) { 1322 File f1 = FileUtil.toFile(file1); 1323 if (f1 == null) return null; 1324 1325 return new File (f1.getParentFile(), ".nbattrs"); 1326 } 1327 1328 1329 public void testGetFileObject() { 1330 checkSetUp(); 1331 FileObject file1 = getTestFile1(root); 1332 FileObject file2 = getTestFile2(root); 1333 FileObject fold1 = getTestFolder1(root); 1334 FileObject fold2 = getTestFolder2(root); 1335 1336 1337 fsAssert("Result doesn`t correspond to TestSetup",root.getFileObject(file1.getName(),file1.getExt()).equals(file1)); 1338 fsAssert("Result doesn`t correspond to TestSetup",root.getFileObject(file2.getName(),file2.getExt()).equals(file2)); 1339 fsAssert("Result doesn`t correspond to TestSetup",root.getFileObject(fold1.getName()).equals(fold1)); 1340 fsAssert("Result doesn`t correspond to TestSetup",root.getFileObject(fold2.getName()).equals(fold2)); 1341 } 1342 1343 public void testGetFileObject2() { 1344 checkSetUp(); 1345 FileObject fold1 = getTestFolder1(root); 1346 FileObject fold2 = getTestFolder1(fold1); 1347 1348 FileObject file1 = getTestFile1(fold2); 1349 FileObject file2 = getTestFile2(fold2); 1350 1351 1352 assertEquals("",FileUtil.getRelativePath(root, root)); 1353 assertEquals(null,FileUtil.getRelativePath(file1, root)); 1354 1355 assertEquals(file1, root.getFileObject(FileUtil.getRelativePath(root, file1))); 1356 assertEquals(file2, root.getFileObject(FileUtil.getRelativePath(root, file2))); 1357 1358 assertEquals(file1, fold1.getFileObject(FileUtil.getRelativePath(fold1, file1))); 1359 assertEquals(file2, fold1.getFileObject(FileUtil.getRelativePath(fold1, file2))); 1360 1361 assertEquals(file1, fold2.getFileObject(FileUtil.getRelativePath(fold2, file1))); 1362 assertEquals(file2, fold2.getFileObject(FileUtil.getRelativePath(fold2, file2))); 1363 } 1364 1365 1367 public void testGetFileSystem() throws FileStateInvalidException { 1368 checkSetUp(); 1369 1370 fsAssert ("FileObject should return his FileSystem", 1371 root.getFileSystem().equals(fs)); 1372 fsAssert ("FileObject should return his FileSystem", 1373 getTestFolder1 (root).getFileSystem().equals(fs)); 1374 fsAssert ("FileObject should return his FileSystem", 1375 getTestFile1 (root).getFileSystem().equals(fs)); 1376 } 1377 1378 1379 public void testGetParent() { 1380 checkSetUp(); 1381 1382 FileObject folder1 = getTestFolder1 (root); 1383 FileObject file1 = getTestFile1(folder1); 1384 fsAssert ("Unexpected parent",file1.getParent().equals(folder1)); 1385 fsAssert ("Unexpected parent",folder1.getParent().equals(root)); 1386 1387 if (root == fs.getRoot()) 1388 fsAssert ("Parent of parent should be null",root.getParent() == null); 1389 } 1390 1391 1392 1393 public void testIsFolder() { 1394 checkSetUp(); 1395 1396 FileObject folder1 = getTestFolder1 (root); 1397 FileObject folder2 = getTestFolder2 (root); 1398 FileObject file1 = getTestFile1 (root); 1399 FileObject file2 = getTestFile2 (root); 1400 1401 fsAssert ("Expected to be folder",folder1.isFolder()); 1402 fsAssert ("Expected to be folder",folder2.isFolder()); 1403 fsAssert ("Expected not to be folder",!file1.isFolder()); 1404 fsAssert ("Expected not to be folder",!file2.isFolder()); 1405 } 1406 1407 1408 1410 1411 1412 public void testIsRoot() { 1413 if (fs.getRoot() != root) return; 1414 checkSetUp(); 1415 FileObject testedRoot = root; 1416 FileObject folder1 = getTestFolder1 (testedRoot); 1417 FileObject folder2 = getTestFolder2 (testedRoot); 1418 FileObject file1 = getTestFile1 (testedRoot); 1419 FileObject file2 = getTestFile2 (testedRoot); 1420 1421 fsAssert ("Expected to be root",testedRoot.isRoot()); 1422 fsAssert ("Expected not to be root",!folder1.isRoot()); 1423 fsAssert ("Expected not to be root",!folder2.isRoot()); 1424 fsAssert ("Expected not to be root",!file1.isRoot()); 1425 fsAssert ("Expected not to be root",!file2.isRoot()); 1426 } 1427 1428 1429 public void testIsData() { 1430 checkSetUp(); 1431 1432 FileObject folder1 = getTestFolder1 (root); 1433 FileObject folder2 = getTestFolder2 (root); 1434 FileObject file1 = getTestFile1 (root); 1435 FileObject file2 = getTestFile2 (root); 1436 1437 fsAssert ("Expected not to be data",!root.isData()); 1438 fsAssert ("Expected not to be data",!folder1.isData()); 1439 fsAssert ("Expected not to be data",!folder2.isData()); 1440 fsAssert ("Expected to be data",file1.isData()); 1441 fsAssert ("Expected to be data",file2.isData()); 1442 } 1443 1444 1445 1446 public void testIsValid2 () { 1447 checkSetUp(); 1448 1449 FileObject folder1 = getTestFolder1 (root); 1450 FileObject folder2 = getTestFolder2 (root); 1451 FileObject file1 = getTestFile1 (root); 1452 FileObject file2 = getTestFile2 (root); 1453 1454 fsAssert ("Expected to be valid",root.isValid()); 1455 fsAssert ("Expected to be valid",folder1.isValid()); 1456 fsAssert ("Expected to be valid",folder2.isValid()); 1457 fsAssert ("Expected be valid",file1.isValid()); 1458 fsAssert ("Expected be valid",file2.isValid()); 1459 1460 FileLock lock1 = null; 1461 FileLock lock2 = null; 1462 FileLock lockf1 = null; 1463 FileLock lockf2 = null; 1464 try { 1465 lock1 = folder1.lock(); 1466 lock2 = folder2.lock(); 1467 lockf1 = file1.lock(); 1468 lockf2 = file2.lock(); 1469 1470 folder1.delete (lock1); 1471 folder2.delete (lock2); 1472 file1.delete (lockf1); 1473 file2.delete (lockf2); 1474 1475 fsAssert ("Expected not to be valid",!folder1.isValid()); 1476 fsAssert ("Expected not to be valid",!folder2.isValid()); 1477 fsAssert ("Expected not to be valid",!file1.isValid()); 1478 fsAssert ("Expected not to be valid",!file2.isValid()); 1479 1480 1481 } catch (IOException iex) { 1482 fsAssert("FileObject could not be deleted. So there was expected fs or fo are read-only", 1483 fs.isReadOnly() || root.isReadOnly()); 1484 return; 1485 1486 } finally { 1487 if (lock1 != null) lock1.releaseLock(); 1488 if (lock2 != null) lock2.releaseLock(); 1489 if (lockf1 != null) lockf1.releaseLock(); 1490 if (lockf2 != null) lockf2.releaseLock(); 1491 } 1492 } 1493 1494 1495 public void testDelete() throws IOException { 1496 checkSetUp(); 1497 1498 FileObject folder1 = getTestFolder1 (root); 1499 FileObject file1 = getTestFile1 (root); 1500 1501 1502 FileLock lock1 = null; 1503 FileLock lockf1 = null; 1504 1505 1506 try { 1507 lock1 = folder1.lock(); 1508 lockf1 = file1.lock(); 1509 1510 folder1.delete (lock1); 1511 file1.delete (lockf1); 1512 1513 } catch (IOException iex) { 1514 fsAssert("FileObject could not be deleted. So there was expected fs or fo are read-only", 1515 fs.isReadOnly() || root.isReadOnly()); 1516 return; 1517 1518 } finally { 1519 if (lock1 != null) lock1.releaseLock(); 1520 if (lockf1 != null) lockf1.releaseLock(); 1521 } 1522 1523 fsAssert ("Expected not to be valid",!folder1.isValid()); 1524 fsAssert ("Expected not to be valid",!file1.isValid()); 1525 1526 1527 try { 1528 lock1 = folder1.lock(); 1529 lockf1 = file1.lock(); 1530 1531 folder1.delete (lock1); 1532 file1.delete (lockf1); 1533 1534 } catch (IOException iex) { 1535 return; 1536 1537 } finally { 1538 if (lock1 != null) lock1.releaseLock(); 1539 if (lockf1 != null) lockf1.releaseLock(); 1540 } 1541 } 1542 1543 public void testDelete2() throws IOException { 1544 checkSetUp(); 1545 1546 FileObject folder = getTestFolder1 (root); 1547 1548 try { 1549 FileObject testFo = FileUtil.createFolder(folder,"testDelete2/subTest2"); 1550 assertNotNull(testFo); 1551 1552 FileObject newFolder = testFo.createFolder("subTest"); 1553 assertNotNull(newFolder); 1554 assertEquals(1,testFo.getChildren().length); 1555 1556 newFolder.delete(); 1557 assertFalse(newFolder.isValid()); 1558 assertEquals(0,testFo.getChildren().length); 1559 implOfTestGetFileObjectForSubversion(testFo, newFolder.getNameExt()); 1560 } catch (IOException iex) { 1561 fsAssert("FileObject could not be deleted. So there was expected fs or fo are read-only", 1562 fs.isReadOnly() || root.isReadOnly()); 1563 return; 1564 } 1565 1566 } 1567 1568 public static void implOfTestGetFileObjectForSubversion(final FileObject folder, final String childName) { 1569 final List l = new ArrayList(); 1570 folder.addFileChangeListener(new FileChangeAdapter(){ 1571 public void fileFolderCreated(FileEvent fe) { 1572 l.add(fe.getFile()); 1573 } 1574 }); 1575 FileObject child = folder.getFileObject(childName); 1576 if (l.size() == 0) { 1577 assertNull(child); 1578 } else { 1579 assertNotNull(child); 1580 } 1581 } 1582 1583 1584 public void testCreateDeleteFolderCreate () throws IOException { 1585 checkSetUp(); 1586 1587 FileObject folder1 = getTestFolder1 (root); 1588 FileObject file1 = getTestFile1 (root); 1589 1590 1591 1592 try { 1593 FileObject obj = FileUtil.createData (folder1, "my/sub/children/children.java"); 1594 FileObject fo = folder1.getFileObject ("my"); 1595 1596 assertNotNull (fo); 1597 assertTrue (fo.isValid ()); 1598 assertTrue (fo.isFolder ()); 1599 assertTrue (obj.isValid ()); 1600 1601 fo.delete (); 1602 1603 assertFalse ("Not valid anymore", fo.isValid ()); 1604 assertFalse ("Neither the data file", obj.isValid ()); 1605 1606 FileObject newObj = FileUtil.createData (folder1, "my/sub/children/children.java"); 1607 1608 assertTrue ("old data file is not valid", !obj.isValid () || obj == newObj); 1609 assertTrue ("New one is ", newObj.isValid ()); 1610 assertEquals ("They have the same name", newObj.getPath (), obj.getPath ()); 1611 1612 } catch (IOException iex) { 1613 if (fs.isReadOnly() || root.isReadOnly()) return; 1614 throw iex; 1615 } finally { 1616 } 1617 } 1618 1619 1620 public void testGetAttribute() { 1621 checkSetUp(); 1622 1623 FileObject file1 = getTestFile1 (root); 1624 FileObject file2 = getTestFile2 (root); 1625 1626 fsAssert ("If attributes are not set getAttribute should return null", 1627 file1.getAttribute("UnexpectedName") == null); 1628 fsAssert ("If attributes are not set getAttribute should return null", 1629 file2.getAttribute("UnexpectedName") == null); 1630 try { 1631 file1.setAttribute("attrName","attrName"); 1632 String value = (String )file1.getAttribute("attrName"); 1633 fsAssert ("setAttibute or getAttribute failure: " + value,value != null); 1634 fsAssert ("setAttibute or getAttribute failure: " + value,value.equals("attrName")); 1635 } catch (IOException iex) { 1636 fsAssert("Attributes could not be attached to FileObject. So there was expected fs or fo are read-only", 1637 fs.isReadOnly() || root.isReadOnly()); 1638 } 1639 1640 } 1641 1642 1643 public void testSetAttribute() throws IOException { 1644 checkSetUp(); 1645 1646 FileObject file1 = getTestFile1 (root); 1647 FileObject file2 = getTestFile2 (root); 1648 1649 try { 1650 file1.setAttribute("attrName","value"); 1651 file2.setAttribute("attrName","value"); 1652 fsAssert ("setAttibute or getAttribute failure", 1653 file1.getAttribute("attrName").equals(file2.getAttribute("attrName"))); 1654 } catch (IOException iex) { 1655 fsAssert("Attributes could not be attached to FileObject. So there was expected fs or fo are read-only", 1656 fs.isReadOnly() || root.isReadOnly()); 1657 } 1658 1659 } 1660 1661 1662 public void testGetAttributes() { 1663 checkSetUp(); 1664 String [] names = new String [] {"name1","name2","name3","name4"}; 1665 List namesList = Arrays.asList(names); 1666 List<String > compareList = new ArrayList<String > (); 1667 FileObject file1 = getTestFile1 (root); 1668 1669 try { 1670 for (int i = 0; i < names.length; i++) 1671 file1.setAttribute(names[i],"value"); 1672 1673 Enumeration en = file1.getAttributes(); 1674 while (en.hasMoreElements()) { 1675 String name = (String )en.nextElement(); 1676 fsAssert ("Expected getAttributes return this key: "+ name,namesList.contains(name)); 1677 compareList.add (name); 1678 } 1679 fsAssert ("All keys should be enumerated: " + namesList.size() +"|"+compareList.size(),namesList.size() == compareList.size()); 1680 } catch (IOException iex) { 1681 fsAssert("Attributes could not be attached to FileObject. So there was expected fs or fo are read-only", 1682 fs.isReadOnly() || root.isReadOnly()); 1683 } 1684 } 1685 1686 1687 public void testCreateFolder() throws IOException { 1688 checkSetUp(); 1689 1690 FileObject fo = null; 1691 try { 1692 fo = root.createFolder("Folder"); 1693 } catch (IOException iex) { 1694 fsAssert ("Writable fs should allow to create new folder",fs.isReadOnly() || root.isReadOnly()); 1695 return; 1696 } 1697 1698 fsAssert ("Writable fs should allow to create new folder",fo != null); 1699 1700 try { 1701 root.createFolder("Folder"); 1702 } catch (IOException iex) { 1703 return; } 1705 1706 fsFail ("createData should fire exception if file already exists"); 1707 } 1708 1709 1710 public void testCreateFolder2() throws IOException { 1711 checkSetUp(); 1712 1713 FileObject fo = null; 1714 try { 1715 fo = root.createFolder("Folder.With.Dot"); 1716 } catch (IOException iex) { 1717 fsAssert ("Writable fs should allow to create new folder",fs.isReadOnly() || root.isReadOnly()); 1718 return; 1719 } 1720 1721 fsAssert ("Writable fs should allow to create new folder",fo != null); 1722 } 1723 1724 1725 public void testCreateData () throws IOException { 1726 checkSetUp(); 1727 1728 FileObject fo = null; 1729 try { 1730 fo = root.createData("Data","tst"); 1731 } catch (IOException iex) { 1732 fsAssert ("Writable fs should allow to create new data",fs.isReadOnly() || root.isReadOnly()); 1733 return; 1734 } 1735 1736 fsAssert ("Writable fs should allow to create new data",fo != null); 1737 1738 try { 1739 root.createData("Data","tst"); 1740 } catch (IOException iex) { 1741 return; } 1743 1744 fsFail ("createData should fire exception if file already exists"); 1745 } 1746 1747 1748 public void testCreateData2 () throws IOException { 1749 checkSetUp(); 1750 1751 FileObject fo = null; 1752 try { 1753 fo = root.createData("Data.With.Dot","tst"); 1754 } catch (IOException iex) { 1755 fsAssert ("Writable fs should allow to create new data",fs.isReadOnly() || root.isReadOnly()); 1756 return; 1757 } 1758 1759 fsAssert ("Writable fs should allow to create new data",fo != null); 1760 1761 } 1762 1763 1764 public void testGetSize() { 1765 checkSetUp(); 1766 1767 String testStr = "Content of file. Size of this file is important"; 1768 FileObject fo1 = getTestFile1 (root); 1769 1770 try { 1771 writeStr(fo1, testStr); 1772 } catch (IOException iex) { 1773 fsAssert("File was modified", 1774 fs.isReadOnly() || fo1.isReadOnly()) ; 1775 return; 1776 } 1777 fsAssert ("Unexpected size of file",fo1.getSize() == testStr.length()); 1778 } 1779 1780 1781 public void testGetSize1() { 1782 checkSetUp(); 1783 FileObject fo1 = getTestFile1 (root); 1784 1785 fsAssert ("Size of file should be >= 0",fo1.getSize() >= 0); 1786 } 1787 1788 1789 public void testGetInputStream() throws java.io.FileNotFoundException , IOException { 1790 checkSetUp(); 1791 1792 FileObject fo1 = getTestFile1 (root); 1793 InputStream is = null; 1794 FileLock lock = null; 1795 try { 1796 is = fo1.getInputStream(); 1797 } catch (IOException iex) { 1798 fsAssert ("Expected that FS provides InputStream ",fo1.getSize () == 0); 1799 } finally { 1800 if (is != null) is.close(); 1801 } 1802 1803 try { 1804 lock = fo1.lock (); 1805 fo1.delete(lock); 1806 } catch (IOException iex) { 1807 fsAssert("FileObject shopuld be allowd to be modified.", 1808 fs.isReadOnly() || fo1.isReadOnly()) ; 1809 return; 1810 } finally { 1811 if (lock != null) lock.releaseLock(); 1812 1813 } 1814 1815 fsAssert ("After delete should be invalid",!fo1.isValid()); 1816 } 1817 1818 1819 public void testGetOutputStream() throws java.io.IOException { 1820 checkSetUp(); 1821 1822 FileObject fo1 = getTestFile1 (root); 1823 OutputStream os = null; 1824 FileLock lock = null; 1825 1826 try { 1827 lock = fo1.lock (); 1828 os = fo1.getOutputStream(lock); 1829 os.close(); 1830 os = null; 1831 fo1.delete(lock); 1832 } catch (IOException iex) { 1833 fsAssert("FileObject should be allowed to be modified.", 1834 fs.isReadOnly() || fo1.isReadOnly()) ; 1835 return; 1836 } finally { 1837 if (lock != null) lock.releaseLock(); 1838 if (os != null) os.close(); 1839 } 1840 1841 fsAssert ("After should be invalid",!fo1.isValid()); 1842 1843 } 1844 1845 public void testGetOutputStream1() throws java.io.IOException { 1846 checkSetUp(); 1847 1848 FileObject fo1 = getTestFile1 (root); 1849 registerDefaultListener(fo1.getParent()); 1850 OutputStream os = null; 1851 FileLock lock = null; 1852 1853 try { 1854 lock = fo1.lock (); 1855 os = fo1.getOutputStream(lock); 1856 fileChangedAssert("No event should be fired",0); 1857 os.write(new byte[] {'a','b'}); 1858 os.close (); 1859 os = null; 1860 fileChangedAssert("Only one event should be fired",1); 1861 } catch (IOException iex) { 1862 fsAssert("FileObject should be allowed to be modified.", 1863 fs.isReadOnly() || fo1.isReadOnly()) ; 1864 return; 1865 } finally { 1866 if (lock != null) lock.releaseLock(); 1867 if (os != null) os.close(); 1868 } 1869 } 1870 1871 public void testGetOutputStream1_FS() throws java.io.IOException { 1872 checkSetUp(); 1873 FileObject fo1 = getTestFile1 (root); 1874 registerDefaultListener(testedFS); 1875 OutputStream os = null; 1876 FileLock lock = null; 1877 1878 try { 1879 lock = fo1.lock (); 1880 os = fo1.getOutputStream(lock); 1881 fileChangedAssert("No event should be fired",0); 1882 os.write("alkdsakldsaklafdsaklfalkfaklfalkf".getBytes()); 1883 os.close (); 1884 os = null; 1885 fileChangedAssert("Only one event should be fired",1); 1886 fo1.refresh(false); 1887 fileChangedAssert("Unexpected event",1); 1888 } catch (IOException iex) { 1889 fsAssert("FileObject should be allowed to be modified.", 1890 fs.isReadOnly() || fo1.isReadOnly()) ; 1891 return; 1892 } finally { 1893 if (lock != null) lock.releaseLock(); 1894 if (os != null) os.close(); 1895 } 1896 } 1897 1898 public void testOutputStream75826() throws IOException { 1899 checkSetUp(); 1900 if (testedFS.isReadOnly()) return; 1901 FileObject testFo = getTestFile1(root); 1902 FileLock lock = testFo.lock(); 1903 try { 1904 testFo.getOutputStream(); 1905 fail(); 1906 } catch (IOException ex) { 1907 }finally { 1908 lock.releaseLock(); 1909 } 1910 } 1911 1912 1913 public void testIsReadOnly() { 1914 FileObject file1 = getTestFile1 (root); 1915 FileObject file2 = getTestFile2 (root); 1916 1917 fsAssert ("Expected that if fs is read-only than all files are read-only",fs.isReadOnly() == file1.isReadOnly()); 1918 fsAssert ("Expected that if fs is read-only than all files are read-only",fs.isReadOnly() == file2.isReadOnly()); 1919 1920 } 1921 1922 1923 1924 public void testLock() throws IOException { 1925 checkSetUp(); 1926 1927 FileObject fo1 = getTestFile1 (root); 1928 FileLock lock = null; 1929 try { 1930 lock = fo1.lock (); 1931 } catch (IOException iex) { 1932 fsAssert("FileObject could not be locked", 1933 fs.isReadOnly() || fo1.isReadOnly() ) ; 1934 return; 1935 } 1936 try { 1937 fo1.lock (); 1938 fsFail ("FileAlreadyLockedException should be fired"); 1939 } catch (FileAlreadyLockedException fax) { 1940 return; 1941 } finally { 1942 if (lock != null) lock.releaseLock(); 1943 } 1944 } 1945 1946 public void testUseWrongLock() throws IOException { 1947 if (!root.canWrite() || root.getFileSystem().isReadOnly()) { 1948 return; 1949 } 1950 checkSetUp(); 1951 1952 FileObject fo1 = getTestFile1 (root); 1953 FileObject fo2 = getTestFile2 (root); 1954 FileObject target = getTestFolder1(root); 1955 1956 FileLock lock = null; 1957 try { 1958 lock = fo1.lock(); 1959 try { 1960 fo2.delete(lock); 1961 fail(); 1962 } catch (IOException ex) {} 1963 try { 1964 fo2.move(lock,target,"by","hi"); 1965 fail(); 1966 } catch (IOException ex) {} 1967 try { 1968 fo2.rename(lock,"hi","by"); 1969 fail(); 1970 } catch (IOException ex) {} 1971 } finally { 1972 if (lock != null) lock.releaseLock(); 1973 } 1974 } 1975 1976 1977 1978 public void testRename() throws IOException { 1979 checkSetUp(); 1980 FileObject fold = getTestFolder1(root); 1981 FileObject fo1 = getTestFile1(fold); 1982 FileObject fo2 = getTestFile2(fold); 1983 1984 String attrName = "attrName"; 1985 String value = "value56"; 1986 FileLock lock = null; 1987 1988 registerDefaultListener(fold); 1989 1990 try { 1991 fo1.setAttribute(attrName,value); 1992 fsAssert("attributes should be saved " + fo1.getAttribute(attrName),value.equals((String )fo1.getAttribute(attrName)) ); 1993 lock = fo1.lock(); 1994 fo1.rename(lock,fo2.getExt(),fo2.getName()); 1995 } catch (IOException iex) { 1996 fsAssert("expected rename will success on writable FS", 1997 fs.isReadOnly() || fo1.isReadOnly()); 1998 return; 1999 } finally { 2000 if (lock != null) lock.releaseLock(); 2001 } 2002 fsAssert("attributes should be available too: " + fo1.getAttribute(attrName),value.equals((String )fo1.getAttribute(attrName)) ); 2003 fsAssert ("",fold.getFileObject(fo1.getName(),fo1.getExt()) != null); 2004 fsAssert ("",fo1.getName().equals(fo2.getExt()) && fo1.getExt().equals(fo2.getName())); 2005 this.fileRenamedAssert("File was actually renamed.",1); 2006 } 2007 2008 2009 2010 public void testRename_FS() throws IOException { 2011 checkSetUp(); 2012 FileObject fold = getTestFolder1(root); 2013 FileObject fo1 = getTestFile1(fold); 2014 FileObject fo2 = getTestFile2(fold); 2015 2016 String attrName = "attrName"; 2017 String value = "value56"; 2018 FileLock lock = null; 2019 2020 registerDefaultListener(testedFS); 2021 2022 try { 2023 fo1.setAttribute(attrName,value); 2024 fsAssert("attributes should be saved " + fo1.getAttribute(attrName),value.equals((String )fo1.getAttribute(attrName)) ); 2025 lock = fo1.lock(); 2026 fo1.rename(lock,fo2.getExt(),fo2.getName()); 2027 } catch (IOException iex) { 2028 fsAssert("expected rename will success on writable FS", 2029 fs.isReadOnly() || fo1.isReadOnly()); 2030 return; 2031 } finally { 2032 if (lock != null) lock.releaseLock(); 2033 } 2034 fsAssert("attributes should be available too: " + fo1.getAttribute(attrName),value.equals((String )fo1.getAttribute(attrName)) ); 2035 fsAssert ("",fold.getFileObject(fo1.getName(),fo1.getExt()) != null); 2036 fsAssert ("",fo1.getName().equals(fo2.getExt()) && fo1.getExt().equals(fo2.getName())); 2037 this.fileRenamedAssert("File was actually renamed.",1); 2038 } 2039 2040 public void testRename2() throws IOException { 2041 checkSetUp(); 2042 FileObject fold = getTestFolder1(root); 2043 FileObject fo1 = getTestFolder1(fold); 2044 2045 String attrName = "attrName"; 2046 String value = "value"; 2047 FileLock lock = null; 2048 2049 registerDefaultListener(fold); 2050 2051 try { 2052 fo1.setAttribute(attrName,value); 2053 fsAssert("attributes should be saved " + fo1.getAttribute(attrName), 2054 value.equals((String )fo1.getAttribute(attrName)) ); 2055 lock = fo1.lock(); 2056 fo1.rename(lock,"testXY",""); 2057 } catch (IOException iex) { 2058 fsAssert("expected rename will success on writable FS",fs.isReadOnly() || fo1.isReadOnly()); 2059 return; 2060 } finally { 2061 if (lock != null) lock.releaseLock(); 2062 } 2063 fsAssert("attributes should be available too: " + fo1.getAttribute(attrName), 2064 value.equals((String )fo1.getAttribute(attrName)) ); 2065 fsAssert ("",fold.getFileObject(fo1.getName(),fo1.getExt()) != null); 2066 fsAssert ("",fo1.getName().equals("testXY") && fo1.getExt().equals("")); 2067 this.fileRenamedAssert("File was actually renamed.",1); 2068 } 2069 2070 public void testRename2_FS() throws IOException { 2071 checkSetUp(); 2072 FileObject fold = getTestFolder1(root); 2073 FileObject fo1 = getTestFolder1(fold); 2074 2075 String attrName = "attrName"; 2076 String value = "value"; 2077 FileLock lock = null; 2078 2079 registerDefaultListener(testedFS); 2080 2081 try { 2082 fo1.setAttribute(attrName,value); 2083 fsAssert("attributes should be saved " + fo1.getAttribute(attrName), 2084 value.equals((String )fo1.getAttribute(attrName)) ); 2085 lock = fo1.lock(); 2086 fo1.rename(lock,"testXY",""); 2087 } catch (IOException iex) { 2088 fsAssert("expected rename will success on writable FS",fs.isReadOnly() || fo1.isReadOnly()); 2089 return; 2090 } finally { 2091 if (lock != null) lock.releaseLock(); 2092 } 2093 fsAssert("attributes should be available too: " + fo1.getAttribute(attrName), 2094 value.equals((String )fo1.getAttribute(attrName)) ); 2095 fsAssert ("",fold.getFileObject(fo1.getName(),fo1.getExt()) != null); 2096 fsAssert ("",fo1.getName().equals("testXY") && fo1.getExt().equals("")); 2097 this.fileRenamedAssert("File was actually renamed.",1); 2098 } 2099 2100 2101 public void testAddFileChangeListener() { 2102 checkSetUp(); 2103 FileObject fo1 = getTestFile1(root); 2104 String value = "value"; 2105 2106 registerDefaultListener(fo1); 2107 registerDefaultListener(fo1); 2108 registerDefaultListener(fo1); 2109 registerDefaultListener(fo1); 2110 registerDefaultListener(fo1); 2111 try { 2112 fo1.setAttribute("attrName",value); 2113 fileAttributeChangedAssert("",5); 2114 fsAssert("",((String )fo1.getAttribute("attrName")).equals(value)); 2115 } catch (IOException iex) { 2116 fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", 2117 fs.isReadOnly() || root.isReadOnly()); 2118 fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); 2119 return; 2120 } 2121 } 2122 2123 2124 public void testAddFileChangeListener_FS() { 2125 checkSetUp(); 2126 FileObject fo1 = getTestFile1(root); 2127 String value = "value"; 2128 2129 registerDefaultListener(testedFS); 2130 registerDefaultListener(testedFS); 2131 registerDefaultListener(testedFS); 2132 registerDefaultListener(testedFS); 2133 registerDefaultListener(testedFS); 2134 try { 2135 fo1.setAttribute("attrName",value); 2136 fileAttributeChangedAssert("",5); 2137 fsAssert("",((String )fo1.getAttribute("attrName")).equals(value)); 2138 } catch (IOException iex) { 2139 fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", 2140 fs.isReadOnly() || root.isReadOnly()); 2141 fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); 2142 return; 2143 } 2144 } 2145 2146 2147 public void testRemoveFileChangeListener() throws IOException { 2148 checkSetUp(); 2149 FileObject fo1 = getTestFile1(root); 2150 String value = "value"; 2151 2152 registerDefaultListener(fo1); 2153 FileChangeListener secondListener = createFileChangeListener (); 2154 fo1.addFileChangeListener(secondListener); 2155 try { 2156 fo1.setAttribute("attrName",value); 2157 fileAttributeChangedAssert("",2); 2158 fsAssert("",((String )fo1.getAttribute("attrName")).equals(value)); 2159 } catch (IOException iex) { 2160 fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", 2161 fs.isReadOnly() || root.isReadOnly()); 2162 fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); 2163 return; 2164 } 2165 2166 fo1.removeFileChangeListener(secondListener); 2167 fo1.setAttribute("attrName","value2"); 2168 fileAttributeChangedAssert("",3); 2169 fsAssert("",((String )fo1.getAttribute("attrName")).equals("value2")); 2170 } 2171 2172 2173 2174 2175 public void testRemoveFileChangeListener_FS() throws IOException { 2176 checkSetUp(); 2177 FileObject fo1 = getTestFile1(root); 2178 String value = "value"; 2179 2180 registerDefaultListener(testedFS); 2181 FileChangeListener secondListener = createFileChangeListener (); 2182 fo1.addFileChangeListener(secondListener); 2183 try { 2184 fo1.setAttribute("attrName",value); 2185 fileAttributeChangedAssert("",2); 2186 fsAssert("",((String )fo1.getAttribute("attrName")).equals(value)); 2187 } catch (IOException iex) { 2188 fsAssert("Attribute could not be set. So there was expected fs or fo are read-only", 2189 fs.isReadOnly() || root.isReadOnly()); 2190 fileAttributeChangedAssert("fs or fo is read-only. So no event should be fired",0); 2191 return; 2192 } 2193 2194 fo1.removeFileChangeListener(secondListener); 2195 fo1.setAttribute("attrName","value2"); 2196 fileAttributeChangedAssert("",3); 2197 fsAssert("",((String )fo1.getAttribute("attrName")).equals("value2")); 2198 } 2199 2200 2201 public void testSetImportant() { 2202 2203 checkSetUp(); 2204 2205 FileObject fo1 = getTestFile1 (root); 2206 fo1.setImportant(true); 2207 fo1.setImportant(false); 2208 } 2209 2210 2211 2212 2213 public void testRefresh() throws IOException{ 2214 checkSetUp(); 2215 FileObject fo = getTestFile1 (root); 2216 File f = FileUtil.toFile(fo); 2217 if (f == null) return; 2218 2219 2220 registerDefaultListener(fo); 2221 if (f.exists()) { 2222 try { 2223 Thread.sleep(2000); 2224 } catch (InterruptedException e) { 2225 return; 2226 } 2227 fsAssert("delete failed",f.delete()); 2228 fsAssert("file can't be created", f.createNewFile()); 2229 } 2230 fo.refresh(); 2231 fileChangedAssert("unexpected count of events", 1); 2232 } 2233 2234 2235 public void testRefresh2 () throws IOException{ 2236 checkSetUp(); 2237 FileObject fo = getTestFile1 (root); 2238 File f = FileUtil.toFile(fo); 2239 if (f == null) return; 2240 fo.getParent().getChildren(); 2241 2242 registerDefaultListener(fo); 2243 if (f.exists()) { 2244 try { 2245 Thread.sleep(2000); 2246 } catch (InterruptedException e) { 2247 return; 2248 } 2249 fsAssert("delete failed",f.delete()); 2250 fsAssert("file can't be created", f.createNewFile()); 2251 } 2252 fo.getParent().refresh(); 2253 fileChangedAssert("unexpected count of events", 1); 2254 } 2255 2256 public void testRefresh3 () throws IOException{ 2257 String assertMessage = "unexpected count of events"; 2258 checkSetUp(); 2259 if (!root.canWrite() || fs.isReadOnly() || fs instanceof MultiFileSystem) { 2260 return; 2261 } 2262 fs.refresh(true); 2263 2264 FileObject[] fos = new FileObject [2]; 2265 fos[0] = getTestFile1 (root); 2266 fos[1] = root.createFolder("toDelFolder"); 2267 assertNotNull(fos[0]); 2268 assertNotNull(fos[1]); 2269 2270 File file = FileUtil.toFile(fos[0]); 2271 File folder = FileUtil.toFile(fos[1]); 2272 2273 if (file == null || folder == null) return; 2275 2276 2277 root.getChildren (); 2278 for (int i = 0; i < 4; i++) { 2279 assertTrue(file.exists()); 2280 assertTrue(folder.exists()); 2281 2282 registerListenerForTestRefresh3(i); 2283 2284 file.delete(); 2285 refreshForTestRefresh3(i); 2286 assertNull(root.getFileObject(file.getName())); 2287 fileDeletedAssert(assertMessage, 1); 2288 2289 folder.delete(); 2290 refreshForTestRefresh3(i); 2291 assertNull(root.getFileObject(folder.getName())); 2292 fileDeletedAssert(assertMessage, 2); 2293 2294 2295 new File(file.getParentFile(), file.getName()).createNewFile(); 2296 refreshForTestRefresh3(i); 2297 assertNotNull(root.getFileObject(file.getName())); 2298 fileDataCreatedAssert(assertMessage + " : " + i, 1); 2299 2300 new File(folder.getParentFile(), folder.getName()).mkdir(); 2301 refreshForTestRefresh3(i); 2302 assertNotNull(root.getFileObject(folder.getName())); 2303 fileFolderCreatedAssert(assertMessage, 1); 2304 } 2305 2306 2307 } 2308 2309 private void refreshForTestRefresh3(int mode) { 2310 switch (mode) { 2311 case 0: 2312 fs.refresh(true); 2313 break; 2314 case 1: 2315 root.refresh(); 2316 break; 2317 case 2: 2318 fs.refresh(true); 2319 break; 2320 case 3: 2321 root.refresh(); 2322 break; 2323 2324 } 2325 2326 } 2327 2328 private void registerListenerForTestRefresh3(int mode) { 2329 switch (mode) { 2330 case 0: 2331 registerDefaultListener(fs); 2332 break; 2333 case 1: 2334 deregisterDefaultListener(fs); 2335 registerDefaultListener(root); 2336 break; 2337 case 2: 2338 deregisterDefaultListener(root); 2339 registerDefaultListener(root); 2340 break; 2341 case 3: 2342 deregisterDefaultListener(root); 2343 registerDefaultListener(fs); 2344 break; 2345 2346 } 2347 } 2348 2349 2350 public void testGetURL() throws Exception { 2351 checkSetUp(); 2352 2353 FileObject fo1 = getTestFile1 (root); 2354 URL url = null; 2355 url = fo1.getURL(); 2356 2357 fsAssert ("Expected valid url",url != null); 2358 2359 FileObject f2 = getTestFile1(root); 2363 FileObject f1 = f2.getParent(); 2364 assertNotNull("had a parent of " + f2, f1); 2365 URL u1 = f1.getURL(); 2366 assertNotNull("had a URL for " + f1, u1); 2367 URI uri1 = new URI(u1.toExternalForm()); 2368 String path1 = uri1.getPath(); 2369 if (path1 != null) { 2370 assertTrue("path of " + uri1 + " ends with /", path1.endsWith("/")); 2371 String path2 = path1 + f2.getNameExt(); 2372 assertNull("No query for " + uri1, uri1.getQuery()); 2373 assertNull("No fragment for " + uri1, uri1.getFragment()); 2374 URI uri2 = new URI(uri1.getScheme(), path2, null); 2375 Repository.getDefault().addFileSystem(fs); FileObject[] fos; 2377 try { 2378 fos = URLMapper.findFileObjects(uri2.toURL()); 2379 } finally { 2380 Repository.getDefault().removeFileSystem(fs); 2381 } 2382 assertTrue("computed child URI " + uri2 + " is correct as is in: " + Arrays.asList(fos), Arrays.asList(fos).contains(f2)); 2383 } else { 2384 } 2387 } 2388 2389 2390 2391 public void testExternalChange () throws Exception { 2392 checkSetUp(); 2393 if (!root.canWrite() || fs.isReadOnly()) { 2394 return; 2395 } 2396 2397 2398 FileObject fo1 = getTestFile1 (root); 2399 registerDefaultListener(fo1); 2400 File f = FileUtil.toFile(fo1); 2401 if (f == null) return; 2402 2403 FileLock lck = fo1.lock(); 2404 lck.releaseLock(); 2405 Thread.sleep(2000); 2406 FileOutputStream fos = new FileOutputStream(f); 2407 fos.close(); 2408 fo1.refresh(); 2409 fileChangedAssert("expected FileChangeListener: ", 1); 2410 } 2411 2412 protected String [] getResources(String testName) { 2413 if (res == null ) { 2414 res = new HashSet(Arrays.asList(resources)); 2415 createResource("",0,3, true); 2416 } 2417 2418 2419 String [] retVal = new String [res.size()]; 2420 res.toArray(retVal); 2421 return retVal; 2422 } 2423 2424 private static void createResource(String prevLevel, int level, int maxLevel, boolean folder) { 2425 if (level < maxLevel && prevLevel.indexOf('.') == -1) { 2426 for (int i = 0; i < 2; i++) { 2427 createResource(prevLevel + FOLDER_CHILD + new Integer (i).toString(),level + 1, maxLevel, true); 2428 createResource(prevLevel + FILE_CHILD + new Integer (i).toString(),level + 1, maxLevel, false); 2429 } 2430 } 2431 2432 if (prevLevel.startsWith("/") && prevLevel.length() != 0) 2434 res.add(folder ? (prevLevel + "/") : prevLevel); 2435 2436 } 2437 2438 2439 2440 private FileObject getTestFile1(FileObject folder) { 2441 return getChild(folder, false, 0, FOLDER_CHILD_NAME, FILE_CHILD_NAME, FILE_CHILD_EXT); 2442 } 2443 2444 private FileObject getTestFolder1(FileObject folder) { 2445 return getChild(folder, true, 0 ,FOLDER_CHILD_NAME, FILE_CHILD_NAME,FILE_CHILD_EXT); 2446 } 2447 2448 private FileObject getTestFile2(FileObject folder) { 2449 return getChild(folder, false, 1, FOLDER_CHILD_NAME, FILE_CHILD_NAME, FILE_CHILD_EXT); 2450 } 2451 2452 private FileObject getTestFolder2(FileObject folder) { 2453 return getChild(folder, true, 1 ,FOLDER_CHILD_NAME, FILE_CHILD_NAME,FILE_CHILD_EXT); 2454 } 2455 2456 2457 private FileObject getChild(FileObject folder, boolean isFolder, int fileNumber, String folderName, String fileName, String fileExt) { 2458 FileObject retVal; 2459 2460 if (isFolder) { 2461 retVal = folder.getFileObject(folderName+new Integer (fileNumber).toString()); 2462 fsTestFrameworkErrorAssert ("Unexpected setUp behaviour: resource " + FOLDER_CHILD_NAME +" not found",retVal != null); 2463 fsTestFrameworkErrorAssert("Not really a folder: " + retVal.getPath(), retVal.isFolder()); 2464 return retVal; 2465 } 2466 2467 retVal = folder.getFileObject(fileName,fileExt+new Integer (fileNumber).toString()); 2468 fsTestFrameworkErrorAssert ("Unexpected setUp behaviour: resource " + FILE_CHILD_NAME +" not found",retVal != null); 2469 fsTestFrameworkErrorAssert("Not really a file: " + retVal.getPath(), retVal.isData()); 2470 return retVal; 2471 } 2472 2473 private static void writeStr(FileObject fo, String str) throws IOException { 2474 FileLock lock = fo.lock(); 2475 OutputStream os = fo.getOutputStream(lock); 2476 try { 2477 os.write(str.getBytes()); 2478 } finally { 2479 lock.releaseLock(); 2480 if (os != null) os.close(); 2481 } 2482 2484 } 2485 2486 private String readStr(FileObject fo) throws IOException { 2487 InputStream is = fo.getInputStream(); 2488 2489 try { 2490 byte[] content = new byte[is.available()]; 2491 is.read(content); 2492 is.close(); 2493 return new String (content); 2494 } finally { 2495 if (is != null) is.close (); 2496 } 2497 } 2498 2499 private void checkSetUp() { 2500 fsTestFrameworkErrorAssert ("Unexpected setUp behaviour: fs == null", fs != null); 2501 fsTestFrameworkErrorAssert ("Unexpected setUp behaviour: root == null: " + getResourcePrefix(), root != null); 2502 } 2503 2504 private List makeList(Enumeration<? extends FileObject> e) { 2505 List l = new LinkedList(); 2506 while (e.hasMoreElements()) { 2507 l.add(e.nextElement()); 2508 } 2509 return l; 2510 } 2511 2512} 2513 | Popular Tags |