1/*
2 * Copyright (c) 1999, 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 *
31 * @build SetCwd
32 * @run shell setcwd.sh
33 */
34import java.io.*;
35
36public class SetCwd {
37    public static void testExec(String cmd, String[] cmdarray, boolean flag)
38        throws Exception {
39        File dir = new File(".");
40        File[] files = dir.listFiles();
41        String curDir = dir.getCanonicalPath();
42
43        for (int i = 0; i < files.length; i++) {
44            File f = files[i];
45            if (f.isDirectory() && (new File(f, "SetCwd.class")).exists()) {
46                String newDir = f.getCanonicalPath();
47                // exec a new SetCwd in the sub directory
48                Process p = null;
49                if (flag) {
50                    p = Runtime.getRuntime().exec(cmd, null, f);
51                } else {
52                    p = Runtime.getRuntime().exec(cmdarray, null, f);
53                }
54
55                BufferedReader in = new BufferedReader
56                    (new InputStreamReader(p.getInputStream()));
57                // Read back output from child
58                String s = in.readLine();
59                if (!s.startsWith(newDir)) {
60                    throw new Exception("inconsistent directory after exec");
61                }
62                // Join on the child
63                p.waitFor();
64            }
65        }
66        System.out.println(curDir);
67    }
68
69    public static void main (String args[]) throws Exception {
70        String cmdarray[] = new String[2];
71        cmdarray[0] = System.getProperty("java.home") + File.separator +
72            "bin" + File.separator + "java";
73        cmdarray[1] = "SetCwd";
74        String cmd = cmdarray[0] + " " + cmdarray[1];
75        // test the two new methods
76        testExec(cmd, null, true);
77        testExec(null, cmdarray, false);
78    }
79}
80