incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mnutt...@apache.org
Subject svn commit: r1031589 - in /incubator/aries/trunk/application/application-utils/src: main/java/org/apache/aries/application/utils/filesystem/IOUtils.java test/java/org/apache/aries/application/utils/filesystem/IOUtilsTest.java
Date Fri, 05 Nov 2010 14:34:57 GMT
Author: mnuttall
Date: Fri Nov  5 14:34:57 2010
New Revision: 1031589

URL: http://svn.apache.org/viewvc?rev=1031589&view=rev
Log:
ARIES-483: Fix some file locking problems occasionally seen in IOUtils

Modified:
    incubator/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/IOUtils.java
    incubator/aries/trunk/application/application-utils/src/test/java/org/apache/aries/application/utils/filesystem/IOUtilsTest.java

Modified: incubator/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/IOUtils.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/IOUtils.java?rev=1031589&r1=1031588&r2=1031589&view=diff
==============================================================================
--- incubator/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/IOUtils.java
(original)
+++ incubator/aries/trunk/application/application-utils/src/main/java/org/apache/aries/application/utils/filesystem/IOUtils.java
Fri Nov  5 14:34:57 2010
@@ -48,10 +48,7 @@ public class IOUtils
   public static void copy(InputStream in, OutputStream out) throws IOException
   {
     try {
-      int len;
-      byte[] b = new byte[1024];
-      while ((len = in.read(b)) != -1)
-        out.write(b,0,len);
+      copyAndDoNotCloseInputStream(in, out);
     }
     finally {
       close(in);
@@ -59,6 +56,17 @@ public class IOUtils
   }
   
   /**
+   * Copy an InputStream to an OutputStream and do not close the InputStream afterwards.
+   */
+  public static void copyAndDoNotCloseInputStream(InputStream in, OutputStream out) throws
IOException
+  {
+    int len;
+    byte[] b = new byte[1024];
+    while ((len = in.read(b)) != -1)
+      out.write(b,0,len);
+  }
+  
+  /**
    * Close some xStream for good :)
    */
   public static void close(Closeable c)
@@ -67,7 +75,9 @@ public class IOUtils
       if (c != null)
         c.close();
     }
-    catch (IOException e) { c=null; } //in other words do nothing in a language findbugs
can understand 
+    catch (IOException e) {
+      c = null;
+    }
   }
   
   public static OutputStream getOutputStream(File outputDir, String relativePath) throws
IOException
@@ -81,18 +91,18 @@ public class IOUtils
     {
       dirName = relativePath.substring(0, lastSeparatorIndex);
       fileName = relativePath.substring(lastSeparatorIndex + 1);
+
       outputDirectory = new File(outputDir, dirName);
       
+      if (!!!outputDirectory.exists() && !!!outputDirectory.mkdirs())
+        throw new IOException(MessageUtil.getMessage("APPUTILS0012E", relativePath));
     }
     else
     {
       outputDirectory = outputDir;
       fileName = relativePath;
     }
-    if (!!!outputDirectory.exists() && !!!outputDirectory.mkdirs()) {
-        throw new IOException(MessageUtil.getMessage("APPUTILS0012E", relativePath));
-    }
-
+    
     File outputFile = new File(outputDirectory, fileName);
     return new FileOutputStream(outputFile);
   }
@@ -113,7 +123,23 @@ public class IOUtils
     }
   }
   
-  /** 
+  /**
+   * Write the given InputStream to a file given by a root directory (outputDir) and a relative
directory.
+   * Necessary subdirectories will be created. This method will not close the supplied InputStream.
+   */
+  public static void writeOutAndDontCloseInputStream(File outputDir, String relativePath,
InputStream content) throws IOException
+  {
+    OutputStream out = null;
+    try {
+      out = getOutputStream(outputDir, relativePath);
+      IOUtils.copyAndDoNotCloseInputStream(content, out);
+    }
+    finally {
+      close(out);
+    }
+  }
+  
+   /** 
    * Zip up all contents of rootDir (recursively) into targetStream
    */
   @SuppressWarnings("unchecked")
@@ -131,9 +157,17 @@ public class IOUtils
   /**
    * Zip up all contents of rootDir (recursively) into targetFile
    */
