|
46 | 46 | import java.io.InputStream; |
47 | 47 | import java.io.OutputStream; |
48 | 48 | import java.net.URI; |
| 49 | +import java.nio.ByteBuffer; |
49 | 50 | import java.nio.channels.SeekableByteChannel; |
50 | 51 | import java.nio.file.AccessDeniedException; |
51 | 52 | import java.nio.file.AccessMode; |
@@ -641,6 +642,148 @@ void testNewByteChannelWriteAppend() throws IOException { |
641 | 642 | } |
642 | 643 | } |
643 | 644 |
|
| 645 | + @Test |
| 646 | + void testNewByteChannelCreateWriteExisting() throws IOException { |
| 647 | + FileEntry bar = addFile("/foo/bar"); |
| 648 | + |
| 649 | + byte[] newContents = "Lorem ipsum".getBytes(); |
| 650 | + |
| 651 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
| 652 | + try (SeekableByteChannel channel = fileSystem.newByteChannel(createPath("/foo/bar"), options)) { |
| 653 | + // don't do anything with the channel, there's a separate test for that |
| 654 | + assertEquals(0, channel.size()); |
| 655 | + channel.write(ByteBuffer.wrap(newContents)); |
| 656 | + assertEquals(newContents.length, channel.size()); |
| 657 | + } |
| 658 | + |
| 659 | + assertArrayEquals(newContents, getContents(bar)); |
| 660 | + } |
| 661 | + |
| 662 | + @Test |
| 663 | + void testNewByteChannelCreateAppendExisting() throws IOException { |
| 664 | + FileEntry bar = addFile("/foo/bar"); |
| 665 | + bar.setContents(new byte[1024]); |
| 666 | + |
| 667 | + byte[] newContents = "Lorem ipsum".getBytes(); |
| 668 | + |
| 669 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.APPEND); |
| 670 | + try (SeekableByteChannel channel = fileSystem.newByteChannel(createPath("/foo/bar"), options)) { |
| 671 | + // don't do anything with the channel, there's a separate test for that |
| 672 | + long size = bar.getSize(); |
| 673 | + assertEquals(size, channel.size()); |
| 674 | + channel.write(ByteBuffer.wrap(newContents)); |
| 675 | + assertEquals(size + newContents.length, channel.size()); |
| 676 | + } |
| 677 | + |
| 678 | + byte[] totalNewContents = new byte[1024 + newContents.length]; |
| 679 | + System.arraycopy(newContents, 0, totalNewContents, 1024, newContents.length); |
| 680 | + |
| 681 | + assertArrayEquals(totalNewContents, getContents(bar)); |
| 682 | + } |
| 683 | + |
| 684 | + @Test |
| 685 | + void testNewByteChannelCreateWriteNonExisting() throws IOException { |
| 686 | + addDirectory("/foo"); |
| 687 | + |
| 688 | + byte[] newContents = "Lorem ipsum".getBytes(); |
| 689 | + |
| 690 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
| 691 | + try (SeekableByteChannel channel = fileSystem.newByteChannel(createPath("/foo/bar"), options)) { |
| 692 | + // don't do anything with the channel, there's a separate test for that |
| 693 | + assertEquals(0, channel.size()); |
| 694 | + channel.write(ByteBuffer.wrap(newContents)); |
| 695 | + assertEquals(newContents.length, channel.size()); |
| 696 | + } |
| 697 | + |
| 698 | + FileEntry bar = getFile("/foo/bar"); |
| 699 | + |
| 700 | + assertArrayEquals(newContents, getContents(bar)); |
| 701 | + } |
| 702 | + |
| 703 | + @Test |
| 704 | + void testNewByteChannelCreateAppendNonExisting() throws IOException { |
| 705 | + addDirectory("/foo"); |
| 706 | + |
| 707 | + byte[] newContents = "Lorem ipsum".getBytes(); |
| 708 | + |
| 709 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.APPEND); |
| 710 | + try (SeekableByteChannel channel = fileSystem.newByteChannel(createPath("/foo/bar"), options)) { |
| 711 | + // don't do anything with the channel, there's a separate test for that |
| 712 | + assertEquals(0, channel.size()); |
| 713 | + channel.write(ByteBuffer.wrap(newContents)); |
| 714 | + assertEquals(newContents.length, channel.size()); |
| 715 | + } |
| 716 | + |
| 717 | + FileEntry bar = getFile("/foo/bar"); |
| 718 | + |
| 719 | + assertArrayEquals(newContents, getContents(bar)); |
| 720 | + } |
| 721 | + |
| 722 | + @Test |
| 723 | + void testNewByteChannelCreateNewWriteExisting() throws IOException { |
| 724 | + FileEntry bar = addFile("/foo/bar"); |
| 725 | + byte[] oldContents = "Hello World".getBytes(); |
| 726 | + bar.setContents(oldContents); |
| 727 | + |
| 728 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); |
| 729 | + FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class, |
| 730 | + () -> fileSystem.newByteChannel(createPath("/foo/bar"), options)); |
| 731 | + assertEquals("/foo/bar", exception.getFile()); |
| 732 | + |
| 733 | + assertArrayEquals(oldContents, getContents(bar)); |
| 734 | + } |
| 735 | + |
| 736 | + @Test |
| 737 | + void testNewByteChannelCreateNewAppendExisting() throws IOException { |
| 738 | + FileEntry bar = addFile("/foo/bar"); |
| 739 | + bar.setContents(new byte[1024]); |
| 740 | + |
| 741 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND); |
| 742 | + FileAlreadyExistsException exception = assertThrows(FileAlreadyExistsException.class, |
| 743 | + () -> fileSystem.newByteChannel(createPath("/foo/bar"), options)); |
| 744 | + assertEquals("/foo/bar", exception.getFile()); |
| 745 | + |
| 746 | + assertArrayEquals(new byte[1024], getContents(bar)); |
| 747 | + } |
| 748 | + |
| 749 | + @Test |
| 750 | + void testNewByteChannelCreateNewWriteNonExisting() throws IOException { |
| 751 | + addDirectory("/foo"); |
| 752 | + |
| 753 | + byte[] newContents = "Lorem ipsum".getBytes(); |
| 754 | + |
| 755 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); |
| 756 | + try (SeekableByteChannel channel = fileSystem.newByteChannel(createPath("/foo/bar"), options)) { |
| 757 | + // don't do anything with the channel, there's a separate test for that |
| 758 | + assertEquals(0, channel.size()); |
| 759 | + channel.write(ByteBuffer.wrap(newContents)); |
| 760 | + assertEquals(newContents.length, channel.size()); |
| 761 | + } |
| 762 | + |
| 763 | + FileEntry bar = getFile("/foo/bar"); |
| 764 | + |
| 765 | + assertArrayEquals(newContents, getContents(bar)); |
| 766 | + } |
| 767 | + |
| 768 | + @Test |
| 769 | + void testNewByteChannelCreateNewAppendNonExisting() throws IOException { |
| 770 | + addDirectory("/foo"); |
| 771 | + |
| 772 | + byte[] newContents = "Lorem ipsum".getBytes(); |
| 773 | + |
| 774 | + Set<? extends OpenOption> options = EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND); |
| 775 | + try (SeekableByteChannel channel = fileSystem.newByteChannel(createPath("/foo/bar"), options)) { |
| 776 | + // don't do anything with the channel, there's a separate test for that |
| 777 | + assertEquals(0, channel.size()); |
| 778 | + channel.write(ByteBuffer.wrap(newContents)); |
| 779 | + assertEquals(newContents.length, channel.size()); |
| 780 | + } |
| 781 | + |
| 782 | + FileEntry bar = getFile("/foo/bar"); |
| 783 | + |
| 784 | + assertArrayEquals(newContents, getContents(bar)); |
| 785 | + } |
| 786 | + |
644 | 787 | // FTPFileSystem.newDirectoryStream |
645 | 788 |
|
646 | 789 | @Test |
|
0 commit comments