Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Runtime/exec/SetCwd.java
47211 views
/*1* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 415627826* @summary Basic functional test for27* Runtime.exec(String[] command, String[] env, File path) and28* Runtime.exec(String command, String[] env, File path).29*30* @build SetCwd31* @run shell setcwd.sh32*/33import java.io.*;3435public class SetCwd {36public static void testExec(String cmd, String[] cmdarray, boolean flag)37throws Exception {38File dir = new File(".");39File[] files = dir.listFiles();40String curDir = dir.getCanonicalPath();4142for (int i = 0; i < files.length; i++) {43File f = files[i];44if (f.isDirectory() && (new File(f, "SetCwd.class")).exists()) {45String newDir = f.getCanonicalPath();46// exec a new SetCwd in the sub directory47Process p = null;48if (flag) {49p = Runtime.getRuntime().exec(cmd, null, f);50} else {51p = Runtime.getRuntime().exec(cmdarray, null, f);52}5354BufferedReader in = new BufferedReader55(new InputStreamReader(p.getInputStream()));56// Read back output from child57String s = in.readLine();58if (!s.startsWith(newDir)) {59throw new Exception("inconsistent directory after exec");60}61// Join on the child62p.waitFor();63}64}65System.out.println(curDir);66}6768public static void main (String args[]) throws Exception {69String cmdarray[] = new String[2];70cmdarray[0] = System.getProperty("java.home") + File.separator +71"bin" + File.separator + "java";72cmdarray[1] = "SetCwd";73String cmd = cmdarray[0] + " " + cmdarray[1];74// test the two new methods75testExec(cmd, null, true);76testExec(null, cmdarray, false);77}78}798081