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 0fde91c Add FileObject.getPath(). 0fde91c is described below commit 0fde91c3026856f12a9f0ddbe5f8b6dd3b6f4406 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Sep 26 17:45:18 2020 -0400 Add FileObject.getPath(). --- .../java/org/apache/commons/vfs2/FileObject.java | 12 +++ .../org/apache/commons/vfs2/test/PathTests.java | 117 +++++++++++++++++++++ src/changes/changes.xml | 3 + 3 files changed, 132 insertions(+) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileObject.java index c5feba8..e09d327 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileObject.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileObject.java @@ -19,6 +19,8 @@ package org.apache.commons.vfs2; import java.io.Closeable; import java.net.URI; import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import org.apache.commons.vfs2.operations.FileOperations; @@ -302,6 +304,16 @@ public interface FileObject extends Comparable<FileObject>, Iterable<FileObject> } /** + * Returns a Path representing this file. + * + * @return the Path for the file. + * @since 2.7.0 + */ + default Path getPath() { + return Paths.get(getURI()); + } + + /** * Returns a URL representing this file. * * @return the URL for the file. diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/PathTests.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/PathTests.java new file mode 100644 index 0000000..f2c6b03 --- /dev/null +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/PathTests.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.vfs2.test; + +import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.commons.vfs2.Capability; +import org.apache.commons.vfs2.FileName; +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.FileSystemException; +import org.apache.commons.vfs2.FileSystemOptions; +import org.junit.Test; + +/** + * Path test cases for providers. + */ +public class PathTests extends AbstractProviderTestCase { + + /** + * Returns the capabilities required by the tests of this test case. The tests are not run if the provider being + * tested does not support all the required capabilities. Return null or an empty array to always run the tests. + */ + @Override + protected Capability[] getRequiredCaps() { + return new Capability[] {Capability.URI}; + } + + /** + * Tests resolution of absolute URI. + */ + public void testAbsoluteURI() throws Exception { + final FileObject readFolder = getReadFolder(); + + // Try fetching base folder again by its Path + final String pathStr = readFolder.getPath().toString(); + try (FileObject fileObject = getManager().resolveFile(pathStr, + readFolder.getFileSystem().getFileSystemOptions())) { + assertSame("file object", readFolder, fileObject); + } + + // Try fetching the filesystem root by its Path + final Path rootPath = Paths.get(readFolder.getName().getRootURI()); + try (FileObject fileObject = getManager().resolveFile(rootPath.toString(), + readFolder.getFileSystem().getFileSystemOptions())) { + assertSame(readFolder.getFileSystem().getRoot(), fileObject); + assertEquals(rootPath, Paths.get(fileObject.getName().getRootURI())); + assertEquals(rootPath, fileObject.getName().getPath()); + assertEquals(FileName.ROOT_PATH, fileObject.getName().getPath()); + } + } + + @Test + public void testGetPath() throws Exception { + try (final FileObject fileObject = getReadFolder().resolveFile("some-dir/")) { + final Path path = fileObject.getPath(); + + // FileName#getURI() returns a String, not a URI. + assertEquals(Paths.get(fileObject.getName().getURI()).toString(), path.toString()); + assertEquals(Paths.get(fileObject.getName().getURI()), path); + + assertEquals(fileObject.getPath().toString(), fileObject.getURI().toString()); + } + } + + @Test + public void testReservedCharacterSpace() throws FileSystemException { + try (final FileObject fileObject = getReadFolder().resolveFile("file with spaces.txt")) { + final Path path = fileObject.getPath(); + final String string = path.toString(); + assertTrue(string, string.contains("file%20with%20spaces.txt")); + } + try (final FileObject fileObject = getReadFolder().resolveFile("file%20with%20spaces.txt")) { + final Path path = fileObject.getPath(); + final String string = path.toString(); + assertTrue(string, string.contains("file%20with%20spaces.txt")); + } + } + + /** + * Tests content. + */ + @Test + public void testURIContentProvider() throws Exception { + // Test non-empty file + try (final FileObject fileObject = getReadFolder().resolveFile("file1.txt")) { + assertTrue(fileObject.exists()); + + final Path path = fileObject.getPath(); + final String pathStr = path.toString(); + final FileSystemOptions options = getReadFolder().getFileSystem().getFileSystemOptions(); + + try (final FileObject f1 = getManager().resolveFile(pathStr, options); + final FileObject f2 = getManager().resolveFile(pathStr, options)) { + + assertEquals("Two files resolved by URI must be equals on " + pathStr, f1, f2); + assertSame("Resolving two times should not produce new filesystem on " + pathStr, f1.getFileSystem(), + f2.getFileSystem()); + } + } + } +} \ No newline at end of file diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9c7895c..1ee0b48 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -113,6 +113,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" due-to="Gary Gregory" type="add"> Add FileObject.getURI(). </action> + <action dev="ggregory" due-to="Gary Gregory" type="add"> + Add FileObject.getPath(). + </action> <!-- UPDATES --> <action issue="VFS-755" dev="ggregory" due-to="Gary Gregory" type="update"> Update org.apache.httpcomponents:httpclient from 4.5.10 to 4.5.11.