Permalink
Browse files

Fix TISTUD-3447 The string '24-cr' in version '3.0.24-cr' for CLI thr…

…ows error when we run diagnostic test from studio

      - Use VersionUtil.parseVersion to be more forgiving of various version formats
      - Add more logic to VersionUtil.parseVersion to handle more version string types.
      - Add unit tests to verify a number of version parsing cases.
  • Loading branch information...
1 parent 4052684 commit 58bbe49e7ba181857963ac9d9ae475db5e67e530 @sgtcoolguy sgtcoolguy committed Feb 14, 2013
@@ -26,7 +26,7 @@
public final class VersionUtil
{
// Match x.y and x.y.z
- private static final String VERSION_SPLIT_PATTERN = "(\\d+)\\.(\\d+)(\\.(\\d+))?"; //$NON-NLS-1$
+ private static final String VERSION_SPLIT_PATTERN = "(\\d+)\\.(\\d+)(([a-zA-Z0-9_\\-]+)|(\\.(\\d+)(\\.?[a-zA-Z0-9_\\-]+)?))?"; //$NON-NLS-1$
// Match any dot separated string
private static Pattern VERSION_DOT_PATTERN = Pattern.compile("\\."); //$NON-NLS-1$
@@ -113,15 +113,46 @@ public static int compareVersions(String left, String right, boolean isStrict)
* Parse the raw output and return a {@link Version} instance out of it.
*
* @param rawOutput
- * @return A {@link Version} instance. Null if the output did not contain a parsable version number.
+ * @return A {@link Version} instance. Null if the output did not contain a parseable version number.
*/
public static Version parseVersion(String rawOutput)
{
Pattern pattern = Pattern.compile(VERSION_SPLIT_PATTERN);
Matcher matcher = pattern.matcher(rawOutput);
if (matcher.find())
{
- String version = matcher.group();
+ String major = matcher.group(1);
+ String minor = matcher.group(2);
+ String micro;
+ String qualifier;
+ if (matcher.group(5) != null)
+ {
+ // We have 3 parts with an optional qualifier
+ micro = matcher.group(6);
+ qualifier = matcher.group(7);
+ }
+ else
+ { // We have a major and minor with optional qualifier
+ qualifier = matcher.group(4);
+ micro = "0"; //$NON-NLS-1$
+ }
+ String version = major + '.' + minor + '.' + micro;
+ if (!StringUtil.isEmpty(qualifier))
+ {
+ char c = qualifier.charAt(0);
+ switch (c)
+ {
+ case '-':
+ case '_':
+ case '.':
+ qualifier = qualifier.substring(1);
+ break;
+
+ default:
+ break;
+ }
+ version = version + '.' + qualifier;
+ }
try
{
return Version.parseVersion(version);
@@ -36,6 +36,7 @@
import com.aptana.core.util.ProcessUtil;
import com.aptana.core.util.StringUtil;
import com.aptana.core.util.SudoCommandProcessRunnable;
+import com.aptana.core.util.VersionUtil;
import com.aptana.js.core.JSCorePlugin;
import com.aptana.js.core.node.INodePackageManager;
@@ -350,7 +351,7 @@ public Version getInstalledVersion(String packageName) throws CoreException
{
output = output.substring(0, space);
}
- return Version.parseVersion(output);
+ return VersionUtil.parseVersion(output);
}
return null;
}
@@ -376,7 +377,7 @@ public Version getLatestVersionAvailable(String packageName) throws CoreExceptio
Matcher m = VERSION_PATTERN.matcher(message);
if (m.find())
{
- return Version.parseVersion(m.group(1));
+ return VersionUtil.parseVersion(m.group(1));
}
return null;
}
@@ -1,6 +1,6 @@
/**
* Aptana Studio
- * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
+ * Copyright (c) 2005-2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
* Please see the license.html included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
@@ -9,9 +9,64 @@
import junit.framework.TestCase;
+import org.osgi.framework.Version;
+
public class VersionUtilTest extends TestCase
{
+ public void testParseVersionQualifierSeparatedByHyphen()
+ {
+ assertVersion(3, 0, 24, "cr", VersionUtil.parseVersion("3.0.24-cr"));
+ }
+
+ public void testParseVersionWithMajorMinorMicro()
+ {
+ assertVersion(3, 0, 24, VersionUtil.parseVersion("3.0.24"));
+ assertVersion(1, 12, 127, VersionUtil.parseVersion("1.12.127"));
+ }
+
+ public void testParseVersionWithMajorMinorMicroQualifier()
+ {
+ assertVersion(3, 0, 0, "GA", VersionUtil.parseVersion("3.0.0.GA"));
+ // Eclipse-style version #s
+ assertVersion(1, 3, 0, "v20100106-170", VersionUtil.parseVersion("1.3.0.v20100106-170"));
+ assertVersion(1, 2, 3, "1000a", VersionUtil.parseVersion("1.2.3.1000a"));
+ }
+
+ public void testParseVersionWithMajorMinorMicroQualifierWithNoLeadingSeparator()
+ {
+ assertVersion(3, 0, 0, "GA", VersionUtil.parseVersion("3.0.0GA"));
+ assertVersion(1, 2, 1, "b1", VersionUtil.parseVersion("1.2.1b1"));
+ }
+
+ public void testParseVersionWithMajorMinor()
+ {
+ assertVersion(2, 0, 0, VersionUtil.parseVersion("2.0"));
+ assertVersion(2, 10, 0, VersionUtil.parseVersion("2.10"));
+ }
+
+ public void testParseVersionWithMajorMinorQualifierWithNoSeparator()
+ {
+ assertVersion(2, 1, 0, "a", VersionUtil.parseVersion("2.1a"));
+ }
+
+ private void assertVersion(int major, int minor, int micro, Version v)
+ {
+ assertVersion(major, minor, micro, null, v);
+ }
+
+ private void assertVersion(int major, int minor, int micro, String qualifier, Version v)
+ {
+ assertNotNull(v);
+ assertEquals(major, v.getMajor());
+ assertEquals(minor, v.getMinor());
+ assertEquals(micro, v.getMicro());
+ if (qualifier != null)
+ {
+ assertEquals(qualifier, v.getQualifier());
+ }
+ }
+
public void testCompareVersions()
{
assertTrue(VersionUtil.compareVersions("2.0", "1.0") > 0);

0 comments on commit 58bbe49

Please sign in to comment.