Skip to content

Commit 992a232

Browse files
committed
Fixed mojohaus#1313: Do not show existing version as update
1 parent 773d0f3 commit 992a232

9 files changed

Lines changed: 151 additions & 0 deletions

File tree

versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ protected Optional<Restriction> getSelectedRestriction(ArtifactVersion selectedV
120120
return Optional.ofNullable(getCurrentVersionRange())
121121
.map(VersionRange::getRestrictions)
122122
.flatMap(r -> r.stream()
123+
.filter(rr -> rr.getLowerBound() != null || rr.getUpperBound() != null)
123124
.filter(rr -> rr.containsVersion(selectedVersion))
124125
.findAny());
125126
}

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/internal/DependencyUpdatesLoggingHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public DependencyUpdatesResult getDependencyUpdates(Map<Dependency, ArtifactVers
142142
latestVersion = Optional.of(newVersionRestriction)
143143
.map(restriction -> versions.getNewestVersion(restriction, allowSnapshots));
144144
}
145+
145146
String right =
146147
" " + latestVersion.map(v -> currentVersion + " -> " + v).orElse(currentVersion);
147148
List<String> t = latestVersion.isPresent() ? withUpdates : usingCurrent;

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,36 @@ public void testDependenciesMatchTestScope() {
701701
assertTrue(DisplayDependencyUpdatesMojo.dependenciesMatch(
702702
dependency, createManagedDependency(dependency, null, null, "sources")));
703703
}
704+
705+
@Test
706+
public void testShouldOnlyShowUpdates() throws Exception {
707+
try (CloseableTempFile tempFile = new CloseableTempFile("display-dependency-updates")) {
708+
new DisplayDependencyUpdatesMojo(
709+
artifactFactory,
710+
mockAetherRepositorySystem(new HashMap<String, String[]>() {
711+
{
712+
put("default-dependency", new String[] {"0.0.1", "0.0.2", "1.0.0"});
713+
}
714+
}),
715+
null,
716+
null) {
717+
{
718+
setProject(createProject());
719+
allowMajorUpdates = true;
720+
processDependencies = true;
721+
processDependencyManagement = false;
722+
dependencyIncludes = singletonList(WildcardMatcher.WILDCARD);
723+
dependencyExcludes = emptyList();
724+
allowSnapshots = true;
725+
outputFile = tempFile.getPath().toFile();
726+
setPluginContext(new HashMap<>());
727+
728+
session = mockMavenSession();
729+
mojoExecution = mock(MojoExecution.class);
730+
}
731+
}.execute();
732+
733+
assertThat(String.join("", Files.readAllLines(tempFile.getPath())), not(containsString("1.0.0")));
734+
}
735+
}
704736
}

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayExtensionUpdatesMojoTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.Path;
2222
import java.util.Collections;
2323
import java.util.HashMap;
24+
import java.util.List;
2425

2526
import org.apache.maven.artifact.Artifact;
2627
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
@@ -54,6 +55,7 @@
5455
import static org.hamcrest.Matchers.containsString;
5556
import static org.hamcrest.Matchers.empty;
5657
import static org.hamcrest.Matchers.equalTo;
58+
import static org.hamcrest.Matchers.hasItem;
5759
import static org.hamcrest.Matchers.instanceOf;
5860
import static org.junit.Assert.fail;
5961
import static org.mockito.ArgumentMatchers.any;
@@ -312,4 +314,29 @@ public void testProblemCausingArtifact()
312314
assertThat(vre.getArtifact().map(Artifact::getArtifactId).orElse(""), equalTo("problem-causing-artifact"));
313315
}
314316
}
317+
318+
@Test
319+
public void testLatestVersion()
320+
throws MojoExecutionException, MojoFailureException, IOException, IllegalAccessException {
321+
setVariableValueToObject(mojo, "extensionExcludes", emptyList());
322+
setVariableValueToObject(mojo, "extensionIncludes", singletonList("*"));
323+
mojo.getProject().setBuild(new Build());
324+
mojo.getProject()
325+
.getBuild()
326+
.setExtensions(Collections.singletonList(ExtensionBuilder.newBuilder()
327+
.withGroupId("default-group")
328+
.withArtifactId("artifactC")
329+
.withVersion("1.0.0")
330+
.build()));
331+
332+
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
333+
pomHelper
334+
.when(() -> PomHelper.getChildModels(ArgumentMatchers.any(MavenProject.class), any()))
335+
.then(i -> Collections.singletonMap(null, ((MavenProject) i.getArgument(0)).getModel()));
336+
mojo.execute();
337+
}
338+
339+
List<String> output = Files.readAllLines(tempPath);
340+
assertThat(output, hasItem(containsString("No extensions have newer versions.")));
341+
}
315342
}

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.Path;
2323
import java.util.Collections;
2424
import java.util.HashMap;
25+
import java.util.List;
2526

