Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-476] Rename ResolveUtil to ResolverUtil #260

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public final class BanTransitiveDependencies extends AbstractStandardEnforcerRul

private final MavenSession session;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

@Inject
public BanTransitiveDependencies(MavenSession session, ResolveUtil resolveUtil) {
public BanTransitiveDependencies(MavenSession session, ResolverUtil resolverUtil) {
this.session = Objects.requireNonNull(session);
this.resolveUtil = Objects.requireNonNull(resolveUtil);
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

/**
Expand Down Expand Up @@ -147,7 +147,7 @@ public void execute() throws EnforcerRuleException {
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toSet());

DependencyNode rootNode = resolveUtil.resolveTransitiveDependencies();
DependencyNode rootNode = resolverUtil.resolveTransitiveDependencies();
StringBuilder generatedMessage = new StringBuilder();
if (searchTree(rootNode, 0, exclusions, directDependencies, generatedMessage)) {
throw new EnforcerRuleException(ofNullable(getMessage()).orElse(generatedMessage.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
public final class BannedDependencies extends BannedDependenciesBase {

@Inject
BannedDependencies(MavenSession session, ResolveUtil resolveUtil) {
super(session, resolveUtil);
BannedDependencies(MavenSession session, ResolverUtil resolverUtil) {
super(session, resolverUtil);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ abstract class BannedDependenciesBase extends AbstractStandardEnforcerRule {

private final MavenSession session;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

BannedDependenciesBase(MavenSession session, ResolveUtil resolveUtil) {
BannedDependenciesBase(MavenSession session, ResolverUtil resolverUtil) {
this.session = Objects.requireNonNull(session);
this.resolveUtil = Objects.requireNonNull(resolveUtil);
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

protected MavenSession getSession() {
Expand Down Expand Up @@ -103,7 +103,7 @@ public void execute() throws EnforcerRuleException {
}
} else {
StringBuilder messageBuilder = new StringBuilder();
DependencyNode rootNode = resolveUtil.resolveTransitiveDependenciesVerbose(Collections.emptyList());
DependencyNode rootNode = resolverUtil.resolveTransitiveDependenciesVerbose(Collections.emptyList());
if (!validate(rootNode, 0, messageBuilder)) {
String message = "";
if (getMessage() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ public final class DependencyConvergence extends AbstractStandardEnforcerRule {

private DependencyVersionMap dependencyVersionMap;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

@Inject
public DependencyConvergence(ResolveUtil resolveUtil) {
this.resolveUtil = Objects.requireNonNull(resolveUtil);
public DependencyConvergence(ResolverUtil resolverUtil) {
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

@Override
public void execute() throws EnforcerRuleException {

DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(excludedScopes);
DependencyNode node = resolverUtil.resolveTransitiveDependenciesVerbose(excludedScopes);
dependencyVersionMap = new DependencyVersionMap().setUniqueVersions(uniqueVersions);
node.accept(dependencyVersionMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public final class RequireReleaseDeps extends BannedDependenciesBase {
private boolean failWhenParentIsSnapshot = true;

@Inject
public RequireReleaseDeps(MavenSession session, ResolveUtil resolveUtil) {
super(session, resolveUtil);
public RequireReleaseDeps(MavenSession session, ResolverUtil resolverUtil) {
super(session, resolverUtil);
}

// Override parent to allow optional ignore of this rule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public final class RequireUpperBoundDeps extends AbstractStandardEnforcerRule {

private RequireUpperBoundDepsVisitor upperBoundDepsVisitor;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

@Inject
public RequireUpperBoundDeps(ResolveUtil resolveUtil) {
this.resolveUtil = Objects.requireNonNull(resolveUtil);
public RequireUpperBoundDeps(ResolverUtil resolverUtil) {
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

/**
Expand All @@ -102,7 +102,7 @@ public void setIncludes(List<String> includes) {
@Override
public void execute() throws EnforcerRuleException {
DependencyNode node =
resolveUtil.resolveTransitiveDependenciesVerbose(Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED));
resolverUtil.resolveTransitiveDependenciesVerbose(Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED));
upperBoundDepsVisitor = new RequireUpperBoundDepsVisitor()
.setUniqueVersions(uniqueVersions)
.setIncludes(includes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;
import org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor;

import static java.util.Optional.ofNullable;
import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
Expand All @@ -50,7 +52,7 @@
* Resolver helper class.
*/
@Named
class ResolveUtil {
class ResolverUtil {

private final RepositorySystem repositorySystem;

Expand All @@ -60,7 +62,7 @@ class ResolveUtil {
* Default constructor
*/
@Inject
ResolveUtil(RepositorySystem repositorySystem, MavenSession session) {
ResolverUtil(RepositorySystem repositorySystem, MavenSession session) {
this.repositorySystem = Objects.requireNonNull(repositorySystem);
this.session = Objects.requireNonNull(session);
}
Expand Down Expand Up @@ -136,4 +138,36 @@ private DependencyNode resolveTransitiveDependencies(
throw new EnforcerRuleException("Could not build dependency tree " + e.getLocalizedMessage(), e);
}
}

/**
* Dump a {@link DependencyNode} as a tree.
*
* @param rootNode node to inspect
* @return dependency tree as String
*/
public CharSequence dumpTree(DependencyNode rootNode) {
StringBuilder result = new StringBuilder(System.lineSeparator());

rootNode.accept(new TreeDependencyVisitor(new DependencyVisitor() {
String indent = "";

@Override
public boolean visitEnter(org.eclipse.aether.graph.DependencyNode dependencyNode) {
result.append(indent);
result.append("Node: ").append(dependencyNode);
result.append(" data map: ").append(dependencyNode.getData());
result.append(System.lineSeparator());
indent += " ";
return true;
}

@Override
public boolean visitLeave(org.eclipse.aether.graph.DependencyNode dependencyNode) {
indent = indent.substring(0, indent.length() - 2);
return true;
}
}));

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BannedDependenciesTest {
private MavenSession session;

@Mock
private ResolveUtil resolveUtil;
private ResolverUtil resolverUtil;

@InjectMocks
private BannedDependencies rule;
Expand Down Expand Up @@ -95,7 +95,7 @@ void excludesAndIncludesDoNotUseTransitiveDependencies() throws Exception {
@Test
void excludesUseTransitiveDependencies() throws Exception {

when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder()
.withType(DependencyNodeBuilder.Type.POM)
.withChildNode(new DependencyNodeBuilder()
Expand Down Expand Up @@ -128,7 +128,7 @@ void excludesUseTransitiveDependencies() throws Exception {
@Test
void excludesAndIncludesUseTransitiveDependencies() throws Exception {

when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder()
.withType(DependencyNodeBuilder.Type.POM)
.withChildNode(new DependencyNodeBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RequireReleaseDepsTest {
private MavenSession session;

@Mock
private ResolveUtil resolveUtil;
private ResolverUtil resolverUtil;

@InjectMocks
private RequireReleaseDeps rule;
Expand All @@ -76,12 +76,12 @@ void testSearchNonTransitive() throws IOException {

assertThatCode(rule::execute).doesNotThrowAnyException();

verifyNoInteractions(resolveUtil);
verifyNoInteractions(resolverUtil);
}

@Test
void testSearchTransitiveMultipleFailures() throws Exception {
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(getDependencyNodeWithMultipleSnapshots());
rule.setSearchTransitive(true);

Expand All @@ -96,7 +96,7 @@ void testSearchTransitiveMultipleFailures() throws Exception {
@Test
void testSearchTransitiveNoFailures() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder().build());

rule.setSearchTransitive(true);
Expand All @@ -111,13 +111,13 @@ void testShouldFailOnlyWhenRelease() throws Exception {

assertThatCode(rule::execute).doesNotThrowAnyException();

verifyNoInteractions(resolveUtil);
verifyNoInteractions(resolverUtil);
}

@Test
void testWildcardExcludeTests() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(getDependencyNodeWithMultipleTestSnapshots());

rule.setExcludes(Collections.singletonList("*:*:*:*:test"));
Expand All @@ -129,7 +129,7 @@ void testWildcardExcludeTests() throws Exception {
@Test
void testWildcardExcludeAll() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(getDependencyNodeWithMultipleTestSnapshots());

rule.setExcludes(Collections.singletonList("*"));
Expand All @@ -140,7 +140,7 @@ void testWildcardExcludeAll() throws Exception {

@Test
void testExcludesAndIncludes() throws Exception {
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(getDependencyNodeWithMultipleTestSnapshots());

rule.setExcludes(Collections.singletonList("*"));
Expand All @@ -167,7 +167,7 @@ void testId() {
void testFailWhenParentIsSnapshot() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact());
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder().build());

rule.setFailWhenParentIsSnapshot(true);
Expand All @@ -181,7 +181,7 @@ void testFailWhenParentIsSnapshot() throws Exception {
void parentShouldBeExcluded() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact());
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder().build());

rule.setFailWhenParentIsSnapshot(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
class RequireUpperBoundDepsTest {

@Mock
private ResolveUtil resolveUtil;
private ResolverUtil resolverUtil;

@InjectMocks
private RequireUpperBoundDeps rule;

@Test
void testRule() throws Exception {

when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder()
.withType(DependencyNodeBuilder.Type.POM)
.withChildNode(new DependencyNodeBuilder()
Expand Down