Merge branch '2.7.x' into 3.0.x

Closes gh-37200
pull/37462/head
Andy Wilkinson 1 year ago
commit 7486d9da72

@ -28,24 +28,19 @@ import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
public enum UpgradePolicy implements BiPredicate<DependencyVersion, DependencyVersion> {
/**
* All versions more recent than the current version will be suggested as possible
* upgrades.
* Any version.
*/
ANY((candidate, current) -> current.compareTo(candidate) < 0),
ANY((candidate, current) -> true),
/**
* New minor versions of the current major version will be suggested as possible
* upgrades. For example, if the current version is 1.2.3, all 1.x.y versions after
* 1.2.3 will be suggested. 2.x versions will not be offered.
* Minor versions of the current major version.
*/
SAME_MAJOR_VERSION(DependencyVersion::isSameMajorAndNewerThan),
SAME_MAJOR_VERSION((candidate, current) -> candidate.isSameMajor(current)),
/**
* New patch versions of the current minor version will be offered as possible
* upgrades. For example, if the current version is 1.2.3, all 1.2.x versions after
* 1.2.3 will be suggested. 1.x versions will not be offered.
* Patch versions of the current minor version.
*/
SAME_MINOR_VERSION(DependencyVersion::isSameMinorAndNewerThan);
SAME_MINOR_VERSION((candidate, current) -> candidate.isSameMinor(current));
private final BiPredicate<DependencyVersion, DependencyVersion> delegate;

@ -36,7 +36,7 @@ public abstract class MoveToSnapshots extends UpgradeDependencies {
@Inject
public MoveToSnapshots(BomExtension bom) {
super(bom);
super(bom, true);
getRepositoryUris().add(this.REPOSITORY_URI);
}

@ -50,9 +50,13 @@ class StandardLibraryUpdateResolver implements LibraryUpdateResolver {
private final UpgradePolicy upgradePolicy;
StandardLibraryUpdateResolver(VersionResolver versionResolver, UpgradePolicy upgradePolicy) {
private final boolean movingToSnapshots;
StandardLibraryUpdateResolver(VersionResolver versionResolver, UpgradePolicy upgradePolicy,
boolean movingToSnapshots) {
this.versionResolver = versionResolver;
this.upgradePolicy = upgradePolicy;
this.movingToSnapshots = movingToSnapshots;
}
@Override
@ -154,6 +158,7 @@ class StandardLibraryUpdateResolver implements LibraryUpdateResolver {
DependencyVersion currentVersion) {
SortedSet<DependencyVersion> versions = this.versionResolver.resolveVersions(groupId, artifactId);
versions.removeIf((candidate) -> !this.upgradePolicy.test(candidate, currentVersion));
versions.removeIf((candidate) -> !currentVersion.isUpgrade(candidate, this.movingToSnapshots));
return versions;
}

@ -61,9 +61,16 @@ public abstract class UpgradeDependencies extends DefaultTask {
private final BomExtension bom;
private final boolean movingToSnapshots;
@Inject
public UpgradeDependencies(BomExtension bom) {
this(bom, false);
}
protected UpgradeDependencies(BomExtension bom, boolean movingToSnapshots) {
this.bom = bom;
this.movingToSnapshots = movingToSnapshots;
getThreads().convention(2);
}
@ -210,7 +217,7 @@ public abstract class UpgradeDependencies extends DefaultTask {
List<Upgrade> upgrades = new InteractiveUpgradeResolver(getServices().get(UserInputHandler.class),
new MultithreadedLibraryUpdateResolver(getThreads().get(),
new StandardLibraryUpdateResolver(new MavenMetadataVersionResolver(getRepositoryUris().get()),
this.bom.getUpgrade().getPolicy())))
this.bom.getUpgrade().getPolicy(), this.movingToSnapshots)))
.resolveUpgrades(matchingLibraries(), this.bom.getLibraries());
return upgrades;
}

@ -38,6 +38,14 @@ abstract class AbstractDependencyVersion implements DependencyVersion {
return this.comparableVersion.compareTo(otherComparable);
}
@Override
public boolean isUpgrade(DependencyVersion candidate, boolean movingToSnapshots) {
ComparableVersion comparableCandidate = (candidate instanceof AbstractDependencyVersion)
? ((AbstractDependencyVersion) candidate).comparableVersion
: new ComparableVersion(candidate.toString());
return comparableCandidate.compareTo(this.comparableVersion) > 0;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {

@ -22,6 +22,8 @@ import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.springframework.util.StringUtils;
/**
* A {@link DependencyVersion} backed by an {@link ArtifactVersion}.
*
@ -32,47 +34,75 @@ class ArtifactVersionDependencyVersion extends AbstractDependencyVersion {
private final ArtifactVersion artifactVersion;
protected ArtifactVersionDependencyVersion(ArtifactVersion artifactVersion) {
super(new ComparableVersion(artifactVersion.toString()));
super(new ComparableVersion(toNormalizedString(artifactVersion)));
this.artifactVersion = artifactVersion;
}
private static String toNormalizedString(ArtifactVersion artifactVersion) {
String versionString = artifactVersion.toString();
if (versionString.endsWith(".RELEASE")) {
return versionString.substring(0, versionString.length() - 8);
}
if (versionString.endsWith(".BUILD-SNAPSHOT")) {
return versionString.substring(0, versionString.length() - 15) + "-SNAPSHOT";
}
return versionString;
}
protected ArtifactVersionDependencyVersion(ArtifactVersion artifactVersion, ComparableVersion comparableVersion) {
super(comparableVersion);
this.artifactVersion = artifactVersion;
}
@Override
public boolean isNewerThan(DependencyVersion other) {
public boolean isSameMajor(DependencyVersion other) {
if (other instanceof ReleaseTrainDependencyVersion) {
return false;
}
return compareTo(other) > 0;
return extractArtifactVersionDependencyVersion(other).map(this::isSameMajor).orElse(true);
}
private boolean isSameMajor(ArtifactVersionDependencyVersion other) {
return this.artifactVersion.getMajorVersion() == other.artifactVersion.getMajorVersion();
}
@Override
public boolean isSameMajorAndNewerThan(DependencyVersion other) {
public boolean isSameMinor(DependencyVersion other) {
if (other instanceof ReleaseTrainDependencyVersion) {
return false;
}
return extractArtifactVersionDependencyVersion(other).map(this::isSameMajorAndNewerThan).orElse(true);
return extractArtifactVersionDependencyVersion(other).map(this::isSameMinor).orElse(true);
}
private boolean isSameMajorAndNewerThan(ArtifactVersionDependencyVersion other) {
return this.artifactVersion.getMajorVersion() == other.artifactVersion.getMajorVersion() && isNewerThan(other);
private boolean isSameMinor(ArtifactVersionDependencyVersion other) {
return isSameMajor(other) && this.artifactVersion.getMinorVersion() == other.artifactVersion.getMinorVersion();
}
@Override
public boolean isSameMinorAndNewerThan(DependencyVersion other) {
if (other instanceof ReleaseTrainDependencyVersion) {
public boolean isUpgrade(DependencyVersion candidate, boolean movingToSnapshots) {
if (!(candidate instanceof ArtifactVersionDependencyVersion)) {
return false;
}
return extractArtifactVersionDependencyVersion(other).map(this::isSameMinorAndNewerThan).orElse(true);
}
private boolean isSameMinorAndNewerThan(ArtifactVersionDependencyVersion other) {
return this.artifactVersion.getMajorVersion() == other.artifactVersion.getMajorVersion()
&& this.artifactVersion.getMinorVersion() == other.artifactVersion.getMinorVersion()
&& isNewerThan(other);
ArtifactVersion other = ((ArtifactVersionDependencyVersion) candidate).artifactVersion;
if (this.artifactVersion.equals(other)) {
return false;
}
if (this.artifactVersion.getMajorVersion() == other.getMajorVersion()
&& this.artifactVersion.getMinorVersion() == other.getMinorVersion()
&& this.artifactVersion.getIncrementalVersion() == other.getIncrementalVersion()) {
if (!StringUtils.hasLength(this.artifactVersion.getQualifier())
|| "RELEASE".equals(this.artifactVersion.getQualifier())) {
return false;
}
if ("SNAPSHOT".equals(this.artifactVersion.getQualifier())
|| "BUILD".equals(this.artifactVersion.getQualifier())) {
return true;
}
else if ("SNAPSHOT".equals(other.getQualifier()) || "BUILD".equals(other.getQualifier())) {
return movingToSnapshots;
}
}
return super.isUpgrade(candidate, movingToSnapshots);
}
@Override

@ -41,14 +41,6 @@ class CalendarVersionDependencyVersion extends ArtifactVersionDependencyVersion
super(artifactVersion, comparableVersion);
}
@Override
public boolean isNewerThan(DependencyVersion other) {
if (other instanceof ReleaseTrainDependencyVersion) {
return true;
}
return super.isNewerThan(other);
}
static CalendarVersionDependencyVersion parse(String version) {
if (!CALENDAR_VERSION_PATTERN.matcher(version).matches()) {
return null;

@ -28,29 +28,30 @@ import java.util.function.Function;
public interface DependencyVersion extends Comparable<DependencyVersion> {
/**
* Returns whether this version is newer than the given {@code other} version.
* @param other version to test
* @return {@code true} if this version is newer, otherwise {@code false}
* Returns whether this version has the same major and minor versions as the
* {@code other} version.
* @param other the version to test
* @return {@code true} if this version has the same major and minor, otherwise
* {@code false}
*/
boolean isNewerThan(DependencyVersion other);
boolean isSameMinor(DependencyVersion other);
/**
* Returns whether this version has the same major versions as the {@code other}
* version while also being newer.
* @param other version to test
* @return {@code true} if this version has the same major and is newer, otherwise
* {@code false}
* Returns whether this version has the same major version as the {@code other}
* version.
* @param other the version to test
* @return {@code true} if this version has the same major, otherwise {@code false}
*/
boolean isSameMajorAndNewerThan(DependencyVersion other);
boolean isSameMajor(DependencyVersion other);
/**
* Returns whether this version has the same major and minor versions as the
* {@code other} version while also being newer.
* @param other version to test
* @return {@code true} if this version has the same major and minor and is newer,
* otherwise {@code false}
* Returns whether the given {@code candidate} is an upgrade of this version.
* @param candidate the version the consider
* @param movingToSnapshots whether the upgrade is to be considered as part of moving
* to snaphots
* @return {@code true} if the candidate is an upgrade, otherwise false
*/
boolean isSameMinorAndNewerThan(DependencyVersion other);
boolean isUpgrade(DependencyVersion candidate, boolean movingToSnapshots);
static DependencyVersion parse(String version) {
List<Function<String, DependencyVersion>> parsers = Arrays.asList(CalendarVersionDependencyVersion::parse,

@ -28,7 +28,8 @@ import org.springframework.util.StringUtils;
*/
final class ReleaseTrainDependencyVersion implements DependencyVersion {
private static final Pattern VERSION_PATTERN = Pattern.compile("([A-Z][a-z]+)-([A-Z]+)([0-9]*)");
private static final Pattern VERSION_PATTERN = Pattern
.compile("([A-Z][a-z]+)-((BUILD-SNAPSHOT)|([A-Z-]+)([0-9]*))");
private final String releaseTrain;
@ -62,30 +63,44 @@ final class ReleaseTrainDependencyVersion implements DependencyVersion {
}
@Override
public boolean isNewerThan(DependencyVersion other) {
if (other instanceof CalendarVersionDependencyVersion) {
return false;
public boolean isUpgrade(DependencyVersion candidate, boolean movingToSnapshots) {
if (!(candidate instanceof ReleaseTrainDependencyVersion)) {
return true;
}
if (!(other instanceof ReleaseTrainDependencyVersion otherReleaseTrain)) {
ReleaseTrainDependencyVersion candidateReleaseTrain = (ReleaseTrainDependencyVersion) candidate;
int comparison = this.releaseTrain.compareTo(candidateReleaseTrain.releaseTrain);
if (comparison != 0) {
return comparison < 0;
}
if (movingToSnapshots && !"BUILD-SNAPSHOT".equals(this.type)
&& "BUILD-SNAPSHOT".equals(candidateReleaseTrain.type)) {
return true;
}
return otherReleaseTrain.compareTo(this) < 0;
comparison = this.type.compareTo(candidateReleaseTrain.type);
if (comparison != 0) {
return comparison < 0;
}
return Integer.compare(this.version, candidateReleaseTrain.version) < 0;
}
@Override
public boolean isSameMajorAndNewerThan(DependencyVersion other) {
return isNewerThan(other);
public boolean isSameMajor(DependencyVersion other) {
return isSameReleaseTrain(other);
}
@Override
public boolean isSameMinorAndNewerThan(DependencyVersion other) {
public boolean isSameMinor(DependencyVersion other) {
return isSameReleaseTrain(other);
}
private boolean isSameReleaseTrain(DependencyVersion other) {
if (other instanceof CalendarVersionDependencyVersion) {
return false;
}
if (!(other instanceof ReleaseTrainDependencyVersion otherReleaseTrain)) {
return true;
if (other instanceof ReleaseTrainDependencyVersion otherReleaseTrain) {
return otherReleaseTrain.releaseTrain.equals(this.releaseTrain);
}
return otherReleaseTrain.releaseTrain.equals(this.releaseTrain) && isNewerThan(other);
return true;
}
@Override
@ -121,8 +136,9 @@ final class ReleaseTrainDependencyVersion implements DependencyVersion {
if (!matcher.matches()) {
return null;
}
return new ReleaseTrainDependencyVersion(matcher.group(1), matcher.group(2),
(StringUtils.hasLength(matcher.group(3))) ? Integer.parseInt(matcher.group(3)) : 0, input);
return new ReleaseTrainDependencyVersion(matcher.group(1),
StringUtils.hasLength(matcher.group(3)) ? matcher.group(3) : matcher.group(4),
(StringUtils.hasLength(matcher.group(5))) ? Integer.parseInt(matcher.group(5)) : 0, input);
}
}

@ -34,18 +34,13 @@ final class UnstructuredDependencyVersion extends AbstractDependencyVersion impl
}
@Override
public boolean isNewerThan(DependencyVersion other) {
return compareTo(other) > 0;
public boolean isSameMajor(DependencyVersion other) {
return true;
}
@Override
public boolean isSameMajorAndNewerThan(DependencyVersion other) {
return compareTo(other) > 0;
}
@Override
public boolean isSameMinorAndNewerThan(DependencyVersion other) {
return compareTo(other) > 0;
public boolean isSameMinor(DependencyVersion other) {
return true;
}
@Override

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,78 +38,28 @@ class ArtifactVersionDependencyVersionTests {
}
@Test
void isNewerThanWhenInputIsOlderMajorShouldReturnTrue() {
assertThat(version("2.1.2").isNewerThan(version("1.9.0"))).isTrue();
void isSameMajorWhenSameMajorAndMinorShouldReturnTrue() {
assertThat(version("1.10.2").isSameMajor(version("1.10.0"))).isTrue();
}
@Test
void isNewerThanWhenInputIsOlderMinorShouldReturnTrue() {
assertThat(version("2.1.2").isNewerThan(version("2.0.2"))).isTrue();
void isSameMajorWhenSameMajorShouldReturnTrue() {
assertThat(version("1.10.2").isSameMajor(version("1.9.0"))).isTrue();
}
@Test
void isNewerThanWhenInputIsOlderPatchShouldReturnTrue() {
assertThat(version("2.1.2").isNewerThan(version("2.1.1"))).isTrue();
void isSameMajorWhenDifferentMajorShouldReturnFalse() {
assertThat(version("2.0.2").isSameMajor(version("1.9.0"))).isFalse();
}
@Test
void isNewerThanWhenInputIsNewerMajorShouldReturnFalse() {
assertThat(version("2.1.2").isNewerThan(version("3.2.1"))).isFalse();
void isSameMinorWhenSameMinorShouldReturnTrue() {
assertThat(version("1.10.2").isSameMinor(version("1.10.1"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWhenMinorIsOlderShouldReturnTrue() {
assertThat(version("1.10.2").isSameMajorAndNewerThan(version("1.9.0"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWhenMajorIsOlderShouldReturnFalse() {
assertThat(version("2.0.2").isSameMajorAndNewerThan(version("1.9.0"))).isFalse();
}
@Test
void isSameMajorAndNewerThanWhenPatchIsNewerShouldReturnTrue() {
assertThat(version("2.1.2").isSameMajorAndNewerThan(version("2.1.1"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWhenMinorIsNewerShouldReturnFalse() {
assertThat(version("2.1.2").isSameMajorAndNewerThan(version("2.2.1"))).isFalse();
}
@Test
void isSameMajorAndNewerThanWhenMajorIsNewerShouldReturnFalse() {
assertThat(version("2.1.2").isSameMajorAndNewerThan(version("3.0.1"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenPatchIsOlderShouldReturnTrue() {
assertThat(version("1.10.2").isSameMinorAndNewerThan(version("1.10.1"))).isTrue();
}
@Test
void isSameMinorAndNewerThanWhenMinorIsOlderShouldReturnFalse() {
assertThat(version("2.1.2").isSameMinorAndNewerThan(version("2.0.1"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenVersionsAreTheSameShouldReturnFalse() {
assertThat(version("2.1.2").isSameMinorAndNewerThan(version("2.1.2"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenPatchIsNewerShouldReturnFalse() {
assertThat(version("2.1.2").isSameMinorAndNewerThan(version("2.1.3"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenMinorIsNewerShouldReturnFalse() {
assertThat(version("2.1.2").isSameMinorAndNewerThan(version("2.0.1"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenMajorIsNewerShouldReturnFalse() {
assertThat(version("3.1.2").isSameMinorAndNewerThan(version("2.0.1"))).isFalse();
void isSameMinorWhenDifferentMinorShouldReturnFalse() {
assertThat(version("1.10.2").isSameMinor(version("1.9.1"))).isFalse();
}
private ArtifactVersionDependencyVersion version(String version) {

@ -38,93 +38,38 @@ class CalendarVersionDependencyVersionTests {
}
@Test
void isNewerThanWhenInputIsEarlierYearShouldReturnTrue() {
assertThat(version("2020.1.2").isNewerThan(version("2019.9.0"))).isTrue();
void isSameMajorWhenSameMajorAndMinorShouldReturnTrue() {
assertThat(version("2020.0.0").isSameMajor(version("2020.0.1"))).isTrue();
}
@Test
void isNewerThanWhenInputIsOlderMinorShouldReturnTrue() {
assertThat(version("2020.1.2").isNewerThan(version("2020.0.2"))).isTrue();
void isSameMajorWhenSameMajorShouldReturnTrue() {
assertThat(version("2020.0.0").isSameMajor(version("2020.1.0"))).isTrue();
}
@Test
void isNewerThanWhenInputIsOlderMicroShouldReturnTrue() {
assertThat(version("2020.1.2").isNewerThan(version("2020.1.1"))).isTrue();
void isSameMajorWhenDifferentMajorShouldReturnFalse() {
assertThat(version("2020.0.0").isSameMajor(version("2021.0.0"))).isFalse();
}
@Test
void isNewerThanWhenInputIsLaterYearShouldReturnFalse() {
assertThat(version("2020.1.2").isNewerThan(version("2021.2.1"))).isFalse();
void isSameMinorWhenSameMinorShouldReturnTrue() {
assertThat(version("2020.0.0").isSameMinor(version("2020.0.1"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWhenMinorIsOlderShouldReturnTrue() {
assertThat(version("2020.10.2").isSameMajorAndNewerThan(version("2020.9.0"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWhenMajorIsOlderShouldReturnFalse() {
assertThat(version("2020.0.2").isSameMajorAndNewerThan(version("2019.9.0"))).isFalse();
}
@Test
void isSameMajorAndNewerThanWhenMicroIsNewerShouldReturnTrue() {
assertThat(version("2020.1.2").isSameMajorAndNewerThan(version("2020.1.1"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWhenMinorIsNewerShouldReturnFalse() {
assertThat(version("2020.1.2").isSameMajorAndNewerThan(version("2020.2.1"))).isFalse();
}
@Test
void isSameMajorAndNewerThanWhenMajorIsNewerShouldReturnFalse() {
assertThat(version("2019.1.2").isSameMajorAndNewerThan(version("2020.0.1"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenMicroIsOlderShouldReturnTrue() {
assertThat(version("2020.10.2").isSameMinorAndNewerThan(version("2020.10.1"))).isTrue();
}
@Test
void isSameMinorAndNewerThanWhenMinorIsOlderShouldReturnFalse() {
assertThat(version("2020.1.2").isSameMinorAndNewerThan(version("2020.0.1"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenVersionsAreTheSameShouldReturnFalse() {
assertThat(version("2020.1.2").isSameMinorAndNewerThan(version("2020.1.2"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenMicroIsNewerShouldReturnFalse() {
assertThat(version("2020.1.2").isSameMinorAndNewerThan(version("2020.1.3"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenMinorIsNewerShouldReturnFalse() {
assertThat(version("2020.1.2").isSameMinorAndNewerThan(version("2020.0.1"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenMajorIsNewerShouldReturnFalse() {
assertThat(version("2020.1.2").isSameMinorAndNewerThan(version("2019.0.1"))).isFalse();
}
@Test
void calendarVersionIsNewerThanReleaseTrainVersion() {
assertThat(version("2020.0.0").isNewerThan(releaseTrainVersion("Aluminium-RELEASE"))).isTrue();
void isSameMinorWhenDifferentMinorShouldReturnFalse() {
assertThat(version("2020.0.0").isSameMinor(version("2020.1.0"))).isFalse();
}
@Test
void calendarVersionIsNotSameMajorAsReleaseTrainVersion() {
assertThat(version("2020.0.0").isSameMajorAndNewerThan(releaseTrainVersion("Aluminium-RELEASE"))).isFalse();
assertThat(version("2020.0.0").isSameMajor(releaseTrainVersion("Aluminium-RELEASE"))).isFalse();
}
@Test
void calendarVersionIsNotSameMinorAsReleaseTrainVersion() {
assertThat(version("2020.0.0").isSameMinorAndNewerThan(releaseTrainVersion("Aluminium-RELEASE"))).isFalse();
assertThat(version("2020.0.0").isSameMinor(releaseTrainVersion("Aluminium-RELEASE"))).isFalse();
}
private ReleaseTrainDependencyVersion releaseTrainVersion(String version) {

@ -68,9 +68,4 @@ class DependencyVersionTests {
assertThat(DependencyVersion.parse("2020.0.0-M1")).isInstanceOf(CalendarVersionDependencyVersion.class);
}
@Test
void calendarVersionShouldBeNewerThanAReleaseCalendarVersion() {
}
}

@ -0,0 +1,304 @@
/*
* Copyright 2023 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.boot.build.bom.bomr.version;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DependencyVersion#isUpgrade} of {@link DependencyVersion}
* implementations.
*
* @author Andy Wilkinson
*/
public class DependencyVersionUpgradeTests {
@ParameterizedTest
@ArtifactVersion(current = "1.2.3", candidate = "1.2.3")
@ArtifactVersion(current = "1.2.3.RELEASE", candidate = "1.2.3.RELEASE")
@CalendarVersion(current = "2023.0.0", candidate = "2023.0.0")
@ReleaseTrain(current = "Kay-RELEASE", candidate = "Kay-RELEASE")
void isUpgradeWhenSameVersionShouldReturnFalse(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isFalse();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-SNAPSHOT", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.BUILD-SNAPSHOT", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0-SNAPSHOT", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-BUILD-SNAPSHOT", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenSameSnapshotVersionShouldReturnFalse(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isFalse();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-SNAPSHOT", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.BUILD-SNAPSHOT", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0-SNAPSHOT", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-BUILD-SNAPSHOT", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenSameSnapshotVersionAndMovingToSnapshotsShouldReturnFalse(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, true)).isFalse();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3", candidate = "1.2.4")
@ArtifactVersion(current = "1.2.3.RELEASE", candidate = "1.2.4.RELEASE")
@CalendarVersion(current = "2023.0.0", candidate = "2023.0.1")
@ReleaseTrain(current = "Kay-RELEASE", candidate = "Kay-SR1")
void isUpgradeWhenLaterPatchReleaseShouldReturnTrue(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3", candidate = "1.2.4-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.RELEASE", candidate = "1.2.4.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0", candidate = "2023.0.1-SNAPSHOT")
void isUpgradeWhenSnapshotOfLaterPatchReleaseShouldReturnTrue(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3", candidate = "1.2.4-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.RELEASE", candidate = "1.2.4.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0", candidate = "2023.0.1-SNAPSHOT")
@ReleaseTrain(current = "Kay-RELEASE", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenSnapshotOfLaterPatchReleaseAndMovingToSnapshotsShouldReturnTrue(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, true)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.RELEASE", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-RELEASE", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenSnapshotOfSameVersionShouldReturnFalse(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isFalse();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-SNAPSHOT", candidate = "1.2.3-M2")
@ArtifactVersion(current = "1.2.3.BUILD-SNAPSHOT", candidate = "1.2.3.M2")
@CalendarVersion(current = "2023.0.0-SNAPSHOT", candidate = "2023.0.0-M2")
@ReleaseTrain(current = "Kay-BUILD-SNAPSHOT", candidate = "Kay-M2")
void isUpgradeWhenSnapshotToMilestoneShouldReturnTrue(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-SNAPSHOT", candidate = "1.2.3-RC1")
@ArtifactVersion(current = "1.2.3.BUILD-SNAPSHOT", candidate = "1.2.3.RC1")
@CalendarVersion(current = "2023.0.0-SNAPSHOT", candidate = "2023.0.0-RC1")
@ReleaseTrain(current = "Kay-BUILD-SNAPSHOT", candidate = "Kay-RC1")
void isUpgradeWhenSnapshotToReleaseCandidateShouldReturnTrue(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-SNAPSHOT", candidate = "1.2.3")
@ArtifactVersion(current = "1.2.3.BUILD-SNAPSHOT", candidate = "1.2.3.RELEASE")
@CalendarVersion(current = "2023.0.0-SNAPSHOT", candidate = "2023.0.0")
@ReleaseTrain(current = "Kay-BUILD-SNAPSHOT", candidate = "Kay-RELEASE")
void isUpgradeWhenSnapshotToReleaseShouldReturnTrue(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-M1", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.M1", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0-M1", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-M1", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenMilestoneToSnapshotShouldReturnFalse(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isFalse();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-RC1", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.RC1", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0-RC1", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-RC1", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenReleaseCandidateToSnapshotShouldReturnFalse(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isFalse();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.RELEASE", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-RELEASE", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenReleaseToSnapshotShouldReturnFalse(DependencyVersion current, DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, false)).isFalse();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-M1", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.M1", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0-M1", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-M1", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenMilestoneToSnapshotAndMovingToSnapshotsShouldReturnTrue(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, true)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3-RC1", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.RC1", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0-RC1", candidate = "2023.0.0-SNAPSHOT")
@ReleaseTrain(current = "Kay-RC1", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenReleaseCandidateToSnapshotAndMovingToSnapshotsShouldReturnTrue(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, true)).isTrue();
}
@ParameterizedTest
@ArtifactVersion(current = "1.2.3", candidate = "1.2.3-SNAPSHOT")
@ArtifactVersion(current = "1.2.3.RELEASE", candidate = "1.2.3.BUILD-SNAPSHOT")
@CalendarVersion(current = "2023.0.0", candidate = "2023.0.0-SNAPSHOT")
void isUpgradeWhenReleaseToSnapshotAndMovingToSnapshotsShouldReturnFalse(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, true)).isFalse();
}
@ParameterizedTest
@ReleaseTrain(current = "Kay-RELEASE", candidate = "Kay-BUILD-SNAPSHOT")
void isUpgradeWhenReleaseTrainToSnapshotAndMovingToSnapshotsShouldReturnTrue(DependencyVersion current,
DependencyVersion candidate) {
assertThat(current.isUpgrade(candidate, true)).isTrue();
}
@Repeatable(ArtifactVersions.class)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(InputProvider.class)
@interface ArtifactVersion {
String current();
String candidate();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface ArtifactVersions {
ArtifactVersion[] value();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(InputProvider.class)
@interface ReleaseTrain {
String current();
String candidate();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(InputProvider.class)
@interface CalendarVersion {
String current();
String candidate();
}
static class InputProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
Method testMethod = context.getRequiredTestMethod();
Stream<Arguments> artifactVersions = artifactVersions(testMethod)
.map((artifactVersion) -> Arguments.of(VersionType.ARTIFACT_VERSION.parse(artifactVersion.current()),
VersionType.ARTIFACT_VERSION.parse(artifactVersion.candidate())));
Stream<Arguments> releaseTrains = releaseTrains(testMethod)
.map((releaseTrain) -> Arguments.of(VersionType.RELEASE_TRAIN.parse(releaseTrain.current()),
VersionType.RELEASE_TRAIN.parse(releaseTrain.candidate())));
Stream<Arguments> calendarVersions = calendarVersions(testMethod)
.map((calendarVersion) -> Arguments.of(VersionType.CALENDAR_VERSION.parse(calendarVersion.current()),
VersionType.CALENDAR_VERSION.parse(calendarVersion.candidate())));
return Stream.concat(Stream.concat(artifactVersions, releaseTrains), calendarVersions);
}
private Stream<ArtifactVersion> artifactVersions(Method testMethod) {
ArtifactVersions artifactVersions = testMethod.getAnnotation(ArtifactVersions.class);
if (artifactVersions != null) {
return Stream.of(artifactVersions.value());
}
return versions(testMethod, ArtifactVersion.class);
}
private Stream<ReleaseTrain> releaseTrains(Method testMethod) {
return versions(testMethod, ReleaseTrain.class);
}
private Stream<CalendarVersion> calendarVersions(Method testMethod) {
return versions(testMethod, CalendarVersion.class);
}
private <T extends Annotation> Stream<T> versions(Method testMethod, Class<T> type) {
T annotation = testMethod.getAnnotation(type);
return (annotation != null) ? Stream.of(annotation) : Stream.empty();
}
}
enum VersionType {
ARTIFACT_VERSION(ArtifactVersionDependencyVersion::parse),
CALENDAR_VERSION(CalendarVersionDependencyVersion::parse),
RELEASE_TRAIN(ReleaseTrainDependencyVersion::parse);
private final Function<String, DependencyVersion> parser;
VersionType(Function<String, DependencyVersion> parser) {
this.parser = parser;
}
DependencyVersion parse(String version) {
return this.parser.apply(version);
}
}
}

@ -29,43 +29,23 @@ import static org.assertj.core.api.Assertions.assertThat;
class MultipleComponentsDependencyVersionTests {
@Test
void isNewerThanOnVersionWithNumericQualifierWhenInputHasNoQualifierShouldReturnTrue() {
assertThat(version("2.9.9.20190806").isNewerThan(DependencyVersion.parse("2.9.9"))).isTrue();
void isSameMajorOfFiveComponentVersionWithSameMajorShouldReturnTrue() {
assertThat(version("21.4.0.0.1").isSameMajor(version("21.1.0.0"))).isTrue();
}
@Test
void isNewerThanOnVersionWithNumericQualifierWhenInputHasOlderQualifierShouldReturnTrue() {
assertThat(version("2.9.9.20190806").isNewerThan(version("2.9.9.20190805"))).isTrue();
void isSameMajorOfFiveComponentVersionWithDifferentMajorShouldReturnFalse() {
assertThat(version("21.4.0.0.1").isSameMajor(version("22.1.0.0"))).isFalse();
}
@Test
void isNewerThanOnVersionWithNumericQualifierWhenInputHasNewerQualifierShouldReturnFalse() {
assertThat(version("2.9.9.20190806").isNewerThan(version("2.9.9.20190807"))).isFalse();
void isSameMinorOfFiveComponentVersionWithSameMinorShouldReturnTrue() {
assertThat(version("21.4.0.0.1").isSameMinor(version("21.4.0.0"))).isTrue();
}
@Test
void isNewerThanOnVersionWithNumericQualifierWhenInputHasSameQualifierShouldReturnFalse() {
assertThat(version("2.9.9.20190806").isNewerThan(version("2.9.9.20190806"))).isFalse();
}
@Test
void isNewerThanWorksWith5Components() {
assertThat(version("21.4.0.0.1").isNewerThan(version("21.1.0.0"))).isTrue();
}
@Test
void isNewerThanWorksWith5ComponentsAndLastComponentIsConsidered() {
assertThat(version("21.1.0.0.1").isNewerThan(version("21.1.0.0"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWorksWith5Components() {
assertThat(version("21.4.0.0.1").isSameMajorAndNewerThan(version("21.1.0.0"))).isTrue();
}
@Test
void isSameMinorAndNewerThanWorksWith5Components() {
assertThat(version("21.4.0.0.1").isSameMinorAndNewerThan(version("21.1.0.0"))).isFalse();
void isSameMinorOfFiveComponentVersionWithDifferentMinorShouldReturnFalse() {
assertThat(version("21.4.0.0.1").isSameMinor(version("21.5.0.0"))).isFalse();
}
private MultipleComponentsDependencyVersion version(String version) {

@ -38,81 +38,33 @@ class ReleaseTrainDependencyVersionTests {
}
@Test
void isNewerThanWhenReleaseTrainIsNewerShouldReturnTrue() {
assertThat(version("Lovelace-RELEASE").isNewerThan(version("Kay-SR5"))).isTrue();
void isSameMajorWhenReleaseTrainIsDifferentShouldReturnFalse() {
assertThat(version("Lovelace-RELEASE").isSameMajor(version("Kay-SR5"))).isFalse();
}
@Test
void isNewerThanWhenVersionIsNewerShouldReturnTrue() {
assertThat(version("Kay-SR10").isNewerThan(version("Kay-SR5"))).isTrue();
void isSameMajorWhenReleaseTrainIsTheSameShouldReturnTrue() {
assertThat(version("Lovelace-RELEASE").isSameMajor(version("Lovelace-SR5"))).isTrue();
}
@Test
void isNewerThanWhenVersionIsOlderShouldReturnFalse() {
assertThat(version("Kay-RELEASE").isNewerThan(version("Kay-SR5"))).isFalse();
void isSameMinorWhenReleaseTrainIsDifferentShouldReturnFalse() {
assertThat(version("Lovelace-RELEASE").isSameMajor(version("Kay-SR5"))).isFalse();
}
@Test
void isNewerThanWhenReleaseTrainIsOlderShouldReturnFalse() {
assertThat(version("Ingalls-RELEASE").isNewerThan(version("Kay-SR5"))).isFalse();
}
@Test
void isSameMajorAndNewerWhenWhenReleaseTrainIsNewerShouldReturnTrue() {
assertThat(version("Lovelace-RELEASE").isSameMajorAndNewerThan(version("Kay-SR5"))).isTrue();
}
@Test
void isSameMajorAndNewerThanWhenReleaseTrainIsOlderShouldReturnFalse() {
assertThat(version("Ingalls-RELEASE").isSameMajorAndNewerThan(version("Kay-SR5"))).isFalse();
}
@Test
void isSameMajorAndNewerThanWhenVersionIsNewerShouldReturnTrue() {
assertThat(version("Kay-SR6").isSameMajorAndNewerThan(version("Kay-SR5"))).isTrue();
}
@Test
void isSameMinorAndNewerThanWhenReleaseTrainIsNewerShouldReturnFalse() {
assertThat(version("Lovelace-RELEASE").isSameMinorAndNewerThan(version("Kay-SR5"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenReleaseTrainIsTheSameAndVersionIsNewerShouldReturnTrue() {
assertThat(version("Kay-SR6").isSameMinorAndNewerThan(version("Kay-SR5"))).isTrue();
}
@Test
void isSameMinorAndNewerThanWhenReleaseTrainAndVersionAreTheSameShouldReturnFalse() {
assertThat(version("Kay-SR6").isSameMinorAndNewerThan(version("Kay-SR6"))).isFalse();
}
@Test
void isSameMinorAndNewerThanWhenReleaseTrainIsTheSameAndVersionIsOlderShouldReturnFalse() {
assertThat(version("Kay-SR6").isSameMinorAndNewerThan(version("Kay-SR7"))).isFalse();
}
@Test
void releaseTrainVersionIsNotNewerThanCalendarVersion() {
assertThat(version("Kay-SR6").isNewerThan(calendarVersion("2020.0.0"))).isFalse();
void isSameMinorWhenReleaseTrainIsTheSameShouldReturnTrue() {
assertThat(version("Lovelace-RELEASE").isSameMajor(version("Lovelace-SR5"))).isTrue();
}
@Test
void releaseTrainVersionIsNotSameMajorAsCalendarTrainVersion() {
assertThat(version("Kay-SR6").isSameMajorAndNewerThan(calendarVersion("2020.0.0"))).isFalse();
assertThat(version("Kay-SR6").isSameMajor(calendarVersion("2020.0.0"))).isFalse();
}
@Test
void releaseTrainVersionIsNotSameMinorAsCalendarVersion() {
assertThat(version("Kay-SR6").isSameMinorAndNewerThan(calendarVersion("2020.0.0"))).isFalse();
}
@Test
void whenComparedWithADifferentDependencyVersionTypeThenTheResultsAreNonZero() {
DependencyVersion dysprosium = ReleaseTrainDependencyVersion.parse("Dysprosium-SR16");
DependencyVersion twentyTwenty = ArtifactVersionDependencyVersion.parse("2020.0.0");
assertThat(dysprosium).isLessThan(twentyTwenty);
assertThat(twentyTwenty).isGreaterThan(dysprosium);
assertThat(version("Kay-SR6").isSameMinor(calendarVersion("2020.0.0"))).isFalse();
}
private static ReleaseTrainDependencyVersion version(String input) {

Loading…
Cancel
Save