Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

SHRINKWRAP-402 Add support for checking the type of the Archive #53

Open
wants to merge 1 commit into from

2 participants

@aslakknutsen

The new method Archive.isOfType(Class<? extends Archive<?>>) can be used to verify if a given Archive is of a given ArchiveType.
The check matches the Archive.name against the ArchiveTypes extension mapping.

ShrinkWrap.create(JavaArchive.class).isOfType(WebArchive.class) == false
ShrinkWrap.create(JavaArchive.class, "my.war").isOfType(WebArchive.class) == true
ShrinkWrap.create(MySpecialWebArchive.class, "my.war").isOfType(WebArchive.class) == true

@aslakknutsen aslakknutsen SHRINKWRAP-402 Add support for checking the type of the Archive
The new method Archive.isOfType(Class<? extends Archive<?>>) can be used to verify if a given Archive is of a given ArchiveType.
The check matches the Archive.name against the ArchiveTypes extension mapping.

ShrinkWrap.create(JavaArchive.class).isOfType(WebArchive.class) == false
ShrinkWrap.create(JavaArchive.class, "my.war").isOfType(WebArchive.class) == true
ShrinkWrap.create(MySpecialWebArchive.class, "my.war").isOfType(WebArchive.class) == true
6061261
@ALRubinger
Owner

As mentioned to Aslak on IRC, I suspect this issue may be out-of-scope for SW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 22, 2012
  1. @aslakknutsen

    SHRINKWRAP-402 Add support for checking the type of the Archive

    aslakknutsen authored
    The new method Archive.isOfType(Class<? extends Archive<?>>) can be used to verify if a given Archive is of a given ArchiveType.
    The check matches the Archive.name against the ArchiveTypes extension mapping.
    
    ShrinkWrap.create(JavaArchive.class).isOfType(WebArchive.class) == false
    ShrinkWrap.create(JavaArchive.class, "my.war").isOfType(WebArchive.class) == true
    ShrinkWrap.create(MySpecialWebArchive.class, "my.war").isOfType(WebArchive.class) == true
