Add support for SemVer and CalVer versions

This commit improves the version parser to handle qualifiers that are
separated by either a `.` or a `-`. This makes the parsing of
`1.2.0-RC1` or `2020.0.0-M1` possible.

Closes gh-1083
This commit is contained in:
Stephane Nicoll
2020-05-26 11:25:32 +02:00
parent 9255ae9fbe
commit 0b7614c4f0
5 changed files with 107 additions and 71 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -35,23 +35,35 @@ class VersionParserTests {
private VersionParser parser = new VersionParser(Collections.emptyList());
@Test
void noQualifierString() {
void versionWithNoQualifier() {
Version version = this.parser.parse("1.2.0");
assertThat(version.toString()).isEqualTo("1.2.0");
}
@Test
void withQualifierString() {
void versionWithQualifierAndDotSeparator() {
Version version = this.parser.parse("1.2.0.RELEASE");
assertThat(version.toString()).isEqualTo("1.2.0.RELEASE");
}
@Test
void withQualifierAndVersionString() {
void versionWithQualifierAndDashSeparator() {
Version version = this.parser.parse("1.2.0-SNAPSHOT");
assertThat(version.toString()).isEqualTo("1.2.0-SNAPSHOT");
}
@Test
void versionWithQualifierVersionAndDotSeparator() {
Version version = this.parser.parse("1.2.0.RC2");
assertThat(version.toString()).isEqualTo("1.2.0.RC2");
}
@Test
void versionWithQualifierVersionAndDashSeparator() {
Version version = this.parser.parse("1.2.0-M3");
assertThat(version.toString()).isEqualTo("1.2.0-M3");
}
@Test
void parseInvalidVersion() {
assertThatExceptionOfType(InvalidVersionException.class).isThrownBy(() -> this.parser.parse("foo"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -17,6 +17,9 @@
package io.spring.initializr.generator.version;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
@@ -123,6 +126,33 @@ class VersionTests {
assertThat(parse("1.2.0.BUILD-SNAPSHOT")).isLessThan(parse("1.2.0.RELEASE"));
}
@Test
void orderVersionSchemeWithQualifiedVersions() {
List<String> sortedVersions = Stream
.of("2.3.0.BUILD-SNAPSHOT", "2.3.0.RC1", "2.3.0.M2", "2.3.0.M1", "2.3.0.RELEASE", "2.3.0.RC2")
.map(this::parse).sorted().map(Version::toString).collect(Collectors.toList());
assertThat(sortedVersions).containsExactly("2.3.0.M1", "2.3.0.M2", "2.3.0.RC1", "2.3.0.RC2",
"2.3.0.BUILD-SNAPSHOT", "2.3.0.RELEASE");
}
@Test
void orderVersionSchemeWithSemVer() {
List<String> sortedVersions = Stream
.of("2.3.0-SNAPSHOT", "2.3.0-RC1", "2.3.0-M2", "2.3.0-M1", "2.3.0", "2.3.0-RC2").map(this::parse)
.sorted().map(Version::toString).collect(Collectors.toList());
assertThat(sortedVersions).containsExactly("2.3.0-M1", "2.3.0-M2", "2.3.0-RC1", "2.3.0-RC2", "2.3.0-SNAPSHOT",
"2.3.0");
}
@Test
void orderVersionSchemeWithCalVer() {
List<String> sortedVersions = Stream
.of("2020.0.0-SNAPSHOT", "2020.0.0-RC1", "2020.0.0-M2", "2020.0.0-M1", "2020.0.0", "2020.0.0-RC2")
.map(this::parse).sorted().map(Version::toString).collect(Collectors.toList());
assertThat(sortedVersions).containsExactly("2020.0.0-M1", "2020.0.0-M2", "2020.0.0-RC1", "2020.0.0-RC2",
"2020.0.0-SNAPSHOT", "2020.0.0");
}
private Version parse(String text) {
return this.parser.parse(text);
}