[jboss-svn-commits] JBoss Common SVN: r3829 - in shrinkwrap/trunk: api/src/main/java/org/jboss/shrinkwrap/api/container and 6 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Dec 4 14:29:57 EST 2009
Author: aslak
Date: 2009-12-04 14:29:56 -0500 (Fri, 04 Dec 2009)
New Revision: 3829
Added:
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filter.java
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filters.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/ExcludeRegExpPaths.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllClasses.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllPaths.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeRegExpPaths.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/FiltersTestCase.java
Modified:
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/container/ClassContainer.java
shrinkwrap/trunk/doc/reference/src/main/docbook/en-US/api.xml
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/ArchiveBase.java
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/ContainerBase.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicContainerTestBase.java
Log:
SHRINKWRAP-91 Added support for filtering merge/addPakcages operations. Filters factory for creating common filters based on regexp.
Modified: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Archive.java 2009-12-04 16:52:51 UTC (rev 3828)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Archive.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -135,6 +135,18 @@
* @throws IllegalArgumentException If the existing archive is not specified
*/
T merge(Archive<?> source) throws IllegalArgumentException;
+
+ /**
+ * Merge the contents from an existing archive without
+ * maintaining the archive name in the context path.
+ *
+ * The filter control which {@link Path}s to include form the source {@link Archive}.
+ *
+ * @param source Archive to add contents from
+ * @return
+ * @throws IllegalArgumentException If the existing archive is not specified
+ */
+ T merge(Archive<?> source, Filter<Path> filter) throws IllegalArgumentException;
/**
* Merge the contents from an existing archive in a specific path
@@ -148,6 +160,19 @@
T merge(Archive<?> source, Path path) throws IllegalArgumentException;
/**
+ * Merge the contents from an existing archive in a specific path
+ * without maintaining the archive name in the context path.
+ * The filter control which {@link Path}s to include form the source {@link Archive}.
+ *
+ * @param source Archive to add contents from
+ * @param path Path to add contents to
+ * @param filter Filter to use for including {@link Asset}s in the merge.
+ * @return
+ * @throws IllegalArgumentException If the path or existing archive is not specified
+ */
+ T merge(Archive<?> source, Path path, Filter<Path> filter) throws IllegalArgumentException;
+
+ /**
* Returns a multiline "ls -l"-equse output of the contents of
* this deployment and (recursively) its children if the verbosity
* flag is set to "true". Otherwise the no-arg version is invoked
Added: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filter.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filter.java (rev 0)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filter.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -0,0 +1,38 @@
+/*
+ * 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.api;
+
+/**
+ * Filter
+ *
+ * Used to filter what to add to the target {@link Archive}.
+ * Used on 'multi' add operations like {@link Archive#merge(Archive, Filter)}.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public interface Filter<T>
+{
+
+ /**
+ * Called pr operation in a multi add operation.
+ *
+ * @param object a object to filter on
+ * @return true if the object should be included in target
+ */
+ boolean include(T object);
+}
Added: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filters.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filters.java (rev 0)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/Filters.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -0,0 +1,113 @@
+/*
+ * 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.api;
+
+/**
+ * Filters
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public final class Filters
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ private static final String INCLUDE_ALL_CLASSES = "org.jboss.shrinkwrap.impl.base.filter.IncludeAllClasses";
+
+ private static final String INCLUDE_ALL_PATHS = "org.jboss.shrinkwrap.impl.base.filter.IncludeAllPaths";
+
+ private static final String INCLUDE_REGEXP_PATHS = "org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths";
+
+ private static final String EXCLUDE_REGEXP_PATHS = "org.jboss.shrinkwrap.impl.base.filter.ExcludeRegExpPaths";
+
+ /**
+ * {@link Filter} that include all {@link Class}s.
+ *
+ * Only meant to be used internally.
+ *
+ * @return A {@link Filter} that always return true
+ */
+ @SuppressWarnings("unchecked")
+ public static Filter<Class<?>> includeAllClasses()
+ {
+ return SecurityActions.newInstance(
+ INCLUDE_ALL_CLASSES,
+ new Class<?>[]{},
+ new Object[]{},
+ Filter.class);
+ }
+
+ /**
+ * {@link Filter} that includes all {@link Path}s.
+ *
+ * Only meant to be used internally.
+ *
+ * @return A {@link Filter} that always return true
+ */
+ @SuppressWarnings("unchecked")
+ public static Filter<Path> includeAll()
+ {
+ return SecurityActions.newInstance(
+ INCLUDE_ALL_PATHS,
+ new Class<?>[]{},
+ new Object[]{},
+ Filter.class);
+ }
+
+ /**
+ * {@link Filer} that include all {@link Path}s that match the given Regular Expression {@link Pattern}.
+ *
+ * @param regexp The expression to include
+ * @return A Regular Expression based include {@link Filter}
+ */
+ @SuppressWarnings("unchecked")
+ public static Filter<Path> include(String regexp)
+ {
+ return SecurityActions.newInstance(
+ INCLUDE_REGEXP_PATHS,
+ new Class<?>[]{String.class},
+ new Object[]{regexp},
+ Filter.class);
+ }
+
+ /**
+ * {@link Filter} that exclude all {@link Path}s that match a given Regular Expression {@link Pattern}.
+ *
+ * @param regexp The expression to exclude
+ * @return A Regular Expression based exclude {@link Filter}
+ */
+ @SuppressWarnings("unchecked")
+ public static Filter<Path> exclude(String regexp)
+ {
+ return SecurityActions.newInstance(
+ EXCLUDE_REGEXP_PATHS,
+ new Class<?>[]{String.class},
+ new Object[]{regexp},
+ Filter.class);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * No instantiation
+ */
+ private Filters() {}
+}
Modified: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/container/ClassContainer.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/container/ClassContainer.java 2009-12-04 16:52:51 UTC (rev 3828)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/container/ClassContainer.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -17,6 +17,7 @@
package org.jboss.shrinkwrap.api.container;
import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.Filter;
/**
* ClassContainer
@@ -69,20 +70,35 @@
/**
* Adds all classes in the specified {@link Package} to the {@link Archive}.
+ * <br/>
+ * SubPackages are excluded.
*
* @param pack The {@link Package} to add
* @return This virtual archive
* @throws IllegalArgumentException If no package were specified
+ * @see #addPackages(boolean, Package...)
*/
T addPackage(Package pack) throws IllegalArgumentException;
/**
- * Adds all classes in the specified {@link Package}s to the {@link Archive}.
+ * Adds all classes in the specified {@link Package}s to the {@link Archive}.
*
* @param recursive Should the sub packages be added
* @param packages All the packages to add
* @return This virtual archive
* @throws IllegalArgumentException If no packages were specified
+ * @see #addPackages(boolean, Filter, Package...)
*/
T addPackages(boolean recursive, Package... packages) throws IllegalArgumentException;
+
+ /**
+ * Adds all classes accepted by the filter in the specified {@link Package}s to the {@link Archive}.
+ *
+ * @param recursive Should the sub packages be added
+ * @param filter filter out specific classes
+ * @param packages All the packages to add
+ * @return This virtual archive
+ * @throws IllegalArgumentException If no packages were specified
+ */
+ T addPackages(boolean recursive, Filter<Class<?>> filter, Package... packages) throws IllegalArgumentException;
}
Modified: shrinkwrap/trunk/doc/reference/src/main/docbook/en-US/api.xml
===================================================================
--- shrinkwrap/trunk/doc/reference/src/main/docbook/en-US/api.xml 2009-12-04 16:52:51 UTC (rev 3828)
+++ shrinkwrap/trunk/doc/reference/src/main/docbook/en-US/api.xml 2009-12-04 19:29:56 UTC (rev 3829)
@@ -29,6 +29,16 @@
<para></para>
</section>
+ <section id="api.filter">
+ <title>Filter</title>
+ <para></para>
+ </section>
+
+ <section id="api.filters">
+ <title>Filters</title>
+ <para></para>
+ </section>
+
<section id="api.container">
<title>Containers</title>
<para></para>
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/ArchiveBase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/ArchiveBase.java 2009-12-04 16:52:51 UTC (rev 3828)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/ArchiveBase.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -24,6 +24,8 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Asset;
import org.jboss.shrinkwrap.api.ExtensionLoader;
+import org.jboss.shrinkwrap.api.Filter;
+import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.Path;
import org.jboss.shrinkwrap.api.Assignable;
import org.jboss.shrinkwrap.impl.base.asset.ArchiveAsset;
@@ -190,6 +192,15 @@
return merge(source, new BasicPath());
}
+ /* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.Archive#merge(org.jboss.shrinkwrap.api.Archive, org.jboss.shrinkwrap.api.Filter)
+ */
+ @Override
+ public T merge(Archive<?> source, Filter<Path> filter) throws IllegalArgumentException
+ {
+ return merge(source, new BasicPath(), filter);
+ }
+
/**
* {@inheritDoc}
* @see org.jboss.shrinkwrap.api.Archive#merge(org.jboss.shrinkwrap.api.Path, org.jboss.shrinkwrap.api.Archive)
@@ -197,9 +208,22 @@
@Override
public T merge(final Archive<?> source, final Path path) throws IllegalArgumentException
{
+ Validate.notNull(source, "No source archive was specified");
+ Validate.notNull(path, "No path was specified");
+
+ return merge(source, path, Filters.includeAll());
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.Archive#merge(org.jboss.shrinkwrap.api.Archive, org.jboss.shrinkwrap.api.Path, org.jboss.shrinkwrap.api.Filter)
+ */
+ @Override
+ public T merge(Archive<?> source, Path path, Filter<Path> filter) throws IllegalArgumentException
+ {
// Precondition checks
Validate.notNull(source, "No source archive was specified");
Validate.notNull(path, "No path was specified");
+ Validate.notNull(filter, "No filter was specified");
// Get existing contents from source archive
final Map<Path, Asset> sourceContent = source.getContent();
@@ -209,10 +233,10 @@
for (final Entry<Path, Asset> contentEntry : sourceContent.entrySet())
{
final Asset asset = contentEntry.getValue();
- Path assetPath = contentEntry.getKey();
- if (path != null)
+ Path assetPath = new BasicPath(path, contentEntry.getKey());
+ if( !filter.include(assetPath))
{
- assetPath = new BasicPath(path, assetPath);
+ continue;
}
// Delegate
add(asset, assetPath);
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/ContainerBase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/ContainerBase.java 2009-12-04 16:52:51 UTC (rev 3828)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/ContainerBase.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -25,6 +25,8 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.api.Filter;
+import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.Path;
import org.jboss.shrinkwrap.api.container.ClassContainer;
import org.jboss.shrinkwrap.api.container.LibraryContainer;
@@ -145,6 +147,16 @@
}
/* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.Archive#merge(org.jboss.shrinkwrap.api.Archive, org.jboss.shrinkwrap.api.Filter)
+ */
+ @Override
+ public T merge(Archive<?> source, Filter<Path> filter) throws IllegalArgumentException
+ {
+ archive.merge(source, filter);
+ return covarientReturn();
+ }
+
+ /* (non-Javadoc)
* @see org.jboss.shrinkwrap.api.Archive#merge(org.jboss.shrinkwrap.api.Archive, org.jboss.shrinkwrap.api.Path)
*/
@Override
@@ -154,6 +166,13 @@
return covarientReturn();
}
+ @Override
+ public T merge(Archive<?> source, Path path, Filter<Path> filter) throws IllegalArgumentException
+ {
+ archive.merge(source, path, filter);
+ return covarientReturn();
+ }
+
/* (non-Javadoc)
* @see org.jboss.shrinkwrap.api.Archive#add(org.jboss.shrinkwrap.api.Asset, java.lang.String)
*/
@@ -643,6 +662,17 @@
public T addPackages(boolean recursive, Package... packages) throws IllegalArgumentException
{
Validate.notNull(packages, "Packages must be specified");
+ return addPackages(recursive, Filters.includeAllClasses(), packages);
+ }
+
+ /* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.container.ClassContainer#addPackages(boolean, org.jboss.shrinkwrap.api.Filter, java.lang.Package[])
+ */
+ @Override
+ public T addPackages(boolean recursive, Filter<Class<?>> filter, Package... packages) throws IllegalArgumentException
+ {
+ Validate.notNull(filter, "Filter must be specified");
+ Validate.notNull(packages, "Packages must be specified");
for(Package pack : packages)
{
@@ -651,6 +681,10 @@
Set<Class<?>> classes = scanner.getClasses();
for(Class<?> clazz : classes)
{
+ if(!filter.include(clazz))
+ {
+ continue;
+ }
Asset asset = new ClassAsset(clazz);
Path location = new BasicPath(getClassesPath(), AssetUtil.getFullPathForClassResource(clazz));
add(asset, location);
Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/ExcludeRegExpPaths.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/ExcludeRegExpPaths.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/ExcludeRegExpPaths.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -0,0 +1,65 @@
+/*
+ * 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.filter;
+
+import org.jboss.shrinkwrap.api.Filter;
+import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.impl.base.Validate;
+
+/**
+ * IncludeRegExpPath
+ *
+ * Filter to exclude all {@link Path}s that match a given Regular Expression.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ExcludeRegExpPaths implements Filter<Path>
+{
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ private String expression;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ public ExcludeRegExpPaths(String expression)
+ {
+ Validate.notNull(expression, "Expression must be specified");
+ this.expression = expression;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.Filter#include(java.lang.Object)
+ */
+ @Override
+ public boolean include(Path path)
+ {
+ if(path.get().matches(expression))
+ {
+ return false;
+ }
+ return true;
+ }
+}
Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllClasses.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllClasses.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllClasses.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -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.filter;
+
+import org.jboss.shrinkwrap.api.Filter;
+
+/**
+ * IncludeAllClasses
+ *
+ * Filter to include all {@link Class}s
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class IncludeAllClasses implements Filter<Class<?>>
+{
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.Filter#include(java.lang.Object)
+ */
+ @Override
+ public boolean include(Class<?> object)
+ {
+ return true;
+ }
+}
Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllPaths.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllPaths.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeAllPaths.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -0,0 +1,44 @@
+/*
+ * 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.filter;
+
+import org.jboss.shrinkwrap.api.Filter;
+import org.jboss.shrinkwrap.api.Path;
+
+/**
+ * IncludeAllPaths
+ *
+ * Filter that include all {@link Path}s.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class IncludeAllPaths implements Filter<Path>
+{
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.Filter#include(java.lang.Object)
+ */
+ @Override
+ public boolean include(Path object)
+ {
+ return true;
+ }
+}
Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeRegExpPaths.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeRegExpPaths.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/filter/IncludeRegExpPaths.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -0,0 +1,65 @@
+/*
+ * 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.filter;
+
+import org.jboss.shrinkwrap.api.Filter;
+import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.impl.base.Validate;
+
+/**
+ * IncludeRegExpPath
+ *
+ * Filter to include all {@link Path}s that match a given Regular Expression.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class IncludeRegExpPaths implements Filter<Path>
+{
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ private String expression;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ public IncludeRegExpPaths(String expression)
+ {
+ Validate.notNull(expression, "Expression must be specified");
+ this.expression = expression;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /* (non-Javadoc)
+ * @see org.jboss.shrinkwrap.api.Filter#include(java.lang.Object)
+ */
+ @Override
+ public boolean include(Path path)
+ {
+ if(path.get().matches(expression))
+ {
+ return true;
+ }
+ return false;
+ }
+}
Added: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/FiltersTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/FiltersTestCase.java (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/FiltersTestCase.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -0,0 +1,126 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.jboss.shrinkwrap.api.Filter;
+import org.jboss.shrinkwrap.api.Filters;
+import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.api.Paths;
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/**
+ * FiltersTestCase
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class FiltersTestCase
+{
+
+ @Test
+ public void shouldIncludeAll() throws Exception
+ {
+ List<Path> paths = Arrays.asList(
+ Paths.create("/META-INF/"), Paths.create("/WEB-INF/"));
+ List<Path> filteredPaths = executeFilter(Path.class, paths, Filters.includeAll());
+
+ Assert.assertArrayEquals(
+ "Should include all paths",
+ paths.toArray(),
+ filteredPaths.toArray());
+ }
+
+ @Test
+ public void shouldIncludePathRegExp() throws Exception
+ {
+ List<Path> paths = Arrays.asList(
+ Paths.create("/META-INF/"), Paths.create("/WEB-INF/"));
+ List<Path> filteredPaths = executeFilter(Path.class, paths, Filters.include(".*META-INF.*"));
+
+ Assert.assertEquals(
+ "Should only contain one",
+ 1,
+ filteredPaths.size());
+
+ Assert.assertEquals(
+ "Should only contain metainf",
+ Paths.create("/META-INF/"),
+ filteredPaths.get(0));
+ }
+
+ @Test
+ public void shouldExcludePathRegExp() throws Exception
+ {
+ List<Path> paths = Arrays.asList(
+ Paths.create("/META-INF/"), Paths.create("/WEB-INF/"));
+ List<Path> filteredPaths = executeFilter(Path.class, paths, Filters.exclude(".*META-INF.*"));
+
+ Assert.assertEquals(
+ "Should only contain one",
+ 1,
+ filteredPaths.size());
+
+ Assert.assertEquals(
+ "Should only contain webinf",
+ Paths.create("/WEB-INF/"),
+ filteredPaths.get(0));
+ }
+
+ @Test
+ public void shouldIncludeAllClasses() throws Exception
+ {
+ List<Class<?>> classes = new ArrayList<Class<?>>();
+ classes.add(FiltersTestCase.class);
+
+ List<Class<?>> filteredClasses = executeFilter(classes, Filters.includeAllClasses());
+
+ Assert.assertArrayEquals(
+ "Should include all classes",
+ classes.toArray(),
+ filteredClasses.toArray());
+ }
+
+ private <T> List<T> executeFilter(Class<T> clazz, List<T> items, Filter<T> filter)
+ {
+ List<T> result = new ArrayList<T>();
+ for(T item : items)
+ {
+ if(filter.include(item)) {
+ result.add(item);
+ }
+ }
+ return result;
+ }
+
+ private List<Class<?>> executeFilter(List<Class<?>> items, Filter<Class<?>> filter)
+ {
+ List<Class<?>> result = new ArrayList<Class<?>>();
+ for(Class<?> item : items)
+ {
+ if(filter.include(item)) {
+ result.add(item);
+ }
+ }
+ return result;
+ }
+}
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java 2009-12-04 16:52:51 UTC (rev 3828)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -17,7 +17,6 @@
package org.jboss.shrinkwrap.impl.base.test;
import java.util.Arrays;
-
import java.util.Map;
import junit.framework.Assert;
@@ -25,6 +24,7 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Archives;
import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.Path;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.impl.base.Validate;
@@ -539,6 +539,66 @@
}
/**
+ * Ensure that the filter is used when merging.
+ * @throws Exception
+ */
+ @Test
+ public void testMergeToPathWithFilter() throws Exception
+ {
+ Archive<?> archive = getArchive();
+ Archive<T> sourceArchive = createNewArchive();
+ Path location = new BasicPath("/", "test.properties");
+ Path locationTwo = new BasicPath("/", "test2.properties");
+
+ Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES);
+ Asset assetTwo = new ClassLoaderAsset(NAME_TEST_PROPERTIES_2);
+ sourceArchive.add(asset, location).add(assetTwo, locationTwo);
+
+ Path baseLocation = new BasicPath("somewhere");
+
+ archive.merge(sourceArchive, baseLocation, Filters.include(".*test2.*"));
+
+ Assert.assertEquals(
+ "Should only have merged 1",
+ 1,
+ archive.getContent().size());
+
+ Path expectedPath = new BasicPath(baseLocation, locationTwo);
+
+ Assert.assertTrue(
+ "Asset should have been added to path: " + expectedPath.get(),
+ this.compareAssets(archive.get(expectedPath), asset));
+ }
+
+ /**
+ * Ensure that the filter is used when merging.
+ * @throws Exception
+ */
+ @Test
+ public void testMergeWithFilter() throws Exception
+ {
+ Archive<?> archive = getArchive();
+ Archive<T> sourceArchive = createNewArchive();
+ Path location = new BasicPath("/", "test.properties");
+ Path locationTwo = new BasicPath("/", "test2.properties");
+
+ Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES);
+ Asset assetTwo = new ClassLoaderAsset(NAME_TEST_PROPERTIES_2);
+ sourceArchive.add(asset, location).add(assetTwo, locationTwo);
+
+ archive.merge(sourceArchive, Filters.include(".*test2.*"));
+
+ Assert.assertEquals(
+ "Should only have merged 1",
+ 1,
+ archive.getContent().size());
+
+ Assert.assertTrue(
+ "Asset should have been added to path: " + locationTwo.get(),
+ this.compareAssets(archive.get(locationTwo), asset));
+ }
+
+ /**
* Ensure merging content from another archive requires a path
* @throws Exception
*/
@@ -548,7 +608,7 @@
Archive<T> archive = getArchive();
try
{
- archive.merge(createNewArchive(), null);
+ archive.merge(createNewArchive(), (Path)null);
Assert.fail("Should have throw an IllegalArgumentException");
}
catch (IllegalArgumentException expectedException)
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicContainerTestBase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicContainerTestBase.java 2009-12-04 16:52:51 UTC (rev 3828)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicContainerTestBase.java 2009-12-04 19:29:56 UTC (rev 3829)
@@ -23,6 +23,7 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.Path;
import org.jboss.shrinkwrap.api.container.ClassContainer;
import org.jboss.shrinkwrap.api.container.LibraryContainer;
@@ -479,7 +480,38 @@
"A class should be located at " + expectedPath.get(),
getArchive().contains(expectedPath));
}
+
+ /**
+ * Ensure packages can be added with filters
+ *
+ * @throws Exception
+ */
+ @Test
+ @ArchiveType(ClassContainer.class)
+ public void testAddPakcageRecursiveFiltered() throws Exception
+ {
+ getClassContainer().addPackages(true, new Filter<Class<?>>()
+ {
+ @Override
+ public boolean include(Class<?> clazz)
+ {
+ return clazz == DynamicContainerTestBase.class;
+ }
+ }, DynamicContainerTestBase.class.getPackage());
+
+ Path expectedPath = new BasicPath(
+ getClassPath(), AssetUtil.getFullPathForClassResource(DynamicContainerTestBase.class));
+ Assert.assertEquals(
+ "Should only be one class added",
+ 1,
+ getArchive().getContent().size());
+
+ Assert.assertTrue(
+ "A class should be located at " + expectedPath.get(),
+ getArchive().contains(expectedPath));
+ }
+
//-------------------------------------------------------------------------------------||
// Test Implementations - LibraryContainer ----------------------------------------------||
//-------------------------------------------------------------------------------------||
More information about the jboss-svn-commits
mailing list