Anonymous avatar Anonymous committed 0527e4d

Move is4947220Fixed into ProcessUtils and add tests for its behaviour.

Comments (0)

Files changed (3)

src/main/java/com/atlassian/utils/process/ExternalProcessImpl.java

         return OS_NAME.contains("windows");
     }
 
-    /**
-     *
-     * @return true iff this JVM has a fix for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4947220.
-     * According to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4947220 &
-     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2213209
-     * Sun/Oracle JVM versions 7 & 6u30(and above) do not suffer from 4947220.
-     */
-    private boolean is4947220Fixed() {
-        final String jvmVersion = System.getProperty("java.version");
-        final String jvmVendor = System.getProperty("java.vendor");
-        int minorVersion = -1;
-        double majorVersion = -1.0;
-        if (jvmVersion.split("_").length == 2) {
-            try {
-                majorVersion = Double.parseDouble(jvmVersion.substring(0, 3));
-                minorVersion = Integer.parseInt(jvmVersion.split("_")[1]);
-            }
-            catch (NumberFormatException e) {
-                LOG.error("Failed to parse the JVM version", e);
-            }
-        }
-        for (String vendor : Arrays.asList("sun", "oracle", "openjdk")) {
-            if (jvmVendor.toLowerCase(Locale.US).contains(vendor)) {
-                if (majorVersion == 1.6 && minorVersion < 30) {
-                    return false;
-                }
-                if (majorVersion < 1.6) {
-                    return false;
-                }
-            }
-        }
-        return true;
-    }
-
     private String quoteString(String value) {
         StringBuilder builder = new StringBuilder()
                 .append("\"")
 
     protected Process createProcess(List<String> command, Map<String, String> environment, File workingDir)
             throws IOException {
-        if (!suppressSpecialWindowsBehaviour && isWindows() && !is4947220Fixed()) {
+        if (!suppressSpecialWindowsBehaviour && isWindows() && !ProcessUtils.is4947220Fixed()) {
             return createWinProcess(command, environment, workingDir);
         } else {
             ProcessBuilder builder = new ProcessBuilder(command).directory(workingDir);

src/main/java/com/atlassian/utils/process/ProcessUtils.java

 package com.atlassian.utils.process;
 
+import org.apache.log4j.Logger;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Locale;
 
 /**
  */
 public class ProcessUtils {
+    private static final Logger log = Logger.getLogger(ProcessUtils.class);
     private ProcessUtils() {
     }
 
     public static List<String> tokenizeAsList(String commandLine) {
         return new ArrayList<String>(Arrays.asList(tokenizeCommand(commandLine)));
     }
+
+    /**
+     *
+     * @return true iff this JVM has a fix for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4947220.
+     * According to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4947220 &
+     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2213209
+     * Sun/Oracle JVM versions 7 & 6u30(and above) do not suffer from 4947220.
+     */
+    public static boolean is4947220Fixed() {
+        final String jvmVersion = System.getProperty("java.version");
+        final String jvmVendor = System.getProperty("java.vendor");
+        int minorVersion = -1;
+        double majorVersion = -1.0;
+        if (jvmVersion.split("_").length == 2) {
+            try {
+                majorVersion = Double.parseDouble(jvmVersion.substring(0, 3));
+                minorVersion = Integer.parseInt(jvmVersion.split("_")[1]);
+            }
+            catch (NumberFormatException e) {
+                log.error("Failed to parse the JVM version", e);
+            }
+        }
+        for (String vendor : Arrays.asList("sun", "oracle", "openjdk")) {
+            if (jvmVendor.toLowerCase(Locale.US).contains(vendor)) {
+                if (majorVersion == 1.6 && minorVersion < 30) {
+                    return false;
+                }
+                if (majorVersion < 1.6) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
 }

src/test/java/com/atlassian/utils/process/ProcessUtilsTest.java

+package com.atlassian.utils.process;
+
+import org.junit.After;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class ProcessUtilsTest {
+
+    private static final String JVM_VENDOR = "java.vendor";
+    private static final String JVM_VERSION = "java.version";
+    private final String ORIGINAL_VENDOR = System.getProperty(JVM_VENDOR);
+    private final String ORIGINAL_VERSION = System.getProperty(JVM_VERSION);
+
+    @After
+    public void tearDown() {
+        System.setProperty(JVM_VENDOR, ORIGINAL_VENDOR);
+        System.setProperty(JVM_VERSION, ORIGINAL_VERSION);
+    }
+
+    @Test
+    public void testIs4947220FixedAgainstNotFixedVersions() throws InterruptedException {
+        for (String vendor : Arrays.asList("sun", "oracle", "openjdk")) {
+            System.setProperty(JVM_VENDOR, vendor);
+            for (String notFixedVersion : Arrays.asList("1.3", "1.4.0_18", "1.5.0_19", "1.6.0_29")) {
+                System.setProperty(JVM_VERSION, notFixedVersion);
+                assertThat(ProcessUtils.is4947220Fixed(), is(false));
+            }
+        }
+    }
+
+    @Test
+    public void testIs4947220FixedAgainstFixedVersions() {
+        for (String vendor : Arrays.asList("sun", "oracle", "openjdk")) {
+            System.setProperty(JVM_VENDOR, vendor);
+            for (String fixedVersion : Arrays.asList("1.6.0_30", "1.6.0_31", "1.7.0_21", "1.8.0_21")) {
+                System.setProperty(JVM_VERSION, fixedVersion);
+                assertThat(ProcessUtils.is4947220Fixed(), is(true));
+            }
+        }
+    }
+
+    @Test
+    public void testIs4947220FixedInNonSunJVM() {
+        System.setProperty(JVM_VENDOR, "ibm");
+        System.setProperty(JVM_VERSION, "1.4.0_18");
+        assertThat(ProcessUtils.is4947220Fixed(), is(true));
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.