+  @SuppressWarnings("unchecked")
   public static void zipUp(File rootDir, File targetFile) throws IOException
   {
-    zipUp (rootDir, new FileOutputStream (targetFile));
+    ZipOutputStream out = null; 
+    try {
+      out = new ZipOutputStream(new FileOutputStream(targetFile));
+      zipUpRecursive(out, "", rootDir, (Set<String>) Collections.EMPTY_SET);
+    }
+    finally {
+      close(out);
+    }
   }
   
   /**
@@ -232,11 +266,6 @@ public class IOUtils
         do { 
           if (!zipEntry.isDirectory()) { 
             writeOutAndDontCloseInputStream(outputDir, zipEntry.getName(), zis);
-          } else { 
-            File f = new File (outputDir, zipEntry.getName());
-            if (!f.exists()) { 
-              success &= f.mkdirs();
-            }
           }
           zis.closeEntry();
           zipEntry = zis.getNextEntry();
@@ -250,31 +279,5 @@ public class IOUtils
     return success;
   }
   
-  /**
-   * Write the given InputStream to a file given by a root directory (outputDir) and a relative
directory.
-   * Necessary subdirectories will be created. This method will not close the supplied InputStream.
-   */
-  public static void writeOutAndDontCloseInputStream(File outputDir, String relativePath,
InputStream content) throws IOException
-  {
-    OutputStream out = null;
-    try {
-      out = getOutputStream(outputDir, relativePath);
-      IOUtils.copyAndDoNotCloseInputStream(content, out);
-    }
-    finally {
-      close(out);
-    }
-  }
-  
-  /**
-   * Copy an InputStream to an OutputStream and do not close the InputStream afterwards.
-   */
-  public static void copyAndDoNotCloseInputStream(InputStream in, OutputStream out) throws
IOException
-  {
-    int len;
-    byte[] b = new byte[1024];
-    while ((len = in.read(b)) != -1)
-      out.write(b,0,len);
-  }
-}
 
+}

Modified: incubator/aries/trunk/application/application-utils/src/test/java/org/apache/aries/application/utils/filesystem/IOUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/application/application-utils/src/test/java/org/apache/aries/application/utils/filesystem/IOUtilsTest.java?rev=1031589&r1=1031588&r2=1031589&view=diff
==============================================================================
--- incubator/aries/trunk/application/application-utils/src/test/java/org/apache/aries/application/utils/filesystem/IOUtilsTest.java
(original)
+++ incubator/aries/trunk/application/application-utils/src/test/java/org/apache/aries/application/utils/filesystem/IOUtilsTest.java
Fri Nov  5 14:34:57 2010
@@ -31,23 +31,17 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.StringBufferInputStream;
 import java.util.zip.ZipFile;
 
+import org.apache.aries.application.filesystem.IDirectory;
 import org.apache.aries.application.filesystem.IFile;
-import org.apache.aries.application.utils.filesystem.impl.FileImpl;
 import org.junit.AfterClass;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 public class IOUtilsTest
 {
-  @BeforeClass 
-  public static void setup()
-  { 
-    new File("ioUtilsTest").mkdir();
-  }
-  
   @AfterClass
   public static void cleanUp()
   {
@@ -56,22 +50,38 @@ public class IOUtilsTest
   }
   
   @Test
-  public void testZipUp() throws IOException
+  public void testZipUpAndUnzipAndDeleteRecursive() throws IOException
   {
+    new File ("ioUtilsTest").mkdir();
     IOUtils.zipUp(new File("../src/test/resources/zip"), new File("ioUtilsTest/test.zip"));
     
     ZipFile zip = new ZipFile("ioUtilsTest/test.zip");
     assertNotNull(zip.getEntry("file.txt"));
     assertNotNull(zip.getEntry("subdir/someFile.txt"));
     zip.close();
+    
+    IDirectory dir = FileSystem.getFSRoot(new File("ioUtilsTest"));
+    IFile izip = dir.getFile("test.zip");
+    File output = new File("ioUtilsTest/zipout");
+    output.mkdirs();
+    IOUtils.unpackZip(izip, output);
+    File a = new File(output,"file.txt");
+    File b = new File(output,"subdir");
+    File c = new File(b,"someFile.txt");
+    assertTrue(output.exists());
+    assertTrue(a.exists() && a.isFile());
+    assertTrue(b.exists() && b.isDirectory());
+    assertTrue(c.exists() && c.isFile());
+    
+    IOUtils.deleteRecursive(output);
+    assertFalse(output.exists());
   }
   
   @Test
   public void testWriteOut() throws IOException
   {
     File tmpDir = new File("ioUtilsTest/tmp");
-    // Do not call mkdirs() on tmpDir. writeOut() should create it. 
-    // tmpDir.mkdirs(); 
+    tmpDir.mkdir();
     
     IOUtils.writeOut(tmpDir, "simple.txt", new StringBufferInputStream("abc"));
     IOUtils.writeOut(tmpDir, "some/relative/directory/complex.txt", new StringBufferInputStream("def"));
@@ -121,7 +131,7 @@ public class IOUtilsTest
         fail("Close was invoked");
       }
     };
