Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Update semver library" #248

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion multiapps-mta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>org.semver4j</groupId>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion multiapps-mta/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
requires org.apache.commons.collections4;
requires org.apache.commons.io;
requires org.apache.commons.lang3;
requires org.semver4j;
requires semver4j;

requires static java.compiler;
requires static org.immutables.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

import org.cloudfoundry.multiapps.common.ParsingException;
import org.cloudfoundry.multiapps.mta.Messages;
import org.semver4j.Semver;
import org.semver4j.SemverException;
import org.cloudfoundry.multiapps.mta.parsers.PartialVersionConverter;

import com.vdurmont.semver4j.Semver;
import com.vdurmont.semver4j.Semver.SemverType;
import com.vdurmont.semver4j.SemverException;

public class Version implements Comparable<Version> {

private static final PartialVersionConverter PARTIAL_VERSION_CONVERTER = new PartialVersionConverter();

private final Semver version;

private Version(Semver version) {
Expand All @@ -27,12 +32,21 @@ public int getPatch() {
return version.getPatch();
}

public String getBuild() {
return version.getBuild();
}

public String[] getSuffixTokens() {
return version.getSuffixTokens();
}

public static Version parseVersion(String versionString) {
var version = Semver.coerce(versionString); //allows incomplete version strings like "3" or "3.0"
if (version == null) {
throw new ParsingException(Messages.UNABLE_TO_PARSE_VERSION, versionString);
try {
String fullVersionString = PARTIAL_VERSION_CONVERTER.convertToFullVersionString(versionString);
return new Version(new Semver(fullVersionString, SemverType.NPM));
} catch (SemverException e) {
throw new ParsingException(e, Messages.UNABLE_TO_PARSE_VERSION, versionString);
}
return new Version(version);
}

@Override
Expand All @@ -42,7 +56,7 @@ public int hashCode() {

@Override
public String toString() {
return version.toString();
return version.getValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.cloudfoundry.multiapps.mta.model;

import java.util.function.Predicate;

public enum VersionRule {

SAME_HIGHER(VersionRule::isNotDowngrade),
Expand All @@ -10,14 +8,22 @@ public enum VersionRule {

ALL(VersionRule::isAny);

private final Predicate<DeploymentType> versionRuleValidator;
private interface VersionRuleValidator {
boolean allows(DeploymentType deploymentType);
}

private VersionRuleValidator versionRuleValidator;

VersionRule(Predicate<DeploymentType> versionRuleValidator) {
VersionRule(VersionRuleValidator versionRuleValidator) {
this.versionRuleValidator = versionRuleValidator;
}

public boolean allows(DeploymentType deploymentType) {
return versionRuleValidator.test(deploymentType);
return versionRuleValidator.allows(deploymentType);
}

public static VersionRule value(String caseInsensitiveValue) {
return VersionRule.valueOf(caseInsensitiveValue.toUpperCase());
}

private static boolean isNotDowngrade(DeploymentType deploymentType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.cloudfoundry.multiapps.mta.parsers;

import com.vdurmont.semver4j.Semver;
import com.vdurmont.semver4j.Semver.SemverType;

public class PartialVersionConverter {

private static final String VERSION_STRING_TEMPLATE = "%s.%s.%s%s%s";
private static final String VERSION_SUFFIX_STRING_TEMPLATE = "-%s";
private static final String VERSION_BUILD_STRING_TEMPLATE = "+%s";
private static final String DEFAULT_VERSION_SUFFIX = "";

public String convertToFullVersionString(String partialVersionString) {
Semver partialVersion = new Semver(partialVersionString, SemverType.LOOSE);
Integer majorVersion = partialVersion.getMajor();
Integer minorVersion = partialVersion.getMinor();
Integer patchVersion = partialVersion.getPatch();

if (minorVersion == null) {
minorVersion = 0;
}
if (patchVersion == null) {
patchVersion = 0;
}
return buildVersionString(majorVersion, minorVersion, patchVersion, partialVersion.getSuffixTokens(), partialVersion.getBuild());
}

private String buildVersionString(int major, int minor, int patch, String[] suffixTokens, String buildVersion) {
String formattedSuffixTokens = formatSuffixTokens(suffixTokens);
String formattedBuildVersion = formatBuildVersion(buildVersion);
return String.format(VERSION_STRING_TEMPLATE, major, minor, patch, formattedSuffixTokens, formattedBuildVersion);
}

private String formatSuffixTokens(String[] suffixTokens) {
if (suffixTokens.length == 0) {
return DEFAULT_VERSION_SUFFIX;
}
return String.format(VERSION_SUFFIX_STRING_TEMPLATE, String.join(".", suffixTokens));
}

private String formatBuildVersion(String buildVersion) {
if (buildVersion == null) {
return DEFAULT_VERSION_SUFFIX;
}
return String.format(VERSION_BUILD_STRING_TEMPLATE, buildVersion);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.cloudfoundry.multiapps.mta.parsers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import com.vdurmont.semver4j.SemverException;

class PartialVersionConverterTest {

private final PartialVersionConverter partialVersionConverter = new PartialVersionConverter();

@ParameterizedTest
@MethodSource
void testConvertWithInvalidVersions(String versionString, String expectedExceptionMessage) {
SemverException exception = assertThrows(SemverException.class,
() -> partialVersionConverter.convertToFullVersionString(versionString));

assertEquals(expectedExceptionMessage, exception.getMessage());
}

static Stream<Arguments> testConvertWithInvalidVersions() {
return Stream.of(
// @formatter:off
Arguments.of("1.0.0-beta+", "The build cannot be empty."),
Arguments.of("3.a", "Invalid version (no minor version): 3.a"),
Arguments.of("a.b.c", "Invalid version (no major version): a.b.c"),
Arguments.of( "", "Invalid version (no major version): "),
Arguments.of("[ 2.0, 2.1 ]", "Invalid version (no major version): [ 2.0, 2.1 ]")
// @formatter:on
);
}

@ParameterizedTest
@MethodSource
void testConvertWithValidVersions(String versionString, String expectedResult) {
String fullVersionString = partialVersionConverter.convertToFullVersionString(versionString);

assertEquals(expectedResult, fullVersionString);
}

static Stream<Arguments> testConvertWithValidVersions() {
return Stream.of(
// @formatter:off
// Full version:
Arguments.of("1.0.0", "1.0.0"),
// Partial version with minor version:
Arguments.of("2.1", "2.1.0"),
// Partial version with patch version:
Arguments.of("2", "2.0.0"),
// Full version with suffix tokens:
Arguments.of("1.9.0-SHAPSHOT", "1.9.0-SHAPSHOT"),
// Partial version with suffix tokens:
Arguments.of("1.9-SHAPSHOT", "1.9.0-SHAPSHOT"),
// Partial version with suffix tokens:
Arguments.of("1-SHAPSHOT", "1.0.0-SHAPSHOT"),
// Full version with suffix tokens and build information:
Arguments.of("1.2.0-beta+exp.sha.5114f85", "1.2.0-beta+exp.sha.5114f85"),
// Partial version with suffix tokens and build information:
Arguments.of("1.2-beta+exp.sha.5114f85", "1.2.0-beta+exp.sha.5114f85"),
// Partial version with suffix tokens and build information:
Arguments.of("1-beta+exp.sha.5114f85", "1.0.0-beta+exp.sha.5114f85")
// @formatter:on
);
}

}
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<commons-io.version>2.11.0</commons-io.version>
<slf4j.version>2.0.6</slf4j.version>
<snakeyaml.version>2.0</snakeyaml.version>
<semver4j.version>5.2.2</semver4j.version>
<!-- Be very wary when updating Semver to newer versions because there is an issue -->
<!-- https://github.com/semver4j/semver4j/issues/208 which must fixed before adoption -->
<semver4j.version>3.1.0</semver4j.version>
<immutables.version>2.8.8</immutables.version>
<jaxb-api.version>2.3.3</jaxb-api.version>
<jaxb-core.version>2.3.0.1</jaxb-core.version>
Expand Down Expand Up @@ -292,9 +294,9 @@
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.semver4j/semver4j -->
<!-- https://mvnrepository.com/artifact/com.vdurmont/semver4j -->
<dependency>
<groupId>org.semver4j</groupId>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>${semver4j.version}</version>
</dependency>
Expand Down