[jboss-svn-commits] JBoss Common SVN: r3827 - in shrinkwrap/trunk/impl-base/src: test/java/org/jboss/shrinkwrap/impl/base/exporter and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Dec 4 01:00:21 EST 2009


Author: ALRubinger
Date: 2009-12-04 01:00:21 -0500 (Fri, 04 Dec 2009)
New Revision: 3827

Removed:
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterUtil.java
Modified:
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExportDelegate.java
   shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java
Log:
[SHRINKWRAP-95] Explicilty write directory entries for all parent paths

Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExportDelegate.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExportDelegate.java	2009-12-04 05:58:55 UTC (rev 3826)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExportDelegate.java	2009-12-04 06:00:21 UTC (rev 3827)
@@ -19,6 +19,8 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.zip.ZipEntry;
@@ -31,6 +33,8 @@
 import org.jboss.shrinkwrap.impl.base.io.IOUtil;
 import org.jboss.shrinkwrap.impl.base.io.StreamErrorHandler;
 import org.jboss.shrinkwrap.impl.base.io.StreamTask;
+import org.jboss.shrinkwrap.impl.base.path.PathUtil;
+import org.jboss.shrinkwrap.spi.PathProvider;
 
 public class ZipExportDelegate extends AbstractExporterDelegate<InputStream>
 {
@@ -56,6 +60,12 @@
     * ZipOutputStream used to write the zip entries
     */
    private ZipOutputStream zipOutputStream;
+   
+   /**
+    * A Set of Paths we've exported so far (so that we don't write
+    * any entries twice)
+    */
+   private Set<Path> pathsExported = new HashSet<Path>();
 
    //-------------------------------------------------------------------------------------||
    // Constructor ------------------------------------------------------------------------||
@@ -111,25 +121,69 @@
    @Override
    protected void processAsset(final Path path, final Asset asset)
    {
-      final String pathName = ZipExporterUtil.toZipEntryPath(path);
+      // Precondition checks
+      if (path == null)
+      {
+         throw new IllegalArgumentException("Path must be specified");
+      }
+      
+      /*
+       * SHRINKWRAP-94
+       * Add entries for all parents of this Path
+       * by recursing first and adding parents that
+       * haven't already been written.
+       */
+      final Path parent;
+      try
+      {
+         parent = ((PathProvider) path).parent();
+      }
+      catch (final ClassCastException cce)
+      {
+         throw new RuntimeException("Path implementation provided does not implement the SPI "
+               + PathProvider.class.getName(), cce);
+      }
+      if (parent != null && !this.pathsExported.contains(parent))
+      {
+         // Process the parent without any asset (it's a directory)
+         this.processAsset(parent, null);
+      }
+      // Mark if we're writing a directory
+      final boolean isDirectory = asset == null;
 
-      final ZipEntry entry = new ZipEntry(pathName);
-
-      // Get Asset InputStream
-      final InputStream assetStream = asset.openStream();
-
+      // Get Asset InputStream if the asset is specified (else it's a directory so use null)
+      final InputStream assetStream = !isDirectory ? asset.openStream() : null;
+      final String pathName = PathUtil.optionallyRemovePrecedingSlash(path.get());
+      
+      // Make a task for this stream and close when done
       IOUtil.closeOnComplete(assetStream, new StreamTask<InputStream>()
       {
 
          @Override
          public void execute(InputStream stream) throws Exception
          {
+            // If we're writing a directory, ensure we trail a slash for the ZipEntry
+            String resolvedPath = pathName;
+            if (isDirectory)
+            {
+               resolvedPath = PathUtil.optionallyAppendSlash(resolvedPath);
+            }
+            
+            // Make a ZipEntry
+            final ZipEntry entry = new ZipEntry(resolvedPath);
+
             // Write the Asset under the same Path name in the Zip
-            // Make a Zip Entry
             zipOutputStream.putNextEntry(entry);
+            
+            // Mark that we've written this Path 
+            pathsExported.add(path);
 
-            // Read the contents of the asset and write to the JAR
-            IOUtil.copy(stream, zipOutputStream);
+            // Read the contents of the asset and write to the JAR, 
+            // if we're not just a directory
+            if (!isDirectory)
+            {
+               IOUtil.copy(stream, zipOutputStream);
+            }
 
             // Close up the instream and the entry
             zipOutputStream.closeEntry();

Deleted: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterUtil.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterUtil.java	2009-12-04 05:58:55 UTC (rev 3826)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterUtil.java	2009-12-04 06:00:21 UTC (rev 3827)
@@ -1,72 +0,0 @@
-/*
- * 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.exporter;
-
-import org.jboss.shrinkwrap.api.Path;
-
-/**
- * ZipExporterUtil
- * 
- * Utility used to assist exporting to Zip format. 
- * 
- * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
- * @version $Revision: $
- */
-public class ZipExporterUtil
-{
-   //-------------------------------------------------------------------------------------||
-   // Class Members ----------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Slash character
-    */
-   static final String SLASH = "/";
-
-   //-------------------------------------------------------------------------------------||
-   // Constructor ------------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * No instantiation
-    */
-   private ZipExporterUtil()
-   {
-      throw new UnsupportedOperationException("Constructor should never be invoked; this is a static util class");
-   }
-
-   //-------------------------------------------------------------------------------------||
-   // Utilities --------------------------------------------------------------------------||
-   //-------------------------------------------------------------------------------------||
-
-   /**
-    * Converts a Path to a ZipEntry path name
-    * 
-    *  @param path to convert
-    *  @return String representing a ZipEntry path
-    */
-   public static String toZipEntryPath(Path path)
-   {
-
-      String context = path.get();
-      if (context.startsWith(SLASH))
-      {
-         context = context.substring(1);
-      }
-      return context;
-   }
-}

Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java	2009-12-04 05:58:55 UTC (rev 3826)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/exporter/ZipExporterTestCase.java	2009-12-04 06:00:21 UTC (rev 3827)
@@ -30,9 +30,9 @@
 import org.jboss.shrinkwrap.api.Path;
 import org.jboss.shrinkwrap.api.exporter.ArchiveExportException;
 import org.jboss.shrinkwrap.api.exporter.ZipExporter;
-import org.jboss.shrinkwrap.impl.base.exporter.ZipExporterUtil;
 import org.jboss.shrinkwrap.impl.base.io.IOUtil;
 import org.jboss.shrinkwrap.impl.base.path.BasicPath;
+import org.jboss.shrinkwrap.impl.base.path.PathUtil;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -84,9 +84,13 @@
       // Validate entries were written out
       assertAssetInZip(expectedZip, PATH_ONE, ASSET_ONE);
       assertAssetInZip(expectedZip, PATH_TWO, ASSET_TWO);
-
+      
+      // Validate all paths were written
+      // SHRINKWRAP-94
+      getEntryFromZip(expectedZip, NESTED_PATH);
    }
 
+
    /**
     * Test to make sue an archive can be exported to Zip and nested archives are also in exported as nested Zip.
     * @throws Exception
@@ -116,7 +120,7 @@
       Path nestedArchivePath = new BasicPath(NAME_NESTED_ARCHIVE);
 
       // Get Zip entry path
-      String nestedArchiveZipEntryPath = ZipExporterUtil.toZipEntryPath(nestedArchivePath);
+      String nestedArchiveZipEntryPath = PathUtil.optionallyRemovePrecedingSlash(nestedArchivePath.get());
 
       // Get nested archive entry from exported zip
       ZipEntry nestedArchiveEntry = expectedZip.getEntry(nestedArchiveZipEntryPath);
@@ -134,7 +138,7 @@
       Path nestedArchiveTwoPath = new BasicPath(NESTED_PATH, NAME_NESTED_ARCHIVE_2);
 
       // Get Zip entry path
-      String nestedArchiveTwoZipEntryPath = ZipExporterUtil.toZipEntryPath(nestedArchiveTwoPath);
+      String nestedArchiveTwoZipEntryPath = PathUtil.optionallyRemovePrecedingSlash(nestedArchiveTwoPath.get());
 
       // Get second nested archive entry from exported zip
       ZipEntry nestedArchiveTwoEntry = expectedZip.getEntry(nestedArchiveTwoZipEntryPath);
@@ -151,7 +155,7 @@
    }
 
    @Test(expected = ArchiveExportException.class)
-   public void testExportThrowsArchiveExcepitonOnAssetWriteFailure()
+   public void testExportThrowsArchiveExceptionOnAssetWriteFailure()
    {
       log.info("testExportThrowsArchiveExcepitonOnAssetWriteFailure");
       Archive<?> archive = createArchiveWithAssets();
@@ -200,13 +204,29 @@
    private void assertAssetInZip(ZipFile expectedZip, Path path, Asset asset) throws IllegalArgumentException,
          IOException
    {
-      String entryPath = ZipExporterUtil.toZipEntryPath(path);
-      ZipEntry entry = expectedZip.getEntry(entryPath);
-      Assert.assertNotNull(entry);
+      final ZipEntry entry = this.getEntryFromZip(expectedZip, path);
       byte[] expectedContents = IOUtil.asByteArray(asset.openStream());
       byte[] actualContents = IOUtil.asByteArray(expectedZip.getInputStream(entry));
       Assert.assertArrayEquals(expectedContents, actualContents);
    }
+   
+   /**
+    * Obtains the entry from the specified ZIP file at the specified Path, ensuring
+    * it exists along the way
+    * @param expectedZip
+    * @param path
+    * @return
+    * @throws IllegalArgumentException
+    * @throws IOException
+    */
+   private ZipEntry getEntryFromZip(final ZipFile expectedZip, final Path path) throws IllegalArgumentException,
+         IOException
+   {
+      String entryPath = PathUtil.optionallyRemovePrecedingSlash(path.get());
+      ZipEntry entry = expectedZip.getEntry(entryPath);
+      Assert.assertNotNull("Expected path not found in ZIP: " + path, entry);
+      return entry;
+   }
 
    /**
     * Write a InputStream out to file.



More information about the jboss-svn-commits mailing list