Skip to content

Commit

Permalink
dcoraboeuf#272 Accept patch level in version
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Nov 6, 2013
1 parent e1c6634 commit c5dda46
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
33 changes: 29 additions & 4 deletions ontrack-core/src/main/java/net/ontrack/core/support/Version.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package net.ontrack.core.support;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class Version implements Comparable<Version> {

public static final Pattern REGEX = Pattern.compile("(\\d+)\\.(\\d+)");
public static final Pattern REGEX = Pattern.compile("(\\d+)\\.(\\d+)(\\.(\\d+))?");

public static Version of(String value) {
if (StringUtils.isBlank(value)) {
Expand All @@ -19,7 +22,12 @@ public static Version of(String value) {
if (matcher.matches()) {
int major = Integer.parseInt(matcher.group(1), 10);
int minor = Integer.parseInt(matcher.group(2), 10);
return new Version(major, minor);
int patch = 0;
String patchValue = matcher.group(4);
if (StringUtils.isNotBlank(patchValue)) {
patch = Integer.parseInt(patchValue, 10);
}
return new Version(major, minor, patch);
} else {
throw new VersionFormatException(value);
}
Expand All @@ -28,16 +36,33 @@ public static Version of(String value) {

private final int major;
private final int minor;
private final int patch;

public Version(int major) {
this(major, 0, 0);
}

public Version(int major, int minor) {
this(major, minor, 0);
}

@Override
public String toString() {
return String.format("%d.%d", major, minor);
if (patch == 0) {
return String.format("%d.%d", major, minor);
} else {
return String.format("%d.%d.%d", major, minor, patch);
}
}

@Override
public int compareTo(Version o) {
if (this.major == o.major) {
return this.minor - o.minor;
if (this.minor == o.minor) {
return this.patch - o.patch;
} else {
return this.minor - o.minor;
}
} else {
return this.major - o.major;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,48 @@ public void basics() {
Version v = new Version(4, 15);
assertEquals(v.getMajor(), 4);
assertEquals(v.getMinor(), 15);
assertEquals(v.getPatch(), 0);
assertEquals("4.15", v.toString());
assertEquals(new Version(4, 15), v);
assertEquals(new Version(4, 15, 0), v);
assertEquals(new Version(4, 15).hashCode(), v.hashCode());
}

@Test
public void basics_patch() {
Version v = new Version(4, 15, 2);
assertEquals(v.getMajor(), 4);
assertEquals(v.getMinor(), 15);
assertEquals(v.getPatch(), 2);
assertEquals("4.15.2", v.toString());
assertEquals(new Version(4, 15, 2), v);
assertEquals(new Version(4, 15, 2).hashCode(), v.hashCode());
}

@Test
public void parsing_ok() {
Version v = of("4.15");
assertEquals(new Version(4, 15), v);
}

@Test
public void parsing_patch_ok() {
Version v = of("4.15.2");
assertEquals(new Version(4, 15, 2), v);
}

@Test
public void parsing_ok_with_zeros() {
Version v = of("04.015");
assertEquals(new Version(4, 15), v);
}

@Test
public void parsing_patch_ok_with_zeros() {
Version v = of("04.015.001");
assertEquals(new Version(4, 15, 1), v);
}

@Test(expected = VersionBlankException.class)
public void parsing_null() {
of(null);
Expand Down Expand Up @@ -68,6 +93,19 @@ public void parsing_format_m() {
@Test
public void compare_equal() {
assertEquals(0, of("4.15").compareTo(of("4.15")));
assertEquals(0, of("4.15.2").compareTo(of("4.15.2")));
}

@Test
public void compare_minor_equal_gt() {
assertTrue(of("4.15.2").compareTo(of("4.15")) > 0);
assertTrue(of("4.15.2").compareTo(of("4.15.1")) > 0);
}

@Test
public void compare_minor_equallgt() {
assertTrue(of("4.15.1").compareTo(of("4.15.5")) < 0);
assertTrue(of("4.15").compareTo(of("4.15.1")) < 0);
}

@Test
Expand Down

0 comments on commit c5dda46

Please sign in to comment.