Skip to content
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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<version.ph-genericode>6.2.0</version.ph-genericode>
<version.reflections>0.10.2</version.reflections>
<version.resolver>1.8.2</version.resolver>
<version.semver4j>3.1.0</version.semver4j>
<version.slf4j>2.0.3</version.slf4j>

<!-- Versions - Plugins -->
Expand Down Expand Up @@ -220,6 +221,11 @@
<artifactId>junit-jupiter-api</artifactId>
<version>${version.junit}</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>${version.semver4j}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -341,6 +347,10 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
74 changes: 21 additions & 53 deletions src/main/java/eu/europa/ted/eforms/sdk/SdkVersion.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,60 @@
package eu.europa.ted.eforms.sdk;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

public class SdkVersion implements Comparable<SdkVersion> {
private static final String FORMAT_PATTERN = "{0}.{1}.{2}";

private String major = "0";

private String minor = "0";
import com.vdurmont.semver4j.Semver;
import com.vdurmont.semver4j.Semver.SemverType;

private String patch = "0";

private boolean isPatch = false;
public class SdkVersion implements Comparable<SdkVersion> {

@SuppressWarnings("unused")
private SdkVersion() {}
private final Semver version;

public SdkVersion(final String version) {
Validate.notBlank(version, "Undefined version");
Validate.matchesPattern(version, "[0-9]+(\\.[0-9]+)*(-SNAPSHOT)?", "Invalid version format");

String[] versionParts = version.split("\\.");

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

if (versionParts.length > 1) {
this.minor = versionParts[1];
}

if (versionParts.length > 2) {
this.isPatch = true;
this.patch = versionParts[2];
}
// Check that we did get a MINOR part
Validate.notNull(this.version.getMinor());
}

public String getMajor() {
return major;
return version.getMajor().toString();
}

public String getMinor() {
return minor;
return version.getMinor().toString();
}

public String getPatch() {
return patch;
return version.getPatch() == null ? "0" : version.getPatch().toString();
}

public String getNextMajor() {
return new SdkVersion(MessageFormat.format(FORMAT_PATTERN, getAsInt(major) + 1, minor, patch))
.toString();
return version.withIncMajor().toString();
}

public String getNextMinor() {
return new SdkVersion(MessageFormat.format(FORMAT_PATTERN, major, getAsInt(minor) + 1, patch))
.toString();
return version.withIncMinor().toString();
}

public boolean isPatch() {
return isPatch;
return version.getPatch() != null;
}

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

parts.add(major);
parts.add(minor);
parts.add(getMajor());
parts.add(getMinor());

if (withPatch) {
parts.add(patch);
parts.add(getPatch());
}

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

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

@Override
Expand All @@ -98,24 +79,12 @@ public int compareTo(SdkVersion that) {
return 0;
}

if (getAsInt(this.getMajor()) == getAsInt(that.getMajor())) {
if (getAsInt(this.getMinor()) == getAsInt(that.getMinor())) {
return getAsInt(this.getPatch()) < getAsInt(that.getPatch()) ? -1 : 1;
} else {
return getAsInt(this.getMinor()) < getAsInt(that.getMinor()) ? -1 : 1;
}
} else {
return getAsInt(this.getMajor()) < getAsInt(that.getMajor()) ? -1 : 1;
}
}

private int getAsInt(String versionPart) {
return Integer.parseInt(Optional.ofNullable(versionPart).orElse("0").replace("-SNAPSHOT", ""));
return version.compareTo(that.version);
}

@Override
public int hashCode() {
return Objects.hash(major, minor, patch);
return Objects.hash(version);
}

@Override
Expand All @@ -127,7 +96,6 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
SdkVersion other = (SdkVersion) obj;
return Objects.equals(major, other.major) && Objects.equals(minor, other.minor)
&& Objects.equals(patch, other.patch);
return Objects.equals(version, other.version);
}
}
63 changes: 63 additions & 0 deletions src/test/java/eu/europa/ted/eforms/sdk/SdkVersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package eu.europa.ted.eforms.sdk;

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

import org.junit.jupiter.api.Test;

public class SdkVersionTest {
@Test
void testGetMajor() {
assertEquals("1", new SdkVersion("1.2.3").getMajor());
}

@Test
void testGetMinor() {
assertEquals("2", new SdkVersion("1.2.3").getMinor());
}

@Test
void testGetPatch() {
assertEquals("3", new SdkVersion("1.2.3").getPatch());
}

@Test
void testGetNextMajor() {
assertEquals("2.2.3", new SdkVersion("1.2.3").getNextMajor());
}

@Test
void testGetNextMinor() {
assertEquals("1.3.3", new SdkVersion("1.2.3").getNextMinor());
}

@Test
void testIsPatch() {
assertEquals(false, new SdkVersion("1.2").isPatch());

assertEquals(true, new SdkVersion("1.2.3").isPatch());
assertEquals(true, new SdkVersion("1.2.3-rc.4").isPatch());
}

@Test
void testToNormalisedStringWithPatch() {
assertEquals("1.2.3", new SdkVersion("1.2.3").toNormalisedString(true));
assertEquals("1.2.3", new SdkVersion("1.2.3-SNAPSHOT").toNormalisedString(true));
assertEquals("1.2.3", new SdkVersion("1.2.3-rc.4").toNormalisedString(true));
}

@Test
void testToStringWithoutPatch() {
assertEquals("1.2", new SdkVersion("1.2.3").toStringWithoutPatch());
assertEquals("1.2", new SdkVersion("1.2.3-SNAPSHOT").toStringWithoutPatch());
assertEquals("1.2", new SdkVersion("1.2.3-rc.4").toStringWithoutPatch());
}

@Test
void testCompare() {
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2.2")) > 0);
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2")) > 0);
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2.3-SNAPSHOT")) > 0);
assert(new SdkVersion("1.2.3").compareTo(new SdkVersion("1.2.3-rc.3")) > 0);
assert(new SdkVersion("2.0.0").compareTo(new SdkVersion("2.0.0-alpha.1")) > 0);
}
}