Merge pull request #473 from jclouds/support-alpha-versions

Now supporting 'x.y.z-alpha.n' type versions
This commit is contained in:
Adrian Cole 2012-03-19 12:15:54 -07:00
commit ea382b9f9e
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"; static final String VERSION_RESOURCE_FILE = "META-INF/maven/org.jclouds/jclouds-core/pom.properties";
private static final String VERSION_PROPERTY_NAME = "version"; 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 = 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(); private static final JcloudsVersion INSTANCE = new JcloudsVersion();
public final int majorVersion; public final int majorVersion;
public final int minorVersion; public final int minorVersion;
public final int patchVersion; 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; public final boolean releaseCandidate;
/** /**
@ -83,18 +90,32 @@ public class JcloudsVersion {
checkArgument(versionMatcher.matches(), "Version '%s' did not match expected pattern '%s'", checkArgument(versionMatcher.matches(), "Version '%s' did not match expected pattern '%s'",
version, SEMANTIC_VERSION_PATTERN); version, SEMANTIC_VERSION_PATTERN);
this.version = version; 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)); majorVersion = Integer.valueOf(versionMatcher.group(1));
minorVersion = Integer.valueOf(versionMatcher.group(2)); minorVersion = Integer.valueOf(versionMatcher.group(2));
patchVersion = Integer.valueOf(versionMatcher.group(3)); patchVersion = Integer.valueOf(versionMatcher.group(3));
String releaseCandidateVersionIfPresent = versionMatcher.group(4);
if (releaseCandidateVersionIfPresent != null) { String alphaOrReleaseCandidateVersionIfPresent = versionMatcher.group(4);
releaseCandidate = true; if (alphaOrReleaseCandidateVersionIfPresent != null) {
releaseCandidateVersion = Integer.valueOf(releaseCandidateVersionIfPresent); 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 { } else {
alpha = false;
alphaVersion = null;
releaseCandidate = false; releaseCandidate = false;
releaseCandidateVersion = null; releaseCandidateVersion = null;
} }
// endsWith("T") would be cheaper but we only do this once... // endsWith("T") would be cheaper but we only do this once...
snapshot = version.endsWith("-SNAPSHOT"); snapshot = version.endsWith("-SNAPSHOT");
} }

View File

@ -55,6 +55,16 @@ public class JcloudsVersionTest {
new JcloudsVersion("1.2.3-rc-4"); 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 @Test
public void testExtractsVersionFromResourceFile() { public void testExtractsVersionFromResourceFile() {
JcloudsVersion version = new JcloudsVersion(); JcloudsVersion version = new JcloudsVersion();
@ -82,12 +92,26 @@ public class JcloudsVersionTest {
} }
@Test @Test
public void testSupportsNonReleaseCandidate() { public void testSupportsReleaseVersion() {
JcloudsVersion version = new JcloudsVersion("1.2.3"); JcloudsVersion version = new JcloudsVersion("1.2.3");
assertFalse(version.alpha, "Expected non-alpha");
assertNull(version.alphaVersion);
assertFalse(version.releaseCandidate, "Expected non-release candidate"); assertFalse(version.releaseCandidate, "Expected non-release candidate");
assertNull(version.releaseCandidateVersion); 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 @Test
public void testRecognisesReleaseCandidate() { public void testRecognisesReleaseCandidate() {
JcloudsVersion version = new JcloudsVersion("1.2.3-rc.4"); JcloudsVersion version = new JcloudsVersion("1.2.3-rc.4");
@ -100,13 +124,6 @@ public class JcloudsVersionTest {
assertEquals(Integer.valueOf(4), version.releaseCandidateVersion); 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 static class ResourceHidingClassLoader extends ClassLoader {
private final ClassLoader delegate; private final ClassLoader delegate;
private final List<String> resourcesToHide; private final List<String> resourcesToHide;