Now supporting 'x.y.z-alpha.n' type versions

This commit is contained in:
Andrew Phillips 2012-03-19 15:10:04 -04:00
parent afb67793e8
commit c16a581e1f
2 changed files with 53 additions and 15 deletions

View File

@ -39,15 +39,22 @@ public class JcloudsVersion {
static final String VERSION_RESOURCE_FILE = "META-INF/maven/org.jclouds/jclouds-core/pom.properties";
private static final String VERSION_PROPERTY_NAME = "version";
// x.y.z or x.y.z-rc.n, optionally with -SNAPSHOT suffix - see http://semver.org
// x.y.z or x.y.z-alpha.n or x.y.z-rc.n or x.y.z-SNAPSHOT - see http://semver.org
private static final Pattern SEMANTIC_VERSION_PATTERN =
Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(?:-rc\\.(\\d+))?(?:-SNAPSHOT)?");
Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(?:-(alpha|rc)\\.(\\d+)|-SNAPSHOT)?");
private static final String ALPHA_VERSION_IDENTIFIER = "alpha";
private static final JcloudsVersion INSTANCE = new JcloudsVersion();
public final int majorVersion;
public final int minorVersion;
public final int patchVersion;
public final boolean alpha;
/**
* Non-null iff {@link #alpha} is {@code true}
*/
public final @Nullable Integer alphaVersion;
public final boolean releaseCandidate;
/**
@ -83,18 +90,32 @@ public class JcloudsVersion {
checkArgument(versionMatcher.matches(), "Version '%s' did not match expected pattern '%s'",
version, SEMANTIC_VERSION_PATTERN);
this.version = version;
// a match will produce three or four matching groups (release candidate version optional)
// a match will produce three or five matching groups (alpha/release candidate identifier and version optional)
majorVersion = Integer.valueOf(versionMatcher.group(1));
minorVersion = Integer.valueOf(versionMatcher.group(2));
patchVersion = Integer.valueOf(versionMatcher.group(3));
String releaseCandidateVersionIfPresent = versionMatcher.group(4);
if (releaseCandidateVersionIfPresent != null) {
releaseCandidate = true;
releaseCandidateVersion = Integer.valueOf(releaseCandidateVersionIfPresent);
String alphaOrReleaseCandidateVersionIfPresent = versionMatcher.group(4);
if (alphaOrReleaseCandidateVersionIfPresent != null) {
Integer alphaOrReleaseCandidateVersion = Integer.valueOf(versionMatcher.group(5));
if (alphaOrReleaseCandidateVersionIfPresent.equals(ALPHA_VERSION_IDENTIFIER)) {
alpha = true;
alphaVersion = alphaOrReleaseCandidateVersion;
releaseCandidate = false;
releaseCandidateVersion = null;
} else {
alpha = false;
alphaVersion = null;
releaseCandidate = true;
releaseCandidateVersion = alphaOrReleaseCandidateVersion;
}
} else {
alpha = false;
alphaVersion = null;
releaseCandidate = false;
releaseCandidateVersion = null;
}
// endsWith("T") would be cheaper but we only do this once...
snapshot = version.endsWith("-SNAPSHOT");
}

View File

@ -55,6 +55,16 @@ public class JcloudsVersionTest {
new JcloudsVersion("1.2.3-rc-4");
}
@Test(expectedExceptions = { IllegalArgumentException.class })
public void testFailsIfAlphaSnapshot() {
new JcloudsVersion("1.2.3-alpha.5-SNAPSHOT");
}
@Test(expectedExceptions = { IllegalArgumentException.class })
public void testFailsIfReleaseCandidateSnapshot() {
new JcloudsVersion("1.2.3-rc.4-SNAPSHOT");
}
@Test
public void testExtractsVersionFromResourceFile() {
JcloudsVersion version = new JcloudsVersion();
@ -82,12 +92,26 @@ public class JcloudsVersionTest {
}
@Test
public void testSupportsNonReleaseCandidate() {
public void testSupportsReleaseVersion() {
JcloudsVersion version = new JcloudsVersion("1.2.3");
assertFalse(version.alpha, "Expected non-alpha");
assertNull(version.alphaVersion);
assertFalse(version.releaseCandidate, "Expected non-release candidate");
assertNull(version.releaseCandidateVersion);
}
@Test
public void testRecognisesAlpha() {
JcloudsVersion version = new JcloudsVersion("1.2.3-alpha.5");
assertTrue(version.alpha, "Expected alpha");
}
@Test
public void testExtractsAlphaVersion() {
JcloudsVersion version = new JcloudsVersion("1.2.3-alpha.5");
assertEquals(Integer.valueOf(5), version.alphaVersion);
}
@Test
public void testRecognisesReleaseCandidate() {
JcloudsVersion version = new JcloudsVersion("1.2.3-rc.4");
@ -100,13 +124,6 @@ public class JcloudsVersionTest {
assertEquals(Integer.valueOf(4), version.releaseCandidateVersion);
}
@Test
public void testRecognisesReleaseCandidateSnapshot() {
JcloudsVersion version = new JcloudsVersion("1.2.3-rc.4-SNAPSHOT");
assertTrue(version.releaseCandidate, "Expected release candidate");
assertTrue(version.snapshot, "Expected snapshot");
}
private static class ResourceHidingClassLoader extends ClassLoader {
private final ClassLoader delegate;
private final List<String> resourcesToHide;