commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject svn commit: r1706261 - in /commons/proper/vfs/trunk: core/src/main/java/org/apache/commons/vfs2/impl/ core/src/test/java/org/apache/commons/vfs2/impl/test/ core/src/test/java/org/apache/commons/vfs2/test/ src/changes/
Date Thu, 01 Oct 2015 14:27:25 GMT
Author: ecki
Date: Thu Oct  1 14:27:25 2015
New Revision: 1706261

URL: http://svn.apache.org/viewvc?rev=1706261&view=rev
Log:
[VFS-490] Do not layer folders with .jar extension, add tests. closes #2 on GitHub.

Modified:
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/FileTypeMap.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/impl/test/VfsClassLoaderTests.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/FileTypeMap.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/FileTypeMap.java?rev=1706261&r1=1706260&r2=1706261&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/FileTypeMap.java
(original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/FileTypeMap.java
Thu Oct  1 14:27:25 2015
@@ -48,7 +48,10 @@ class FileTypeMap
     }
 
     /**
-     * Finds the provider to use to create a filesystem from a given file.
+     * Find the scheme for the provider of a layered file system.
+     * <p>
+     * This will check the FileContentInfo or file extension.
+     * @return Scheme supporting the file type or null (if unknonw).
      */
     public String getScheme(final FileObject file) throws FileSystemException
     {
@@ -60,7 +63,11 @@ class FileTypeMap
             return mimeTypeMap.get(mimeType);
         }
 
-        // Check the file's extension for a match
+        // no specific mime-type - if it is a file also check the extension
+        if (!file.isFile())
+        {
+            return null; // VFS-490 folders don't use extensions for mime-type
+        }
         final String extension = file.getName().getExtension();
         return extensionMap.get(extension);
     }

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/impl/test/VfsClassLoaderTests.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/impl/test/VfsClassLoaderTests.java?rev=1706261&r1=1706260&r2=1706261&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/impl/test/VfsClassLoaderTests.java
(original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/impl/test/VfsClassLoaderTests.java
Thu Oct  1 14:27:25 2015
@@ -103,38 +103,36 @@ public class VfsClassLoaderTests
     }
 
     /**
-     * Tests retrieving resources (from JAR searchpath)
+     * Tests retrieving resources (from JAR searchpath).
+     * <p>
+     * This is run for all providers, but only when a local
+     * provider is present and jar extension is registered
+     * it will actually carry out all tests.
      */
     public void testGetResourcesJARs() throws Exception
     {
         final FileSystemManager manager = getManager();
-        final File baseDir = AbstractVfsTestCase.getTestDirectoryFile();
-
-        // make sure the provider config is useable
-        if (baseDir == null || manager == null || !baseDir.isDirectory()) 
-        {
-            return;
-        }
-
-        // build search path without using #getBaseFolder()
-        // because NestedJarTestCase redefines it
-        final FileObject nestedJar;
-        final FileObject testJar;
         try
         {
-            nestedJar = manager.resolveFile(baseDir, "nested.jar");
-            testJar = manager.resolveFile(baseDir, "test.jar");
+            // hasProvider("file") cannot be used as it triggers default provider URL
+            manager.toFileObject(new File("."));
         }
-        catch (FileSystemException ignored)
+        catch (FileSystemException e)
         {
-            return; // this suite cannot handle localFiles
+            System.out.println("VfsClassLoaderTestCase has no local file provider, skipping.");
+            return;
         }
 
-        final FileObject[] search = new FileObject[] { nestedJar, testJar };
+        // build search path without using #getBaseFolder()
+        // because NestedJarTestCase redefines it
+        final File baseDir = AbstractVfsTestCase.getTestDirectoryFile();
+        final FileObject nestedJar = manager.resolveFile(baseDir, "nested.jar");
+        final FileObject testJar = manager.resolveFile(baseDir, "test.jar");
 
         // test setup needs to know about .jar extension - i.e. NestedJarTestCase
-        if (!manager.canCreateFileSystem(nestedJar)) 
+        if (!manager.canCreateFileSystem(nestedJar))
         {
+            System.out.println("VfsClassLoaderTestCase has no .jar provider, skipping.");
             return;
         }
 
@@ -146,7 +144,7 @@ public class VfsClassLoaderTests
         // to returning resources for META-INF/MANIFEST.MF (see VFS-500)
         // so we use our own which is guaranteed to not return any hit
         final ClassLoader mockClassloader = new MockClassloader();
-
+        final FileObject[] search = new FileObject[] { nestedJar, testJar };
         final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);
 
         final Enumeration<URL> urls = loader.getResources("META-INF/MANIFEST.MF");
@@ -157,6 +155,44 @@ public class VfsClassLoaderTests
         assertTrue("Second resource must refer to test.jar but was " + url2, url2.toString().endsWith("test.jar!/META-INF/MANIFEST.MF"));
     }
 
+    /**
+     * Tests retrieving resources (from local directory with .jar extension).
+     * <p>
+     * This test is repeatet with various provider configurations but works on local files,
only.
+     */
+    public void testGetResourcesNoLayerLocal() throws Exception
+    {
+        final FileSystemManager manager = getManager();
+        try
+        {
+            // hasProvider("file") cannot be used as it triggers default provider URL
+            manager.toFileObject(new File("."));
+        }
+        catch (FileSystemException e)
+        {
+            System.out.println("TestCase has no local file provider, skipping.");
+            return;
+        }
+        final File baseDir = AbstractVfsTestCase.getTestDirectoryFile();
+
+        // setup test folder
+        final FileObject dir = manager.resolveFile(baseDir, "read-tests/dir1/subdir4.jar");
+        System.out.println("vfsclassloadertests: " + dir);
+        assertTrue("subdir4.jar/ is required for testing " + dir, dir.getType() == FileType.FOLDER);
+        assertFalse(manager.canCreateFileSystem(dir));
+
+        // prepare classloader
+        final FileObject[] search = new FileObject[] { dir };
+        final ClassLoader mockClassloader = new MockClassloader();
+        final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);
+
+        // verify resource loading
+        final Enumeration<URL> urls = loader.getResources("file1.txt");
+        final URL url1 = urls.nextElement();
+        assertFalse("Only one hit expected", urls.hasMoreElements());
+        assertTrue("not pointing to resource " + url1, url1.toString().endsWith("subdir4.jar/file1.txt"));
+    }
+
 
     /**
      * Verify the package loaded with class loader.

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java?rev=1706261&r1=1706260&r2=1706261&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
(original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/ProviderReadTests.java
Thu Oct  1 14:27:25 2015
@@ -363,4 +363,34 @@ public class ProviderReadTests extends A
             assertEquals(expected, actual);
         }
     }
+
+    /**
+     * Tests that we can traverse a folder that has JAR name.
+     */
+    public void testDotJarFolderName() throws Exception
+    {
+        final FileObject folder = getReadFolderDir1().resolveFile("subdir4.jar");
+        Assert.assertTrue(folder.exists());
+        final FileObject file = folder.resolveFile("file1.txt");
+        Assert.assertTrue(file.exists());
+    }
+
+    /**
+     * Tests that a folder can't be layered.
+     */
+    public void testDotJarFolderNameLayer() throws Exception
+    {
+        final FileObject folder = getReadFolderDir1().resolveFile("subdir4.jar");
+        Assert.assertTrue("subdir4.jar/ must exist as folder, check test setup.", folder.isFolder());
+        Assert.assertFalse("subdir4.jar/ must not be layerable", getManager().canCreateFileSystem(folder));
+        try
+        {
+            FileObject ignored = getManager().createFileSystem(folder);
+            fail("Should not be able to create a layered filesystem on a directory. " + ignored);
+        }
+        catch (final FileSystemException e)
+        {
+            assertSame("Creation of layered filesystem should fail" + e, "vfs.impl/no-provider-for-file.error",
e.getCode());
+        }
+    }
 }

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1706261&r1=1706260&r2=1706261&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Thu Oct  1 14:27:25 2015
@@ -26,6 +26,9 @@
 <!--       <action issue="VFS-443" dev="ggregory" type="update" due-to="nickallen">
-->
 <!--        [Local] Need an easy way to convert from a FileObject to a File. -->
 <!--       </action> -->
+      <action issue="VFS-490" dev="ecki" type="fix">
+        [vfsclassloader] Do not open folders with .jar extension. Adds tests.
+     </action>
       <action issue="VFS-582" dev="ecki" type="fix">
         [tests] revert rename of getTestDirectoryFile to make test classes more compatible
for external providers.
      </action>



Mime
View raw message