Index: api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java =================================================================== --- api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java (revision 3955) +++ api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java Mon Jan 25 09:22:51 CET 2010 @@ -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; } Index: impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java =================================================================== --- impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java (revision 3945) +++ impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java Mon Jan 25 09:31:26 CET 2010 @@ -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,7 +139,35 @@ archive, SecurityActions.getThreadContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI()); } - + + @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); + } + + @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 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. * Index: impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java =================================================================== --- impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java (revision 3836) +++ impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java Mon Jan 25 09:32:11 CET 2010 @@ -147,24 +147,28 @@ public ZipImporter importZip(ZipFile file) { Validate.notNull(file, "File must be specified"); - + + try { - Enumeration entries = file.entries(); - while(entries.hasMoreElements()) - { - ZipEntry entry = entries.nextElement(); + Enumeration 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)); - } + // 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; + return this; } }