Index: src/main/groovy/lang/GroovySystem.java =================================================================== --- src/main/groovy/lang/GroovySystem.java (revision 18415) +++ src/main/groovy/lang/GroovySystem.java (working copy) @@ -81,9 +81,9 @@ } /** - * Returns the groovy version + * Returns the groovy version string */ public static String getVersion() { - return ReleaseInfo.getVersion(); + return ReleaseInfo.getVersion().toString(); } } Index: src/main/org/codehaus/groovy/runtime/InvokerHelper.java =================================================================== --- src/main/org/codehaus/groovy/runtime/InvokerHelper.java (revision 18400) +++ src/main/org/codehaus/groovy/runtime/InvokerHelper.java (working copy) @@ -450,7 +450,7 @@ * @deprecated Use GroovySystem version instead. */ public static String getVersion() { - return ReleaseInfo.getVersion(); + return GroovySystem.getVersion(); } /** Index: src/main/org/codehaus/groovy/util/GroovyVersion.java =================================================================== --- src/main/org/codehaus/groovy/util/GroovyVersion.java (revision 0) +++ src/main/org/codehaus/groovy/util/GroovyVersion.java (revision 0) @@ -0,0 +1,87 @@ +package org.codehaus.groovy.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Groovy version information + * + * @author Roshan Dawrani + */ +public class GroovyVersion implements Comparable { + public static final String GROOVY_VERSION_FORMAT = "(\\d+)\\.{1}(\\d+)(?:\\.{1}(\\d+))?(?:\\-{1}(.*))?"; + + private final String version; + + private String major = ""; + private String minor = ""; + private String micro = ""; + private String qualifier = ""; + + boolean parsed = false; + + public GroovyVersion(String ver) { + version = (ver != null) ? ver : ""; + } + + public String getMajor() { + parse(); + return major != null ? major : ""; + } + + public String getMinor() { + parse(); + return minor != null ? minor : ""; + } + + public String getMicro() { + parse(); + return micro != null ? micro : ""; + } + + public String getQualifier() { + parse(); + return qualifier != null ? qualifier : ""; + } + + public String toString() { + return version; + } + + public int compareTo(GroovyVersion that) { + if(that == null) return -1; + return this.toString().compareTo(that.toString()); + } + + public boolean equals(Object that) { + if (that instanceof GroovyVersion) { + return equals((GroovyVersion) that); + } + return false; + } + + public boolean equals(GroovyVersion that) { + return toString().equals(that.toString()); + } + + public int hashCode() { + return 37 + toString().hashCode(); + } + + private void parse() { + if(!parsed) { + parsed = true; + if(version == null || version.length() == 0) return; + Pattern versionPattern = Pattern.compile(GROOVY_VERSION_FORMAT); + Matcher m = versionPattern.matcher(version); + if (!m.find()) { + throw new IllegalArgumentException("Groovy version must be in form .[.][-]"); + } else { + major = m.group(1); + minor = m.group(2); + micro = m.group(3); + qualifier = m.group(4); + } + } + } +} Index: src/main/org/codehaus/groovy/util/ReleaseInfo.java =================================================================== --- src/main/org/codehaus/groovy/util/ReleaseInfo.java (revision 18415) +++ src/main/org/codehaus/groovy/util/ReleaseInfo.java (working copy) @@ -20,8 +20,8 @@ private static String KEY_BUILD_TIME = "BuildTime"; private static boolean loaded = false; - public static String getVersion() { - return get(KEY_IMPLEMENTATION_VERSION); + public static GroovyVersion getVersion() { + return new GroovyVersion(get(KEY_IMPLEMENTATION_VERSION)); } public static Properties getAllProperties() { Index: src/test/groovy/bugs/Groovy3884Bug.groovy =================================================================== --- src/test/groovy/bugs/Groovy3884Bug.groovy (revision 0) +++ src/test/groovy/bugs/Groovy3884Bug.groovy (revision 0) @@ -0,0 +1,59 @@ +package groovy.bugs + +import org.codehaus.groovy.util.GroovyVersion + +class Groovy3884Bug extends GroovyTestCase { + void testEmptyGroovyVersion() { + def ver = new GroovyVersion("") + assert ver.major == '' + assert ver.minor == '' + assert ver.micro == '' + assert ver.qualifier == '' + } + + void testNullGroovyVersion() { + def ver = new GroovyVersion(null) + assert ver.major == '' + assert ver.minor == '' + assert ver.micro == '' + assert ver.qualifier == '' + } + + void testGroovyVersionString1() { + def ver = new GroovyVersion("1.7-rc-1-SNAPSHOT") + assert ver.major == '1' + assert ver.minor == '7' + assert ver.micro == '' + assert ver.qualifier == 'rc-1-SNAPSHOT' + } + + void testGroovyVersionString2() { + def ver = new GroovyVersion("1.6.2") + assert ver.major == '1' + assert ver.minor == '6' + assert ver.micro == '2' + assert ver.qualifier == '' + } + + void testGroovyVersionString3() { + def ver = new GroovyVersion("1.5.3-SNAPSHOT") + assert ver.major == '1' + assert ver.minor == '5' + assert ver.micro == '3' + assert ver.qualifier == 'SNAPSHOT' + } + + void testGroovyVersionsComparsion1() { + def ver1 = new GroovyVersion("1.5.3-SNAPSHOT") + def ver2 = new GroovyVersion("1.5.3-SNAPSHOT") + assert ver1.compareTo(ver2) == 0 + } + + void testGroovyVersionsComparsion2() { + def list = [] + def ver1 = new GroovyVersion("1.5.3-SNAPSHOT") + list << ver1 + def ver2 = new GroovyVersion("1.5.3-SNAPSHOT") + assert list.contains(ver2) + } +}