-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
159 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...pps-mta/src/main/java/org/cloudfoundry/multiapps/mta/parsers/PartialVersionConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...mta/src/test/java/org/cloudfoundry/multiapps/mta/parsers/PartialVersionConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters