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 2017/07/20 01:33:57 UTC

svn commit: r1802443 - in /commons/proper/vfs/trunk: core/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java core/src/test/java/org/apache/commons/vfs2/provider/zip/test/FileLockTestCase.java src/changes/changes.xml

Author: ggregory
Date: Thu Jul 20 01:33:56 2017
New Revision: 1802443

URL: http://svn.apache.org/viewvc?rev=1802443&view=rev
Log:
[VFS-291] ZIP archives are not properly closed after unzipping and cannot be deleted until the JVM exists.

Modified:
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/zip/test/FileLockTestCase.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java?rev=1802443&r1=1802442&r2=1802443&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java Thu Jul 20 01:33:56 2017
@@ -168,4 +168,14 @@ public class ZipFileObject extends Abstr
 
         return getAbstractFileSystem().getZipFile().getInputStream(entry);
     }
+
+    @Override
+    protected void doAttach() throws Exception {
+        getAbstractFileSystem().getZipFile();
+    }
+
+    @Override
+    protected void doDetach() throws Exception {
+        getAbstractFileSystem().close();
+    }
 }

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/zip/test/FileLockTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/zip/test/FileLockTestCase.java?rev=1802443&r1=1802442&r2=1802443&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/zip/test/FileLockTestCase.java (original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/zip/test/FileLockTestCase.java Thu Jul 20 01:33:56 2017
@@ -19,14 +19,16 @@ package org.apache.commons.vfs2.provider
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.apache.commons.AbstractVfsTestCase;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.vfs2.FileObject;
+import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileSystemManager;
 import org.apache.commons.vfs2.VFS;
 import org.junit.Assert;
-import org.junit.Ignore;
+import org.junit.Before;
 import org.junit.Test;
 
 /**
@@ -34,30 +36,108 @@ import org.junit.Test;
  */
 public class FileLockTestCase {
 
-    /**
-     * This test checks whether we can modify an underlying zip file after we have performed IO operations on files
-     * within it, but although we no longer have any FileObjects explicitely open.
-     * 
-     * @throws IOException
-     */
-    @Test
-    @Ignore
-    public void testResourcesReleasedByZipFileProvider() throws Exception {
-        FileSystemManager manager = VFS.getManager();
+    private FileSystemManager manager;
+    private File newZipFile;
+
+    private String uri;
+
+    private void assertDelete() {
+        // We do not use newZipFile in the Assert message to avoid touching it before calling delete().
+        Assert.assertTrue("Could not delete file", newZipFile.delete());
+    }
+
+    private void resolveAndOpenCloseContent() throws FileSystemException {
+        try (final FileObject zipFileObject = manager.resolveFile(uri)) {
+            zipFileObject.getContent().close();
+        }
+    }
+
+    public void resolveAndOpenCloseInputStream() throws IOException, FileSystemException {
+        try (final FileObject zipFileObject = manager.resolveFile(uri)) {
+            zipFileObject.getContent().getInputStream().close();
+        }
+    }
+
+    @Before
+    public void setup() throws IOException {
         //
         // We copy the normal test zip to a second, nominally temporary, file so that we can try to delete it with
         // impunity. Since the test fails, the file will be left behind, so it should probably be created in a temporary
         // directory somewhere.
         //
         final File zipFile = new File("src/test/resources/test-data/test.zip");
-        final File newZipFile = new File(AbstractVfsTestCase.getTestDirectory(), "test2.zip");
+        newZipFile = new File(AbstractVfsTestCase.getTestDirectory(), "test2.zip");
         FileUtils.copyFile(zipFile, newZipFile);
+        uri = "zip:file:" + newZipFile.getAbsolutePath() + "!/read-tests/file1.txt";
+        manager = VFS.getManager();
+    }
 
-        final String uri = "zip:file:" + newZipFile.getAbsolutePath() + "!/read-tests/file1.txt";
-        try (final FileObject fileOne = manager.resolveFile(uri)) {
-            fileOne.getContent().getInputStream().close();
+    @Test
+    public void testCannotDeleteWhileStreaming() throws Exception {
+        try (final FileObject zipFileObject = manager.resolveFile(uri)) {
+            try (InputStream inputStream = zipFileObject.getContent().getInputStream()) {
+                // We do not use newZipFile in the Assert message to avoid touching it before calling delete().
+                Assert.assertFalse("Could not delete file", newZipFile.delete());
+            }
         }
+        assertDelete();
+    }
 
-        Assert.assertTrue("Could not delete file", newZipFile.delete());
+    @Test
+    public void testCannotDeleteWhileStreaming2() throws Exception {
+        try (final FileObject zipFileObject = manager.resolveFile(uri)) {
+            try (InputStream inputStream = zipFileObject.getContent().getInputStream()) {
+                // We do not use newZipFile in the Assert message to avoid touching it before calling delete().
+                Assert.assertFalse("Could not delete file", newZipFile.delete());
+            }
+        }
     }
+
+    @Test
+    public void testContent() throws Exception {
+        resolveAndOpenCloseContent();
+        assertDelete();
+    }
+
+    @Test
+    public void testContent3() throws Exception {
+        resolveAndOpenCloseContent();
+        resolveAndOpenCloseContent();
+        resolveAndOpenCloseContent();
+
+        assertDelete();
+    }
+
+    /**
+     * This test checks whether we can modify an underlying zip file after we have performed IO operations on files
+     * within it, but although we no longer have any FileObjects explicitely open.
+     * 
+     * @throws IOException
+     */
+    @Test
+    public void testInputStream() throws Exception {
+        resolveAndOpenCloseInputStream();
+        assertDelete();
+    }
+
+    @Test
+    public void testInputStream3() throws Exception {
+        resolveAndOpenCloseInputStream();
+        resolveAndOpenCloseInputStream();
+        resolveAndOpenCloseInputStream();
+
+        assertDelete();
+    }
+
+    @Test
+    public void testNestedInputStreams() throws Exception {
+        try (final FileObject zipFileObject = manager.resolveFile(uri)) {
+            try (final FileObject zipFileObject2 = manager.resolveFile(uri)) {
+                zipFileObject2.getContent().getInputStream().close();
+            }
+            zipFileObject.getContent().getInputStream().close();
+        }
+        assertDelete();
+    }
+
 }

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1802443&r1=1802442&r2=1802443&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Thu Jul 20 01:33:56 2017
@@ -77,6 +77,9 @@ The <action> type attribute can be add,u
      <action issue="VFS-620" dev="ggregory" type="fix" due-to="stevezhuang">
         FileObject.moveTo(FileObject) API doesn't work well for a Linux FTP.
      </action>
+     <action issue="VFS-291" dev="ggregory" type="fix">
+        ZIP archives are not properly closed after unzipping and cannot be deleted until the JVM exists.
+     </action>
    </release>  
     <release version="2.1" date="2016-05-19" description="New features and bug fix release.