diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedDependenciesBase.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedDependenciesBase.java index 11e9a783..af17c60c 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedDependenciesBase.java +++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedDependenciesBase.java @@ -20,7 +20,7 @@ import java.util.List; -import org.apache.commons.lang3.StringUtils; +import com.google.common.base.Strings; import org.apache.maven.artifact.Artifact; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; @@ -74,7 +74,6 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { throw new EnforcerRuleException("Cannot resolve MavenSession", e); } - DependencyNode rootNode = ArtifactUtils.resolveTransitiveDependencies(helper); if (!searchTransitive) { String result = session.getCurrentProject().getDependencyArtifacts().stream() .filter(a -> !validate(a)) @@ -91,6 +90,7 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { } } else { StringBuilder messageBuilder = new StringBuilder(); + DependencyNode rootNode = ArtifactUtils.resolveTransitiveDependencies(helper); if (!validate(rootNode, 0, messageBuilder)) { throw new EnforcerRuleException(messageBuilder.toString()); } @@ -105,7 +105,7 @@ protected boolean validate(DependencyNode node, int level, StringBuilder message .map(childNode -> validate(childNode, level + 1, childMessageBuilder)) .reduce(true, Boolean::logicalAnd)) { messageBuilder - .append(StringUtils.repeat(" ", level)) + .append(Strings.repeat(" ", level)) .append(ArtifactUtils.toArtifact(node).getId()); if (rootFailed) { messageBuilder.append(" <--- ").append(getErrorMessage()); diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireReleaseDeps.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireReleaseDeps.java index 71305da4..f78ff3a2 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireReleaseDeps.java +++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireReleaseDeps.java @@ -29,6 +29,7 @@ import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; import static java.util.Optional.ofNullable; +import static org.apache.maven.plugins.enforcer.utils.ArtifactUtils.matchDependencyArtifact; /** * This rule checks that no snapshots are included. @@ -114,8 +115,9 @@ private MavenProject getProject(EnforcerRuleHelper helper) throws EnforcerRuleEx @Override protected boolean validate(Artifact artifact) { - return ArtifactUtils.matchDependencyArtifact(artifact, excludes) - && !ArtifactUtils.matchDependencyArtifact(artifact, includes) + // only check isSnapshot() if the artifact does not match (excludes minus includes) + // otherwise true + return (matchDependencyArtifact(artifact, excludes) && !matchDependencyArtifact(artifact, includes)) || !artifact.isSnapshot(); } diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BannedDependenciesTestSetup.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BannedDependenciesTestSetup.java index 55d40be0..7f3e9762 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BannedDependenciesTestSetup.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BannedDependenciesTestSetup.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.List; -import org.apache.maven.artifact.Artifact; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; import org.apache.maven.plugin.testing.ArtifactStubFactory; @@ -38,12 +37,11 @@ public BannedDependenciesTestSetup() throws IOException { ArtifactStubFactory factory = new ArtifactStubFactory(); project = new MockProject(); - project.setArtifacts(factory.getMixedArtifacts()); project.setDependencyArtifacts(factory.getScopedArtifacts()); this.helper = EnforcerTestUtils.getHelper(project); - this.rule = newBannedDependenciesRule(); + this.rule = new BannedDependencies(); this.rule.setMessage(null); this.rule.setExcludes(this.excludes); @@ -80,14 +78,4 @@ public List getExcludes() { public void setExcludes(List excludes) { this.excludes = excludes; } - - private BannedDependencies newBannedDependenciesRule() { - return new BannedDependencies() { - @Override - protected boolean validate(Artifact artifact) { - return (isSearchTransitive() ? project.getArtifacts() : project.getDependencyArtifacts()) - .stream().map(super::validate).reduce(true, Boolean::logicalAnd); - } - }; - } } diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java index 14f5e1cd..c1ef1e21 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/EnforcerTestUtils.java @@ -20,8 +20,6 @@ import java.util.Properties; -import org.apache.maven.RepositoryUtils; -import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; @@ -33,6 +31,7 @@ import org.apache.maven.plugin.PluginParameterExpressionEvaluator; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.logging.SystemStreamLog; +import org.apache.maven.plugins.enforcer.utils.DependencyNodeBuilder; import org.apache.maven.plugins.enforcer.utils.MockEnforcerExpressionEvaluator; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingRequest; @@ -41,15 +40,12 @@ import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.artifact.ArtifactType; -import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.collection.CollectRequest; import org.eclipse.aether.collection.CollectResult; import org.eclipse.aether.collection.DependencyCollectionException; -import org.eclipse.aether.graph.DefaultDependencyNode; import org.eclipse.aether.graph.DependencyNode; -import static java.util.Arrays.asList; +import static org.apache.maven.artifact.Artifact.SCOPE_TEST; import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -60,6 +56,10 @@ * @author Brian Fox */ public final class EnforcerTestUtils { + + private static RepositorySystem REPOSITORY_SYSTEM = mock(RepositorySystem.class); + ; + /** * Gets the maven session. * @@ -112,20 +112,17 @@ public static EnforcerRuleHelper getHelper(MavenProject project) { return getHelper(project, false); } - private static RepositorySystem mockRepositorySystem() throws DependencyCollectionException { - ArtifactType jarType = RepositoryUtils.newArtifactType("jar", new DefaultArtifactHandler("jar")); - final DependencyNode node = new DefaultDependencyNode( - new DefaultArtifact("groupId", "artifactId", "classifier", "jar", "version", jarType)); - node.setChildren(asList( - new DefaultDependencyNode( - new DefaultArtifact("groupId", "artifact", "classifier", "jar", "1.0.0", jarType)), - new DefaultDependencyNode( - new DefaultArtifact("groupId", "artifact", "classifier", "jar", "2.0.0", jarType)))); - - RepositorySystem mockRepositorySystem = mock(RepositorySystem.class); - when(mockRepositorySystem.collectDependencies(any(), any(CollectRequest.class))) - .then(i -> new CollectResult(i.getArgument(1)).setRoot(node)); - return mockRepositorySystem; + public static void provideCollectDependencies(DependencyNode node) { + try { + when(REPOSITORY_SYSTEM.collectDependencies(any(), any(CollectRequest.class))) + .then(i -> new CollectResult(i.getArgument(1)).setRoot(node)); + } catch (DependencyCollectionException e) { + throw new RuntimeException(e); + } + } + + public static void provideCollectDependencies() { + provideCollectDependencies(getUniformDependencyNodeTree()); } /** @@ -148,7 +145,8 @@ public static EnforcerRuleHelper getHelper(MavenProject project, boolean mockExp } PlexusContainer container = mock(PlexusContainer.class); - when(container.lookup(RepositorySystem.class)).then(i -> mockRepositorySystem()); + when(container.lookup(RepositorySystem.class)).thenReturn(REPOSITORY_SYSTEM); + provideCollectDependencies(); ClassWorld classWorld = new ClassWorld("test", EnforcerTestUtils.class.getClassLoader()); MojoDescriptor mojoDescriptor = new MojoDescriptor(); @@ -192,4 +190,56 @@ public static Plugin newPlugin(String groupId, String artifactId, String version plugin.setLocation("version", new InputLocation(0, 0, inputSource)); return plugin; } + + public static DependencyNode getUniformDependencyNodeTree() { + return new DependencyNodeBuilder() + .withType(DependencyNodeBuilder.Type.POM) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childA") + .withVersion("1.0.0") + .build()) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childB") + .withVersion("2.0.0") + .build()) + .build(); + } + + public static DependencyNode getDependencyNodeWithMultipleSnapshots() { + return new DependencyNodeBuilder() + .withType(DependencyNodeBuilder.Type.POM) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childA") + .withVersion("1.0.0") + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childAA") + .withVersion("1.0.0-SNAPSHOT") + .build()) + .build()) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childB") + .withVersion("2.0.0-SNAPSHOT") + .build()) + .build(); + } + + public static DependencyNode getDependencyNodeWithMultipleTestSnapshots() { + return new DependencyNodeBuilder() + .withType(DependencyNodeBuilder.Type.POM) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childA") + .withVersion("1.0.0") + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childAA") + .withVersion("1.0.0-SNAPSHOT") + .withScope(SCOPE_TEST) + .build()) + .build()) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childB") + .withVersion("2.0.0-SNAPSHOT") + .withScope(SCOPE_TEST) + .build()) + .build(); + } } diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDepsTest.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDepsTest.java index 4963aac7..ef2a11db 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDepsTest.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDepsTest.java @@ -18,11 +18,9 @@ */ package org.apache.maven.plugins.enforcer; -import java.io.IOException; - import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; -import org.apache.maven.plugin.testing.ArtifactStubFactory; +import org.apache.maven.plugins.enforcer.utils.DependencyNodeBuilder; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -32,19 +30,28 @@ public class RequireUpperBoundDepsTest { @Test - public void testRule() throws IOException { - ArtifactStubFactory factory = new ArtifactStubFactory(); + public void testRule() { MockProject project = new MockProject(); EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(project); - project.setArtifacts(factory.getMixedArtifacts()); - project.setDependencyArtifacts(factory.getScopedArtifacts()); RequireUpperBoundDeps rule = new RequireUpperBoundDeps(); + EnforcerTestUtils.provideCollectDependencies(new DependencyNodeBuilder() + .withType(DependencyNodeBuilder.Type.POM) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childA") + .withVersion("1.0.0") + .build()) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childA") + .withVersion("2.0.0") + .build()) + .build()); try { rule.execute(helper); fail("Did not detect upper bounds error"); } catch (EnforcerRuleException ex) { - assertThat(ex.getMessage(), containsString("groupId:artifactId:version:classifier")); + assertThat(ex.getMessage(), containsString("default-group:childA:1.0.0:classifier")); + assertThat(ex.getMessage(), containsString("default-group:childA:2.0.0:classifier")); } } } diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestBannedDependencies.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestBannedDependencies.java index 6fd89ec3..c7663347 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestBannedDependencies.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestBannedDependencies.java @@ -21,9 +21,11 @@ import java.io.IOException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.plugins.enforcer.utils.DependencyNodeBuilder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.apache.maven.plugins.enforcer.EnforcerTestUtils.provideCollectDependencies; import static org.junit.jupiter.api.Assertions.assertThrows; /** @@ -78,33 +80,48 @@ private void addExcludeAndRunRule(String toAdd) throws EnforcerRuleException { @Test public void testGroupIdArtifactIdVersion() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("testGroupId:release:1.0")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("default-group:childA:1.0.0")); } @Test public void testGroupIdArtifactId() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("testGroupId:release")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("default-group:childA")); } @Test public void testGroupId() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("testGroupId")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("default-group")); } @Test public void testSpaceTrimmingGroupIdArtifactIdVersion() { assertThrows( - EnforcerRuleException.class, () -> addExcludeAndRunRule(" testGroupId : release : 1.0 ")); + EnforcerRuleException.class, + () -> addExcludeAndRunRule(" default-group : childA : 1.0.0 ")); } @Test public void groupIdArtifactIdVersionType() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("g:a:1.0:war")); + provideCollectDependencies(new DependencyNodeBuilder() + .withType(DependencyNodeBuilder.Type.POM) + .withChildNode(new DependencyNodeBuilder() + .withArtifactId("childA") + .withVersion("1.0.0") + .withChildNode(new DependencyNodeBuilder() + .withType(DependencyNodeBuilder.Type.WAR) + .withArtifactId("childAA") + .withVersion("1.0.0-SNAPSHOT") + .build()) + .build()) + .build()); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:*:*:war")); } @Test public void groupIdArtifactIdVersionTypeScope() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("g:a:1.0:war:compile")); + EnforcerTestUtils.provideCollectDependencies( + EnforcerTestUtils.getDependencyNodeWithMultipleTestSnapshots()); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:*:*:*:test")); } // @Test(expected = EnforcerRuleException.class) @@ -130,22 +147,22 @@ private void addExcludeAndRunRule(String toAdd) throws EnforcerRuleException { @Test public void testWildcardForGroupIdArtifactIdVersion() throws Exception { - addExcludeAndRunRule("*:release:1.2"); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:childA:1.0.0")); } @Test public void testWildCardForGroupIdArtifactId() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:release")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:childA")); } @Test public void testWildcardForGroupIdWildcardForArtifactIdVersion() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:*:1.0")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:*:1.0.0")); } @Test public void testWildcardForGroupIdArtifactIdWildcardForVersion() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:release:*")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("*:childA:*")); } } @@ -165,17 +182,18 @@ private void addExcludeAndRunRule(String toAdd) throws EnforcerRuleException { @Test public void groupIdArtifactIdWithWildcard() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("testGroupId:re*")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("default-group:ch*")); } @Test public void groupIdArtifactIdVersionTypeWildcardScope() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("g:a:1.0:war:co*")); + assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("default-group:childA:1.0.0:jar:co*")); } @Test public void groupIdArtifactIdVersionWildcardTypeScope() { - assertThrows(EnforcerRuleException.class, () -> addExcludeAndRunRule("g:a:1.0:w*:compile")); + assertThrows( + EnforcerRuleException.class, () -> addExcludeAndRunRule("default-group:childA:1.0.0:j*:compile")); } } diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireReleaseDeps.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireReleaseDeps.java index 10a0fd57..3be6b31f 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireReleaseDeps.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequireReleaseDeps.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.util.Collections; -import org.apache.maven.artifact.Artifact; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; import org.apache.maven.plugin.testing.ArtifactStubFactory; import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtilsHelper; @@ -29,82 +28,81 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.apache.maven.plugins.enforcer.EnforcerTestUtils.getDependencyNodeWithMultipleSnapshots; +import static org.apache.maven.plugins.enforcer.EnforcerTestUtils.getDependencyNodeWithMultipleTestSnapshots; +import static org.apache.maven.plugins.enforcer.EnforcerTestUtils.provideCollectDependencies; import static org.assertj.core.api.Assertions.assertThat; /** - * The Class TestRequireReleaseDeps. + * Unit tests for {@link RequireReleaseDeps} * - * @author Brian Fox + * @author Brian Fox, Andrzej Jarmoniuk */ -class TestRequireReleaseDeps { +public class TestRequireReleaseDeps { private MavenProject project; + private static final ArtifactStubFactory ARTIFACT_STUB_FACTORY = new ArtifactStubFactory(); + ; + private EnforcerRuleHelper ruleHelper; + private RequireReleaseDeps rule; @BeforeEach public void setUp() { project = new MockProject(); + ruleHelper = EnforcerTestUtils.getHelper(project); + rule = new RequireReleaseDeps(); } - /** - * Test rule. - * - * @throws Exception if any occurs - */ @Test - void testRule() throws Exception { - ArtifactStubFactory factory = new ArtifactStubFactory(); - EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(project); - project.setArtifacts(factory.getMixedArtifacts()); - project.setDependencyArtifacts(factory.getScopedArtifacts()); - RequireReleaseDeps rule = newRequireReleaseDeps(); + public void testSearchNonTransitive() throws IOException { + project.setDependencyArtifacts(ARTIFACT_STUB_FACTORY.getScopedArtifacts()); rule.setSearchTransitive(false); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, false); + } - EnforcerRuleUtilsHelper.execute(rule, helper, false); - + @Test + public void testSearchTransitiveMultipleFailures() { rule.setSearchTransitive(true); + provideCollectDependencies(getDependencyNodeWithMultipleSnapshots()); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, true); + } - EnforcerRuleUtilsHelper.execute(rule, helper, true); - - // test onlyWhenRelease in each case - - project.setArtifact(factory.getSnapshotArtifact()); - - EnforcerRuleUtilsHelper.execute(rule, helper, true); + @Test + public void testSearchTransitiveNoFailures() { + rule.setSearchTransitive(true); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, false); + } + @Test + public void testShouldFailOnlyWhenRelease() throws IOException { + project.setArtifact(ARTIFACT_STUB_FACTORY.getSnapshotArtifact()); + provideCollectDependencies(getDependencyNodeWithMultipleSnapshots()); rule.setOnlyWhenRelease(true); - - EnforcerRuleUtilsHelper.execute(rule, helper, false); - - project.setArtifact(factory.getReleaseArtifact()); - - EnforcerRuleUtilsHelper.execute(rule, helper, true); - - MockProject parent = new MockProject(); - parent.setArtifact(factory.getSnapshotArtifact()); - project.setParent(parent); - project.setArtifacts(null); - project.setDependencyArtifacts(null); - helper = EnforcerTestUtils.getHelper(project); - - rule.setFailWhenParentIsSnapshot(true); - EnforcerRuleUtilsHelper.execute(rule, helper, true); - - rule.setFailWhenParentIsSnapshot(false); - EnforcerRuleUtilsHelper.execute(rule, helper, false); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, false); } @Test - void testWildcardIgnore() throws Exception { - RequireReleaseDeps rule = newRequireReleaseDeps(); + void testWildcardExcludeTests() throws Exception { rule.setExcludes(Collections.singletonList("*:*:*:*:test")); - rule.setOnlyWhenRelease(true); - rule.setSearchTransitive(false); + provideCollectDependencies(getDependencyNodeWithMultipleTestSnapshots()); + rule.setSearchTransitive(true); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, false); + } - ArtifactStubFactory factory = new ArtifactStubFactory(); - project.setArtifact(factory.getReleaseArtifact()); - project.setDependencyArtifacts(Collections.singleton(factory.createArtifact("g", "a", "1.0-SNAPSHOT", "test"))); - EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(project); + @Test + void testWildcardExcludeAll() throws Exception { + rule.setExcludes(Collections.singletonList("*")); + provideCollectDependencies(getDependencyNodeWithMultipleSnapshots()); + rule.setSearchTransitive(true); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, false); + } - EnforcerRuleUtilsHelper.execute(rule, helper, false); + @Test + void testExcludesAndIncludes() throws Exception { + rule.setExcludes(Collections.singletonList("*")); + rule.setIncludes(Collections.singletonList("*:*:*:*:test")); + provideCollectDependencies(getDependencyNodeWithMultipleTestSnapshots()); + rule.setSearchTransitive(true); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, true); } /** @@ -112,34 +110,24 @@ void testWildcardIgnore() throws Exception { */ @Test void testId() { - RequireReleaseDeps rule = newRequireReleaseDeps(); assertThat(rule.getCacheId()).isEqualTo("0"); } @Test - void parentShouldBeExcluded() throws IOException { - ArtifactStubFactory factory = new ArtifactStubFactory(); - project.setArtifact(factory.getSnapshotArtifact()); - + void testFailWhenParentIsSnapshotFalse() throws IOException { MavenProject parent = new MockProject(); - parent.setArtifact(factory.getSnapshotArtifact()); + parent.setArtifact(ARTIFACT_STUB_FACTORY.getSnapshotArtifact()); project.setParent(parent); - - EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(project); - - RequireReleaseDeps rule = newRequireReleaseDeps(); - rule.setExcludes(Collections.singletonList(parent.getArtifact().getGroupId() + ":*")); - - EnforcerRuleUtilsHelper.execute(rule, helper, false); + rule.setFailWhenParentIsSnapshot(false); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, false); } - private RequireReleaseDeps newRequireReleaseDeps() { - return new RequireReleaseDeps() { - @Override - protected boolean validate(Artifact artifactl) { - return (isSearchTransitive() ? project.getArtifacts() : project.getDependencyArtifacts()) - .stream().map(super::validate).reduce(true, Boolean::logicalAnd); - } - }; + @Test + void parentShouldBeExcluded() throws IOException { + MavenProject parent = new MockProject(); + parent.setArtifact(ARTIFACT_STUB_FACTORY.getSnapshotArtifact()); + project.setParent(parent); + rule.setExcludes(Collections.singletonList(parent.getArtifact().getGroupId() + ":*")); + EnforcerRuleUtilsHelper.execute(rule, ruleHelper, false); } } diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/DependencyNodeBuilder.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/DependencyNodeBuilder.java new file mode 100644 index 00000000..40795234 --- /dev/null +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/DependencyNodeBuilder.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.maven.plugins.enforcer.utils; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.RepositoryUtils; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.ArtifactType; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.graph.DefaultDependencyNode; +import org.eclipse.aether.graph.Dependency; +import org.eclipse.aether.graph.DependencyNode; + +import static java.util.Optional.ofNullable; +import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; + +public class DependencyNodeBuilder { + public enum Type { + WAR(RepositoryUtils.newArtifactType("war", new DefaultArtifactHandler("war"))), + JAR(RepositoryUtils.newArtifactType("jar", new DefaultArtifactHandler("jar"))), + POM(RepositoryUtils.newArtifactType("pom", new DefaultArtifactHandler("pom"))); + + private final ArtifactType artifactType; + + Type(ArtifactType artifactType) { + this.artifactType = artifactType; + } + + ArtifactType asArtifactType() { + return artifactType; + } + + String asString() { + return name().toLowerCase(); + } + } + + private String groupId; + + private String artifactId; + + private String version; + + private Type type; + + private String scope; + + private boolean optional; + + private List children = new ArrayList<>(); + + public DependencyNodeBuilder withGroupId(String val) { + groupId = val; + return this; + } + + public DependencyNodeBuilder withArtifactId(String val) { + artifactId = val; + return this; + } + + public DependencyNodeBuilder withVersion(String val) { + version = val; + return this; + } + + public DependencyNodeBuilder withType(Type val) { + type = val; + return this; + } + + public DependencyNodeBuilder withScope(String val) { + scope = val; + return this; + } + + public DependencyNodeBuilder withOptional(boolean val) { + optional = val; + return this; + } + + public DependencyNodeBuilder withChildNode(DependencyNode val) { + children.add(val); + return this; + } + + public DependencyNode build() { + Artifact artifact = new DefaultArtifact( + ofNullable(groupId).orElse("default-group"), + ofNullable(artifactId).orElse("default-artifact"), + "classifier", + ofNullable(type).map(Type::asString).orElse("pom"), + ofNullable(version).orElse("default-version"), + ofNullable(type).orElse(Type.JAR).asArtifactType()); + Dependency dependency = new Dependency(artifact, ofNullable(scope).orElse(SCOPE_COMPILE), optional); + DependencyNode instance = new DefaultDependencyNode(dependency); + instance.setChildren(children); + return instance; + } +} diff --git a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtilsHelper.java b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtilsHelper.java index a856e2e4..207e4a72 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtilsHelper.java +++ b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/utils/EnforcerRuleUtilsHelper.java @@ -48,7 +48,7 @@ public static void execute(EnforcerRule rule, EnforcerRuleHelper helper, boolean if (!shouldFail) { fail("No Exception expected:" + e.getMessage()); } - helper.getLog().debug(e.getMessage()); + helper.getLog().debug("Rule failed as expected: " + e.getMessage()); } } }