From c16a581e1f53f81887482e63308c7e982ddd9ce9 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 19 Mar 2012 15:10:04 -0400 Subject: [PATCH] Now supporting 'x.y.z-alpha.n' type versions --- .../main/java/org/jclouds/JcloudsVersion.java | 35 +++++++++++++++---- .../java/org/jclouds/JcloudsVersionTest.java | 33 ++++++++++++----- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/jclouds/JcloudsVersion.java b/core/src/main/java/org/jclouds/JcloudsVersion.java index 42101066ba..6aaf24426f 100644 --- a/core/src/main/java/org/jclouds/JcloudsVersion.java +++ b/core/src/main/java/org/jclouds/JcloudsVersion.java @@ -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"); } diff --git a/core/src/test/java/org/jclouds/JcloudsVersionTest.java b/core/src/test/java/org/jclouds/JcloudsVersionTest.java index 18a4bc6c0b..9f7bac8c72 100644 --- a/core/src/test/java/org/jclouds/JcloudsVersionTest.java +++ b/core/src/test/java/org/jclouds/JcloudsVersionTest.java @@ -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 resourcesToHide;