This page is out of date. Refresh to see the latest.
View
8 api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
@@ -321,6 +321,14 @@
ArchiveFormat archiveFormat);
/**
+ * Check if this Archive is of a specific type, e.g. WebArchive or EnterpriseArchive.
+ *
+ * @param archiveType The archive type to check against
+ * @return true if the default extension of the given archiveType match the extension of this archive
+ */
+ boolean isOfType(Class<? extends Archive<?>> archiveType);
+
+ /**
* Denotes whether this archive contains a resource at the specified path
*
* @param path
View
17 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/ArchiveBase.java
@@ -37,6 +37,7 @@
import org.jboss.shrinkwrap.api.IllegalArchivePathException;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.UnknownExtensionTypeException;
import org.jboss.shrinkwrap.api.asset.ArchiveAsset;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.NamedAsset;
@@ -331,6 +332,21 @@ public Node get(final String path) throws IllegalArgumentException {
/**
* {@inheritDoc}
*
+ * @see org.jboss.shrinkwrap.api.Archive#isOfType(java.lang.Class)
+ */
+ @Override
+ public boolean isOfType(Class<? extends Archive<?>> archiveType) {
+ try {
+ String extension = this.configuration.getExtensionLoader().getExtensionFromExtensionMapping(archiveType);
+ return getName().endsWith(extension);
+ } catch (UnknownExtensionTypeException e) {
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.shrinkwrap.api.Archive#add(org.jboss.shrinkwrap.api.Archive, org.jboss.shrinkwrap.api.ArchivePath,
* java.lang.Class)
*/
@@ -424,6 +440,7 @@ public T addAsDirectories(final String... paths) throws IllegalArgumentException
*
* @see org.jboss.shrinkwrap.api.Archive#getName()
*/
+ @Override
public final String getName() {
return name;
}
View
10 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/ContainerBase.java
@@ -479,6 +479,16 @@ public Node get(String path) throws IllegalArgumentException {
/**
* {@inheritDoc}
*
+ * @see org.jboss.shrinkwrap.api.Archive#isOfType(java.lang.Class)
+ */
+ @Override
+ public boolean isOfType(Class<? extends Archive<?>> archiveType) {
+ return this.getArchive().isOfType(archiveType);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.shrinkwrap.api.Archive#getContent()
*/
@Override
View
5 ...base/src/test/java/org/jboss/shrinkwrap/impl/base/spec/EnterpriseArchiveImplTestCase.java
@@ -77,6 +77,11 @@ public void ls() {
// Required Impls - ArchiveTestBase ---------------------------------------------------||
// -------------------------------------------------------------------------------------||
+ @Override
+ protected Class<EnterpriseArchive> getExpectedArchiveType() {
+ return EnterpriseArchive.class;
+ }
+
/**
* Return the current EnterpriseArchive
*/
View
5 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/spec/GenericArchiveImplTestCase.java
@@ -74,6 +74,11 @@ public void ls() {
// Required Impls - ArchiveTestBase ---------------------------------------------------||
// -------------------------------------------------------------------------------------||
+ @Override
+ protected Class<GenericArchive> getExpectedArchiveType() {
+ return GenericArchive.class;
+ }
+
/**
* Return the archive to super class
*/
View
4 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/spec/JavaArchiveImplTestCase.java
@@ -73,6 +73,10 @@ public void ls() {
// Required Impls - ArchiveTestBase ---------------------------------------------------||
// -------------------------------------------------------------------------------------||
+ protected Class<JavaArchive> getExpectedArchiveType() {
+ return JavaArchive.class;
+ }
+
/**
* Return the archive to super class
*/
View
4 ...src/test/java/org/jboss/shrinkwrap/impl/base/spec/ResourceAdapterArchiveImplTestCase.java
@@ -78,6 +78,10 @@ public void ls() {
// Required Impls - ArchiveTestBase ---------------------------------------------------||
// -------------------------------------------------------------------------------------||
+ protected Class<ResourceAdapterArchive> getExpectedArchiveType() {
+ return ResourceAdapterArchive.class;
+ }
+
/**
* Return the current ResourceAdapterArchive
*/
View
11 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/spec/WebArchiveImplTestCase.java
@@ -16,6 +16,8 @@
*/
package org.jboss.shrinkwrap.impl.base.spec;
+import junit.framework.Assert;
+
import org.jboss.shrinkwrap.api.ArchiveFormat;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
@@ -26,11 +28,15 @@
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.container.WebContainer;
+import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.test.ArchiveType;
import org.jboss.shrinkwrap.impl.base.test.DynamicWebContainerTestBase;
import org.junit.After;
import org.junit.Before;
+import org.junit.Test;
/**
* WebArchiveImplTestCase
@@ -85,6 +91,11 @@ public void ls() {
// -------------------------------------------------------------------------------------||
@Override
+ protected Class<WebArchive> getExpectedArchiveType() {
+ return WebArchive.class;
+ }
+
+ @Override
protected WebArchive getArchive() {
return archive;
}
View
10 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java
@@ -46,6 +46,7 @@
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.TestIOUtil;
import org.jboss.shrinkwrap.impl.base.Validate;
import org.jboss.shrinkwrap.impl.base.io.IOUtil;
@@ -54,6 +55,7 @@
import org.jboss.shrinkwrap.impl.base.test.handler.SimpleHandler;
import org.jboss.shrinkwrap.spi.ArchiveFormatAssociable;
import org.junit.After;
+import org.junit.Assume;
import org.junit.Test;
/**
@@ -101,11 +103,19 @@
protected abstract ArchiveFormat getExpectedArchiveFormat();
+ protected abstract Class<T> getExpectedArchiveType();
+
// -------------------------------------------------------------------------------------||
// Tests ------------------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||
@Test
+ public void shouldBeOfType() throws Exception {
+ Assume.assumeNotNull(getExpectedArchiveType()); // assume not null, e.g. MemoryMapArchive is not a type
+ Assert.assertTrue("Should be archive of type " + getExpectedArchiveType(), getArchive().isOfType(getExpectedArchiveType()));
+ }
+
+ @Test
public void testDefaultArchiveFormatIsSet() throws Exception {
Assert.assertEquals("Unexpected default archive format", getExpectedArchiveFormat(), getDefaultArchiveFormat());
}
View
5 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/unit/MemoryMapArchiveTestCase.java
@@ -38,6 +38,11 @@
public class MemoryMapArchiveTestCase extends ArchiveTestBase<MemoryMapArchive> {
private MemoryMapArchive archive;
+ @Override
+ protected Class<MemoryMapArchive> getExpectedArchiveType() {
+ return null;
+ }
+
/**
* Create a new Archive instance per Test.
*
Something went wrong with that request. Please try again.