[jboss-svn-commits] JBoss Common SVN: r4610 - in shrinkwrap/trunk/extension-tar: src/main/java/org/jboss/shrinkwrap/tar/api/exporter and 8 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jun 29 11:55:19 EDT 2010


Author: ALRubinger
Date: 2010-06-29 11:55:18 -0400 (Tue, 29 Jun 2010)
New Revision: 4610

Added:
   shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzArchiveInputStreamFactory.java
   shrinkwrap/trunk/extension-tar/src/test/java/org/
   shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/
   shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/shrinkwrap/
   shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/shrinkwrap/tar/
   shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/shrinkwrap/tar/impl/
   shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/shrinkwrap/tar/impl/TarGzExporterTestCase.java
Modified:
   shrinkwrap/trunk/extension-tar/pom.xml
   shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/api/exporter/TarGzExporter.java
   shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterDelegate.java
   shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterImpl.java
   shrinkwrap/trunk/extension-tar/src/main/resources/META-INF/services/org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter
Log:
[SHRINKWRAP-194] First tests passing for TAR.GZ export

Modified: shrinkwrap/trunk/extension-tar/pom.xml
===================================================================
--- shrinkwrap/trunk/extension-tar/pom.xml	2010-06-29 15:54:44 UTC (rev 4609)
+++ shrinkwrap/trunk/extension-tar/pom.xml	2010-06-29 15:55:18 UTC (rev 4610)
@@ -41,6 +41,13 @@
       <artifactId>shrinkwrap-impl-base</artifactId>
       <version>${project.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.jboss.shrinkwrap</groupId>
+      <artifactId>shrinkwrap-impl-base</artifactId>
+      <version>${project.version}</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
 
     <!-- 
     External Projects

Modified: shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/api/exporter/TarGzExporter.java
===================================================================
--- shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/api/exporter/TarGzExporter.java	2010-06-29 15:54:44 UTC (rev 4609)
+++ shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/api/exporter/TarGzExporter.java	2010-06-29 15:55:18 UTC (rev 4610)
@@ -18,11 +18,11 @@
 
 import java.io.File;
 import java.io.InputStream;
-import java.io.OutputStream;
 
 import org.jboss.shrinkwrap.api.Assignable;
 import org.jboss.shrinkwrap.api.exporter.ArchiveExportException;
 import org.jboss.shrinkwrap.api.exporter.FileExistsException;
+import org.jboss.shrinkwrap.api.exporter.StreamExporter;
 
 /**
  * Exporter used to represent an {@link Assignable} in TAR format encoded w/
@@ -32,20 +32,13 @@
  * @see http://www.gnu.org/software/tar/manual/html_node/Standard.html
  * @see http://www.gzip.org/
  */
-public interface TarGzExporter extends Assignable
+public interface TarGzExporter extends StreamExporter
 {
    //-------------------------------------------------------------------------------------||
    // Contracts --------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
    /**
-    * Exports this reference as a TAR.GZ archive.
-    * 
-    * @return {@link InputStream} for exported TAR.GZ
-    */
-   InputStream exportTarGz();
-
-   /**
     * Exports provided archive as a TAR.GZ archive, written to the 
     * specified {@link File} target.  If the target exists this call will
     * fail with {@link IllegalArgumentException}
@@ -60,17 +53,6 @@
 
    /**
     * Exports provided archive as a TAR.GZ archive, written to the 
-    * specified {@link OutputStream} target.  The specified
-    * target will be closed upon completion.
-    * 
-    * @param target
-    * @throws ArchiveExportException
-    * @throws IllegalArgumentException If the target is not specified
-    */
-   void exportTarGz(OutputStream target) throws ArchiveExportException, IllegalArgumentException;
-
-   /**
-    * Exports provided archive as a TAR.GZ archive, written to the 
     * specified {@link File} target.  If the target both exists and the "overwrite"
     * flag is true, this call will allow the existing file to be overwritten, else
     * the invocation will fail with {@link IllegalArgumentException}

Added: shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzArchiveInputStreamFactory.java
===================================================================
--- shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzArchiveInputStreamFactory.java	                        (rev 0)
+++ shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzArchiveInputStreamFactory.java	2010-06-29 15:55:18 UTC (rev 4610)
@@ -0,0 +1,50 @@
+/*
+ * 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.tar.impl.exporter;
+
+import java.io.InputStream;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.impl.base.ArchiveInputStreamFactory;
+import org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter;
+
+/**
+ * Factory used create an InputStream from an Archive by exporting the archive as TAR.GZ.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class TarGzArchiveInputStreamFactory extends ArchiveInputStreamFactory
+{
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.ArchiveInputStreamFactory#doGetInputStream(Archive)
+    */
+   @Override
+   protected InputStream doGetInputStream(final Archive<?> archive)
+   {
+      // Get InputStream from the ZipExporter
+      final InputStream inputStream = archive.as(TarGzExporter.class).export();
+      // Return input stream
+      return inputStream;
+   }
+
+}

Modified: shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterDelegate.java
===================================================================
--- shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterDelegate.java	2010-06-29 15:54:44 UTC (rev 4609)
+++ shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterDelegate.java	2010-06-29 15:55:18 UTC (rev 4610)
@@ -63,13 +63,13 @@
       super(archive);
 
       //TODO Can this impl export archives with no entries?
-//      // Precondition check
-//      if (archive.getContent().isEmpty())
-//      {
-//         throw new IllegalArgumentException(
-//               "[SHRINKWRAP-93] Cannot use this JDK-based implementation to export as ZIP an archive with no content: "
-//                     + archive.toString());
-//      }
+      //      // Precondition check
+      //      if (archive.getContent().isEmpty())
+      //      {
+      //         throw new IllegalArgumentException(
+      //               "[SHRINKWRAP-93] Cannot use this JDK-based implementation to export as ZIP an archive with no content: "
+      //                     + archive.toString());
+      //      }
    }
 
    //-------------------------------------------------------------------------------------||
