Skip to content

Commit a3ab213

Browse files
authored
Resolves #1060: Bugfix in UpdateParentMojo + add tests (#1181)
1 parent 380264f commit a3ab213

File tree

2 files changed

+98
-40
lines changed

2 files changed

+98
-40
lines changed

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

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.codehaus.mojo.versions.utils.SegmentUtils;
4949
import org.eclipse.aether.RepositorySystem;
5050

51+
import static java.util.stream.StreamSupport.stream;
5152
import static org.apache.commons.lang3.StringUtils.isBlank;
5253

5354
/**
@@ -164,12 +165,9 @@ protected void update(MutableXMLStreamReader pom)
164165
throw new MojoExecutionException("skipResolution is only valid if parentVersion is set");
165166
}
166167

167-
String initialVersion = Optional.ofNullable(parentVersion)
168-
.orElse(getProject().getParent().getVersion());
169168
try {
170-
ArtifactVersion artifactVersion = skipResolution
171-
? DefaultArtifactVersionCache.of(parentVersion)
172-
: resolveTargetVersion(initialVersion);
169+
ArtifactVersion artifactVersion =
170+
skipResolution ? DefaultArtifactVersionCache.of(parentVersion) : resolveTargetVersion();
173171
if (artifactVersion != null) {
174172
getLog().info("Updating parent from " + getProject().getParent().getVersion() + " to "
175173
+ artifactVersion);
@@ -190,25 +188,25 @@ protected void update(MutableXMLStreamReader pom)
190188
}
191189
}
192190
} catch (InvalidVersionSpecificationException e) {
193-
throw new MojoExecutionException("Invalid version range specification: " + initialVersion, e);
191+
throw new MojoExecutionException("Invalid version range specification: " + parentVersion, e);
194192
} catch (InvalidSegmentException e) {
195-
throw new MojoExecutionException("Invalid segment specification for version " + initialVersion, e);
193+
throw new MojoExecutionException("Invalid segment specification for version " + parentVersion, e);
196194
}
197195
}
198196

199-
protected ArtifactVersion resolveTargetVersion(String initialVersion)
197+
protected ArtifactVersion resolveTargetVersion()
200198
throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException,
201199
InvalidSegmentException {
202200
Artifact artifact = getHelper()
203201
.createDependencyArtifact(DependencyBuilder.newBuilder()
204202
.withGroupId(getProject().getParent().getGroupId())
205203
.withArtifactId(getProject().getParent().getArtifactId())
206-
.withVersion(initialVersion)
204+
.withVersion(getProject().getParent().getVersion())
207205
.withType("pom")
208206
.build());
209207

210-
VersionRange targetVersionRange = VersionRange.createFromVersionSpec(initialVersion);
211-
if (targetVersionRange.getRecommendedVersion() != null) {
208+
VersionRange targetVersionRange = VersionRange.createFromVersionSpec(parentVersion);
209+
if (targetVersionRange != null && targetVersionRange.getRecommendedVersion() != null) {
212210
targetVersionRange = targetVersionRange.restrict(
213211
VersionRange.createFromVersionSpec("[" + targetVersionRange.getRecommendedVersion() + ",)"));
214212
}
@@ -219,27 +217,35 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion)
219217

220218
// currentVersion (set to parentVersion here) is not included in the version range for searching upgrades
221219
// unless we set allowDowngrade to true
222-
for (ArtifactVersion candidate : reverse(versions.getNewerVersions(
223-
initialVersion, unchangedSegment, allowSnapshots, !isBlank(parentVersion) || allowDowngrade))) {
224-
if (allowDowngrade
225-
|| targetVersionRange == null
226-
|| ArtifactVersions.isVersionInRange(candidate, targetVersionRange)) {
227-
if (shouldApplyUpdate(artifact, getProject().getParent().getVersion(), candidate, forceUpdate)) {
228-
return candidate;
229-
} else {
230-
getLog().debug("Update not applied. Exiting.");
231-
return null;
232-
}
233-
}
234-
}
235220

236-
if (versions.isEmpty(allowSnapshots)) {
237-
getLog().info("No versions found");
238-
} else {
239-
getLog().info("The parent project is the latest version");
240-
}
241-
242-
return null;
221+
VersionRange finalTargetVersionRange = targetVersionRange;
222+
return stream(
223+
reverse(versions.getNewerVersions(
224+
getProject().getParent().getVersion(),
225+
unchangedSegment,
226+
allowSnapshots,
227+
allowDowngrade))
228+
.spliterator(),
229+
false)
230+
.filter(v -> finalTargetVersionRange == null
231+
|| ArtifactVersions.isVersionInRange(v, finalTargetVersionRange))
232+
.findFirst()
233+
.map(candidate -> {
234+
if (shouldApplyUpdate(artifact, getProject().getParent().getVersion(), candidate, forceUpdate)) {
235+
return candidate;
236+
} else {
237+
getLog().debug("Update not applied. Exiting.");
238+
return null;
239+
}
240+
})
241+
.orElseGet(() -> {
242+
if (versions.isEmpty(allowSnapshots)) {
243+
getLog().info("No versions found");
244+
} else {
245+
getLog().info("The parent project is the latest version");
246+
}
247+
return null;
248+
});
243249
}
244250

245251
private static <T> Iterable<T> reverse(T[] array) {

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

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static void setUpStatic() {
7676
put("dummy-parent2", new String[] {"1.0", "2.0", "3.0", "3.0-alpha-1", "3.0-beta-1"});
7777
put("test-incremental", new String[] {"1.0.0", "1.1.0", "1.1.1", "2.0.0"});
7878
put("unknown-artifact", new String[0]);
79+
put("mojo-parent", new String[] {"70", "78", "86"});
7980
}
8081
});
8182
}
@@ -186,10 +187,10 @@ public void testParentDowngradeAllowedWithRange()
186187
{
187188
setGroupId("default-group");
188189
setArtifactId("parent-artifact");
190+
setVersion("[1.0.1-SNAPSHOT,)");
189191
}
190192
});
191-
192-
ArtifactVersion newVersion = mojo.resolveTargetVersion("[1.0.1-SNAPSHOT,)");
193+
ArtifactVersion newVersion = mojo.resolveTargetVersion();
193194
assertThat(newVersion, notNullValue());
194195
assertThat(newVersion.toString(), is("1.0.0"));
195196
}
@@ -199,7 +200,8 @@ public void testParentDowngradeForbiddenWithRange()
199200
throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException,
200201
InvalidSegmentException {
201202
mojo.allowDowngrade = false;
202-
ArtifactVersion newVersion = mojo.resolveTargetVersion("[1.0.1-SNAPSHOT,)");
203+
mojo.parentVersion = "[1.0.1-SNAPSHOT,)";
204+
ArtifactVersion newVersion = mojo.resolveTargetVersion();
203205
assertThat(newVersion, nullValue());
204206
}
205207

@@ -212,10 +214,11 @@ public void testAllowSnapshots()
212214
{
213215
setGroupId("default-group");
214216
setArtifactId("issue-670-artifact");
217+
setVersion("0.0.1-1");
215218
}
216219
});
217220

218-
ArtifactVersion newVersion = mojo.resolveTargetVersion("0.0.1-1");
221+
ArtifactVersion newVersion = mojo.resolveTargetVersion();
219222
assertThat(newVersion, notNullValue());
220223
assertThat(newVersion.toString(), is("0.0.1-1-impl-SNAPSHOT"));
221224
}
@@ -253,10 +256,11 @@ public void testIgnoredVersions()
253256
{
254257
setGroupId("default-group");
255258
setArtifactId("parent-artifact");
259+
setVersion("0.9.0");
256260
}
257261
});
258262
setVariableValueToObject(mojo, "ignoredVersions", singleton("1.0.0"));
259-
assertThat(mojo.resolveTargetVersion("0.9.0"), nullValue());
263+
assertThat(mojo.resolveTargetVersion(), nullValue());
260264
}
261265

262266
@Test
@@ -337,8 +341,8 @@ public void testAllowMinorUpdates()
337341
mojo.allowMajorUpdates = false;
338342
mojo.allowMinorUpdates = true;
339343
mojo.allowIncrementalUpdates = true;
340-
341-
ArtifactVersion newVersion = mojo.resolveTargetVersion("0.8.0");
344+
mojo.parentVersion = "0.8.0";
345+
ArtifactVersion newVersion = mojo.resolveTargetVersion();
342346

343347
assertThat(newVersion, notNullValue());
344348
assertThat(newVersion.toString(), is("0.9.0"));
@@ -352,13 +356,13 @@ public void testAllowIncrementalUpdates()
352356
{
353357
setGroupId("default-group");
354358
setArtifactId("test-incremental");
359+
setVersion("1.1.0");
355360
}
356361
});
357362
mojo.allowMajorUpdates = false;
358363
mojo.allowMinorUpdates = false;
359364
mojo.allowIncrementalUpdates = true;
360-
361-
ArtifactVersion newVersion = mojo.resolveTargetVersion("1.1.0");
365+
ArtifactVersion newVersion = mojo.resolveTargetVersion();
362366

363367
assertThat(newVersion, notNullValue());
364368
assertThat(newVersion.toString(), is("1.1.1"));
@@ -407,4 +411,52 @@ public void testParentVersionRange2()
407411
}
408412
assertThat(changeRecorder.getChanges(), empty());
409413
}
414+
415+
/*
416+
* Reproduces issue 1060
417+
*/
418+
@Test
419+
public void testIssue1060VersionRangeAllowDowngradeFalse()
420+
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
421+
mojo.getProject().setParent(new MavenProject() {
422+
{
423+
setGroupId("default-group");
424+
setArtifactId("dummy-parent2");
425+
setVersion("2.0");
426+
}
427+
});
428+
mojo.parentVersion = "[,2.9-!)";
429+
mojo.allowDowngrade = false;
430+
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
431+
pomHelper
432+
.when(() -> PomHelper.setProjectParentVersion(any(), any()))
433+
.thenReturn(true);
434+
mojo.update(null);
435+
}
436+
assertThat(changeRecorder.getChanges(), empty());
437+
}
438+
439+
/*
440+
* Reproduces issue 1060
441+
*/
442+
@Test
443+
public void testIssue1060VersionRangeAllowDowngradeTrue()
444+
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
445+
mojo.getProject().setParent(new MavenProject() {
446+
{
447+
setGroupId("default-group");
448+
setArtifactId("mojo-parent");
449+
setVersion("78");
450+
}
451+
});
452+
mojo.parentVersion = "[,79-!)";
453+
mojo.allowDowngrade = true;
454+
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
455+
pomHelper
456+
.when(() -> PomHelper.setProjectParentVersion(any(), any()))
457+
.thenReturn(true);
458+
mojo.update(null);
459+
}
460+
assertThat(changeRecorder.getChanges(), empty());
461+
}
410462
}

0 commit comments

Comments
 (0)