Commit b5553734 authored by GregDThomas's avatar GregDThomas Committed by daryl herzmann

Add support for SNAPSHOT style versioning now Maven is a first class build tool. (#958)

parent 7e2f1fc5
...@@ -28,6 +28,7 @@ import java.util.regex.Pattern; ...@@ -28,6 +28,7 @@ import java.util.regex.Pattern;
public final class Version implements Comparable<Version> { public final class Version implements Comparable<Version> {
private static final Pattern PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(?:\\s+(\\w+))?(?:\\s+(\\d+))?"); private static final Pattern PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(?:\\s+(\\w+))?(?:\\s+(\\d+))?");
private static final Pattern SNAPSHOT_PATTERN = Pattern.compile("(?i)(\\d+)\\.(\\d+)\\.(\\d+)(?:\\.(\\d+))?-SNAPSHOT");
/** /**
* The major number (ie 1.x.x). * The major number (ie 1.x.x).
...@@ -111,16 +112,22 @@ public final class Version implements Comparable<Version> { ...@@ -111,16 +112,22 @@ public final class Version implements Comparable<Version> {
} else { } else {
this.statusVersion = -1; this.statusVersion = -1;
} }
} else { return;
this.major = this.minor = this.micro = 0; }
this.statusVersion = -1; final Matcher snapshotMatcher = SNAPSHOT_PATTERN.matcher(source);
this.status = ReleaseStatus.Release; if (snapshotMatcher.matches()) {
major = Integer.parseInt(snapshotMatcher.group(1));
minor = Integer.parseInt(snapshotMatcher.group(2));
micro = Integer.parseInt(snapshotMatcher.group(3));
status = ReleaseStatus.Snapshot;
final String statusVersionString = snapshotMatcher.group(4);
statusVersion = statusVersionString == null ? -1 : Integer.parseInt(statusVersionString);
return;
} }
} else {
this.major = this.minor = this.micro = 0;
this.statusVersion = -1;
this.status = ReleaseStatus.Release;
} }
this.major = this.minor = this.micro = 0;
this.statusVersion = -1;
this.status = ReleaseStatus.Release;
} }
/** /**
...@@ -133,7 +140,12 @@ public final class Version implements Comparable<Version> { ...@@ -133,7 +140,12 @@ public final class Version implements Comparable<Version> {
if (versionString == null) { if (versionString == null) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(major).append('.').append(minor).append('.').append(micro); sb.append(major).append('.').append(minor).append('.').append(micro);
if (status != ReleaseStatus.Release || statusVersion != -1) { if(status == ReleaseStatus.Snapshot) {
if (statusVersion >= 0) {
sb.append('.').append(statusVersion);
}
sb.append("-SNAPSHOT");
} else if (status != ReleaseStatus.Release || statusVersion != -1) {
sb.append(' ').append(status); sb.append(' ').append(status);
if (statusVersion >= 0) { if (statusVersion >= 0) {
sb.append(' ').append(statusVersion); sb.append(' ').append(statusVersion);
...@@ -196,7 +208,7 @@ public final class Version implements Comparable<Version> { ...@@ -196,7 +208,7 @@ public final class Version implements Comparable<Version> {
* are indicated by type safe enum constants. * are indicated by type safe enum constants.
*/ */
public enum ReleaseStatus { public enum ReleaseStatus {
Release("Release"), Release_Candidate("RC"), Beta("Beta"), Alpha("Alpha"); Release("Release"), Release_Candidate("RC"), Beta("Beta"), Alpha("Alpha"), Snapshot("Snapshot");
private String status; private String status;
......
package org.jivesoftware.util; package org.jivesoftware.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import org.jivesoftware.util.Version.ReleaseStatus; import org.jivesoftware.util.Version.ReleaseStatus;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class VersionTest { public class VersionTest {
@Test @Test
...@@ -100,4 +102,32 @@ public class VersionTest { ...@@ -100,4 +102,32 @@ public class VersionTest {
assertEquals(version1, version2); assertEquals(version1, version2);
assertTrue((version1.compareTo(version2) == 0) == version1.equals(version2)); assertTrue((version1.compareTo(version2) == 0) == version1.equals(version2));
} }
@Test
public void willVersionAThreeDigitSnapshot() throws Exception {
final String versionString = "1.2.3-SNAPSHOT";
Version test = new Version(versionString);
assertThat(test.getMajor(), is(1));
assertThat(test.getMinor(), is(2));
assertThat(test.getMicro(), is(3));
assertThat(test.getStatusVersion(), is(-1));
assertThat(test.getStatus(), is(ReleaseStatus.Snapshot));
assertThat(test.getVersionString(),is(versionString));
}
@Test
public void willVersionAFourDigitSnapshot() throws Exception {
final String versionString = "1.2.3.4-snapshot";
Version test = new Version(versionString);
assertThat(test.getMajor(), is(1));
assertThat(test.getMinor(), is(2));
assertThat(test.getMicro(), is(3));
assertThat(test.getStatusVersion(), is(4));
assertThat(test.getStatus(), is(ReleaseStatus.Snapshot));
assertThat(test.getVersionString(),is(versionString.toUpperCase()));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment