LANG-1102: Make logic for comparing OS versions in SystemUtils smarter

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1669750 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2015-03-28 12:08:17 +00:00
parent 1dc65ff986
commit 11c0df1d9e
3 changed files with 67 additions and 1 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
<action issue="LANG-1102" type="update" dev="britter">Make logic for comparing OS versions in SystemUtils smarter</action>
<action issue="LANG-1091" type="update" dev="britter" due-to="Fabian Lange">Shutdown thread pools in test cases</action>
<action issue="LANG-1101" type="update" dev="chas">FastDateParser and FastDatePrinter support 'X' format</action>
<action issue="LANG-1100" type="update" dev="chas" due-to="mbracher">Avoid memory allocation when using date formating to StringBuffer</action>

View File

@ -1448,7 +1448,7 @@ static boolean isOSMatch(final String osName, final String osVersion, final Stri
if (osName == null || osVersion == null) {
return false;
}
return isOSNameMatch(osName, osNamePrefix) && osVersion.startsWith(osVersionPrefix);
return isOSNameMatch(osName, osNamePrefix) && isOSVersionMatch(osVersion, osVersionPrefix);
}
/**
@ -1467,6 +1467,32 @@ static boolean isOSNameMatch(final String osName, final String osNamePrefix) {
}
return osName.startsWith(osNamePrefix);
}
/**
* Decides if the operating system version matches.
* <p>
* This method is package private instead of private to support unit test invocation.
* </p>
*
* @param osVersion the actual OS version
* @param osVersionPrefix the prefix for the expected OS version
* @return true if matches, or false if not or can't determine
*/
static boolean isOSVersionMatch(final String osVersion, final String osVersionPrefix) {
if (StringUtils.isEmpty(osVersion)) {
return false;
}
// Compare parts of the version string instead of using String.startsWith(String) because otherwise
// osVersionPrefix 10.1 would also match osVersion 10.10
String[] versionPrefixParts = osVersionPrefix.split("\\.");
String[] versionParts = osVersion.split("\\.");
for (int i = 0; i < Math.min(versionPrefixParts.length, versionParts.length); i++) {
if (!versionPrefixParts[i].equals(versionParts[i])) {
return false;
}
}
return true;
}
// -----------------------------------------------------------------------
/**

View File

@ -377,6 +377,45 @@ public void testOSMatchesNameAndVersion() {
assertFalse(SystemUtils.isOSMatch(osName, osVersion, "Windows 9", "4.1"));
}
@Test
public void testOsVersionMatches() throws Exception {
String osVersion = null;
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.1"));
osVersion = "";
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.1"));
osVersion = "10";
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.1"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.1.1"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.10"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.10.1"));
osVersion = "10.1";
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.1"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.1.1"));
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.10"));
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.10.1"));
osVersion = "10.1.1";
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.1"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.1.1"));
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.10"));
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.10.1"));
osVersion = "10.10";
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.1"));
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.1.1"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.10"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.10.1"));
osVersion = "10.10.1";
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.1"));
assertFalse(SystemUtils.isOSVersionMatch(osVersion, "10.1.1"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.10"));
assertTrue(SystemUtils.isOSVersionMatch(osVersion, "10.10.1"));
}
@Test
public void testJavaAwtHeadless() {
final boolean atLeastJava14 = SystemUtils.isJavaVersionAtLeast(JAVA_1_4);