Skip to content

Commit b53e213

Browse files
SdkVersion: Fix handling of suffix in version string (TEDEFO-2299)
Use the Semver4j library to parse and manipulate the version. This allows to properly handle versions with a suffix like "1.2.3-rc.1" and "2.0.0-alpha.1" This also fixes equals() and compareTo() for versions with a suffix. Enable the unit tests that were failing before this fix.
1 parent 5f17b0f commit b53e213

3 files changed

Lines changed: 37 additions & 63 deletions

File tree

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<version.ph-genericode>6.2.0</version.ph-genericode>
6969
<version.reflections>0.10.2</version.reflections>
7070
<version.resolver>1.8.2</version.resolver>
71+
<version.semver4j>3.1.0</version.semver4j>
7172
<version.slf4j>2.0.3</version.slf4j>
7273

7374
<!-- Versions - Plugins -->
@@ -220,6 +221,11 @@
220221
<artifactId>junit-jupiter-api</artifactId>
221222
<version>${version.junit}</version>
222223
</dependency>
224+
<dependency>
225+
<groupId>com.vdurmont</groupId>
226+
<artifactId>semver4j</artifactId>
227+
<version>${version.semver4j}</version>
228+
</dependency>
223229
</dependencies>
224230
</dependencyManagement>
225231

@@ -341,6 +347,10 @@
341347
<artifactId>junit-jupiter-api</artifactId>
342348
<scope>test</scope>
343349
</dependency>
350+
<dependency>
351+
<groupId>com.vdurmont</groupId>
352+
<artifactId>semver4j</artifactId>
353+
</dependency>
344354
</dependencies>
345355

346356
<build>
Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,60 @@
11
package eu.europa.ted.eforms.sdk;
22

3-
import java.text.MessageFormat;
43
import java.util.ArrayList;
54
import java.util.List;
65
import java.util.Objects;
7-
import java.util.Optional;
86
import org.apache.commons.lang3.StringUtils;
97
import org.apache.commons.lang3.Validate;
108

