[jboss-svn-commits] JBoss Common SVN: r3956 - in shrinkwrap/trunk: impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jan 25 10:31:16 EST 2010


Author: ALRubinger
Date: 2010-01-25 10:31:15 -0500 (Mon, 25 Jan 2010)
New Revision: 3956

Modified:
   shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java
   shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java
Log:
[SHRINKWRAP-122] Apply patch by Ken Gullaksen to check for ArchiveImportException on ZipImporter calls.  Also adjusted some JavaDocs.

Modified: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java	2010-01-24 21:41:28 UTC (rev 3955)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java	2010-01-25 15:31:15 UTC (rev 3956)
@@ -23,7 +23,7 @@
 import org.jboss.shrinkwrap.api.Assignable;
 
 /**
- * ZipImporter
+ * {@link Assignable} type capable of importing ZIP content.
  *
  * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
  * @version $Revision: $
@@ -38,7 +38,8 @@
    * 
    * @param stream the stream to import
    * @return Archive of the imported Zip
-   * @throws ArchiveImportException If an error occured during the import process
+   * @throws ArchiveImportException If an error occurred during the import process
+   * @throws IllegalArgumentException If no stream is specified
    */
    ZipImporter importZip(ZipInputStream stream) throws ArchiveImportException;
 
@@ -47,7 +48,8 @@
     * 
     * @param file the file to import
     * @return Archive of the imported Zip
-    * @throws ArchiveImportException If an error occured during the import process
+    * @throws ArchiveImportException If an error occurred during the import process
+    * @throws IllegalArgumentException If no file is specified
     */
    ZipImporter importZip(ZipFile file) throws ArchiveImportException;
 }

Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java	2010-01-24 21:41:28 UTC (rev 3955)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java	2010-01-25 15:31:15 UTC (rev 3956)
@@ -147,24 +147,28 @@
    public ZipImporter importZip(ZipFile file)
    {
       Validate.notNull(file, "File must be specified");
-      
-      Enumeration<? extends ZipEntry> entries = file.entries();
-      while(entries.hasMoreElements())
-      {
-         ZipEntry entry = entries.nextElement();
 
-         // Get the entry (path) name
-         final String entryName = entry.getName();
-         
-         // Handle directories separately
-         if(entry.isDirectory())
-         {
-            archive.add(DirectoryAsset.INSTANCE, entryName);
-            continue; 
-         }
-         
-         archive.add(new ZipFileEntryAsset(file, entry), new BasicPath(entryName));
-      }
-      return this;
+       try {
+           Enumeration<? extends ZipEntry> entries = file.entries();
+           while(entries.hasMoreElements())
+           {
+              ZipEntry entry = entries.nextElement();
+
+              // Get the entry (path) name
+              final String entryName = entry.getName();
+
+              // Handle directories separately
+              if(entry.isDirectory())
+              {
+                 archive.add(DirectoryAsset.INSTANCE, entryName);
+                 continue;
+              }
+
+              archive.add(new ZipFileEntryAsset(file, entry), new BasicPath(entryName));
+           }
+       } catch (Exception e) {
+           throw new ArchiveImportException("Could not import file", e);
+       }
+       return this;
    }
 }

Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java	2010-01-24 21:41:28 UTC (rev 3955)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java	2010-01-25 15:31:15 UTC (rev 3956)
@@ -18,10 +18,12 @@
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.List;
 import java.util.logging.Logger;
 import java.util.zip.ZipEntry;
@@ -33,6 +35,7 @@
 import org.jboss.shrinkwrap.api.Archive;
 import org.jboss.shrinkwrap.api.Archives;
 import org.jboss.shrinkwrap.api.exporter.ZipExporter;
+import org.jboss.shrinkwrap.api.importer.ArchiveImportException;
 import org.jboss.shrinkwrap.api.importer.ZipImporter;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
 import org.jboss.shrinkwrap.impl.base.asset.ClassLoaderAsset;
@@ -136,6 +139,46 @@
             archive, 
             SecurityActions.getThreadContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI());
    }
+
+   /**
+    * Ensures that an import of {@link InputStream} results in {@link ArchiveImportException}
+    * if an unexpected error occurred.
+    * @throws Exception
+    */
+   @Test(expected = ArchiveImportException.class)
+   public void shouldThrowExceptionOnErrorInImportFromStream() throws Exception
+   {
+      ZipInputStream stream = new ZipInputStream(new InputStream()
+      {
+         @Override
+         public int read() throws IOException
+         {
+            throw new IOException("Mock exception");
+         }
+      });
+      Archives.create("test.jar", ZipImporter.class).importZip(stream).as(JavaArchive.class);
+   }
+
+   /**
+    * Ensures that an import of {@link ZipFile} results in {@link ArchiveImportException}
+    * if an unexpected error occurred.
+    * @throws Exception
+    */
+   @Test(expected = ArchiveImportException.class)
+   public void shouldThrowExceptionOnErrorInImportFromFile() throws Exception
+   {
+      ZipFile testZip = new ZipFile(new File(SecurityActions.getThreadContextClassLoader().getResource(
+            EXISTING_ZIP_RESOURCE).toURI()))
+      {
+         @Override
+         public Enumeration<? extends ZipEntry> entries()
+         {
+            throw new IllegalStateException("mock  exception");
+         }
+      };
+      Archives.create("test.jar", ZipImporter.class).importZip(testZip).as(JavaArchive.class);
+   }
+
    
    /**
     * Compare the content of the original file and what was imported.



More information about the jboss-svn-commits mailing list