Skip to content

Commit

Permalink
TRUNK-3751 - new method: checkForAlphaVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
andreapat committed Apr 12, 2013
1 parent 2ebf5be commit 922be64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 60 deletions.
70 changes: 22 additions & 48 deletions api/src/main/java/org/openmrs/module/ModuleUtil.java
Expand Up @@ -37,8 +37,8 @@
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
Expand All @@ -50,7 +50,6 @@
import org.openmrs.api.context.ServiceContext;
import org.openmrs.util.OpenmrsClassLoader;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.util.Version;
import org.springframework.context.support.AbstractRefreshableApplicationContext;

/**
Expand Down Expand Up @@ -246,7 +245,6 @@ 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 @@ -279,25 +277,12 @@ public static boolean matchRequiredVersions(String version, String value) {
* version
* @should throw ModuleException if single entry required version beyond openmrs version
* @should throw ModuleException if SNAPSHOT not handled correctly
* @Should treat SNAPSHOT version as a single value
*/
public static void checkRequiredVersion(String version, String value) throws ModuleException {
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 = "-";
// 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 (value.indexOf("*") > 0 || value.indexOf(separator) > 0) {
// if it contains "*" or "-" then we must separate those two
// assume it's always going to be two part
// assign the upper and lower bound
Expand All @@ -314,6 +299,7 @@ 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 @@ -363,9 +349,8 @@ 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");
String alphaVersion = "";
String alphaValue = "";

try {
if (version == null || value == null)
Expand All @@ -374,22 +359,16 @@ public static int compareVersion(String version, String value) {
List<String> versions = new Vector<String>();
List<String> values = new Vector<String>();

//
// treat "-SNAPSHOT" as the lowest possible version
// e.g. 1.8.4-SNAPSHOT is really 1.8.4.0
//version = version.replace("-SNAPSHOT", ".0");
//value = value.replace("-SNAPSHOT", ".0");

// if alpha version replace -ALPHA with ".0" as above
alphaVersion = fixAlphaVersion(version);
if (alphaVersion != null) {
version.replace(alphaVersion, ".0");
// treat alpha version (i.e. "-SNAPSHOT") as the lowest possible version
// e.g. 1.8.4-SNAPSHOT is really 1.8.4.0
if (checkForAlphaVersion(version)) {
alphaVersion = version.substring(version.indexOf('-'));
version = version.replace(alphaVersion, ".0");
}

// if alpha value replace -ALPHA with ".0" as above
alphaValue = fixAlphaVersion(value);
if (alphaValue != null) {
version.replace(alphaValue, ".0");
if (checkForAlphaVersion(value)) {
alphaValue = value.substring(value.indexOf('-'));
value = value.replace(alphaValue, ".0");
}

Collections.addAll(versions, version.split("\\."));
Expand Down Expand Up @@ -422,21 +401,16 @@ public static int compareVersion(String version, String value) {
return 0;
}

// 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;
// Check for alpha version (i.e 1.9.2-SNAPSHOT ect)
public static boolean checkForAlphaVersion(String version) {
boolean isAlphaVersion = false;

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);
}
Matcher matcher = Pattern.compile("(\\d+)\\.(\\d+)(\\.(\\d+))?(\\-([A-Z]+))").matcher(version);
if (matcher.matches()) {
// Matches
isAlphaVersion = true;
}
return (subStr);
return (isAlphaVersion);
}

/**
Expand Down Expand Up @@ -1122,5 +1096,5 @@ public static Collection<String> getPackagesFromFile(File file) {

return packagesProvided;
}
// New code here

}
12 changes: 0 additions & 12 deletions api/src/test/java/org/openmrs/module/ModuleUtilTest.java
Expand Up @@ -486,16 +486,4 @@ public void checkRequiredVersion_shouldThrowModuleExceptionIfSNAPSHOTNotHandledC
ModuleUtil.checkRequiredVersion(openmrsVersion, requiredOpenmrsVersion);
}

/**
* @see {@link ModuleUtil#checkRequiredVersion(String, String)}
*/
@Test
@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";
ModuleUtil.checkRequiredVersion(openMRSVersion, valueConfigXml);

This comment has been minimized.

Copy link
@wluyima

wluyima Apr 12, 2013

Member

What is the use of this line?

This comment has been minimized.

Copy link
@wluyima

wluyima Apr 12, 2013

Member

Ooops! I guess i should be asking, why are you removing the test and not adding new ones for all the scenarios you fixed in your code

Assert.assertEquals(openMRSVersion, valueConfigXml);
}

}

0 comments on commit 922be64

Please sign in to comment.