This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
The following commit(s) were added to refs/heads/master by this push: new 5a0eba8 [VFS-807] Pick up same change from Commons IO and add tests from VFS-807 from L. For me, on Windows 10 and Java 8, the tests pass with the change to LocalFile but L reports otherwise so this might be OS and Java version dependent. 5a0eba8 is described below commit 5a0eba8f6fcba07194ccc1ce68c61429e634870c Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Aug 5 13:01:42 2021 -0400 [VFS-807] Pick up same change from Commons IO and add tests from VFS-807 from L. For me, on Windows 10 and Java 8, the tests pass with the change to LocalFile but L reports otherwise so this might be OS and Java version dependent. --- .../commons/vfs2/provider/local/LocalFile.java | 5 +- .../commons/vfs2/ProviderWriteAppendTests.java | 76 +++++++++++++++------- .../apache/commons/vfs2/ProviderWriteTests.java | 32 ++++++++- 3 files changed, 85 insertions(+), 28 deletions(-) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java index e0c67d9..2de35f7 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java @@ -23,7 +23,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.OpenOption; -import java.nio.file.Path; import java.nio.file.StandardOpenOption; import org.apache.commons.vfs2.FileObject; @@ -127,9 +126,11 @@ public class LocalFile extends AbstractFileObject<LocalFileSystem> { @Override protected OutputStream doGetOutputStream(final boolean append) throws Exception { // TODO Reuse Apache Commons IO + // @formatter:off return Files.newOutputStream(file.toPath(), append ? - new OpenOption[] {StandardOpenOption.APPEND} : + new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.APPEND} : new OpenOption[] {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING}); + // @formatter:on } @Override diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteAppendTests.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteAppendTests.java index 5519c9d..a6c2d3e 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteAppendTests.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteAppendTests.java @@ -25,6 +25,7 @@ import org.junit.Test; * File system test that check that a file system can be modified. */ public class ProviderWriteAppendTests extends AbstractProviderTestCase { + /** * Sets up a scratch folder for the test to use. */ @@ -43,8 +44,8 @@ public class ProviderWriteAppendTests extends AbstractProviderTestCase { */ @Override protected Capability[] getRequiredCapabilities() { - return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN, - Capability.READ_CONTENT, Capability.WRITE_CONTENT, Capability.APPEND_CONTENT }; + return new Capability[] {Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT, + Capability.WRITE_CONTENT, Capability.APPEND_CONTENT}; } /** @@ -52,36 +53,61 @@ public class ProviderWriteAppendTests extends AbstractProviderTestCase { */ @Test public void testAppendContent() throws Exception { - final FileObject scratchFolder = createScratchFolder(); + try (final FileObject scratchFolder = createScratchFolder(); - // Create direct child of the test folder - final FileObject file = scratchFolder.resolveFile("file1.txt"); - assertFalse(file.exists()); + // Create direct child of the test folder + final FileObject file = scratchFolder.resolveFile("file1.txt")) { + assertFalse(file.exists()); - // Create the source file - final String content = "Here is some sample content for the file. Blah Blah Blah."; - final String contentAppend = content + content; + // Create the source file + final String content = "Here is some sample content for the file. Blah Blah Blah."; + final String contentAppend = content + content; - try (OutputStream os = file.getContent().getOutputStream()) { - os.write(content.getBytes(StandardCharsets.UTF_8)); - } - assertSameContent(content, file); + try (FileContent fileContent = file.getContent(); OutputStream os = fileContent.getOutputStream()) { + os.write(content.getBytes(StandardCharsets.UTF_8)); + } + assertSameContent(content, file); + + // Append to the new file + try (FileContent fileContent = file.getContent(); OutputStream os2 = fileContent.getOutputStream(true)) { + os2.write(content.getBytes(StandardCharsets.UTF_8)); + } + assertSameContent(contentAppend, file); - // Append to the new file - try (OutputStream os2 = file.getContent().getOutputStream(true)) { - os2.write(content.getBytes(StandardCharsets.UTF_8)); + // Make sure we can copy the new file to another file on the same filesystem + try (final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt")) { + assertFalse(fileCopy.exists()); + fileCopy.copyFrom(file, Selectors.SELECT_SELF); + + assertSameContent(contentAppend, fileCopy); + + // Delete the file. + assertTrue(fileCopy.exists()); + assertTrue(fileCopy.delete()); + } } - assertSameContent(contentAppend, file); + } - // Make sure we can copy the new file to another file on the same filesystem - final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt"); - assertFalse(fileCopy.exists()); - fileCopy.copyFrom(file, Selectors.SELECT_SELF); + /** + * Tests append-write into a non-existing file. + * + * See [VFS-807]. + */ + @Test + public void testAppendToNonExsiting() throws Exception { + try (final FileObject scratchFolder = createScratchFolder(); - assertSameContent(contentAppend, fileCopy); + // Create direct child of the test folder + final FileObject file = scratchFolder.resolveFile("file2.txt")) { + assertFalse(file.exists()); - // Delete the file. - assertTrue(fileCopy.exists()); - assertTrue(fileCopy.delete()); + // Create the source file + final String content1 = "Here is some sample content for the file. Blah Blah Blah."; + + try (FileContent fileContent = file.getContent(); OutputStream os = fileContent.getOutputStream()) { + os.write(content1.getBytes(StandardCharsets.UTF_8)); + } + assertSameContent(content1, file); + } } } diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteTests.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteTests.java index 6cfff24..b6a5817 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteTests.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/ProviderWriteTests.java @@ -636,6 +636,37 @@ public class ProviderWriteTests extends AbstractProviderTestCase { } /** + * Tests overwriting the file. + * + * See [VFS-807]. + */ + @Test + public void testOverwriteContent() throws Exception { + final FileObject scratchFolder = createScratchFolder(); + + // Create direct child of the test folder + final FileObject file = scratchFolder.resolveFile("file1.txt"); + assertFalse(file.exists()); + + // Create the source file + final String content1 = "Here is some sample content for the file. Blah Blah Blah."; + + try (OutputStream os = file.getContent().getOutputStream()) { + os.write(content1.getBytes(StandardCharsets.UTF_8)); + } + assertSameContent(content1, file); + + // VFS-807, part 1: verify that writing to the existing file overwrites its content! + // content2 must be shorter than content1 + final String content2 = "0123456789 ABCD"; + + try (OutputStream os = file.getContent().getOutputStream()) { + os.write(content2.getBytes(StandardCharsets.UTF_8)); + } + assertSameContent(content2, file); + } + + /** * Tests overwriting a file on the same file system. */ @Test @@ -741,5 +772,4 @@ public class ProviderWriteTests extends AbstractProviderTestCase { } assertSameContent(expectedString, fileTarget); } - }