Skip to content

Commit

Permalink
MENFORCER-435: Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk committed Dec 30, 2022
1 parent db7fbd2 commit 0b80637
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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());
}
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -80,14 +78,4 @@ public List<String> getExcludes() {
public void setExcludes(List<String> 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);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -60,6 +56,10 @@
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
public final class EnforcerTestUtils {

private static RepositorySystem REPOSITORY_SYSTEM = mock(RepositorySystem.class);
;

/**
* Gets the maven session.
*
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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:*"));
}
}

Expand All @@ -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"));
}
}

Expand Down
Loading

0 comments on commit 0b80637

Please sign in to comment.