2627
import org.apache.maven.artifact.Artifact;
2728
import org.apache.maven.artifact.DefaultArtifact;
@@ -55,7 +56,9 @@
5556
import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactHandlerManager;
5657
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
5758
import static org.hamcrest.MatcherAssert.assertThat;
59+
import static org.hamcrest.Matchers.containsString;
5860
import static org.hamcrest.Matchers.equalTo;
61+
import static org.hamcrest.Matchers.hasItem;
5962
import static org.hamcrest.Matchers.instanceOf;
6063
import static org.hamcrest.Matchers.is;
6164
import static org.hamcrest.Matchers.notNullValue;
@@ -411,4 +414,19 @@ public void testProblemCausingArtifact() throws MojoFailureException {
411414
assertThat(vre.getArtifact().map(Artifact::getArtifactId).orElse(""), equalTo("problem-causing-artifact"));
412415
}
413416
}
417+
418+
@Test
419+
public void testLatestVersion() throws Exception {
420+
mojo.getProject().setParent(new MavenProject() {
421+
{
422+
setGroupId("default-group");
423+
setArtifactId("parent-artifact");
424+
setVersion("1.0.0");
425+
}
426+
});
427+
mojo.allowSnapshots = false;
428+
mojo.execute();
429+
List<String> output = Files.readAllLines(tempFile);
430+
assertThat(output, hasItem(containsString("The parent project is the latest version")));
431+
}
414432
}

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,18 @@ public void testProblemCausingArtifact() throws Exception {
183183
assertThat(vre.getArtifact().map(Artifact::getArtifactId).orElse(""), equalTo("problem-causing-artifact"));
184184
}
185185
}
186+
187+
@Test
188+
public void testLatestVersion() throws Exception {
189+
Files.copy(
190+
Paths.get("src/test/resources/org/codehaus/mojo/display-plugin-updates/updates-only.xml"),
191+
tempDir.resolve("pom.xml"));
192+
193+
DisplayPluginUpdatesMojo mojo = createMojo();
194+
mojo.execute();
195+
196+
List<String> output = Files.readAllLines(outputPath);
197+
assertThat(
198+
output, hasItem(containsString("All plugins with a version specified are using the latest versions.")));
199+
}
186200
}

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojoTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.file.Path;
2020
import java.nio.file.Paths;
2121
import java.util.HashMap;
22+
import java.util.List;
2223

2324
import org.apache.maven.artifact.Artifact;
2425
import org.apache.maven.plugin.MojoExecutionException;
@@ -36,6 +37,7 @@
3637
import static org.hamcrest.MatcherAssert.assertThat;
3738
import static org.hamcrest.Matchers.containsString;
3839
import static org.hamcrest.Matchers.equalTo;
40+
import static org.hamcrest.Matchers.hasItem;
3941
import static org.hamcrest.Matchers.instanceOf;
4042
import static org.hamcrest.Matchers.matchesPattern;
4143
import static org.hamcrest.Matchers.not;
@@ -156,4 +158,22 @@ public void testProblemCausingArtifact() throws Exception {
156158
assertThat(vre.getArtifact().map(Artifact::getArtifactId).orElse(""), equalTo("problem-causing-artifact"));
157159
}
158160
}
161+
162+
@Test
163+
public void testLatestVersion() throws Exception {
164+
TestUtils.copyDir(
165+
Paths.get("src/test/resources/org/codehaus/mojo/display-property-updates/updates-only"), tempDir);
166+
DisplayPropertyUpdatesMojo mojo = (DisplayPropertyUpdatesMojo)
167+
mojoRule.lookupConfiguredMojo(tempDir.toFile(), "display-property-updates");
168+
mojo.outputEncoding = UTF_8;
169+
mojo.outputFile = tempFile.toFile();
170+
mojo.setPluginContext(new HashMap<>());
171+
mojo.repositorySystem = mockAetherRepositorySystem();
172+
mojo.execute();
173+
174+
List<String> output = Files.readAllLines(tempFile);
175+
assertThat(
176+
output,
177+
hasItem(containsString("All version properties are referencing the newest version available.")));
178+
}
159179
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>test-group</groupId>
6+
<artifactId>test-artifact</artifactId>
7+
<version>DEVELOP-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>default-group</groupId>
13+
<artifactId>artifactC</artifactId>
14+
<version>1.0.0</version>
15+
</plugin>
16+
</plugins>
17+
</build>
18+
19+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>default-group</groupId>
4+
<artifactId>parent</artifactId>
5+
<version>1.0.0</version>
6+
<packaging>pom</packaging>
7+
8+
<properties>
9+
<ver>1.0.0</ver>
10+
</properties>
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>default-group</groupId>
15+
<artifactId>artifactC</artifactId>
16+
<version>${ver}</version>
17+
</dependency>
18+
</dependencies>
19+
</project>

0 commit comments

Comments
 (0)