Author: britter
Date: Sat Mar 28 12:08:17 2015
New Revision: 1669750

URL: http://svn.apache.org/r1669750
Log:
LANG-1102: Make logic for comparing OS versions in SystemUtils smarter

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SystemUtils.java
    
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1669750&r1=1669749&r2=1669750&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Sat Mar 28 
12:08:17 2015
@@ -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>

Modified: 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SystemUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SystemUtils.java?rev=1669750&r1=1669749&r2=1669750&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SystemUtils.java
 (original)
+++ 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/SystemUtils.java
 Sat Mar 28 12:08:17 2015
@@ -1448,7 +1448,7 @@ public class SystemUtils {
         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 @@ public class SystemUtils {
         }
         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;
+    }
 
     // -----------------------------------------------------------------------
     /**

Modified: 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java?rev=1669750&r1=1669749&r2=1669750&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
 (original)
+++ 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
 Sat Mar 28 12:08:17 2015
@@ -378,6 +378,45 @@ public class SystemUtilsTest {
     }
 
     @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);
         final String expectedStringValue = 
System.getProperty("java.awt.headless");


Reply via email to