Skip to content

Commit

Permalink
SHRINKWRAP-330 Test cases for exclusion after includeDependenciesFrom…
Browse files Browse the repository at this point in the history
…Pom doesn't exclude artifacts
  • Loading branch information
kpiwko committed Oct 23, 2011
1 parent 75d136c commit 078c831
Show file tree
Hide file tree
Showing 6 changed files with 632 additions and 307 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, 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.resolver.api.maven.filter;

import java.util.Collection;

import org.jboss.shrinkwrap.resolver.api.maven.MavenDependency;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolutionFilter;

/**
* A filter which rejects specific dependency
*
* @author <a href="kpiwko@redhat.com">Karel Piwko</a>
*
*/
public class ExclusionFilter implements MavenResolutionFilter {

private String excludedDependency;

public ExclusionFilter(String coordinates) {
this.excludedDependency = coordinates;
}

@Override
public boolean accept(MavenDependency element) {
return !element.hasSameArtifactAs(excludedDependency);
}

@Override
public MavenResolutionFilter configure(Collection<MavenDependency> dependencies) {
// no-op
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, 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.resolver.api.maven.filter;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.jboss.shrinkwrap.resolver.api.maven.MavenDependency;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolutionFilter;

/**
* A filter which reject specific dependencies
*
* @author <a href="kpiwko@redhat.com">Karel Piwko</a>
*
*/
public class ExclusionsFilter implements MavenResolutionFilter {

private List<String> coordinates;

public ExclusionsFilter(String... coordinates) {
if (coordinates == null || coordinates.length == 0) {
throw new IllegalArgumentException("Unable to instantiate ExclusionsFilter with no coordinates defined");
}

this.coordinates = Arrays.asList(coordinates);
}

@Override
public boolean accept(MavenDependency element) {

for (String coordinate : coordinates) {
if (!(new ExclusionFilter(coordinate).accept(element))) {
return false;
}
}
return true;
}

@Override
public MavenResolutionFilter configure(Collection<MavenDependency> dependencies) {
// NO-OP operation
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, 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.resolver.impl.maven;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;

/**
* Sets a set of files and checks that returned files start with the same names. This means no exclusion is needed which
* defining a collection of required files.
*
* @author <a href="kpiwko@redhat.com">Karel Piwko</a>
*
*/
public class FileValidationUtil {

private String[] files;

private Map<String, Boolean> flags;

public FileValidationUtil(String... allowedFiles) {
this.files = allowedFiles;
this.flags = new HashMap<String, Boolean>(files.length);
for (String file : allowedFiles) {
flags.put(file, Boolean.FALSE);
}
}

public void validate(File[] array) throws AssertionError {
Assert.assertEquals("There must total " + files.length + " files resolved", files.length, array.length);

for (File f : array) {
for (String fname : files) {
if (f.getName().startsWith(fname)) {
flags.put(fname, Boolean.TRUE);
}
}
}

StringBuilder sb = new StringBuilder("Missing files were:\n");
boolean success = true;

for (Map.Entry<String, Boolean> entry : flags.entrySet()) {
if (!entry.getValue()) {
success = false;
sb.append(entry.getKey()).append("\n");
}
}

if (!success) {
throw new AssertionError(sb.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.resolver.impl.maven;

import java.io.File;

import org.jboss.shrinkwrap.resolver.api.DependencyResolvers;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver;
import org.jboss.shrinkwrap.resolver.api.maven.filter.ExclusionFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.ExclusionsFilter;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
* @author <a href="http://community.jboss.org/people/silenius">Samuel Santos</a>
*/
public class PomFilteringUnitTestCase {
@BeforeClass
public static void setRemoteRepository() {
System.setProperty(MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION, "target/the-other-repository");
}

@AfterClass
public static void clearRemoteRepository() {
System.clearProperty(MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION);
}

@Test
public void testIncludeFromPomWithExclusionFilter() {
File[] jars = DependencyResolvers.use(MavenDependencyResolver.class)
.loadEffectiveFromPom("target/poms/test-filter.xml")
.importAnyDependencies(new ExclusionFilter("org.jboss.shrinkwrap.test:test-deps-c")).resolveAsFiles();

Assert.assertEquals("Exactly 3 files were resolved", 3, jars.length);
new FileValidationUtil("test-deps-a", "test-deps-d", "test-deps-e").validate(jars);

}

@Test
public void testIncludeFromPomWithExclusionsFilter() {

File[] jars = DependencyResolvers
.use(MavenDependencyResolver.class)
.loadEffectiveFromPom("target/poms/test-filter.xml")
.importAnyDependencies(
// this is applied before resolution, e.g. has no information about transitive dependencies
// it means:
// 1. it excludes whole tree of the exclusion
// 2. it does not affect transitive dependencies of other elements
new ExclusionsFilter("org.jboss.shrinkwrap.test:test-deps-a", "org.jboss.shrinkwrap.test:test-deps-c",
"org.jboss.shrinkwrap.test:test-deps-d")).resolveAsFiles();

Assert.assertEquals("Exactly 1 file was resolved", 1, jars.length);
new FileValidationUtil("test-deps-e").validate(jars);
}

}

0 comments on commit 078c831

Please sign in to comment.