[jboss-svn-commits] JBoss Common SVN: r3538 - in shrinkwrap/trunk/impl-base/src: main/java/org/jboss/shrinkwrap/impl/base/io and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Sep 22 23:49:53 EDT 2009
Author: johnbailey
Date: 2009-09-22 23:49:53 -0400 (Tue, 22 Sep 2009)
New Revision: 3538
Added:
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamErrorHandler.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamTask.java
Modified:
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/AbstractExporterDelegate.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterImpl.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExportDelegate.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterImpl.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterTestCase.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterTestCase.java
Log:
[SHRINKWRAP-41] - Refactory exporter impls and tests to improve test coverage
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/AbstractExporterDelegate.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/AbstractExporterDelegate.java 2009-09-22 06:24:26 UTC (rev 3537)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/AbstractExporterDelegate.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -69,10 +69,8 @@
/**
* Primary method providing a template for exporting the contents of an archive
- *
- * @return
*/
- protected T export()
+ protected void export()
{
// Get archive
Archive<?> archive = getArchive();
@@ -94,8 +92,6 @@
// Process the asset
processAsset(path, asset);
}
-
- return getResult();
}
//-------------------------------------------------------------------------------------||
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterImpl.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterImpl.java 2009-09-22 06:24:26 UTC (rev 3537)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterImpl.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -74,11 +74,14 @@
ExplodedExporterDelegate exporterDelegate = new ExplodedExporterDelegate(archive, baseDirectory);
// Run the export
- File explodedDirectory = exporterDelegate.export();
+ exporterDelegate.export();
+
+ // Get Result
+ File explodedDirectory = exporterDelegate.getResult();
if (log.isLoggable(Level.FINE))
{
- log.fine("Created Exploded Atchive: " + explodedDirectory.getAbsolutePath());
+ log.fine("Created Exploded Archive: " + explodedDirectory.getAbsolutePath());
}
// Return the exploded dir
return explodedDirectory;
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExportDelegate.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExportDelegate.java 2009-09-22 06:24:26 UTC (rev 3537)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExportDelegate.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -18,7 +18,6 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -30,6 +29,8 @@
import org.jboss.shrinkwrap.api.Path;
import org.jboss.shrinkwrap.api.export.ArchiveExportException;
import org.jboss.shrinkwrap.impl.base.io.IOUtil;
+import org.jboss.shrinkwrap.impl.base.io.StreamErrorHandler;
+import org.jboss.shrinkwrap.impl.base.io.StreamTask;
public class ZipExportDelegate extends AbstractExporterDelegate<InputStream>
{
@@ -77,23 +78,30 @@
* @see org.jboss.shrinkwrap.impl.base.export.AbstractExporterDelegate#export()
*/
@Override
- protected InputStream export()
+ protected void export()
{
+ zipOutputStream = new ZipOutputStream(output);
// Enclose every IO Operation so we can close up cleanly
- try
+ IOUtil.closeOnComplete(zipOutputStream, new StreamTask<ZipOutputStream>()
{
- // Initialize the output streams
- zipOutputStream = new ZipOutputStream(output);
- return super.export();
- }
- catch (Exception ex)
+ @Override
+ public void execute(ZipOutputStream stream) throws Exception
+ {
+ ZipExportDelegate.super.export();
+ }
+
+ }, new StreamErrorHandler()
{
- // Problem occurred make sure the output is closed
- closeOutputStream();
- throw new ArchiveExportException("Failed to export archive " + getArchive().getName(), ex);
- }
+
+ @Override
+ public void handle(Throwable t)
+ {
+ throw new ArchiveExportException("Failed to export Zip: " + getArchive().getName(), t);
+ }
+
+ });
}
/**
@@ -101,42 +109,42 @@
* @see org.jboss.shrinkwrap.impl.base.export.AbstractExporterDelegate#processAsset(Path, Asset)
*/
@Override
- protected void processAsset(Path path, Asset asset)
+ protected void processAsset(final Path path, final Asset asset)
{
final String pathName = ZipExporterUtil.toZipEntryPath(path);
final ZipEntry entry = new ZipEntry(pathName);
- final InputStream in = asset.getStream();
- // Write the Asset under the same Path name in the Zip
- try
+ // Get Asset InputStream
+ final InputStream assetStream = asset.getStream();
+
+ IOUtil.closeOnComplete(assetStream, new StreamTask<InputStream>()
{
- // Make a Zip Entry
- zipOutputStream.putNextEntry(entry);
- // Read the contents of the asset and write to the JAR
- IOUtil.copy(in, zipOutputStream);
+ @Override
+ public void execute(InputStream stream) throws Exception
+ {
+ // Write the Asset under the same Path name in the Zip
+ // Make a Zip Entry
+ zipOutputStream.putNextEntry(entry);
- // Close up the instream and the entry
- zipOutputStream.closeEntry();
- }
- // Some error in writing this entry/asset
- catch (final IOException ioe)
+ // Read the contents of the asset and write to the JAR
+ IOUtil.copy(stream, zipOutputStream);
+
+ // Close up the instream and the entry
+ zipOutputStream.closeEntry();
+ }
+
+ }, new StreamErrorHandler()
{
- // Throw
- throw new ArchiveExportException("Could not start new entry for " + pathName, ioe);
- }
- finally
- {
- // Try to close the instream. Out stream is closed in finally block below.
- try
+
+ @Override
+ public void handle(Throwable t)
{
- in.close();
+ throw new ArchiveExportException("Failed to write asset to Zip: " + pathName, t);
}
- catch (IOException ignored)
- {
- }
- }
+
+ });
}
/**
@@ -146,9 +154,6 @@
@Override
protected InputStream getResult()
{
- // Make sure the output is closed
- closeOutputStream();
-
// Flush the output to a byte array
final byte[] zipContent = output.toByteArray();
if (log.isLoggable(Level.FINE))
@@ -163,26 +168,4 @@
return inputStream;
}
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Close the ZipOutputStream
- */
- private void closeOutputStream()
- {
- try
- {
- if (zipOutputStream != null)
- {
- zipOutputStream.close();
- zipOutputStream = null;
- }
- }
- catch (final IOException ignored)
- {
- }
- }
-
}
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterImpl.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterImpl.java 2009-09-22 06:24:26 UTC (rev 3537)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterImpl.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -29,7 +29,6 @@
* Implementation of ZipExporter used to export an Archive as a Zip format.
*
* @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
* @version $Revision: $
*/
public class ZipExporterImpl extends ZipExporter
@@ -61,7 +60,9 @@
ZipExportDelegate exportDelegate = new ZipExportDelegate(archive);
// Execute export
- InputStream inputStream = exportDelegate.export();
+ exportDelegate.export();
+ // Get results
+ InputStream inputStream = exportDelegate.getResult();
// Return input stream
return inputStream;
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java 2009-09-22 06:24:26 UTC (rev 3537)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/IOUtil.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -17,6 +17,7 @@
package org.jboss.shrinkwrap.impl.base.io;
import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -39,6 +40,20 @@
// Class Members ----------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
+ /**
+ * Default Error Handler
+ */
+ private static final StreamErrorHandler DEFAULT_ERROR_HANDLER = new StreamErrorHandler()
+ {
+
+ @Override
+ public void handle(Throwable t)
+ {
+ throw new RuntimeException(t);
+ }
+
+ };
+
//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -190,4 +205,60 @@
+ ". It is either not a directory or does not exist.");
}
}
+
+ /**
+ * Helper method to run a specified task and automatically handle the closing of the stream.
+ *
+ * @param <S>
+ * @param task
+ * @param errorHandler
+ */
+ public static <S extends Closeable> void closeOnComplete(StreamTask<S> task, StreamErrorHandler errorHandler)
+ {
+
+ }
+
+ /**
+ * Helper method to run a specified task and automatically handle the closing of the stream.
+ *
+ * @param stream
+ * @param task
+ */
+ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task)
+ {
+ closeOnComplete(stream, task, DEFAULT_ERROR_HANDLER);
+ }
+
+ /**
+ * Helper method to run a specified task and automatically handle the closing of the stream.
+ *
+ * @param <S>
+ * @param task
+ * @param errorHandler
+ */
+ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task,
+ StreamErrorHandler errorHandler)
+ {
+ try
+ {
+ task.execute(stream);
+ }
+ catch (Throwable t)
+ {
+ errorHandler.handle(t);
+ }
+ finally
+ {
+ try
+ {
+ if (stream != null)
+ {
+ stream.close();
+ }
+ }
+ catch (Exception ex)
+ {
+ }
+ }
+ }
}
Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamErrorHandler.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamErrorHandler.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamErrorHandler.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.impl.base.io;
+
+/**
+ * Handler for errors that occur during StreamTask processing.
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * @version $Revision: $
+ */
+public interface StreamErrorHandler
+{
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Handles a Throwable that was thrown during stream processing.
+ *
+ * @param t
+ */
+ void handle(Throwable t);
+}
Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamTask.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamTask.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/io/StreamTask.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.impl.base.io;
+
+import java.io.Closeable;
+
+/**
+ * A I/O operation to be executed against a I/O stream in the context of a StreamTemplate
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * @version $Revision: $
+ */
+public interface StreamTask<S extends Closeable>
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * This method execute's this StreamTask with the provided stream.
+ *
+ * @param stream This parameter holds the stream that this StreamTask operates on to execute its task.
+ * @throws Exception This method will throw an instance of Exception if an unrecoverable error is encountered while
+ * performing its task.
+ */
+ void execute(S stream) throws Exception;
+
+}
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterTestCase.java 2009-09-22 06:24:26 UTC (rev 3537)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ExplodedExporterTestCase.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -26,8 +26,10 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Asset;
import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.api.export.ArchiveExportException;
import org.jboss.shrinkwrap.api.export.ExplodedExporter;
import org.jboss.shrinkwrap.impl.base.MemoryMapArchiveImpl;
+import org.jboss.shrinkwrap.impl.base.asset.ClassLoaderAsset;
import org.jboss.shrinkwrap.impl.base.io.IOUtil;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
import org.junit.Assert;
@@ -103,7 +105,7 @@
// Get an archive instance
Archive<?> archive = createArchiveWithNestedArchives();
-
+
// Export as Exploded directory
File explodedDirectory = ExplodedExporter.exportExploded(archive, tempDirectory);
@@ -193,9 +195,9 @@
* @throws Exception
*/
@Test
- public void testExportExplodedRequiresValidDirectory() throws Exception
+ public void testExportExplodedRequiresExistingDirectory() throws Exception
{
- log.info("testExportExplodedRequiresValidDirectory");
+ log.info("testExportExplodedRequiresExistingDirectory");
try
{
final File directory = this.getNonexistantDirectory();
@@ -207,6 +209,76 @@
}
}
+ /**
+ * Ensure ExpolodedExporter requires a directory
+ */
+ @Test
+ public void testExportExplodedRequiresValidDirectory() throws Exception
+ {
+ log.info("testExportExplodedRequiresValidDirectory");
+ try
+ {
+ final File nonDirectory = new File(this.getTarget(), "tempFile.txt");
+ nonDirectory.createNewFile();
+ ExplodedExporter.exportExploded(new MemoryMapArchiveImpl(), nonDirectory);
+ Assert.fail("Should have thrown IllegalArgumentException");
+ }
+ catch (IllegalArgumentException expected)
+ {
+ }
+ }
+
+ /**
+ * Ensure an ArchiveExportException is thrown when output directory can not be created
+ */
+ @Test
+ public void testExportExplodedOutpuDirCreationFails() throws Exception
+ {
+ log.info("testExportExplodedOutpuDirCreationFails");
+ try
+ {
+ final File directory = createTempDirectory("testExportExplodedOutpuDirCreationFails");
+ // Will cause the creation of Archive directory to fail
+ final File existingFile = new File(directory, NAME_ARCHIVE);
+ existingFile.createNewFile();
+ ExplodedExporter.exportExploded(new MemoryMapArchiveImpl(NAME_ARCHIVE), directory);
+ Assert.fail("Should have thrown ArchiveExportException");
+ }
+ catch (ArchiveExportException expected)
+ {
+ }
+ }
+
+ /**
+ * Ensure ArchiveException is thrown if Asset can not be written
+ */
+ @Test
+ public void testExportExplodedThrowsExceptionOnAssetWrite() throws Exception
+ {
+ log.info("testExportExplodedThrowsExceptionOnAssetWrite");
+ try
+ {
+ Archive<?> archive = createArchiveWithAssets();
+ archive.add(new BasicPath("badAsset"), new Asset()
+ {
+
+ @Override
+ public InputStream getStream()
+ {
+ throw new RuntimeException("Mock Esception getting Stream");
+ }
+
+ });
+ final File directory = createTempDirectory("testExportExplodedThrowsExceptionOnAssetWrite");
+
+ ExplodedExporter.exportExploded(archive, directory);
+ Assert.fail("Should have thrown ArchiveExportException");
+ }
+ catch (ArchiveExportException expected)
+ {
+ }
+ }
+
//-------------------------------------------------------------------------------------||
// Internal Helper Methods ------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterTestCase.java 2009-09-22 06:24:26 UTC (rev 3537)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/export/ZipExporterTestCase.java 2009-09-23 03:49:53 UTC (rev 3538)
@@ -28,6 +28,7 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Asset;
import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.api.export.ArchiveExportException;
import org.jboss.shrinkwrap.api.export.ZipExporter;
import org.jboss.shrinkwrap.impl.base.io.IOUtil;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
@@ -166,6 +167,33 @@
}
}
+ @Test
+ public void testExportThrowsArchiveExcepitonOnAssetWriteFailure()
+ {
+ log.info("testExportThrowsArchiveExcepitonOnAssetWriteFailure");
+ try
+ {
+ Archive<?> archive = createArchiveWithAssets();
+
+ archive.add(PATH_ONE, new Asset()
+ {
+
+ @Override
+ public InputStream getStream()
+ {
+ throw new RuntimeException("Mock Exception from an Asset write");
+ }
+
+ });
+
+ ZipExporter.exportZip(archive);
+ Assert.fail("Should have thrown ArchiveExportException");
+ }
+ catch (ArchiveExportException expected)
+ {
+ }
+ }
+
//-------------------------------------------------------------------------------------||
// Internal Helper Methods ------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
More information about the jboss-svn-commits
mailing list