Skip to content

Commit

Permalink
Resolves mojohaus#1197: Handling a no-version case in dependencyManag…
Browse files Browse the repository at this point in the history
…ement for CompareDependenciesMojo
  • Loading branch information
andrzejj0 committed Dec 16, 2024
1 parent a6f4ffb commit 78e6027
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
Expand All @@ -52,6 +53,7 @@
import org.eclipse.aether.RepositorySystem;

import static java.util.Collections.singletonList;
import static java.util.Optional.ofNullable;

/**
* Compare dependency versions of the current project to dependencies or dependency management of a remote repository
Expand Down Expand Up @@ -296,7 +298,7 @@ private List<String> compareVersions(
Dependency remoteDep = remoteDependencies.get(dep.getManagementKey());
if (remoteDep != null) {
String remoteVersion = remoteDep.getVersion();
if (!dep.getVersion().equals(remoteVersion)) {
if (!Objects.equals(remoteVersion, dep.getVersion())) {
StringBuilder buf = writeDependencyDiffMessage(dep, remoteVersion);
updates.add(buf.toString());
if (!reportMode) {
Expand Down Expand Up @@ -414,7 +416,10 @@ private void writeReportFile(List<String> dependenciesUpdate, List<String> prope
*/
private StringBuilder writeDependencyDiffMessage(Dependency dep, String remoteVersion) {
String id = dep.getGroupId() + ":" + dep.getArtifactId();
return writeDiffMessage(id, dep.getVersion(), remoteVersion);
return writeDiffMessage(
id,
ofNullable(dep.getVersion()).orElse("(no version)"),
ofNullable(remoteVersion).orElse("(no version)"));
}

private StringBuilder writeDiffMessage(String id, String originalVersion, String targetVersion) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.codehaus.mojo.versions;

import java.io.File;
import java.util.Collections;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.junit.Rule;
import org.junit.Test;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class CompareDependenciesMojoTest extends AbstractMojoTestCase {

@Rule
public final MojoRule mojoRule = new MojoRule(this);

@Test
public void testCompareVersionlessDependencies() throws Exception {
CompareDependenciesMojo mojo = (CompareDependenciesMojo) mojoRule.lookupConfiguredMojo(
new File("src/test/resources/org/codehaus/mojo/compare-dependencies/versionless"),
"compare-dependencies");
ProjectBuildingResult result = mock(ProjectBuildingResult.class);
when(result.getProblems()).thenReturn(Collections.emptyList());
when(result.getProject()).thenReturn(new MavenProject(new Model() {
{
setDependencyManagement(new DependencyManagement() {
{
setDependencies(Collections.singletonList(DependencyBuilder.newBuilder()
.withGroupId("defaultGroup")
.withArtifactId("artifactA")
.withScope("test")
.withVersion("1.0.0")
.build()));
}
});
}
}));

ProjectBuilder projectBuilder = mock(ProjectBuilder.class);
when(projectBuilder.build(any(Artifact.class), anyBoolean(), any(ProjectBuildingRequest.class)))
.thenReturn(result);
setVariableValueToObject(mojo, "projectBuilder", projectBuilder);
setVariableValueToObject(mojo, "remotePom", "defaultGroup:other-artifact:1.0.0");

mojo.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0.0</version>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>defaultGroup</groupId>
<artifactId>artifactA</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>

0 comments on commit 78e6027

Please sign in to comment.