@@ -106,7 +106,8 @@
    protected final void putNextExtry(final TarGzOutputStream outputStream, final String context) throws IOException
    {
       // Put
-      outputStream.putNextEntry(new TarEntry(context));
+      final TarEntry entry = new TarEntry(context);
+      outputStream.putNextEntry(entry);
    }
 
    /**
@@ -136,19 +137,19 @@
                log.log(Level.WARNING, "Exception encountered during export of archive", e);
 
                //TODO Can this impl export archives with no entries?  JDK ZIP impl cannot. 
-//               // SHRINKWRAP-133 - if the output is empty, it won't close and a deadlock is triggered
-//               final Set<ArchivePath> pathsExported = TarGzExporterDelegate.this.getExportedPaths();
-//               if (pathsExported.isEmpty())
-//               {
-//                  // Ensure the streams are set up before we do any work on them;
-//                  // it's possible that we encountered an exception before 
-//                  // everything has been initialized by the main Thread
-//                  // SHRINKWRAP-137
-//                  latch.await();
-//
-//                  // Write a dummy entry just so the JDK ZIP impl can close cleanly
-//                  putNextExtry(outputStream, "dummy.txt");
-//               }
+               //               // SHRINKWRAP-133 - if the output is empty, it won't close and a deadlock is triggered
+               //               final Set<ArchivePath> pathsExported = TarGzExporterDelegate.this.getExportedPaths();
+               //               if (pathsExported.isEmpty())
+               //               {
+               //                  // Ensure the streams are set up before we do any work on them;
+               //                  // it's possible that we encountered an exception before 
+               //                  // everything has been initialized by the main Thread
+               //                  // SHRINKWRAP-137
+               //                  latch.await();
+               //
+               //                  // Write a dummy entry just so the JDK ZIP impl can close cleanly
+               //                  putNextExtry(outputStream, "dummy.txt");
+               //               }
 
                throw e;
             }

Modified: shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterImpl.java
===================================================================
--- shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterImpl.java	2010-06-29 15:54:44 UTC (rev 4609)
+++ shrinkwrap/trunk/extension-tar/src/main/java/org/jboss/shrinkwrap/tar/impl/exporter/TarGzExporterImpl.java	2010-06-29 15:55:18 UTC (rev 4610)
@@ -47,6 +47,7 @@
    /**
     * Logger
     */
+   @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(TarGzExporterImpl.class.getName());
 
    //-------------------------------------------------------------------------------------||
@@ -67,10 +68,10 @@
 
    /**
     * {@inheritDoc}
-    * @see org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter#exportTarGz()
+    * @see org.jboss.shrinkwrap.api.exporter.StreamExporter#export()
     */
    @Override