11-
public class SdkVersion implements Comparable<SdkVersion> {
12-
private static final String FORMAT_PATTERN = "{0}.{1}.{2}";
13-
14-
private String major = "0";
15-
16-
private String minor = "0";
9+
import com.vdurmont.semver4j.Semver;
10+
import com.vdurmont.semver4j.Semver.SemverType;
1711

18-
private String patch = "0";
19-
20-
private boolean isPatch = false;
12+
public class SdkVersion implements Comparable<SdkVersion> {
2113

22-
@SuppressWarnings("unused")
23-
private SdkVersion() {}
14+
private final Semver version;
2415

2516
public SdkVersion(final String version) {
2617
Validate.notBlank(version, "Undefined version");
27-
Validate.matchesPattern(version, "[0-9]+(\\.[0-9]+)*(-SNAPSHOT)?", "Invalid version format");
28-
29-
String[] versionParts = version.split("\\.");
3018

31-
this.major = versionParts[0];
19+
// LOOSE because we need to accept MAJOR.MINOR
20+
this.version = new Semver(version, SemverType.LOOSE);
3221

33-
if (versionParts.length > 1) {
34-
this.minor = versionParts[1];
35-
}
36-
37-
if (versionParts.length > 2) {
38-
this.isPatch = true;
39-
this.patch = versionParts[2];
40-
}
22+
// Check that we did get a MINOR part
23+
Validate.notNull(this.version.getMinor());
4124
}
4225

4326
public String getMajor() {
44-
return major;
27+
return version.getMajor().toString();
4528
}
4629

4730
public String getMinor() {
48-
return minor;
31+
return version.getMinor().toString();
4932
}
5033

5134
public String getPatch() {
52-
return patch;
35+
return version.getPatch() == null ? "0" : version.getPatch().toString();
5336
}
5437

5538
public String getNextMajor() {
56-
return new SdkVersion(MessageFormat.format(FORMAT_PATTERN, getAsInt(major) + 1, minor, patch))
57-
.toString();
39+
return version.withIncMajor().toString();
5840
}
5941

6042
public String getNextMinor() {
61-
return new SdkVersion(MessageFormat.format(FORMAT_PATTERN, major, getAsInt(minor) + 1, patch))
62-
.toString();
43+
return version.withIncMinor().toString();
6344
}
6445

6546
public boolean isPatch() {
66-
return isPatch;
47+
return version.getPatch() != null;
6748
}
6849

6950
public String toNormalisedString(boolean withPatch) {
7051
List<String> parts = new ArrayList<>();
7152

72-
parts.add(major);
73-
parts.add(minor);
53+
parts.add(getMajor());
54+
parts.add(getMinor());
7455

7556
if (withPatch) {
76-
parts.add(patch);
57+
parts.add(getPatch());
7758
}
7859

7960
return StringUtils.join(parts, ".");
@@ -85,7 +66,7 @@ public String toStringWithoutPatch() {
8566

8667
@Override
8768
public String toString() {
88-
return toNormalisedString(true);
69+
return version.toString();
8970
}
9071

9172
@Override
@@ -98,24 +79,12 @@ public int compareTo(SdkVersion that) {
9879
return 0;
9980
}
10081

101-
if (getAsInt(this.getMajor()) == getAsInt(that.getMajor())) {
102-
if (getAsInt(this.getMinor()) == getAsInt(that.getMinor())) {
103-
return getAsInt(this.getPatch()) < getAsInt(that.getPatch()) ? -1 : 1;
104-
} else {
105-
return getAsInt(this.getMinor()) < getAsInt(that.getMinor()) ? -1 : 1;
106-
}
107-
} else {
108-
return getAsInt(this.getMajor()) < getAsInt(that.getMajor()) ? -1 : 1;
109-
}
110-
}
111-
112-
private int getAsInt(String versionPart) {
113-
return Integer.parseInt(Optional.ofNullable(versionPart).orElse("0").replace("-SNAPSHOT", ""));
82+
return version.compareTo(that.version);
11483
}
11584

11685
@Override
11786
public int hashCode() {
118-
return Objects.hash(major, minor, patch);
87+
return Objects.hash(version);
11988
}
12089

12190
@Override
@@ -127,7 +96,6 @@ public boolean equals(Object obj) {
12796
if (getClass() != obj.getClass())
12897
return false;
12998
SdkVersion other = (SdkVersion) obj;
130-
return Objects.equals(major, other.major) && Objects.equals(minor, other.minor)
131-
&& Objects.equals(patch, other.patch);
99+
return Objects.equals(version, other.version);
132100
}
133101
}

src/test/java/eu/europa/ted/eforms/sdk/SdkVersionTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,29 @@ void testIsPatch() {
3535
assertEquals(false, new SdkVersion("1.2").isPatch());
3636

3737
assertEquals(true, new SdkVersion("1.2.3").isPatch());
38-
// FIXME
39-
//assertEquals(true, new SdkVersion("1.2.3-rc.4").isPatch());
38+
assertEquals(true, new SdkVersion("1.2.3-rc.4").isPatch());
4039
}
4140

4241
@Test
4342
void testToNormalisedStringWithPatch() {
4443
assertEquals("1.2.3", new SdkVersion("1.2.3").toNormalisedString(true));
45-
// FIXME
46-
//assertEquals("1.2.3", new SdkVersion("1.2.3-SNAPSHOT").toNormalisedString(true));
47-
//assertEquals("1.2.3", new SdkVersion("1.2.3-rc.4").toNormalisedString(true));
44+
assertEquals("1.2.3", new SdkVersion("1.2.3-SNAPSHOT").toNormalisedString(true));
45+
assertEquals("1.2.3", new SdkVersion("1.2.3-rc.4").toNormalisedString(true));
4846
}
4947

5048
@Test
5149
void testToStringWithoutPatch() {
5250
assertEquals("1.2", new SdkVersion("1.2.3").toStringWithoutPatch());
5351
assertEquals("1.2", new SdkVersion("1.2.3-SNAPSHOT").toStringWithoutPatch());
54-
// FIXME
55-
//assertEquals("1.2", new SdkVersion("1.2.3-rc.4").toStringWithoutPatch());
52+
assertEquals("1.2", new SdkVersion("1.2.3-rc.4").toStringWithoutPatch());
5653
}
5754

5855
@Test
5956
void testCompare() {
6057
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2.2")) > 0);
6158
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2")) > 0);
6259
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2.3-SNAPSHOT")) > 0);
63-
// FIXME
64-
//assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2.3-rc.3")) > 0);
65-
//assert(new SdkVersion("2.0.0").compareTo(new SdkVersion("2.0.0-alpha.1")) > 0);
60+
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2.3-rc.3")) > 0);
61+
assert(new SdkVersion("2.0.0").compareTo(new SdkVersion("2.0.0-alpha.1")) > 0);
6662
}
6763
}

0 commit comments

Comments
 (0)