Skip to content

Commit

Permalink
Preparing the test
Browse files Browse the repository at this point in the history
Based on the proposed changes by Robert Scholte in
#2378 (comment)
  • Loading branch information
suztomo committed Sep 13, 2024
1 parent c8414f0 commit e399859
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.apache.maven.project.ProjectDependenciesResolver;
import org.eclipse.aether.DefaultRepositoryCache;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
Expand Down Expand Up @@ -109,6 +110,31 @@ public class LinkageCheckerRule extends AbstractEnforcerRule {

private ClassPathBuilder classPathBuilder;

// Properties managed by the dependency injection
private MavenProject project;

private MavenSession session;

private ProjectDependenciesResolver projectDependenciesResolver;

private MojoExecution execution;

private final RepositorySystem repoSystem;

@Inject
public LinkageCheckerRule(
MavenProject project,
MavenSession session,
MojoExecution execution,
ProjectDependenciesResolver projectDependenciesResolver,
RepositorySystem repoSystem) {
this.project = project;
this.session = session;
this.execution = execution;
this.projectDependenciesResolver = projectDependenciesResolver;
this.repoSystem = repoSystem;
}

@VisibleForTesting
void setDependencySection(DependencySection dependencySection) {
this.dependencySection = dependencySection;
Expand All @@ -132,14 +158,6 @@ void setExclusionFile(String exclusionFile) {
this.exclusionFile = exclusionFile;
}

@Inject private MavenProject project;

@Inject private MavenSession session;

@Inject private ProjectDependenciesResolver projectDependenciesResolver;

@Inject private MojoExecution execution;

private static EnforcerLogger logger;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@
import java.util.stream.Collectors;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.enforcer.rule.api.EnforcerLevel;
import org.apache.maven.enforcer.rule.api.EnforcerLogger;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.DependencyResolutionException;
import org.apache.maven.project.DependencyResolutionRequest;
import org.apache.maven.project.DependencyResolutionResult;
Expand Down Expand Up @@ -75,14 +74,13 @@

public class LinkageCheckerRuleTest {

private LinkageCheckerRule rule = new LinkageCheckerRule();
private LinkageCheckerRule rule;
private RepositorySystem repositorySystem;
private RepositorySystemSession repositorySystemSession;
private Artifact dummyArtifactWithFile;

private MavenProject mockProject;
private EnforcerRuleHelper mockRuleHelper;
private Log mockLog;
private EnforcerLogger mockLog;
private MavenSession mockMavenSession;
private MojoExecution mockMojoExecution;
private ProjectDependenciesResolver mockProjectDependenciesResolver;
Expand All @@ -96,6 +94,15 @@ public void setup()
repositorySystemSession = RepositoryUtility.newSession(repositorySystem);
dummyArtifactWithFile = createArtifactWithDummyFile("a:b:0.1");
setupMock();

rule =
new LinkageCheckerRule(
mockProject,
mockMavenSession,
mockMojoExecution,
mockProjectDependenciesResolver,
repositorySystem);
rule.setLog(mockLog);
}

private Artifact createArtifactWithDummyFile(String coordinates) throws URISyntaxException {
Expand All @@ -109,20 +116,13 @@ private void setupMock()
mockProject = mock(MavenProject.class);
mockMavenSession = mock(MavenSession.class);
when(mockMavenSession.getRepositorySession()).thenReturn(repositorySystemSession);
mockRuleHelper = mock(EnforcerRuleHelper.class);
mockProjectDependenciesResolver = mock(ProjectDependenciesResolver.class);
mockDependencyResolutionResult = mock(DependencyResolutionResult.class);
mockLog = mock(Log.class);
when(mockRuleHelper.getLog()).thenReturn(mockLog);
when(mockRuleHelper.getComponent(ProjectDependenciesResolver.class))
.thenReturn(mockProjectDependenciesResolver);
mockLog = mock(EnforcerLogger.class);
when(mockProjectDependenciesResolver.resolve(any(DependencyResolutionRequest.class)))
.thenReturn(mockDependencyResolutionResult);
when(mockRuleHelper.evaluate("${session}")).thenReturn(mockMavenSession);
when(mockRuleHelper.evaluate("${project}")).thenReturn(mockProject);
mockMojoExecution = mock(MojoExecution.class);
when(mockMojoExecution.getLifecyclePhase()).thenReturn("verify");
when(mockRuleHelper.evaluate("${mojoExecution}")).thenReturn(mockMojoExecution);
org.apache.maven.artifact.DefaultArtifact rootArtifact =
new org.apache.maven.artifact.DefaultArtifact(
"com.google.cloud",
Expand Down Expand Up @@ -194,7 +194,7 @@ public void testExecute_shouldPassGoodProject()
// Since Guava 27, it requires com.google.guava:failureaccess artifact in its dependency.
setupMockDependencyResolution("com.google.guava:guava:27.0.1-jre");
// This should not raise an EnforcerRuleException
rule.execute(mockRuleHelper);
rule.execute();
verify(mockLog).info("No error found");
}

Expand All @@ -203,7 +203,7 @@ public void testExecute_shouldPassGoodProject_sessionProperties()
throws EnforcerRuleException, RepositoryException, DependencyResolutionException {
setupMockDependencyResolution("com.google.guava:guava:27.0.1-jre");

rule.execute(mockRuleHelper);
rule.execute();

ArgumentCaptor<DependencyResolutionRequest> argumentCaptor =
ArgumentCaptor.forClass(DependencyResolutionRequest.class);
Expand All @@ -225,7 +225,7 @@ public void testExecute_shouldFailForBadProject() throws RepositoryException {
try {
// This artifact is known to contain classes missing dependencies
setupMockDependencyResolution("com.google.appengine:appengine-api-1.0-sdk:1.9.64");
rule.execute(mockRuleHelper);
rule.execute();
Assert.fail(
"The rule should raise an EnforcerRuleException for artifacts missing dependencies");
} catch (EnforcerRuleException ex) {
Expand Down Expand Up @@ -259,7 +259,7 @@ public void testExecute_shouldFailForBadProject_reachableErrors() throws Reposit
setupMockDependencyResolution(
"com.google.api-client:google-api-client:1.27.0", "io.grpc:grpc-core:1.17.1");
rule.setReportOnlyReachable(true);
rule.execute(mockRuleHelper);
rule.execute();
Assert.fail(
"The rule should raise an EnforcerRuleException for artifacts with reachable errors");
} catch (EnforcerRuleException ex) {
Expand All @@ -278,9 +278,23 @@ public void testExecute_shouldPassForBadProject_levelWarn()
// grpc-core is included in entry point jars, the errors are reachable.
setupMockDependencyResolution(
"com.google.api-client:google-api-client:1.27.0", "io.grpc:grpc-core:1.17.1");

rule =
new LinkageCheckerRule(
mockProject,
mockMavenSession,
mockMojoExecution,
mockProjectDependenciesResolver,
repositorySystem) {
@Override
public EnforcerLevel getLevel() {
return EnforcerLevel.WARN;
}
};
rule.setLog(mockLog);

rule.setReportOnlyReachable(true);
rule.setLevel(EnforcerLevel.WARN);
rule.execute(mockRuleHelper);
rule.execute();
verify(mockLog)
.warn(ArgumentMatchers.startsWith("Linkage Checker rule found 1 reachable error:"));
}
Expand All @@ -293,7 +307,7 @@ public void testExecute_shouldPassGoodProject_unreachableErrors()
setupMockDependencyResolution("com.google.cloud:google-cloud-automl:0.81.0-beta");
rule.setReportOnlyReachable(true);
// This should not raise EnforcerRuleException because the linkage errors are unreachable.
rule.execute(mockRuleHelper);
rule.execute();
}

private void setupMockDependencyManagementSection(String... coordinates) {
Expand Down Expand Up @@ -341,7 +355,7 @@ public void testExecute_shouldPassEmptyBom() throws EnforcerRuleException {
setupMockDependencyManagementSection(); // empty BOM

// This should not raise an EnforcerRuleException
rule.execute(mockRuleHelper);
rule.execute();
}

@Test
Expand All @@ -352,7 +366,7 @@ public void testExecute_shouldPassGoodBom() throws EnforcerRuleException {
"io.grpc:grpc-auth:1.18.0",
"com.google.api:api-common:1.7.0");
// This should not raise an EnforcerRuleException
rule.execute(mockRuleHelper);
rule.execute();
}

@Test
Expand All @@ -362,7 +376,7 @@ public void testExecute_shouldFailBadBom() {
"com.google.api-client:google-api-client:1.27.0", "io.grpc:grpc-core:1.17.1");

try {
rule.execute(mockRuleHelper);
rule.execute();
Assert.fail("Enforcer rule should detect conflict between google-api-client and grpc-core");
} catch (EnforcerRuleException ex) {
// pass
Expand All @@ -384,7 +398,7 @@ public void testExecute_shouldSkipBadBomWithNonPomPackaging() throws EnforcerRul
"jar", // BOM should have pom here
null,
new DefaultArtifactHandler()));
rule.execute(mockRuleHelper);
rule.execute();
}

@Test
Expand All @@ -400,7 +414,7 @@ public void testExecute_shouldSkipNonBomPom() throws EnforcerRuleException {
null,
new DefaultArtifactHandler()));
// No exception
rule.execute(mockRuleHelper);
rule.execute();
}