-    File f = new File("unittest/outtest1");
+    File f = new File("ioUtilsTest/outtest1");
     f.mkdirs();
     IOUtils.writeOutAndDontCloseInputStream(f, "/fred", is);
     File fred = new File(f,"/fred");
@@ -132,48 +142,90 @@ public class IOUtilsTest
     
   }
   
-  @Test
-  public void testEmptyDirectory() throws IOException {
-    /* Create a .zip with a single entry, an empty directory inside. 
-     * Confirm that the directory is recreated on unzip. 
-     */
-    File testDir = new File ("unittest/emptyDirTest/");
-    File parent = new File (testDir, "emptyTestRootDir/");
-    File emptyDir = new File (parent, "foo/");
-    emptyDir.mkdirs();
-    assertTrue (emptyDir.exists());
-    
-    File zipWithEmptyDir = new File (testDir, "empty.zip");
-    IOUtils.zipUp(parent, zipWithEmptyDir);
-    
-    emptyDir.delete();
-    assertFalse (emptyDir.exists());
-    
-    IFile zip = new FileImpl(zipWithEmptyDir, testDir);
-    IOUtils.unpackZip(zip, parent);
-    assertTrue (emptyDir.exists());
+  @Test 
+  public void testCopy() throws IOException{
+    InputStream is = new InputStream(){
+      boolean closed=false;
+      int idx=0;
+      int data[]=new int[]{1,2,3,4,5,-1};
+      @Override
+      public int read() throws IOException
+      {
+        if(idx<data.length)
+          return data[idx++];
+        else
+          return -1;
+      }
+      @Override
+      public void close() throws IOException
+      {
+        closed=true;
+      }
+      @Override
+      public int available() throws IOException
+      {
+        if(!closed)
+          return super.available();
+        else
+          return 123456789;
+      }
+      
+    };
+    
+    OutputStream os = new OutputStream(){
+      int idx=0;
+      int data[]=new int[]{1,2,3,4,5,-1};
+      @Override
+      public void write(int b) throws IOException
+      {
+        if(b!=data[idx++]){
+          fail("Data written to outputstream was not as expected");
+        }
+      }
+    };
+    
+    IOUtils.copy(is,os);
+    if(is.available()!=123456789){
+      fail("close was not invoked");
+    }
+    
+    
   }
   
   @Test
-  public void testSingleRootLevelEntry() throws IOException { 
-    /* Create a .zip with a single entry, a root-level file. 
-     * Confirm that the file is recreated on unzip. 
-     */
-    File testDir = new File ("unittest/singleFileInZipTest/");
-    File parent = new File (testDir, "singleFileRootDir/");
-    File entry = new File (parent, "foo.txt");
-    entry.mkdirs();
-    assertTrue (entry.exists());
-    
-    File zipWithSingleFileInRootdir = new File (testDir, "singleFile.zip");
-    IOUtils.zipUp(parent, zipWithSingleFileInRootdir);
-    
-    entry.delete();
-    assertFalse (entry.exists());
-    
-    IFile zip = new FileImpl(zipWithSingleFileInRootdir, testDir);
-    IOUtils.unpackZip(zip, parent);
-    assertTrue (entry.exists());
+  public void testCopyAndDoNotClose() throws IOException{
+    
+    InputStream is = new InputStream(){
+      int idx=0;
+      int data[]=new int[]{1,2,3,4,5,-1};
+      @Override
+      public int read() throws IOException
+      {
+        if(idx<data.length)
+          return data[idx++];
+        else
+          return -1;
+      }
+      @Override
+      public void close() throws IOException
+      {
+        fail("Close invoked");
+      }
+    };
+    
+    OutputStream os = new OutputStream(){
+      int idx=0;
+      int data[]=new int[]{1,2,3,4,5,-1};
+      @Override
+      public void write(int b) throws IOException
+      {
+        if(b!=data[idx++]){
+          fail("Data written to outputstream was not as expected");
+        }
+      }
+    };
+    
+    IOUtils.copyAndDoNotCloseInputStream(is,os);
+    
   }
 }
-



Mime
View raw message