1 /*
 2  * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
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 4156278
27  * @summary Basic functional test for
28  *          Runtime.exec(String[] command, String[] env, File path) and
29  *          Runtime.exec(String command, String[] env, File path).
30  * @requires vm.flagless
31  * @library /test/lib
32  * @run testng/othervm SetCwd
33  */
34 import org.testng.annotations.BeforeTest;
35 import org.testng.annotations.Test;
36 
37 import java.io.*;
38 
39 import static jdk.test.lib.Asserts.assertTrue;
40 
41 public class SetCwd {
42 
43     private static final String TEST_CLASSES = System.getProperty(
44             "test.classes", ".");
45 
46     private static final String[] CMD_ARRAY = new String[2];
47 
48     @BeforeTest
49     public static void setUp() throws Exception {
50         CMD_ARRAY[0] = System.getProperty("java.home") + File.separator +
51                 "bin" + File.separator + "java";
52         CMD_ARRAY[1] = SimpleProcess.class.getName();
53     }
54 
55     @Test
56     public void testRuntimeExecWithArray() throws Exception {
57         Process process = Runtime.getRuntime().exec(CMD_ARRAY, null,
58                 new File(TEST_CLASSES));
59         verifyProcessOutput(process);
60     }
61 
62     @Test
63     public void testRuntimeExecWithString() throws Exception {
64         String cmd = String.join(" ", CMD_ARRAY);
65         @SuppressWarnings("deprecation")
66         Process process = Runtime.getRuntime().exec(cmd, null,
67                 new File(TEST_CLASSES));
68         verifyProcessOutput(process);
69     }
70 
71     // Verify the process has executed by comparing its output with the expected
72     private void verifyProcessOutput(Process process) throws Exception {
73         process.waitFor();
74         assertTrue(process.exitValue() == 0);
75 
76         try (BufferedReader reader = new BufferedReader(
77                 new InputStreamReader(process.getInputStream()))) {
78             String line = reader.readLine();
79             if (!line.startsWith(TEST_CLASSES)) {
80                 String error = String.format("Expected process output first line: " +
81                         "'%s' Actual: '%s'", TEST_CLASSES, line);
82                 throw new Exception(error);
83             }
84         }
85     }
86 
87     // This class main will be the entry point for test subprocesses
88     static class SimpleProcess {
89         public static void main (String[] args) throws Exception {
90             File dir = new File(".");
91             System.out.println(dir.getCanonicalPath());
92         }
93     }
94 }