1 16 package org.apache.commons.vfs.test; 17 18 import org.apache.commons.vfs.Capability; 19 import org.apache.commons.vfs.FileChangeEvent; 20 import org.apache.commons.vfs.FileListener; 21 import org.apache.commons.vfs.FileObject; 22 import org.apache.commons.vfs.FileSystem; 23 import org.apache.commons.vfs.FileSystemException; 24 import org.apache.commons.vfs.FileType; 25 import org.apache.commons.vfs.Selectors; 26 27 import java.io.OutputStream ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 import java.util.Set ; 31 32 37 public class ProviderWriteTests 38 extends AbstractProviderTestCase 39 { 40 43 protected Capability[] getRequiredCaps() 44 { 45 return new Capability[] 46 { 47 Capability.CREATE, 48 Capability.DELETE, 49 Capability.GET_TYPE, 50 Capability.LIST_CHILDREN, 51 Capability.READ_CONTENT, 52 Capability.WRITE_CONTENT 53 }; 54 } 55 56 59 protected FileObject createScratchFolder() throws Exception 60 { 61 FileObject scratchFolder = getWriteFolder(); 62 63 scratchFolder.delete(Selectors.EXCLUDE_SELF); 65 scratchFolder.createFolder(); 66 67 return scratchFolder; 68 } 69 70 73 public void testFolderCreate() throws Exception 74 { 75 FileObject scratchFolder = createScratchFolder(); 76 77 FileObject folder = scratchFolder.resolveFile("dir1"); 79 assertTrue(!folder.exists()); 80 folder.createFolder(); 81 assertTrue(folder.exists()); 82 assertSame(FileType.FOLDER, folder.getType()); 83 assertEquals(0, folder.getChildren().length); 84 85 folder = scratchFolder.resolveFile("dir2/dir1/dir1"); 87 assertTrue(!folder.exists()); 88 assertTrue(!folder.getParent().exists()); 89 assertTrue(!folder.getParent().getParent().exists()); 90 folder.createFolder(); 91 assertTrue(folder.exists()); 92 assertSame(FileType.FOLDER, folder.getType()); 93 assertEquals(0, folder.getChildren().length); 94 assertTrue(folder.getParent().exists()); 95 assertTrue(folder.getParent().getParent().exists()); 96 97 assertTrue(folder.exists()); 99 folder.createFolder(); 100 } 101 102 105 public void testFileCreate() throws Exception 106 { 107 FileObject scratchFolder = createScratchFolder(); 108 109 FileObject file = scratchFolder.resolveFile("file1.txt"); 111 assertTrue(!file.exists()); 112 file.createFile(); 113 assertTrue(file.exists()); 114 assertSame(FileType.FILE, file.getType()); 115 assertEquals(0, file.getContent().getSize()); 116 assertFalse(file.isHidden()); 117 assertTrue(file.isReadable()); 118 assertTrue(file.isWriteable()); 119 120 file = scratchFolder.resolveFile("file1%25.txt"); 122 assertTrue(!file.exists()); 123 file.createFile(); 124 assertTrue(file.exists()); 125 assertSame(FileType.FILE, file.getType()); 126 assertEquals(0, file.getContent().getSize()); 127 assertFalse(file.isHidden()); 128 assertTrue(file.isReadable()); 129 assertTrue(file.isWriteable()); 130 131 file = scratchFolder.resolveFile("dir1/dir1/file1.txt"); 133 assertTrue(!file.exists()); 134 assertTrue(!file.getParent().exists()); 135 assertTrue(!file.getParent().getParent().exists()); 136 file.createFile(); 137 assertTrue(file.exists()); 138 assertSame(FileType.FILE, file.getType()); 139 assertEquals(0, file.getContent().getSize()); 140 assertTrue(file.getParent().exists()); 141 assertTrue(file.getParent().getParent().exists()); 142 assertFalse(file.getParent().isHidden()); 143 assertFalse(file.getParent().getParent().isHidden()); 144 145 assertTrue(file.exists()); 147 file.createFile(); 148 assertTrue(file.exists()); 149 assertTrue(file.isReadable()); 150 assertTrue(file.isWriteable()); 151 } 152 153 156 public void testFileCreateMismatched() throws Exception 157 { 158 FileObject scratchFolder = createScratchFolder(); 159 160 FileObject file = scratchFolder.resolveFile("dir1/file1.txt"); 162 file.createFile(); 163 assertEquals(FileType.FILE, file.getType()); 164 165 FileObject folder = scratchFolder.resolveFile("dir1/dir2"); 166 folder.createFolder(); 167 assertEquals(FileType.FOLDER, folder.getType()); 168 169 try 171 { 172 folder.createFile(); 173 fail(); 174 } 175 catch (FileSystemException exc) 176 { 177 } 178 179 try 181 { 182 file.createFolder(); 183 fail(); 184 } 185 catch (FileSystemException exc) 186 { 187 } 188 189 FileObject folder2 = file.resolveFile("some-child"); 191 try 192 { 193 folder2.createFolder(); 194 fail(); 195 } 196 catch (FileSystemException exc) 197 { 198 } 199 } 200 201 204 public void testDelete() throws Exception 205 { 206 FileObject folder = createScratchFolder(); 208 folder.resolveFile("file1.txt").createFile(); 209 folder.resolveFile("file%25.txt").createFile(); 210 folder.resolveFile("emptydir").createFolder(); 211 folder.resolveFile("dir1/file1.txt").createFile(); 212 folder.resolveFile("dir1/dir2/file2.txt").createFile(); 213 214 FileObject file = folder.resolveFile("file1.txt"); 216 assertTrue(file.exists()); 217 file.delete(Selectors.SELECT_ALL); 218 assertTrue(!file.exists()); 219 220 file = folder.resolveFile("file%25.txt"); 222 assertTrue(file.exists()); 223 file.delete(Selectors.SELECT_ALL); 224 assertTrue(!file.exists()); 225 226 file = folder.resolveFile("emptydir"); 228 assertTrue(file.exists()); 229 file.delete(Selectors.SELECT_ALL); 230 assertTrue(!file.exists()); 231 232 file = folder.resolveFile("dir1"); 234 FileObject file2 = file.resolveFile("dir2/file2.txt"); 235 assertTrue(file.exists()); 236 assertTrue(file2.exists()); 237 file.delete(Selectors.SELECT_ALL); 238 assertTrue(!file.exists()); 239 assertTrue(!file2.exists()); 240 241 file = folder.resolveFile("some-folder/some-file"); 243 assertTrue(!file.exists()); 244 file.delete(Selectors.SELECT_ALL); 245 assertTrue(!file.exists()); 246 } 247 248 251 279 280 283 317 318 322 public void testCopySameFileSystem() throws Exception 323 { 324 final FileObject scratchFolder = createScratchFolder(); 325 326 final FileObject file = scratchFolder.resolveFile("file1.txt"); 328 assertTrue(!file.exists()); 329 330 final String content = "Here is some sample content for the file. Blah Blah Blah."; 332 final OutputStream os = file.getContent().getOutputStream(); 333 try 334 { 335 os.write(content.getBytes("utf-8")); 336 } 337 finally 338 { 339 os.close(); 340 } 341 342 assertSameContent(content, file); 343 344 FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt"); 346 assertTrue(!fileCopy.exists()); 347 fileCopy.copyFrom(file, Selectors.SELECT_SELF); 348 349 assertSameContent(content, fileCopy); 350 } 351 352 355 public void testOverwriteSameFileSystem() throws Exception 356 { 357 final FileObject scratchFolder = createScratchFolder(); 358 359 final FileObject file = scratchFolder.resolveFile("file1.txt"); 361 assertTrue(!file.exists()); 362 363 final String content = "Here is some sample content for the file. Blah Blah Blah."; 365 final OutputStream os = file.getContent().getOutputStream(); 366 try 367 { 368 os.write(content.getBytes("utf-8")); 369 } 370 finally 371 { 372 os.close(); 373 } 374 375 assertSameContent(content, file); 376 377 FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt"); 379 assertTrue(!fileCopy.exists()); 380 fileCopy.copyFrom(file, Selectors.SELECT_SELF); 381 382 assertSameContent(content, fileCopy); 383 384 assertTrue(fileCopy.exists()); 386 fileCopy.copyFrom(file, Selectors.SELECT_SELF); 387 388 assertSameContent(content, fileCopy); 389 } 390 391 394 public void testCreateDeleteCreateSameFileSystem() throws Exception 395 { 396 final FileObject scratchFolder = createScratchFolder(); 397 398 final FileObject file = scratchFolder.resolveFile("file1.txt"); 400 assertTrue(!file.exists()); 401 402 final String content = "Here is some sample content for the file. Blah Blah Blah."; 404 final OutputStream os = file.getContent().getOutputStream(); 405 try 406 { 407 os.write(content.getBytes("utf-8")); 408 } 409 finally 410 { 411 os.close(); 412 } 413 414 assertSameContent(content, file); 415 416 FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt"); 418 assertTrue(!fileCopy.exists()); 419 fileCopy.copyFrom(file, Selectors.SELECT_SELF); 420 421 assertSameContent(content, fileCopy); 422 423 assertTrue(fileCopy.exists()); 425 assertTrue(fileCopy.delete()); 426 427 assertTrue(!fileCopy.exists()); 429 fileCopy.copyFrom(file, Selectors.SELECT_SELF); 430 431 assertSameContent(content, fileCopy); 432 } 433 434 437 public void testListChildren() throws Exception 438 { 439 FileObject folder = createScratchFolder(); 440 HashSet names = new HashSet (); 441 442 assertEquals(0, folder.getChildren().length); 444 445 folder.resolveFile("dir1").createFolder(); 447 names.add("dir1"); 448 assertSameFileSet(names, folder.getChildren()); 449 450 folder.resolveFile("file1.html").createFile(); 452 names.add("file1.html"); 453 assertSameFileSet(names, folder.getChildren()); 454 455 folder.resolveFile("dir2/file1.txt").createFile(); 457 names.add("dir2"); 458 assertSameFileSet(names, folder.getChildren()); 459 460 OutputStream outstr = folder.resolveFile("file2.txt").getContent().getOutputStream(); 462 outstr.close(); 463 names.add("file2.txt"); 464 assertSameFileSet(names, folder.getChildren()); 465 466 folder.resolveFile("dir1").delete(Selectors.SELECT_ALL); 468 names.remove("dir1"); 469 assertSameFileSet(names, folder.getChildren()); 470 471 folder.resolveFile("file1.html").delete(Selectors.SELECT_ALL); 473 names.remove("file1.html"); 474 assertSameFileSet(names, folder.getChildren()); 475 476 folder.delete(Selectors.SELECT_ALL); 478 folder.createFolder(); 479 assertEquals(0, folder.getChildren().length); 480 } 481 482 485 public void testListener() throws Exception 486 { 487 final FileObject baseFile = createScratchFolder(); 488 489 FileObject child = baseFile.resolveFile("newfile.txt"); 490 assertTrue(!child.exists()); 491 492 FileSystem fs = baseFile.getFileSystem(); 493 TestListener listener = new TestListener(child); 494 fs.addListener(child, listener); 495 496 listener.addCreateEvent(); 498 child.createFolder(); 499 listener.assertFinished(); 500 501 child.createFolder(); 503 504 listener.addDeleteEvent(); 506 child.delete(); 507 listener.assertFinished(); 508 509 child.delete(); 511 512 listener.addCreateEvent(); 514 child.createFile(); 515 listener.assertFinished(); 516 517 child.createFile(); 519 520 listener.addDeleteEvent(); 521 child.delete(); 522 523 listener.addCreateEvent(); 525 child.getContent().getOutputStream().close(); 526 listener.assertFinished(); 527 528 child.getContent().getOutputStream().close(); 530 531 final FileObject otherChild = baseFile.resolveFile("folder1"); 533 otherChild.createFolder(); 534 listener.addDeleteEvent(); 535 listener.addCreateEvent(); 536 child.copyFrom(otherChild, Selectors.SELECT_SELF); 537 listener.assertFinished(); 538 539 fs.removeListener(child, listener); 540 } 541 542 545 private void assertSameFileSet(Set names, FileObject[] files) 546 { 547 assertEquals(names.size(), files.length); 549 550 for (int i = 0; i < files.length; i++) 552 { 553 FileObject file = files[i]; 554 assertTrue(names.contains(file.getName().getBaseName())); 555 } 556 } 557 558 561 private static class TestListener implements FileListener 562 { 563 private final FileObject file; 564 private final ArrayList events = new ArrayList (); 565 private static final Object CREATE = "create"; 566 private static final Object DELETE = "delete"; 567 private static final Object CHANGED = "changed"; 568 569 public TestListener(final FileObject file) 570 { 571 this.file = file; 572 } 573 574 577 public void fileCreated(final FileChangeEvent event) 578 { 579 assertTrue("Unexpected create event", events.size() > 0); 580 assertSame("Expecting a create event", CREATE, events.remove(0)); 581 assertSame(file, event.getFile()); 582 try 583 { 584 assertTrue(file.exists()); 585 } 586 catch (FileSystemException e) 587 { 588 fail(); 589 } 590 } 591 592 595 public void fileDeleted(final FileChangeEvent event) 596 { 597 assertTrue("Unexpected delete event", events.size() > 0); 598 assertSame("Expecting a delete event", DELETE, events.remove(0)); 599 assertSame(file, event.getFile()); 600 try 601 { 602 assertTrue(!file.exists()); 603 } 604 catch (FileSystemException e) 605 { 606 fail(); 607 } 608 } 609 610 public void fileChanged(FileChangeEvent event) throws Exception 611 { 612 assertTrue("Unexpected changed event", events.size() > 0); 613 assertSame("Expecting a changed event", CHANGED, events.remove(0)); 614 assertSame(file, event.getFile()); 615 try 616 { 617 assertTrue(!file.exists()); 618 } 619 catch (FileSystemException e) 620 { 621 fail(); 622 } 623 } 624 625 public void addCreateEvent() 626 { 627 events.add(CREATE); 628 } 629 630 public void addDeleteEvent() 631 { 632 events.add(DELETE); 633 } 634 635 public void assertFinished() 636 { 637 assertEquals("Missing event", 0, events.size()); 638 } 639 } 640 } 641 | Popular Tags |