8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion
authorrriggs
Wed, 03 Jun 2015 18:18:05 -0400
changeset 30956 6f95bc555a6f
parent 30955 9f660470002c
child 30957 1329009c9b57
8067808: java/lang/ProcessBuilder/Basic.java failed on Assertion Summary: Change to use javaChild reporting its pid not portable Reviewed-by: igerasim
jdk/test/java/lang/ProcessBuilder/Basic.java
--- a/jdk/test/java/lang/ProcessBuilder/Basic.java	Wed Jun 03 18:17:04 2015 -0400
+++ b/jdk/test/java/lang/ProcessBuilder/Basic.java	Wed Jun 03 18:18:05 2015 -0400
@@ -36,6 +36,7 @@
  */
 
 import java.lang.ProcessBuilder.Redirect;
+import java.lang.ProcessHandle;
 import static java.lang.ProcessBuilder.Redirect.*;
 
 import java.io.*;
@@ -47,7 +48,6 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.security.*;
-import sun.misc.Unsafe;
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 import static java.lang.System.getenv;
@@ -309,6 +309,8 @@
             String action = args[0];
             if (action.equals("sleep")) {
                 Thread.sleep(10 * 60 * 1000L);
+            } else if (action.equals("pid")) {
+                System.out.println(ProcessHandle.current().getPid());
             } else if (action.equals("testIO")) {
                 String expected = "standard input";
                 char[] buf = new char[expected.length()+1];
@@ -1139,40 +1141,20 @@
     }
 
     static void checkProcessPid() {
-        long actualPid = 0;
-        long expectedPid = -1;
-        if (Windows.is()) {
-            String[] argsTasklist = {"tasklist.exe",  "/NH", "/FI",  "\"IMAGENAME eq tasklist.exe\""};
-            ProcessBuilder pb = new ProcessBuilder(argsTasklist);
-            try {
-                Process proc = pb.start();
-                expectedPid = proc.getPid();
-
-                String output = commandOutput(proc);
-                String[] splits = output.split("\\s+");
-                actualPid = Integer.valueOf(splits[2]);
-            } catch (Throwable t) {
-                unexpected(t);
-            }
-        } else if (Unix.is() || MacOSX.is()) {
-            String[]  shArgs = {"sh", "-c", "echo $$"};
-            ProcessBuilder pb = new ProcessBuilder(shArgs);
-            try {
-                Process proc = pb.start();
-                expectedPid = proc.getPid();
-
-                String output = commandOutput(proc);
-                String[] splits = output.split("\\s+");
-                actualPid = Integer.valueOf(splits[0]);
-            } catch (Throwable t) {
-                unexpected(t);
-            }
-        } else {
-            fail("No test for checkProcessPid on platform: " + System.getProperty("os.name"));
-            return;
+        ProcessBuilder pb = new ProcessBuilder();
+        List<String> list = new ArrayList<String>(javaChildArgs);
+        list.add("pid");
+        pb.command(list);
+        try {
+            Process p = pb.start();
+            String s = commandOutput(p);
+            long actualPid = Long.valueOf(s.trim());
+            long expectedPid = p.getPid();
+            equal(actualPid, expectedPid);
+        } catch (Throwable t) {
+            unexpected(t);
         }
 
-        equal(actualPid, expectedPid);
 
         // Test the default implementation of Process.getPid
         DelegatingProcess p = new DelegatingProcess(null);