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 33b61676 VFS-846: Resolve a FileName to correct FileType (#424)
33b61676 is described below

commit 33b61676c7c5c44e90480e1224eef73e90280ad1
Author: beise <t.be...@web.de>
AuthorDate: Sun Sep 17 13:32:38 2023 +0200

    VFS-846: Resolve a FileName to correct FileType (#424)
    
    * VFS-846: Resolve a FileName to correct FileType
    
    Fix that FileNames which are directories are resolved to FileType.FOLDER.
    
    * Comment
    
    * Javadoc
    
    ---------
    
    Co-authored-by: Thorsten Beise <thorsten.be...@lbbw-am.de>
    Co-authored-by: Gary Gregory <garydgreg...@users.noreply.github.com>
---
 .../vfs2/impl/DefaultFileSystemManager.java        |  9 ++-
 .../apache/commons/vfs2/provider/UriParser.java    |  8 ++-
 .../vfs2/impl/DefaultFileSystemManagerTest.java    | 65 +++++++++++++++++-----
 .../commons/vfs2/provider/UriParserTest.java       | 30 +++++++++-
 4 files changed, 92 insertions(+), 20 deletions(-)

diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
index c17b0c31..2d9380b2 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
@@ -925,12 +925,17 @@ public class DefaultFileSystemManager implements 
FileSystemManager {
             throw new 
FileSystemException("vfs.provider/invalid-descendent-name.error", name);
         }
 
+        // Reappend the removed trailing / in case of a FOLDER, so that the 
following calls to
+        // 'provider.parseUri(realBase, fullPath)' can determine the correct 
FileType
+        // otherwise the resulting FileType is always fileType.FILE
+        final String trailingPathPart = (fileType == FileType.FOLDER) ? 
FileName.SEPARATOR : "";
+
         final String fullPath;
         if (scheme != null) {
-            fullPath = resolvedPath;
+            fullPath = resolvedPath + trailingPathPart;
         } else {
             scheme = realBase.getScheme();
-            fullPath = realBase.getRootURI() + resolvedPath;
+            fullPath = realBase.getRootURI() + resolvedPath + trailingPathPart;
         }
         final FileProvider provider = providers.get(scheme);
         if (provider != null) {
diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java
index 3fcc9aeb..f7a11523 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/UriParser.java
@@ -477,7 +477,13 @@ public final class UriParser {
             return fileType;
         }
 
-        if (path.charAt(path.length() - 1) != '/') {
+        // '/' or '.' or '..' or anyPath/..' or 'anyPath/.'  should always be 
a path
+        if (path.charAt(path.length() - 1) != '/'
+                && path.lastIndexOf("/..") != (path.length() - 3)
+                && path.lastIndexOf("/.") != (path.length() - 2)
+                && path.lastIndexOf("..") != 0
+                && path.lastIndexOf(".") != 0
+        ) {
             fileType = FileType.FILE;
         }
 
diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileSystemManagerTest.java
 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileSystemManagerTest.java
index e64ac002..78669cef 100644
--- 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileSystemManagerTest.java
+++ 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileSystemManagerTest.java
@@ -16,24 +16,12 @@
  */
 package org.apache.commons.vfs2.impl;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
 import java.io.File;
 import java.nio.file.Paths;
 
-import org.apache.commons.vfs2.CacheStrategy;
-import org.apache.commons.vfs2.FileName;
-import org.apache.commons.vfs2.FileObject;
-import org.apache.commons.vfs2.FileSystemException;
-import org.apache.commons.vfs2.FileSystemManager;
-import org.apache.commons.vfs2.FilesCache;
-import org.apache.commons.vfs2.VFS;
+import org.apache.commons.vfs2.*;
 import org.apache.commons.vfs2.cache.NullFilesCache;
+import org.apache.commons.vfs2.provider.GenericURLFileName;
 import org.apache.commons.vfs2.provider.bzip2.Bzip2FileObject;
 import org.apache.commons.vfs2.provider.gzip.GzipFileObject;
 import org.apache.commons.vfs2.provider.jar.JarFileObject;
@@ -43,6 +31,8 @@ import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
 
+import static org.junit.jupiter.api.Assertions.*;
+
 /**
  * Tests {@link DefaultFileSystemManager}.
  *
@@ -176,6 +166,53 @@ public class DefaultFileSystemManagerTest {
         assertThrows(FileSystemException.class, () -> 
VFS.getManager().resolveName((FileName) null, "../"));
     }
 
+    /**
+     * If the path ends with one of '/' or '.' or '..' or anyPath/..' or 
'anyPath/.' ,
+     * the resulting FileName should be of FileType.FOLDER, else of 
FileType.FILE.
+     */
+    @Test
+    public void testResolveFileNameType() {
+        try (DefaultFileSystemManager fileSystemManager = new 
DefaultFileSystemManager()) {
+            FileName baseNameFolder = new GenericURLFileName(
+                    "sftp"
+                    ,"localhost"
+                    ,22
+                    ,22
+                    ,"user"
+                    ,"password"
+                    ,"basePath"
+                    , FileType.FOLDER
+                    ,"query=test");
+
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"/").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
".").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"..").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"./").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"../").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"./Sub Folder/").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"../Descendant Folder/").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"./Sub Folder/.").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"../Descendant Folder/..").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"./Sub Folder/./").getType());
+            
assertEquals(FileType.FOLDER,fileSystemManager.resolveName(baseNameFolder, 
"../Descendant Folder/../").getType());
+
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"/File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"./File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"../File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, "./Sub 
Folder/File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"../Descendant Folder/File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, "./Sub 
Folder/./File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"../Descendant Folder/../File.txt").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"../Descendant Folder/../File.").getType());
+            
assertEquals(FileType.FILE,fileSystemManager.resolveName(baseNameFolder, 
"../Descendant Folder/../File..").getType());
+
+        } catch(FileSystemException fsex){
+            fail(fsex);
+        }
+    }
+
     @Test
     public void testResolveFileObjectNullAbsolute() throws FileSystemException 
{
         final String absolute = new 
File("/").getAbsoluteFile().toURI().toString();
diff --git 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/UriParserTest.java
 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/UriParserTest.java
index d24fd7c8..707de7f1 100644
--- 
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/UriParserTest.java
+++ 
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/UriParserTest.java
@@ -16,11 +16,12 @@
  */
 package org.apache.commons.vfs2.provider;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-
+import org.apache.commons.vfs2.FileSystemException;
+import org.apache.commons.vfs2.FileType;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.*;
+
 public class UriParserTest {
 
     private static final String[] schemes = {"ftp", "file"};
@@ -69,4 +70,27 @@ public class UriParserTest {
         assertEquals("/user:pass@host/some/path/some:file", buffer.toString());
     }
 
+    @Test
+    public void testTypeOfNormalizedPath() {
+        try {
+            assertEquals(FileType.FOLDER, UriParser.normalisePath(new 
StringBuilder("")));
+            assertEquals(FileType.FOLDER, UriParser.normalisePath(new 
StringBuilder("/")));
+            assertEquals(FileType.FOLDER, UriParser.normalisePath(new 
StringBuilder(".")));
+            assertEquals(FileType.FOLDER, UriParser.normalisePath(new 
StringBuilder("./")));
+            assertEquals(FileType.FOLDER, UriParser.normalisePath(new 
StringBuilder("./Sub Folder/")));
+            assertEquals(FileType.FOLDER, UriParser.normalisePath(new 
StringBuilder("./Sub Folder/.")));
+            assertEquals(FileType.FOLDER, UriParser.normalisePath(new 
StringBuilder("./Sub Folder/./")));
+
+            assertEquals(FileType.FILE, UriParser.normalisePath(new 
StringBuilder("File.txt")));
+            assertEquals(FileType.FILE, UriParser.normalisePath(new 
StringBuilder("/File.txt")));
+            assertEquals(FileType.FILE, UriParser.normalisePath(new 
StringBuilder("./File.txt")));
+            assertEquals(FileType.FILE, UriParser.normalisePath(new 
StringBuilder("./Sub Folder/File.txt")));
+            assertEquals(FileType.FILE, UriParser.normalisePath(new 
StringBuilder("./Sub Folder/./File.txt")));
+            assertEquals(FileType.FILE, UriParser.normalisePath(new 
StringBuilder("./Sub Folder/./File.")));
+            assertEquals(FileType.FILE, UriParser.normalisePath(new 
StringBuilder("./Sub Folder/./File..")));
+
+        } catch(FileSystemException e) {
+            fail(e);
+        }
+    }
 }

Reply via email to