-   public InputStream exportTarGz()
+   public InputStream export()
    {
       // Create export delegate
       final AbstractExporterDelegate<InputStream> exportDelegate = new TarGzExporterDelegate(this.getArchive());
@@ -79,12 +80,13 @@
       return exportDelegate.export();
    }
 
+   
    /**
     * {@inheritDoc}
-    * @see org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter#exportTarGz(java.io.OutputStream)
+    * @see org.jboss.shrinkwrap.api.exporter.StreamExporter#export(java.io.OutputStream)
     */
    @Override
-   public void exportTarGz(final OutputStream target) throws ArchiveExportException, IllegalArgumentException
+   public void export(final OutputStream target) throws ArchiveExportException, IllegalArgumentException
    {
       // Precondition checks
       if (target == null)
@@ -93,7 +95,7 @@
       }
 
       // Get Stream
-      final InputStream in = this.exportTarGz();
+      final InputStream in = this.export();
 
       // Write out
       try
@@ -118,7 +120,7 @@
       final OutputStream out = this.getOutputStreamToFile(target, overwrite);
 
       // Write out
-      this.exportTarGz(out);
+      this.export(out);
    }
 
    /**

Modified: shrinkwrap/trunk/extension-tar/src/main/resources/META-INF/services/org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter
===================================================================
--- shrinkwrap/trunk/extension-tar/src/main/resources/META-INF/services/org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter	2010-06-29 15:54:44 UTC (rev 4609)
+++ shrinkwrap/trunk/extension-tar/src/main/resources/META-INF/services/org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter	2010-06-29 15:55:18 UTC (rev 4610)
@@ -1 +1,2 @@
-org.jboss.shrinkwrap.tar.impl.exporter.TarGzExporterImpl
\ No newline at end of file
+implementingClassName=org.jboss.shrinkwrap.tar.impl.exporter.TarGzExporterImpl
+extension=.tar.gz
\ No newline at end of file

Added: shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/shrinkwrap/tar/impl/TarGzExporterTestCase.java
===================================================================
--- shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/shrinkwrap/tar/impl/TarGzExporterTestCase.java	                        (rev 0)
+++ shrinkwrap/trunk/extension-tar/src/test/java/org/jboss/shrinkwrap/tar/impl/TarGzExporterTestCase.java	2010-06-29 15:55:18 UTC (rev 4610)
@@ -0,0 +1,244 @@
+/*
+ * 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.tar.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.GZIPInputStream;
+
+import org.jboss.javatar.TarEntry;
+import org.jboss.javatar.TarInputStream;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePath;
+import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.asset.Asset;
+import org.jboss.shrinkwrap.api.exporter.StreamExporter;
+import org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase;
+import org.jboss.shrinkwrap.impl.base.io.IOUtil;
+import org.jboss.shrinkwrap.impl.base.path.PathUtil;
+import org.jboss.shrinkwrap.tar.api.exporter.TarGzExporter;
+import org.junit.Assert;
+
+/**
+ * TestCase to ensure that the {@link TarGzExporter} correctly exports
+ * archives to TAR.GZ format.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public final class TarGzExporterTestCase extends StreamExporterTestBase
+{
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Extension for archives
+    */
+   private static final String EXTENSION = ".tar.gz";
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#exportAsInputStream(org.jboss.shrinkwrap.api.Archive)
+    */
+   @Override
+   protected InputStream exportAsInputStream(final Archive<?> archive)
+   {
+      assert archive != null : "archive must be specified";
+      return archive.as(TarGzExporter.class).export();
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#exportAsFile(org.jboss.shrinkwrap.api.Archive, java.io.File, boolean)
+    */
+   @Override
+   protected void exportAsFile(final Archive<?> archive, final File file, final boolean overwrite)
+   {
+      // Precondition checks
+      assert file != null : "file must be specified";
+      assert archive != null : "archive must be specified";
+
+      // Export
+      archive.as(TarGzExporter.class).exportTarGz(file, overwrite);
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#exportToOutputStream(org.jboss.shrinkwrap.api.Archive, java.io.OutputStream)
+    */
+   @Override
+   protected void exportToOutputStream(final Archive<?> archive, final OutputStream out)
+   {
+      assert archive != null : "archive must be specified";
+      assert out != null : "outstream must be specified";
+      archive.as(TarGzExporter.class).export(out);
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#ensureInExpectedForm(java.io.File)
+    */
+   @Override
+   protected void ensureInExpectedForm(final File file) throws IOException
+   {
+      // Precondition check
+      assert file != null : "file must be specified";
+
+      // Validate
+      this.ensureTarGzFileInExpectedForm(file);
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#getAssetFromExportedFile(java.io.File, org.jboss.shrinkwrap.api.ArchivePath)
+    */
+   @Override
+   protected InputStream getContentsFromExportedFile(final File file, final ArchivePath path) throws IOException
+   {
+      // Precondition checks
+      assert file != null : "file must be specified";
+      assert path != null : "path must be specified";
+
+      // Get as TAR.GZ
+      return this.getEntryFromTarGz(file, path);
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.exporter.ExportTestBase#getStreamExporter()
+    */
+   @Override
+   protected Class<? extends StreamExporter> getStreamExporter()
+   {
+      return TarGzExporter.class;
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.shrinkwrap.impl.base.exporter.ExportTestBase#getArchiveExtension()
+    */
+   @Override
+   protected String getArchiveExtension()
+   {
+      return EXTENSION;
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   //TODO Necessary?
+   //   /**
+   //    * Test to ensure that the {@link JdkZipExporterDelegate} does not accept 
+   //    * an empty archive as input
+   //    * 
+   //    * SHRINKWRAP-93
+   //    * 
+   //    * @throws Exception
+   //    */
+   //   @Test(expected = IllegalArgumentException.class)
+   //   public void exportEmptyArchiveAsZip() throws Exception
+   //   {
+   //      // Attempt to export an empty archive should fail
+   //      ShrinkWrap.create(JavaArchive.class, NAME_ARCHIVE).as(TarGzExporter.class).exportTarGz();
+   //   }
+
+   //-------------------------------------------------------------------------------------||
+   // Internal Helper Methods ------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that the specified TAR.GZ {@link File} contains entries
+    * in the expected form
+    * @param expectedZip
+    * @throws IOException
+    */
+   private void ensureTarGzFileInExpectedForm(final File archive) throws IOException
+   {
+      // Validate entries were written out
+      assertAssetInTarGz(archive, PATH_ONE, ASSET_ONE);
+      assertAssetInTarGz(archive, PATH_TWO, ASSET_TWO);
+
+      // Validate all paths were written
+      // SHRINKWRAP-94
+      getEntryFromTarGz(archive, NESTED_PATH);
+
+      // Ensure we don't write the root Path
+      // SHRINKWRAP-96
+      InputStream rootEntry = this.getEntryFromTarGz(archive, ArchivePaths.root());
+      Assert.assertNull("TAR.GZ should not have explicit root path written (SHRINKWRAP-96)", rootEntry);
+   }
+
+   /**
+    * Assert an asset is actually in the TAR.GZ file
+    * @throws IOException 
+    * @throws IllegalArgumentException 
+    */
+   private void assertAssetInTarGz(final File archive, final ArchivePath path, final Asset asset)
+         throws IllegalArgumentException, IOException
+   {
+      final InputStream in = this.getEntryFromTarGz(archive, path);
+      byte[] expectedContents = IOUtil.asByteArray(asset.openStream());
+      byte[] actualContents = IOUtil.asByteArray(in);
+      Assert.assertArrayEquals(expectedContents, actualContents);
+   }
+
+   /**
+    * Obtains an {@link InputStream} to an entry of specified name from the specified TAR.GZ file, 
+    * or null if not found.  We have to iterate through all entries for a matching name, as the 
+    * instream does not support random access.
+    * @param expectedZip
+    * @param path
+    * @return
+    * @throws IllegalArgumentException
+    * @throws IOException
+    */
+   private InputStream getEntryFromTarGz(final File archive, final ArchivePath path) throws IllegalArgumentException,
+         IOException
+   {
+      String entryPath = PathUtil.optionallyRemovePrecedingSlash(path.get());
+      final TarInputStream in = new TarInputStream(new GZIPInputStream(new FileInputStream(archive)));
+      TarEntry currentEntry = null;
+      while ((currentEntry = in.getNextEntry()) != null)
+      {
+         final String entryName = currentEntry.getName();
+         if (currentEntry.isDirectory())
+         {
+            entryPath = PathUtil.optionallyAppendSlash(entryPath);
+         }
+         else
+         {
+            entryPath = PathUtil.optionallyRemoveFollowingSlash(entryPath);
+         }
+         if (entryName.equals(entryPath))
+         {
+            return in;
+         }
+      }
+      // Not found
+      return null;
+   }
+
+}



More information about the jboss-svn-commits mailing list