@Test
Expand All @@ -419,7 +433,7 @@ public void testExecute_shouldExcludeTestScope() throws EnforcerRuleException {
when(mockProject.getDependencies())
.thenReturn(ImmutableList.of(dependency));

rule.execute(mockRuleHelper);
rule.execute();
}

@Test
Expand All @@ -440,7 +454,7 @@ public void testExecute_shouldFailForBadProjectWithBundlePackaging() throws Repo
rootArtifact.setFile(new File("dummy.jar"));
when(mockProject.getArtifact()).thenReturn(rootArtifact);

rule.execute(mockRuleHelper);
rule.execute();
Assert.fail(
"The rule should raise an EnforcerRuleException for artifacts missing dependencies");
} catch (EnforcerRuleException ex) {
Expand Down Expand Up @@ -470,7 +484,7 @@ public void testExecute_shouldFilterExclusionRule_java8()
.toAbsolutePath()
.toString();
rule.setExclusionFile(exclusionFileLocation);
rule.execute(mockRuleHelper);
rule.execute();
Assert.fail(
"The rule should raise an EnforcerRuleException for artifacts missing dependencies");
} catch (EnforcerRuleException ex) {
Expand Down Expand Up @@ -510,7 +524,7 @@ public void testArtifactTransferError()
when(mockProjectDependenciesResolver.resolve(any())).thenThrow(exception);

try {
rule.execute(mockRuleHelper);
rule.execute();
fail("The rule should throw EnforcerRuleException upon dependency resolution exception");
} catch (EnforcerRuleException expected) {
verify(mockLog)
Expand Down Expand Up @@ -565,7 +579,7 @@ public void testArtifactTransferError_acceptableMissingArtifact()

// Should not throw DependencyResolutionException, because the missing xerces-impl is under both
// provided and optional dependencies.
rule.execute(mockRuleHelper);
rule.execute();
}

@Test
Expand Down Expand Up @@ -603,7 +617,7 @@ public void testArtifactTransferError_missingArtifactNotInGraph()

when(mockProjectDependenciesResolver.resolve(any())).thenThrow(exception);

rule.execute(mockRuleHelper);
rule.execute();
verify(mockLog)
.warn("xerces:xerces-impl:jar:2.6.2 was not resolved. Dependency path is unknown.");
}
Expand All @@ -620,7 +634,7 @@ public void testSkippingProjectWithoutFile() throws EnforcerRuleException {
"jar",
null,
new DefaultArtifactHandler()));
rule.execute(mockRuleHelper);
rule.execute();
}

@Test
Expand All @@ -638,7 +652,7 @@ public void testValidatePhase() {

when(mockMojoExecution.getLifecyclePhase()).thenReturn("validate");
try {
rule.execute(mockRuleHelper);
rule.execute();
fail("The rule should throw EnforcerRuleException when running in validate phase");
} catch (EnforcerRuleException ex) {
assertEquals(
Expand Down

0 comments on commit e399859

Please sign in to comment.