You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/09/26 21:10:01 UTC

[commons-vfs] branch master updated: Add FileObject.getURI().

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 daec412  Add FileObject.getURI().
daec412 is described below

commit daec4123ea6f109e1c7276b152d4e89f64933caa
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Sep 26 17:09:57 2020 -0400

    Add FileObject.getURI().
---
 .../java/org/apache/commons/vfs2/FileObject.java   | 12 ++++
 .../org/apache/commons/vfs2/test/UriTests.java     | 65 +++++++++++++++++++++-
 .../commons/vfs2/test/UrlStructureTests.java       |  6 +-
 src/changes/changes.xml                            |  3 +
 4 files changed, 80 insertions(+), 6 deletions(-)

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 4ebe734..d4c6183 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
@@ -17,6 +17,7 @@
 package org.apache.commons.vfs2;
 
 import java.io.Closeable;
+import java.net.URI;
 import java.net.URL;
 import java.util.List;
 
@@ -299,6 +300,16 @@ public interface FileObject extends Comparable<FileObject>, Iterable<FileObject>
     URL getURL() throws FileSystemException;
 
     /**
+     * Returns a URI representing this file.
+     *
+     * @return the URI for the file.
+     * @since 2.7.0
+     */
+    default URI getURI() {
+        return URI.create(getName().getURI());
+    }
+
+    /**
      * Checks if the fileObject is attached.
      *
      * @return true if the FileObject is attached.
@@ -365,6 +376,7 @@ public interface FileObject extends Comparable<FileObject>, Iterable<FileObject>
      * @throws FileSystemException On error determining if this file exists.
      * @since 2.4
      */
+    @SuppressWarnings("unused") // FileSystemException actually thrown in implementations.
     default boolean isSymbolicLink() throws FileSystemException {
         return false;
     }
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UriTests.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UriTests.java
index 2fd7e00..5cd8433 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UriTests.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UriTests.java
@@ -16,20 +16,28 @@
  */
 package org.apache.commons.vfs2.test;
 
+import java.net.URI;
+import java.net.URL;
+
 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;
 
 /**
- * Absolute URI test cases.
+ * URL test cases for providers.
  */
 public class UriTests extends AbstractProviderTestCase {
+
     /**
-     * Returns the capabilities required by the tests of this test case.
+     * 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 };
+        return new Capability[] {Capability.URI};
     }
 
     /**
@@ -51,4 +59,55 @@ public class UriTests extends AbstractProviderTestCase {
         assertEquals(rootUri, file.getName().getURI());
         assertEquals(FileName.ROOT_PATH, file.getName().getPath());
     }
+
+    /**
+     * Tests url.
+     */
+    @Test
+    public void testGetURI() throws Exception {
+        final FileObject fileObject = getReadFolder().resolveFile("some-dir/");
+        final URI uri = fileObject.getURI();
+
+        // FileName#getURI() returns a String, not a URI.
+        assertEquals(fileObject.getName().getURI(), uri.toString());
+        assertEquals(URI.create(fileObject.getName().getURI()), uri);
+
+        assertEquals(fileObject.getURL().toString(), fileObject.getURI().toString());
+        assertEquals(fileObject.getURL().toURI(), fileObject.getURI());
+    }
+
+    @Test
+    public void testReservedCharacterSpace() throws FileSystemException {
+        try (final FileObject fileObject = getReadFolder().resolveFile("file with spaces.txt")) {
+            final URI url = fileObject.getURI();
+            final String string = url.toString();
+            assertTrue(string, string.contains("file%20with%20spaces.txt"));
+        }
+        try (final FileObject fileObject = getReadFolder().resolveFile("file%20with%20spaces.txt")) {
+            final URI url = fileObject.getURI();
+            final String string = url.toString();
+            assertTrue(string, string.contains("file%20with%20spaces.txt"));
+        }
+    }
+
+    /**
+     * Tests content.
+     */
+    @Test
+    public void testURIContentProvider() throws Exception {
+        // Test non-empty file
+        final FileObject fileObject = getReadFolder().resolveFile("file1.txt");
+        assertTrue(fileObject.exists());
+
+        final URI uri = fileObject.getURI();
+        final String uriStr = uri.toString();
+        final FileSystemOptions options = getReadFolder().getFileSystem().getFileSystemOptions();
+
+        final FileObject f1 = getManager().resolveFile(uriStr, options);
+        final FileObject f2 = getManager().resolveFile(uriStr, options);
+
+        assertEquals("Two files resolved by URI must be equals on " + uriStr, f1, f2);
+        assertSame("Resolving two times should not produce new filesystem on " + uriStr, f1.getFileSystem(),
+            f2.getFileSystem());
+    }
 }
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UrlStructureTests.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UrlStructureTests.java
index 56ad473..99a7a16 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UrlStructureTests.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/test/UrlStructureTests.java
@@ -17,6 +17,7 @@
 package org.apache.commons.vfs2.test;
 
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.apache.commons.vfs2.Capability;
 import org.apache.commons.vfs2.FileObject;
@@ -30,7 +31,7 @@ public class UrlStructureTests extends AbstractProviderTestCase {
      */
     @Override
     protected Capability[] getRequiredCaps() {
-        return new Capability[] { Capability.GET_TYPE, Capability.URI };
+        return new Capability[] {Capability.GET_TYPE, Capability.URI};
     }
 
     /**
@@ -47,8 +48,7 @@ public class UrlStructureTests extends AbstractProviderTestCase {
         assertTrue(folder.exists());
 
         // Try getting the content of a folder
-        try {
-            folder.getURL().openConnection().getInputStream();
+        try (final InputStream inputStream = folder.getURL().openConnection().getInputStream()) {
             fail();
         } catch (final IOException e) {
             assertSameMessage("vfs.provider/read-not-file.error", folder, e);
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3d88046..9c7895c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -110,6 +110,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" due-to="Gary Gregory" type="fix">
         FileObject.getURL() returns an illegal URL when a it should escape a space.
       </action>
+      <action dev="ggregory" due-to="Gary Gregory" type="add">
+        Add FileObject.getURI().
+      </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.