[jboss-svn-commits] JBoss Common SVN: r4465 - in shrinkwrap/trunk: api/src/test/resources and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Jun 1 15:17:40 EDT 2010
Author: ALRubinger
Date: 2010-06-01 15:17:40 -0400 (Tue, 01 Jun 2010)
New Revision: 4465
Added:
shrinkwrap/trunk/api/src/test/resources/nonzipfile.txt
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipContentAssertionDelegate.java
Modified:
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ArchiveFactory.java
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ShrinkWrap.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java
Log:
[SHRINKWRAP-182] Add ShrinkWrap.createFromZipFile
Modified: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ArchiveFactory.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ArchiveFactory.java 2010-05-30 21:36:50 UTC (rev 4464)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ArchiveFactory.java 2010-06-01 19:17:40 UTC (rev 4465)
@@ -16,8 +16,14 @@
*/
package org.jboss.shrinkwrap.api;
+import java.io.File;
+import java.io.IOException;
import java.util.UUID;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
+import org.jboss.shrinkwrap.api.importer.ArchiveImportException;
+import org.jboss.shrinkwrap.api.importer.ZipImporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
/**
@@ -106,8 +112,8 @@
* @throws IllegalArgumentException if type is not specified
* @throws UnknownExtensionTypeException If no extension mapping is found for the specified type
*/
- public <T extends Assignable> T create(final Class<T> type)
- throws IllegalArgumentException, UnknownExtensionTypeException
+ public <T extends Assignable> T create(final Class<T> type) throws IllegalArgumentException,
+ UnknownExtensionTypeException
{
// Precondition checks
if (type == null)
@@ -121,10 +127,10 @@
{
throw UnknownExtensionTypeException.newInstance(type);
}
-
+
// Generate a random name
String archiveName = UUID.randomUUID().toString();
-
+
// Delegate
return create(type, archiveName += extensionType);
}
@@ -136,11 +142,11 @@
*
* @param type The type of the archive e.g. {@link org.jboss.shrinkwrap.api.spec.WebArchive}
* @param archiveName the archiveName to use
- * @return An {@link Assignable} archive base
+ * @return An {@link Assignable} view
* @throws IllegalArgumentException either argument is not supplied
*/
public <T extends Assignable> T create(final Class<T> type, final String archiveName)
- throws IllegalArgumentException
+ throws IllegalArgumentException
{
// Precondition checks
if (type == null)
@@ -157,4 +163,59 @@
{archiveName, configuration}, Archive.class);
return archive.as(type);
}
+
+ /**
+ * Creates a new archive of the specified type as imported
+ * from the specified {@link File}. The file is expected to be encoded as
+ * ZIP (ie. JAR/WAR/EAR). The name of the archive will be set to {@link File#getName()}.
+ * The archive will be be backed by the {@link Configuration}
+ * specific to this {@link ArchiveFactory}.
+ *
+ * @param type The type of the archive e.g. {@link org.jboss.shrinkwrap.api.spec.WebArchive}
+ * @param archiveName the archiveName to use
+ * @return An {@link Assignable} view
+ * @throws IllegalArgumentException If either argument is not supplied, if the specified
+ * {@link File} does not exist, or is not a valid ZIP file
+ */
+ public <T extends Assignable> T createFromZipFile(final Class<T> type, final File archiveFile)
+ throws IllegalArgumentException, ArchiveImportException
+ {
+ // Precondition checks
+ if (type == null)
+ {
+ throw new IllegalArgumentException("Type must be specified");
+ }
+ if (archiveFile == null)
+ {
+ throw new IllegalArgumentException("file must be specified");
+ }
+ if (!archiveFile.exists())
+ {
+ throw new IllegalArgumentException("File for import exist: " + archiveFile.getAbsolutePath());
+ }
+ if (archiveFile.isDirectory())
+ {
+ throw new IllegalArgumentException("File for import must not be a directory: " + archiveFile.getAbsolutePath());
+ }
+
+ // Construct ZipFile
+ final ZipFile zipFile;
+ try
+ {
+ zipFile = new ZipFile(archiveFile);
+ }
+ catch (final ZipException ze)
+ {
+ throw new IllegalArgumentException("Does not appear to be a valid ZIP file: " + archiveFile.getAbsolutePath());
+ }
+ catch (final IOException ioe)
+ {
+ throw new RuntimeException("I/O Error in importing new archive from ZIP: " + archiveFile.getAbsolutePath(),
+ ioe);
+ }
+
+ // Import
+ return ShrinkWrap.create(type, archiveFile.getName()).as(ZipImporter.class).importZip(zipFile).as(type);
+
+ }
}
Modified: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ShrinkWrap.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ShrinkWrap.java 2010-05-30 21:36:50 UTC (rev 4464)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/ShrinkWrap.java 2010-06-01 19:17:40 UTC (rev 4465)
@@ -16,6 +16,9 @@
*/
package org.jboss.shrinkwrap.api;
+import java.io.File;
+
+import org.jboss.shrinkwrap.api.importer.ArchiveImportException;
import org.jboss.shrinkwrap.api.spec.WebArchive;
/**
@@ -175,8 +178,7 @@
* @throws IllegalArgumentException if type is not specified
* @throws UnknownExtensionTypeException If no extension mapping is found for the specified type
*/
- public static <T extends Assignable> T create(final Class<T> type)
- throws IllegalArgumentException
+ public static <T extends Assignable> T create(final Class<T> type) throws IllegalArgumentException
{
// Precondition checks
if (type == null)
@@ -202,7 +204,7 @@
* @throws IllegalArgumentException either argument is not specified
*/
public static <T extends Assignable> T create(final Class<T> type, final String archiveName)
- throws IllegalArgumentException
+ throws IllegalArgumentException
{
// Precondition checks
if (type == null)
@@ -218,6 +220,26 @@
return ShrinkWrap.getDefaultDomain().getArchiveFactory().create(type, archiveName);
}
+ /**
+ * Creates a new archive of the specified type as imported
+ * from the specified {@link File}. The file is expected to be encoded as
+ * ZIP (ie. JAR/WAR/EAR). The name of the archive will be set to {@link File#getName()}.
+ * The archive will be be backed by the {@link Configuration}
+ * within the {@link ShrinkWrap#getDefaultDomain()}
+ *
+ * @param type The type of the archive e.g. {@link org.jboss.shrinkwrap.api.spec.WebArchive}
+ * @param archiveName the archiveName to use
+ * @return An {@link Assignable} view
+ * @throws IllegalArgumentException If either argument is not supplied, if the specified
+ * {@link File} does not exist, or is not a valid ZIP file
+ */
+ public static <T extends Assignable> T createFromZipFile(final Class<T> type, final File archiveFile)
+ throws IllegalArgumentException, ArchiveImportException
+ {
+ // Delegate
+ return getDefaultDomain().getArchiveFactory().createFromZipFile(type, archiveFile);
+ }
+
//-------------------------------------------------------------------------------------||
// Internal Helper Members ------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
Added: shrinkwrap/trunk/api/src/test/resources/nonzipfile.txt
===================================================================
--- shrinkwrap/trunk/api/src/test/resources/nonzipfile.txt (rev 0)
+++ shrinkwrap/trunk/api/src/test/resources/nonzipfile.txt 2010-06-01 19:17:40 UTC (rev 4465)
@@ -0,0 +1 @@
+This is not a ZIP file, used in testing
\ No newline at end of file
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java 2010-05-30 21:36:50 UTC (rev 4464)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java 2010-06-01 19:17:40 UTC (rev 4465)
@@ -16,6 +16,7 @@
*/
package org.jboss.shrinkwrap.impl.base;
+import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -23,6 +24,7 @@
import junit.framework.TestCase;
import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchiveFactory;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.Assignable;
@@ -38,6 +40,7 @@
import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.container.ContainerBase;
+import org.jboss.shrinkwrap.impl.base.importer.ZipContentAssertionDelegate;
import org.junit.Test;
/**
@@ -52,6 +55,20 @@
{
//-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Delegate for performing ZIP content assertions
+ */
+ private static final ZipContentAssertionDelegate delegate = new ZipContentAssertionDelegate();
+
+ /**
+ * The name of a simple TXT file
+ */
+ private static final String NAME_FILE_NON_ZIP = "nonzipfile.txt";
+
+ //-------------------------------------------------------------------------------------||
// Tests ------------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -261,6 +278,84 @@
}
/**
+ * Ensures we can create a new Archive fro a ZIP file via {@link ShrinkWrap} convenience
+ * class
+ */
+ @Test
+ public void shouldBeAbleToimportZipFileViaShrinkWrap() throws Exception
+ {
+ // Get the test file
+ final File testFile = delegate.getExistingZipResource();
+
+ // Make a new archive via the default domain
+ final JavaArchive archive = ShrinkWrap.createFromZipFile(JavaArchive.class, testFile);
+
+ // Assert
+ Assert.assertNotNull("Should not return a null archive", archive);
+ Assert.assertEquals("name of the archive imported from a ZIP file was not as expected", testFile.getName(),
+ archive.getName());
+ delegate.assertContent(archive, testFile);
+ }
+
+ /**
+ * Ensures we can create a new Archive fro a ZIP file via an {@link ArchiveFactory}
+ */
+ @Test
+ public void shouldBeAbleToimportZipFileViaArchiveFactory() throws Exception
+ {
+ // Get the test file
+ final File testFile = delegate.getExistingZipResource();
+
+ // Make a new archive via the default domain
+ final JavaArchive archive = ShrinkWrap.getDefaultDomain().getArchiveFactory().createFromZipFile(
+ JavaArchive.class, testFile);
+
+ // Assert
+ Assert.assertNotNull("Should not return a null archive", archive);
+ Assert.assertEquals("name of the archive imported from a ZIP file was not as expected", testFile.getName(),
+ archive.getName());
+ delegate.assertContent(archive, testFile);
+ }
+
+ /**
+ * Ensures that attempting to import as ZIP from a non-ZIP file
+ * leads to {@link IllegalArgumentException}
+ * @throws Exception
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void importFromNonZipFileThrowsException() throws Exception
+ {
+ final File nonZipFile = new File(SecurityActions.getThreadContextClassLoader().getResource(NAME_FILE_NON_ZIP)
+ .toURI());
+ ShrinkWrap.createFromZipFile(JavaArchive.class, nonZipFile);
+ }
+
+ /**
+ * Ensures that attempting to import as ZIP from null file
+ * leads to {@link IllegalArgumentException}
+ * @throws Exception
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void importFromNullFileThrowsException() throws Exception
+ {
+ ShrinkWrap.createFromZipFile(JavaArchive.class, null);
+ }
+
+ /**
+ * Ensures that attempting to import as ZIP from a {@link File}
+ * that doesn't exist
+ * leads to {@link IllegalArgumentException}
+ * @throws Exception
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void importFromNonexistantlFileThrowsException() throws Exception
+ {
+ final File file = new File("fileThatDoesntExist.tmp");
+ Assert.assertFalse("Error in test setup, file should not exist: " + file.getAbsolutePath(), file.exists());
+ ShrinkWrap.createFromZipFile(JavaArchive.class, null);
+ }
+
+ /**
* Ensures that creating a default name with no extension configured
* for a specified type results in {@link UnknownExtensionTypeException}
*
@@ -340,7 +435,7 @@
@Override
public String toString(final Formatter formatter) throws IllegalArgumentException
- {
+ {
return formatter.format(this);
}
}
Added: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipContentAssertionDelegate.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipContentAssertionDelegate.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipContentAssertionDelegate.java 2010-06-01 19:17:40 UTC (rev 4465)
@@ -0,0 +1,128 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.importer;
+
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import junit.framework.Assert;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.impl.base.io.IOUtil;
+import org.jboss.shrinkwrap.impl.base.path.BasicPath;
+
+/**
+ * Delegate class for asserting that ZIP contents may be
+ * imported as expected
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ZipContentAssertionDelegate
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * ClassLoader resource of a static ZIP we'll use to test importing
+ */
+ private static final String EXISTING_ZIP_RESOURCE = "org/jboss/shrinkwrap/impl/base/importer/test.zip";
+
+ /**
+ * Name of the expected empty directory
+ */
+ private static final String EXPECTED_EMPTY_DIR = "empty_dir/";
+
+ /**
+ * Name of the expected nested directory
+ */
+ private static final String EXPECTED_NESTED_EMPTY_DIR = "parent/empty_dir/";
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Obtains the test ZIP file
+ */
+ public File getExistingZipResource() throws URISyntaxException
+ {
+ return new File(SecurityActions.getThreadContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI());
+ }
+
+ /**
+ * Compare the content of the original file and what was imported.
+ *
+ * @param importedArchive The archive used for import
+ * @param originalSource The original classpath resource file
+ */
+ public void assertContent(Archive<?> importedArchive, File originalSource) throws Exception
+ {
+ Assert.assertFalse("Should have imported something", importedArchive.getContent().isEmpty());
+
+ ZipFile testZip = new ZipFile(originalSource);
+
+ List<? extends ZipEntry> entries = Collections.list(testZip.entries());
+
+ Assert.assertFalse("Test zip should contain data", entries.isEmpty());
+ Assert.assertEquals("Should have imported all files and directories", entries.size(), importedArchive
+ .getContent().size());
+
+ boolean containsEmptyDir = false;
+ boolean containsEmptyNestedDir = false;
+
+ for (ZipEntry originalEntry : entries)
+ {
+
+ if (originalEntry.isDirectory())
+ {
+ // Check for expected empty dirs
+ if (originalEntry.getName().equals(EXPECTED_EMPTY_DIR))
+ {
+ containsEmptyDir = true;
+ }
+ if (originalEntry.getName().equals(EXPECTED_NESTED_EMPTY_DIR))
+ {
+ containsEmptyNestedDir = true;
+ }
+ continue;
+ }
+
+ Assert.assertTrue("Importer should have imported " + originalEntry.getName() + " from " + originalSource,
+ importedArchive.contains(new BasicPath(originalEntry.getName())));
+
+ byte[] originalContent = IOUtil.asByteArray(testZip.getInputStream(originalEntry));
+ byte[] importedContent = IOUtil.asByteArray(importedArchive.get(new BasicPath(originalEntry.getName()))
+ .getAsset().openStream());
+
+ Assert.assertTrue("The content of " + originalEntry.getName() + " should be equal to the imported content",
+ Arrays.equals(importedContent, originalContent));
+ }
+
+ // Ensure empty directories have come in cleanly
+ Assert.assertTrue("Empty directory not imported", containsEmptyDir);
+ Assert.assertTrue("Empty nested directory not imported", containsEmptyNestedDir);
+ }
+}
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-05-30 21:36:50 UTC (rev 4464)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java 2010-06-01 19:17:40 UTC (rev 4465)
@@ -17,14 +17,11 @@
package org.jboss.shrinkwrap.impl.base.importer;
import java.io.File;
+import java.io.FileInputStream;
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;
import java.util.zip.ZipFile;
@@ -43,10 +40,7 @@
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
import org.junit.Test;
-
/**
- * ZipImporterImplTest
- *
* TestCase to verify the ZipImporter functionality.
*
* @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
@@ -54,7 +48,7 @@
*/
public class ZipImporterImplTestCase
{
-
+
//-------------------------------------------------------------------------------------||
// Class Members ----------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -64,49 +58,36 @@
*/
private static final Logger log = Logger.getLogger(ZipImporterImplTestCase.class.getName());
- private static final String EXISTING_ZIP_RESOURCE = "org/jboss/shrinkwrap/impl/base/importer/test.zip";
-
private static final String EXISTING_RESOURCE = "org/jboss/shrinkwrap/impl/base/asset/Test.properties";
-
+
/**
- * Name of the expected empty directory
+ * Delegate for performing ZIP content assertions
*/
- private static final String EXPECTED_EMPTY_DIR ="empty_dir/";
-
- /**
- * Name of the expected nested directory
- */
- private static final String EXPECTED_NESTED_EMPTY_DIR ="parent/empty_dir/";
-
+ private static final ZipContentAssertionDelegate delegate = new ZipContentAssertionDelegate();
+
//-------------------------------------------------------------------------------------||
// Tests ------------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
-
+
@Test
- public void shouldBeAbleToimportZipFile() throws Exception
+ public void shouldBeAbleToimportZipFile() throws Exception
{
- ZipFile testZip = new ZipFile(
- new File(
- SecurityActions.getThreadContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI()));
-
- Archive<?> archive = ShrinkWrap.create("test.jar", ZipImporter.class)
- .importZip(testZip)
- .as(JavaArchive.class);
-
+ final File testFile = delegate.getExistingZipResource();
+ ZipFile testZip = new ZipFile(testFile);
+
+ Archive<?> archive = ShrinkWrap.create("test.jar", ZipImporter.class).importZip(testZip).as(JavaArchive.class);
+
Assert.assertNotNull("Should not return a null archive", archive);
-
- assertContent(
- archive,
- SecurityActions.getThreadContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI());
+
+ delegate.assertContent(archive, testFile);
}
-
@Test
public void shouldBeAbleToImportAddAndExport() throws Exception
{
- ZipInputStream stream = new ZipInputStream(
- SecurityActions.getThreadContextClassLoader().getResourceAsStream(EXISTING_ZIP_RESOURCE));
-
+ final File testFile = delegate.getExistingZipResource();
+ ZipInputStream stream = new ZipInputStream(new FileInputStream(testFile));
+
final Archive<?> archive;
try
{
@@ -120,21 +101,21 @@
Assert.assertNotNull("Should not return a null archive", archive);
archive.add(new ClassLoaderAsset(EXISTING_RESOURCE), new BasicPath("test.properties"));
-
+
File tempFile = new File("target/test.zip");
tempFile.deleteOnExit();
InputStream zipStream = archive.as(ZipExporter.class).exportZip();
IOUtil.copyWithClose(zipStream, new FileOutputStream(tempFile));
-
- assertContent(archive, tempFile.toURI());
+
+ delegate.assertContent(archive, tempFile);
}
-
+
@Test
public void shouldBeAbleToImportZipInputStream() throws Exception
{
- ZipInputStream stream = new ZipInputStream(
- SecurityActions.getThreadContextClassLoader().getResourceAsStream(EXISTING_ZIP_RESOURCE));
-
+ final File testFile = delegate.getExistingZipResource();
+ ZipInputStream stream = new ZipInputStream(new FileInputStream(testFile));
+
final Archive<?> archive;
try
{
@@ -146,10 +127,8 @@
}
Assert.assertNotNull("Should not return a null archive", archive);
-
- assertContent(
- archive,
- SecurityActions.getThreadContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI());
+
+ delegate.assertContent(archive, testFile);
}
/**
@@ -186,8 +165,8 @@
@Test(expected = ArchiveImportException.class)
public void shouldThrowExceptionOnErrorInImportFromFile() throws Exception
{
- ZipFile testZip = new ZipFile(new File(SecurityActions.getThreadContextClassLoader().getResource(
- EXISTING_ZIP_RESOURCE).toURI()))
+ final File testFile = delegate.getExistingZipResource();
+ ZipFile testZip = new ZipFile(testFile)
{
@Override
public Enumeration<? extends ZipEntry> entries()
@@ -197,75 +176,4 @@
};
ShrinkWrap.create("test.jar", ZipImporter.class).importZip(testZip).as(JavaArchive.class);
}
-
-
- /**
- * Compare the content of the original file and what was imported.
- *
- * @param importedArchive The archive used for import
- * @param originalSource The original classpath resource file
- */
- private void assertContent(Archive<?> importedArchive, URI originalSource) throws Exception
- {
- Assert.assertFalse(
- "Should have imported something",
- importedArchive.getContent().isEmpty());
-
- ZipFile testZip = new ZipFile(
- new File(originalSource));
-
- List<? extends ZipEntry> entries = Collections.list(testZip.entries());
-
- Assert.assertFalse(
- "Test zip should contain data",
- entries.isEmpty());
- Assert.assertEquals(
- "Should have imported all files and directories",
- entries.size(),
- importedArchive.getContent().size());
-
-
- boolean containsEmptyDir = false;
- boolean containsEmptyNestedDir = false;
-
- for(ZipEntry originalEntry : entries)
- {
-
- if(originalEntry.isDirectory())
- {
- // Check for expected empty dirs
- if (originalEntry.getName().equals(EXPECTED_EMPTY_DIR))
- {
- containsEmptyDir = true;
- }
- if (originalEntry.getName().equals(EXPECTED_NESTED_EMPTY_DIR))
- {
- containsEmptyNestedDir = true;
- }
- continue;
- }
-
- Assert.assertTrue(
- "Importer should have imported " + originalEntry.getName() + " from " + originalSource,
- importedArchive.contains(new BasicPath(originalEntry.getName())));
-
- byte[] originalContent = IOUtil.asByteArray(testZip.getInputStream(originalEntry));
- byte[] importedContent = IOUtil.asByteArray(
- importedArchive.get(new BasicPath(originalEntry.getName())).getAsset().openStream());
-
- log.fine(
- Arrays.equals(importedContent, originalContent) + "\t" +
- originalContent.length + "\t" +
- importedContent.length + "\t" +
- originalEntry.getName());
-
- Assert.assertTrue(
- "The content of " + originalEntry.getName() + " should be equal to the imported content",
- Arrays.equals(importedContent, originalContent));
- }
-
- // Ensure empty directories have come in cleanly
- Assert.assertTrue("Empty directory not imported", containsEmptyDir);
- Assert.assertTrue("Empty nested directory not imported", containsEmptyNestedDir);
- }
}
More information about the jboss-svn-commits
mailing list