From 2c0c1413287afa91a192fd0595d5afee09b8ce07 Mon Sep 17 00:00:00 2001
From: Andrzej Jarmoniuk Returns a {@linkplain Restriction} object for computing version upgrades
- * with the given segment allowing updates, with all more major segments locked in place.
The resulting restriction could be thought of as one
* retaining the versions on positions up to the held position,
* the position right after the position held in place will be incremented by one,
@@ -340,12 +341,13 @@ ArtifactVersion[] getAllUpdates(Optional This will allow matching the required versions while not matching versions which are considered
* inferior than the zeroth version, i.e. versions with a qualifier. Represents an immutable artifact version with all segments major to the given segment
+ * Represents an immutable upper bound artifact version
+ * where all segments major or equal to the given segment
* held in place. It can be thought of as an artifact having +∞ as its upper bound
- * on all segments minor to the held segment.
For example:
*A {@link BoundArtifactVersion} of {@code [1.2.3-2, INCREMENTAL]} can be seen as {@code 1.2.+∞} * and will be greater than all versions matching the {@code 1.2.*} pattern.
*A {@link BoundArtifactVersion} of {@code [1.2.3-2, SUBINCREMENTAL]} will be greater - * * than all versions matching the {@code 1.2.3-2.*} pattern.
+ * * than all versions matching the {@code 1.2.3.*} pattern. *When compared to another artifact versions, this results with the other object * with the segment versions up to the held segment being equal, * always comparing lower than this object.
@@ -27,26 +29,33 @@ */ public class BoundArtifactVersion implements ArtifactVersion { /** - * Most major segment that can change, i.e. not held in place. - * All segments that are more major than this one are held in place. + * Least major segment that may not change + * All segments that are more major than this one are also held in place. + * If equal to {@code null}, no restrictions are in place, meaning that all version + * segments can change freely. */ - private final Segment segment; + private final Segment unchangedSegment; private final ArtifactVersion comparable; /** * Constructs the instance given the version in a text format. * @param artifactVersion version in a text format - * @param segment most major segment that can change, i.e. not held in place + * @param unchangedSegment most major segment that may not change, may be {@code null} meaning no restrictions */ - public BoundArtifactVersion(String artifactVersion, Segment segment) { - this.segment = segment; + public BoundArtifactVersion(String artifactVersion, Segment unchangedSegment) { + this.unchangedSegment = unchangedSegment; + comparable = createComparable(artifactVersion, unchangedSegment); + } + + static ArtifactVersion createComparable(String artifactVersion, Segment unchangedSegment) { + final ArtifactVersion comparable; StringBuilder versionBuilder = new StringBuilder(); String[] segments = tokens(artifactVersion); for (int segNr = 0; segNr <= segments.length || segNr <= Segment.SUBINCREMENTAL.value(); segNr++, versionBuilder.append(".")) { - if (segNr < segment.value()) { + if (segNr <= Optional.ofNullable(unchangedSegment).map(Segment::value).orElse(-1)) { versionBuilder.append(segNr < segments.length ? integerItemOrZero(segments[segNr]) : "0"); } else { versionBuilder.append(Integer.MAX_VALUE); @@ -54,15 +63,16 @@ public BoundArtifactVersion(String artifactVersion, Segment segment) { } versionBuilder.append(Integer.MAX_VALUE); comparable = DefaultArtifactVersionCache.of(versionBuilder.toString()); + return comparable; } /** * Constructs the instance given a {@link ArtifactVersion instance} * @param artifactVersion artifact version containing the segment version values - * @param segment most major segment that can change, i.e. not held in place + * @param unchangedSegment least major segment that may not change */ - public BoundArtifactVersion(ArtifactVersion artifactVersion, Segment segment) { - this(artifactVersion.toString(), segment); + public BoundArtifactVersion(ArtifactVersion artifactVersion, Segment unchangedSegment) { + this(artifactVersion.toString(), unchangedSegment); } /** @@ -108,8 +118,8 @@ private static String integerItemOrZero(String item) { * All segments that are more major than this one are held in place. * @return segment that can change */ - public Segment getSegment() { - return segment; + public Segment getUnchangedSegment() { + return unchangedSegment; } @Override @@ -135,7 +145,7 @@ public boolean equals(Object o) { return new EqualsBuilder() .appendSuper(super.equals(o)) - .append(getSegment(), that.getSegment()) + .append(getUnchangedSegment(), that.getUnchangedSegment()) .append(comparable, that.comparable) .isEquals(); } @@ -144,7 +154,7 @@ public boolean equals(Object o) { public int hashCode() { return new HashCodeBuilder(17, 37) .appendSuper(super.hashCode()) - .append(getSegment()) + .append(getUnchangedSegment()) .append(comparable) .toHashCode(); } diff --git a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java index 18bad5911..00e2f650e 100644 --- a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java +++ b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java @@ -27,65 +27,95 @@ import static org.codehaus.mojo.versions.api.Segment.MINOR; import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.*; /** * Unit tests for {@link BoundArtifactVersion} */ public class BoundArtifactVersionTest { @Test - public void testMajorUpperBoundGreaterThanNextMajor() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.2.3", MAJOR); + public void testCreateComparableForNoRestrictions() { + ArtifactVersion comparable = BoundArtifactVersion.createComparable("1.2.3", null); + assertThat(comparable.toString(), matchesPattern("^\\Q2147483647.2147483647.2147483647\\E.*")); + } + + @Test + public void testCreateComparableForMajor() { + ArtifactVersion comparable = BoundArtifactVersion.createComparable("1.2.3", MAJOR); + assertThat(comparable.toString(), matchesPattern("^\\Q1.2147483647.2147483647\\E.*")); + } + + @Test + public void testCreateComparableForMinor() { + ArtifactVersion comparable = BoundArtifactVersion.createComparable("1.2.3", MINOR); + assertThat(comparable.toString(), matchesPattern("^\\Q1.2.2147483647\\E.*")); + } + + @Test + public void testCreateComparableForIncremental() { + ArtifactVersion comparable = BoundArtifactVersion.createComparable("1.2.3", INCREMENTAL); + assertThat(comparable.toString(), matchesPattern("^\\Q1.2.3\\E.*")); + } + + @Test + public void testCreateComparableForSubIncremental() { + ArtifactVersion comparable = BoundArtifactVersion.createComparable("1.2.3-4", SUBINCREMENTAL); + assertThat(comparable.toString(), matchesPattern("^\\Q1.2.3.4\\E.*")); + } + + + @Test + public void testNullLockedGreaterThanNextMajor() { + BoundArtifactVersion bound = new BoundArtifactVersion("1.2.3", null); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("2.0.0"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test - public void testSubIncrementalUpperBoundGreaterThanNextSubIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.2.3-2", SUBINCREMENTAL); + public void testIncrementalLockedGreaterThanNextSubIncremental() { + BoundArtifactVersion bound = new BoundArtifactVersion("1.2.3-2", INCREMENTAL); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.2.3-3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testVersionShorterThanSegment() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.1", INCREMENTAL); + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", MINOR); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testVersionBoundArtifactVersionShorterThanConcreteVersionAndSegment() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.1", SUBINCREMENTAL); + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", INCREMENTAL); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.3"); assertThat(bound.compareTo(artifactVersion), lessThan(0)); } @Test - public void testVersionSubIncrementalBoundGreaterThanSubIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.1", SUBINCREMENTAL); + public void testVersionHeldIncrementalBoundGreaterThanSubIncremental() { + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", INCREMENTAL); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.0-2"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test - public void testVersionSubIncrementalBoundGreaterThanIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.1", INCREMENTAL); + public void testVersionHeldMinorBoundGreaterThanIncremental() { + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", MINOR); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test - public void testVersionSubIncrementalBoundGreaterThanMinor() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.1", MINOR); + public void testVersionHeldMajorBoundGreaterThanMinor() { + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", MAJOR); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testSnapshotWithSubIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion("1.0.0-SNAPSHOT", MINOR); + BoundArtifactVersion bound = new BoundArtifactVersion("1.0.0-SNAPSHOT", MAJOR); ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.0.0"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java index c4154f86f..ace8701a5 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java @@ -206,9 +206,6 @@ public class DisplayDependencyUpdatesMojo extends AbstractVersionsDisplayMojo { /** * Whether to allow the major version number to be changed. * - *Note: {@code false} also implies {@linkplain #allowAnyUpdates} - * to be {@code false}
- * * @since 2.5 */ @Parameter(property = "allowMajorUpdates", defaultValue = "true") @@ -217,8 +214,8 @@ public class DisplayDependencyUpdatesMojo extends AbstractVersionsDisplayMojo { /** *Whether to allow the minor version number to be changed.
* - *Note: {@code false} also implies {@linkplain #allowAnyUpdates} - * and {@linkplain #allowMajorUpdates} to be {@code false}
+ *Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * to be {@code false}
* * @since 2.5 */ @@ -228,32 +225,14 @@ public class DisplayDependencyUpdatesMojo extends AbstractVersionsDisplayMojo { /** *Whether to allow the incremental version number to be changed.
* - *Note: {@code false} also implies {@linkplain #allowAnyUpdates}, - * {@linkplain #allowMajorUpdates}, and {@linkplain #allowMinorUpdates} - * to be {@code false}
+ *Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} to be {@code false}
* * @since 2.5 */ @Parameter(property = "allowIncrementalUpdates", defaultValue = "true") private boolean allowIncrementalUpdates = true; - /** - *Ignored -- largely replaced by {@linkplain #allowMajorUpdates}, - * {@linkplain #allowMinorUpdates} and {@linkplain #allowIncrementalUpdates}, - * which are equal to {@code true} by default.
- * - *Please note: Prior to version 2.16.0, leaving this parameter at its default - * value ({@code true}) would mean that the plugin would ignore - * {@linkplain #allowMajorUpdates}, {@linkplain #allowMinorUpdates}, and {@linkplain #allowIncrementalUpdates}, - * which confused many users.
- * - * @since 2.5 - * @deprecated This will be removed with version 3.0.0 - */ - @Deprecated - @Parameter(property = "allowAnyUpdates", defaultValue = "true") - private boolean allowAnyUpdates = true; - /** * Whether to show additional information such as dependencies that do not need updating. Defaults to false. * @@ -484,11 +463,6 @@ protected void validateInput() throws MojoExecutionException { validateGAVList(pluginDependencyExcludes, 3, "pluginDependencyExcludes"); validateGAVList(pluginManagementDependencyIncludes, 3, "pluginManagementDependencyIncludes"); validateGAVList(pluginManagementDependencyExcludes, 3, "pluginManagementDependencyExcludes"); - if (getLog() != null - && allowAnyUpdates - && !(allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates)) { - getLog().warn("Assuming allowAnyUpdates false because one or more other \"allow\" switches is false."); - } } /** diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java index c8367d0f5..8ae18d77d 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojoTest.java @@ -145,7 +145,6 @@ public void testVersionsWithQualifiersNotConsideredAsMinorUpdates() throws Excep null) { { setProject(createProject()); - setVariableValueToObject(this, "allowAnyUpdates", false); setVariableValueToObject(this, "allowMajorUpdates", false); setVariableValueToObject(this, "processDependencies", true); setVariableValueToObject(this, "dependencyIncludes", singletonList(WildcardMatcher.WILDCARD)); @@ -181,7 +180,6 @@ public void testAllowMajorUpdatesFalse() throws Exception { null) { { setProject(createProject()); - setVariableValueToObject(this, "allowAnyUpdates", false); setVariableValueToObject(this, "allowMajorUpdates", false); setVariableValueToObject(this, "processDependencies", true); setVariableValueToObject(this, "dependencyIncludes", singletonList(WildcardMatcher.WILDCARD)); @@ -214,7 +212,6 @@ public void testAllowMinorUpdatesFalse() throws Exception { null) { { setProject(createProject()); - setVariableValueToObject(this, "allowAnyUpdates", false); setVariableValueToObject(this, "allowMinorUpdates", false); setVariableValueToObject(this, "processDependencies", true); setVariableValueToObject(this, "dependencyIncludes", singletonList(WildcardMatcher.WILDCARD)); @@ -248,7 +245,6 @@ public void testAllowIncrementalUpdatesFalse() throws Exception { null) { { setProject(createProject()); - setVariableValueToObject(this, "allowAnyUpdates", false); setVariableValueToObject(this, "allowIncrementalUpdates", false); setVariableValueToObject(this, "processDependencies", true); setVariableValueToObject(this, "dependencyIncludes", singletonList(WildcardMatcher.WILDCARD)); @@ -285,7 +281,6 @@ public void testVersionsWithQualifiersNotConsideredAsIncrementalUpdates() throws null) { { setProject(createProject()); - setVariableValueToObject(this, "allowAnyUpdates", false); setVariableValueToObject(this, "allowMinorUpdates", false); setVariableValueToObject(this, "processDependencies", true); setVariableValueToObject(this, "dependencyIncludes", singletonList(WildcardMatcher.WILDCARD)); @@ -396,69 +391,4 @@ public void testAllowSnapshots() throws Exception { assertThat(output, containsString("1.0.1-SNAPSHOT")); } } - - /** - * With {@code allowMajorUpdates}, {@code allowMinorUpdates}, or {@code allowIncrementalUpdates} all equal - * {@code true}, {@code allowAnyUpdates} should be respected. - */ - @Test - public void testAllowAnyUpdatesTrue() throws Exception { - try (CloseableTempFile tempFile = new CloseableTempFile("display-dependency-updates")) { - new DisplayDependencyUpdatesMojo( - mockRepositorySystem(), - mockAetherRepositorySystem(new HashMap