Skip to content

Commit

Permalink
TRUNK-3751 new method: fixAlphaVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
andreapat committed Apr 10, 2013
1 parent cbf3d09 commit 2ebf5be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
74 changes: 41 additions & 33 deletions api/src/main/java/org/openmrs/module/ModuleUtil.java
Expand Up @@ -246,6 +246,7 @@ public static boolean matchRequiredVersions(String version, String value) {
* If "value" is not within range specified by "version", then a ModuleException will be thrown.
* Otherwise, just return true at last.
*/

checkRequiredVersion(version, value);
return true;
}
Expand Down Expand Up @@ -281,14 +282,21 @@ public static boolean matchRequiredVersions(String version, String value) {
* @Should treat SNAPSHOT version as a single value
*/
public static void checkRequiredVersion(String version, String value) throws ModuleException {
String snapshot = "SNAPSHOT";
String alphaValue = null;

alphaValue = fixAlphaVersion(value);

log.debug("***** In checkRequiredVersion ***\n");
log.debug("***** Version = " + version + "***\n");
log.debug("***** Value = " + value + "***\n");
if (value != null && !value.equals("")) {

// need to externalize this string
String separator = "-";

if ((value.indexOf("*") > 0 || value.indexOf(separator) > 0)
&& (!StringUtils.containsIgnoreCase(value, snapshot))) {
// no alpha value
if ((value.indexOf("*") > 0 || value.indexOf(separator) > 0) && (alphaValue == null)) {
//&& (!StringUtils.containsIgnoreCase(value, "SNAPSHOT"))) {

// if it a snapshot (1.9.2-SNAPSHOT) treat as a single value
// if it contains "*" or "-" then we must separate those two
// assume it's always going to be two part
Expand All @@ -306,7 +314,6 @@ public static void checkRequiredVersion(String version, String value) throws Mod
break;
indexOfSeparator = value.indexOf(separator, indexOfSeparator + 1);
}

// only preserve part of the string that match the following format:
// - xx.yy.*
// - xx.yy.zz*
Expand Down Expand Up @@ -356,6 +363,10 @@ public static void checkRequiredVersion(String version, String value) throws Mod
* @should treat SNAPSHOT as earliest version
*/
public static int compareVersion(String version, String value) {
String alphaVersion, alphaValue = null;

log.debug("***** In compareVersion ***\n");

try {
if (version == null || value == null)
return 0;
Expand All @@ -367,26 +378,20 @@ public static int compareVersion(String version, String value) {
// treat "-SNAPSHOT" as the lowest possible version
// e.g. 1.8.4-SNAPSHOT is really 1.8.4.0
//version = version.replace("-SNAPSHOT", ".0");
version = version.replace("-([^a-zA-Z]+)", ".0");
//value = value.replace("-SNAPSHOT", ".0");
value = value.replace("-([^a-zA-Z]+)", ".0");

String[] splitVersion;
Matcher matcher = Pattern.compile("(.+)-([^a-zA-Z].*)").matcher(version);
if (matcher.matches()) {
splitVersion = version.split("-");
String newVersion = splitVersion[0] + ".0";
version.replace(version, newVersion);

// if alpha version replace -ALPHA with ".0" as above
alphaVersion = fixAlphaVersion(version);
if (alphaVersion != null) {
version.replace(alphaVersion, ".0");
}

String[] splitValue;
Matcher matcher2 = Pattern.compile("(.+)-([^a-zA-Z].*)").matcher(value);
if (matcher2.matches()) {
splitValue = value.split("-");
String newValue = splitValue[0] + ".0";
version.replace(value, newValue);
// if alpha value replace -ALPHA with ".0" as above
alphaValue = fixAlphaVersion(value);
if (alphaValue != null) {
version.replace(alphaValue, ".0");
}

Collections.addAll(versions, version.split("\\."));
Collections.addAll(values, value.split("\\."));

Expand Down Expand Up @@ -417,20 +422,23 @@ public static int compareVersion(String version, String value) {
return 0;
}

/*
public static int compareVersion(String version, String value) {
try {
int returnValue = new Version(version).compareTo(new Version(value));
return(returnValue);
}
catch (NumberFormatException e) {
log.error("Error while converting a version/value to an integer: " + version + "/" + value, e);
// Returns substring to be replaced in compareVersion
public static String fixAlphaVersion(String version) {
Pattern re = Pattern.compile("(.+)-([^a-zA-Z].*)");
String[] versionParts = re.split(version);
String subStr = null;

for (int i = 0; i < versionParts.length; i++) {
if (versionParts.length > 0) {
String str = versionParts[0];
int index1 = str.indexOf('-');
// Alpha values like "-SNAPSHOT" - will contain '-' followed by alpha version
subStr = str.substring(index1);
}
// default return value if an error occurs or elements are equal
return 0;
}
*/
return (subStr);
}

/**
* Gets the folder where modules are stored. ModuleExceptions are thrown on errors
*
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/java/org/openmrs/module/ModuleUtilTest.java
Expand Up @@ -490,7 +490,7 @@ public void checkRequiredVersion_shouldThrowModuleExceptionIfSNAPSHOTNotHandledC
* @see {@link ModuleUtil#checkRequiredVersion(String, String)}
*/
@Test
@Verifies(value = "Should treat SNAPSHOT version as a single value", method = "checkRequiredVersion(String, String)")
@Verifies(value = "Should handle SNAPSHOT version ", method = "checkRequiredVersion(String, String)")
public void checkRequiredVersion_shouldHandleSNAPSHOTVersion() throws Exception {
String openMRSVersion = "1.9.2-SNAPSHOT";
String valueConfigXml = "1.9.2-SNAPSHOT";
Expand Down

0 comments on commit 2ebf5be

Please sign in to comment.