test/java/lang/ProcessBuilder/Basic.java

Print this page




  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606 7034570 4244896
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @author Martin Buchholz
  33  */
  34 
  35 import java.lang.ProcessBuilder.Redirect;
  36 import static java.lang.ProcessBuilder.Redirect.*;
  37 
  38 import java.io.*;

  39 import java.util.*;
  40 import java.util.concurrent.CountDownLatch;
  41 import java.util.concurrent.TimeUnit;
  42 import java.security.*;
  43 import java.util.regex.Pattern;
  44 import java.util.regex.Matcher;
  45 import static java.lang.System.getenv;
  46 import static java.lang.System.out;
  47 import static java.lang.Boolean.TRUE;
  48 import static java.util.AbstractMap.SimpleImmutableEntry;
  49 
  50 public class Basic {
  51 
  52     /* used for Windows only */
  53     static final String systemRoot = System.getenv("SystemRoot");
  54 
  55     /* used for Mac OS X only */
  56     static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
  57 
  58     private static String commandOutput(Reader r) throws Throwable {


1921                         check(expected.getMessage()
1922                               .matches("[Ss]tream [Cc]losed"));
1923                     }
1924                 }
1925             }
1926         } catch (Throwable t) { unexpected(t); }
1927 
1928         //----------------------------------------------------------------
1929         // Check that reads which are pending when Process.destroy is
1930         // called, get EOF, not IOException("Stream closed").
1931         //----------------------------------------------------------------
1932         try {
1933             final int cases = 4;
1934             for (int i = 0; i < cases; i++) {
1935                 final int action = i;
1936                 List<String> childArgs = new ArrayList<String>(javaChildArgs);
1937                 childArgs.add("sleep");
1938                 final byte[] bytes = new byte[10];
1939                 final Process p = new ProcessBuilder(childArgs).start();
1940                 final CountDownLatch latch = new CountDownLatch(1);






1941                 final Thread thread = new Thread() {
1942                     public void run() {
1943                         try {
1944                             latch.countDown();
1945                             int r;
1946                             switch (action) {
1947                             case 0: r = p.getInputStream().read(); break;
1948                             case 1: r = p.getErrorStream().read(); break;
1949                             case 2: r = p.getInputStream().read(bytes); break;
1950                             case 3: r = p.getErrorStream().read(bytes); break;
1951                             default: throw new Error();
1952                             }
1953                             equal(-1, r);
1954                         } catch (Throwable t) { unexpected(t); }}};
1955 
1956                 thread.start();
1957                 latch.await();
1958                 Thread.sleep(10);




























1959                 p.destroy();
1960                 thread.join();
1961             }
1962         } catch (Throwable t) { unexpected(t); }
1963 
1964         //----------------------------------------------------------------
1965         // Check that subprocesses which create subprocesses of their
1966         // own do not cause parent to hang waiting for file
1967         // descriptors to be closed.
1968         //----------------------------------------------------------------
1969         try {
1970             if (Unix.is()
1971                 && new File("/bin/bash").exists()
1972                 && new File("/bin/sleep").exists()) {
1973                 final String[] cmd = { "/bin/bash", "-c", "(/bin/sleep 6666)" };
1974                 final ProcessBuilder pb = new ProcessBuilder(cmd);
1975                 final Process p = pb.start();
1976                 final InputStream stdout = p.getInputStream();
1977                 final InputStream stderr = p.getErrorStream();
1978                 final OutputStream stdin = p.getOutputStream();




  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606 7034570 4244896
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @author Martin Buchholz
  33  */
  34 
  35 import java.lang.ProcessBuilder.Redirect;
  36 import static java.lang.ProcessBuilder.Redirect.*;
  37 
  38 import java.io.*;
  39 import java.lang.reflect.Field;
  40 import java.util.*;
  41 import java.util.concurrent.CountDownLatch;
  42 import java.util.concurrent.TimeUnit;
  43 import java.security.*;
  44 import java.util.regex.Pattern;
  45 import java.util.regex.Matcher;
  46 import static java.lang.System.getenv;
  47 import static java.lang.System.out;
  48 import static java.lang.Boolean.TRUE;
  49 import static java.util.AbstractMap.SimpleImmutableEntry;
  50 
  51 public class Basic {
  52 
  53     /* used for Windows only */
  54     static final String systemRoot = System.getenv("SystemRoot");
  55 
  56     /* used for Mac OS X only */
  57     static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
  58 
  59     private static String commandOutput(Reader r) throws Throwable {


1922                         check(expected.getMessage()
1923                               .matches("[Ss]tream [Cc]losed"));
1924                     }
1925                 }
1926             }
1927         } catch (Throwable t) { unexpected(t); }
1928 
1929         //----------------------------------------------------------------
1930         // Check that reads which are pending when Process.destroy is
1931         // called, get EOF, not IOException("Stream closed").
1932         //----------------------------------------------------------------
1933         try {
1934             final int cases = 4;
1935             for (int i = 0; i < cases; i++) {
1936                 final int action = i;
1937                 List<String> childArgs = new ArrayList<String>(javaChildArgs);
1938                 childArgs.add("sleep");
1939                 final byte[] bytes = new byte[10];
1940                 final Process p = new ProcessBuilder(childArgs).start();
1941                 final CountDownLatch latch = new CountDownLatch(1);
1942                 final InputStream s;
1943                 switch (action & 0x1) {
1944                     case 0: s = p.getInputStream(); break;
1945                     case 1: s = p.getErrorStream(); break;
1946                     default: throw new Error();
1947                 }
1948                 final Thread thread = new Thread() {
1949                     public void run() {
1950                         try {

1951                             int r;
1952                             latch.countDown();
1953                             switch (action & 0x2) {
1954                                 case 0: r = s.read(bytes); break;
1955                                 case 2: r = s.read(bytes); break;

1956                                 default: throw new Error();
1957                             }
1958                             equal(-1, r);
1959                         } catch (Throwable t) { unexpected(t); }}};
1960 
1961                 thread.start();
1962                 latch.await();
1963 
1964                 String os = System.getProperty("os.name");
1965                 if (os.equalsIgnoreCase("Solaris") || 
1966                     os.equalsIgnoreCase("SunOS")) 
1967                 {
1968                     Field useCountField;
1969                     Integer useCount = null;
1970                     if (s.toString().startsWith(
1971                         "java.lang.UNIXProcess$DeferredCloseInputStream")) 
1972                     {
1973                         useCountField = s.getClass().
1974                             getDeclaredField("useCount");
1975                         useCountField.setAccessible(true);
1976                         useCount = (Integer)useCountField.get(s);
1977                     } else {
1978                         Field deferredField = p.getClass().
1979                             getDeclaredField("stdout_inner_stream");
1980                         deferredField.setAccessible(true);
1981                         Object deferred = deferredField.get(p);
1982                         useCountField = deferred.getClass().
1983                             getDeclaredField("useCount");
1984                         useCountField.setAccessible(true);
1985                         useCount = (Integer)useCountField.get(deferred);
1986                     }
1987                     while (useCount.intValue() <= 0) {
1988                         useCount = (Integer)useCountField.get(s);
1989                         Thread.currentThread().yield();
1990                     }
1991                 }
1992                 p.destroy();
1993                 thread.join();
1994             }
1995         } catch (Throwable t) { unexpected(t); }
1996 
1997         //----------------------------------------------------------------
1998         // Check that subprocesses which create subprocesses of their
1999         // own do not cause parent to hang waiting for file
2000         // descriptors to be closed.
2001         //----------------------------------------------------------------
2002         try {
2003             if (Unix.is()
2004                 && new File("/bin/bash").exists()
2005                 && new File("/bin/sleep").exists()) {
2006                 final String[] cmd = { "/bin/bash", "-c", "(/bin/sleep 6666)" };
2007                 final ProcessBuilder pb = new ProcessBuilder(cmd);
2008                 final Process p = pb.start();
2009                 final InputStream stdout = p.getInputStream();
2010                 final InputStream stderr = p.getErrorStream();
2011                 final OutputStream stdin = p.getOutputStream();