Skip to content

Commit

Permalink
Resolves mojohaus#921: Refactoring segments; work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed May 20, 2023
1 parent c2cbbfd commit 28e86b1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package org.codehaus.mojo.versions.api;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* Copyright MojoHaus and Contributors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.*;
Expand Down Expand Up @@ -77,13 +73,13 @@ public Restriction restrictionForUnchangedSegment(Optional<Segment> unchangedSeg
}

final ArtifactVersion currentVersion = highestLowerBound;
ArtifactVersion nextVersion = unchangedSegment
.filter(s -> s.isGreaterThan(SUBINCREMENTAL))
.map(Segment::ofLesserThan)
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
.orElse(currentVersion);
// ArtifactVersion nextVersion = unchangedSegment
// .filter(s -> s.isGreaterThan(SUBINCREMENTAL))
// .map(Segment::ofLesserThan)
// .map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
// .orElse(currentVersion);
return new Restriction(
nextVersion,
currentVersion,
false,
unchangedSegment
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
Expand Down Expand Up @@ -211,8 +207,9 @@ public final ArtifactVersion[] getNewerVersions(
throws InvalidSegmentException {
ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString);
ArtifactVersion lowerBound = allowDowngrade
? getLowerBound(currentVersion, unchangedSegment)
.map(DefaultArtifactVersionCache::of)
? unchangedSegment
.map(s -> s.isGreaterThan(SUBINCREMENTAL) ? Segment.ofLesserThan(s) : s)
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
.orElse(null)
: currentVersion;
ArtifactVersion upperBound = unchangedSegment
Expand Down Expand Up @@ -375,13 +372,13 @@ public boolean isVersionInRestriction(Restriction restriction, ArtifactVersion c
/**
* Returns the latest version newer than the specified current version, and within the specified update scope,
* or {@code null} if no such version exists.
* @param updateScope the scope of updates to include.
* @param unchangedSegment the segment which must remain unchanged.
* @param includeSnapshots whether snapshots should be included
* @return the newest version after currentVersion within the specified update scope,
* or <code>null</code> if no version is available.
*/
public final ArtifactVersion getReportNewestUpdate(Optional<Segment> updateScope, boolean includeSnapshots) {
return getArtifactVersionStream(updateScope, includeSnapshots)
public final ArtifactVersion getReportNewestUpdate(Optional<Segment> unchangedSegment, boolean includeSnapshots) {
return getArtifactVersionStream(unchangedSegment, includeSnapshots)
.min(Collections.reverseOrder(getVersionComparator()))
.orElse(null);
}
Expand Down Expand Up @@ -427,14 +424,14 @@ public final ArtifactVersion[] getReportUpdates(Optional<Segment> updateScope, b

/**
* Returns all versions newer than the specified current version, and within the specified update scope.
* @param updateScope the scope of updates to include.
* @param unchangedSegment segment that must remain unchanged
* @param includeSnapshots whether snapshots should be included
* @return all versions after currentVersion within the specified update scope.
*/
private Stream<ArtifactVersion> getArtifactVersionStream(Optional<Segment> updateScope, boolean includeSnapshots) {
private Stream<ArtifactVersion> getArtifactVersionStream(Optional<Segment> unchangedSegment, boolean includeSnapshots) {
if (isCurrentVersionDefined()) {
try {
Restriction restriction = restrictionForUnchangedSegment(updateScope);
Restriction restriction = restrictionForUnchangedSegment(unchangedSegment);

return Arrays.stream(getVersions(includeSnapshots))
.filter(candidate -> isVersionInRestriction(restriction, candidate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public ArtifactVersionsCache(TriFunction<AbstractVersionDetails, Optional<Segmen
* @param <V> concrete implementation of {@linkplain AbstractVersionDetails}
* @param <R> return type of the cached function
* @param artifactVersions {@linkplain ArtifactVersions} object referring to the given dependency
* @param updateScope update scope
* @param unchangedSegment update scope
* @param allowSnapshots whether snapshots should be included
* @return last retrieved update information
*/
@SuppressWarnings("unchecked")
public <V extends AbstractVersionDetails, R> R get(
V artifactVersions, Optional<Segment> updateScope, boolean allowSnapshots) {
V artifactVersions, Optional<Segment> unchangedSegment, boolean allowSnapshots) {
return (R) updateCache.computeIfAbsent(
Triple.of(artifactVersions, updateScope, allowSnapshots),
Triple.of(artifactVersions, unchangedSegment, allowSnapshots),
triple -> cachedFunction.apply(triple.getLeft(), triple.getMiddle(), triple.getRight()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import java.util.*;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.versioning.*;
Expand All @@ -28,8 +30,6 @@
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;

import java.util.*;

import static java.util.Optional.empty;

/**
Expand Down Expand Up @@ -333,8 +333,8 @@ public ArtifactVersion getNewestVersion(
}
}
}
if (fromReactor != null && (result != null || !String.valueOf(currentVersion)
.equals(fromReactor.toString()))) {
if (fromReactor != null
&& (result != null || !String.valueOf(currentVersion).equals(fromReactor.toString()))) {
if (property.isPreferReactor()) {
helper.getLog()
.debug("Property ${" + property.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
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.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -202,7 +200,7 @@ public void testGetNewerVersionsWithSnapshot() throws InvalidSegmentException {
public void testAllVersionsForIgnoreScopeSubIncremental() {
ArtifactVersion[] versions = versions("1.0.0", "1.0.0-1", "1.0.1");
ArtifactVersions instance = new ArtifactVersions(
new DefaultArtifact("default-group", "dummy-api", "1.0.0", "foo", "bar", "jar", null),
createArtifact(),
Arrays.asList(versions),
new MavenVersionComparator());
Restriction restriction = instance.restrictionForIgnoreScope(of(SUBINCREMENTAL));
Expand All @@ -211,24 +209,28 @@ public void testAllVersionsForIgnoreScopeSubIncremental() {
assertThat(filteredVersions, arrayContaining(DefaultArtifactVersionCache.of("1.0.1")));
}

private static DefaultArtifact createArtifact() {
return new DefaultArtifact("default-group", "dummy-api", "1.0.0", "foo", "bar", "jar", null);
}

@Test
public void testAllVersionsForIgnoreScopeIncremental() {
ArtifactVersion[] versions = versions("1.0.0", "1.0.0-1", "1.0.1", "1.1.0");
ArtifactVersions instance = new ArtifactVersions(
new DefaultArtifact("default-group", "dummy-api", "1.0.0", "foo", "bar", "jar", null),
createArtifact(),
Arrays.asList(versions),
new MavenVersionComparator());
Restriction restriction = instance.restrictionForIgnoreScope(of(INCREMENTAL));
ArtifactVersion[] filteredVersions = instance.getVersions(restriction, false);
assertThat(filteredVersions, arrayWithSize(1));
assertThat(filteredVersions, arrayContaining(DefaultArtifactVersionCache.of("1.1.0")));
assertThat(filteredVersions, arrayContaining(versions("1.1.0")));
assertThat(filteredVersions, not(arrayContaining(versions("1.0.0-1", "1.0.1"))));
}

@Test
public void testAllVersionsForIgnoreScopeMinor() {
ArtifactVersion[] versions = versions("1.0.0", "1.0.0-1", "1.0.1", "1.1.0", "2.0.0");
ArtifactVersions instance = new ArtifactVersions(
new DefaultArtifact("default-group", "dummy-api", "1.0.0", "foo", "bar", "jar", null),
createArtifact(),
Arrays.asList(versions),
new MavenVersionComparator());
Restriction restriction = instance.restrictionForIgnoreScope(of(MINOR));
Expand All @@ -241,11 +243,23 @@ public void testAllVersionsForIgnoreScopeMinor() {
public void testAllVersionsForIgnoreScopeMajor() {
ArtifactVersion[] versions = versions("1.0.0", "1.0.0-1", "1.0.1", "1.1.0", "2.0.0");
ArtifactVersions instance = new ArtifactVersions(
new DefaultArtifact("default-group", "dummy-api", "1.0.0", "foo", "bar", "jar", null),
createArtifact(),
Arrays.asList(versions),
new MavenVersionComparator());
Restriction restriction = instance.restrictionForIgnoreScope(of(MAJOR));
ArtifactVersion[] filteredVersions = instance.getVersions(restriction, false);
assertThat(filteredVersions, arrayWithSize(0));
}

@Test
public void testRestrictionForUnchangedSegmentMajor() throws InvalidSegmentException {
ArtifactVersion[] versions = versions("1.0.0", "1.0.0-1", "1.0.1", "1.1.0", "2.0.0");
ArtifactVersions instance = new ArtifactVersions(createArtifact(), Arrays.asList(versions),
new MavenVersionComparator());
Restriction restriction = instance.restrictionForUnchangedSegment(of(MAJOR));
ArtifactVersion[] filteredVersions = instance.getVersions(restriction, false);
assertThat(filteredVersions, arrayContaining(versions("1.0.0-1", "1.0.1", "1.1.0")));
assertThat(filteredVersions, not(arrayContaining(DefaultArtifactVersionCache.of("2.0.0"))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.repository.RepositorySystem;
Expand All @@ -48,8 +47,6 @@
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.SegmentUtils;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.apache.maven.shared.utils.StringUtils.isBlank;
import static org.codehaus.mojo.versions.api.Segment.*;

Expand Down Expand Up @@ -216,8 +213,8 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion)
}

final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false);
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(allowMajorUpdates,
allowMinorUpdates, allowIncrementalUpdates, getLog());
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());

// currentVersion (set to parentVersion here) is not included in the version range for searching upgrades
// unless we set allowDowngrade to true
Expand Down

0 comments on commit 28e86b1

Please sign in to comment.