Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/SetCwd.java
41153 views
/*1* Copyright (c) 1999, 2021, 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* @library /test/lib31* @run testng/othervm SetCwd32*/33import org.testng.annotations.BeforeTest;34import org.testng.annotations.Test;3536import java.io.*;3738import static jdk.test.lib.Asserts.assertTrue;3940public class SetCwd {4142private static final String TEST_CLASSES = System.getProperty(43"test.classes", ".");4445private static final String[] CMD_ARRAY = new String[2];4647@BeforeTest48public static void setUp() throws Exception {49CMD_ARRAY[0] = System.getProperty("java.home") + File.separator +50"bin" + File.separator + "java";51CMD_ARRAY[1] = SimpleProcess.class.getName();52}5354@Test55public void testRuntimeExecWithArray() throws Exception {56Process process = Runtime.getRuntime().exec(CMD_ARRAY, null,57new File(TEST_CLASSES));58verifyProcessOutput(process);59}6061@Test62public void testRuntimeExecWithString() throws Exception {63String cmd = String.join(" ", CMD_ARRAY);64Process process = Runtime.getRuntime().exec(cmd, null,65new File(TEST_CLASSES));66verifyProcessOutput(process);67}6869// Verify the process has executed by comparing its output with the expected70private void verifyProcessOutput(Process process) throws Exception {71process.waitFor();72assertTrue(process.exitValue() == 0);7374try (BufferedReader reader = new BufferedReader(75new InputStreamReader(process.getInputStream()))) {76String line = reader.readLine();77if (!line.startsWith(TEST_CLASSES)) {78String error = String.format("Expected process output first line: " +79"'%s' Actual: '%s'", TEST_CLASSES, line);80throw new Exception(error);81}82}83}8485// This class main will be the entry point for test subprocesses86static class SimpleProcess {87public static void main (String[] args) throws Exception {88File dir = new File(".");89System.out.println(dir.getCanonicalPath());90}91}92}939495