Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/ExitValue.java
41153 views
/*1* Copyright (c) 2002, 2020, 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 4680945 487341926* @summary Check process exit code27* @author kladko, Martin Buchholz28*/2930import java.io.File;3132public class ExitValue33{3435public static String join(String separator, String[] elts) {36String result = elts[0];37for (int i = 1; i < elts.length; ++i)38result = result + separator + elts[i];39return result;40}4142public static void checkExitValue(String[] commandArgs,43int expectedExitValue)44throws Exception45{46if (! (new File(commandArgs[0]).exists()))47return;4849System.out.println("Running command: " + join(" ", commandArgs));50Process proc = Runtime.getRuntime().exec(commandArgs);51int val;52byte[] buf = new byte[4096];53int n = proc.getErrorStream().read(buf);54if (n > 0)55throw new Exception56("Unexpected stderr: "57+ new String(buf, 0, n, "ASCII"));58if ((val = proc.waitFor()) != expectedExitValue)59throw new Exception60("waitFor() returned unexpected value " + val);61if ((val = proc.exitValue()) != expectedExitValue)62throw new Exception63("exitValue() returned unexpected value " + val);64}6566public static void checkPosixShellExitValue(String posixShellProgram,67int expectedExitValue)68throws Exception69{70checkExitValue(new String[] { UnixCommands.sh(), "-c", posixShellProgram },71expectedExitValue);72}7374static final int EXIT_CODE = 5;7576public static void main(String[] args) throws Exception {77if (! UnixCommands.isUnix) {78System.out.println("For UNIX only");79return;80}81UnixCommands.ensureCommandsAvailable("sh", "true", "kill");8283String java = join(File.separator, new String []84{ System.getProperty("java.home"), "bin", "java" });8586checkExitValue(new String[]87{ java,88"-classpath", System.getProperty("test.classes", "."),89"ExitValue$Run", String.valueOf(EXIT_CODE)90}, EXIT_CODE);9192checkExitValue(new String[] { UnixCommands.findCommand("true") }, 0);9394checkPosixShellExitValue("exit", 0);9596checkPosixShellExitValue("exit 7", 7);9798int sigoffset = 128;99checkPosixShellExitValue(UnixCommands.kill() + " -9 $$", sigoffset+9);100}101102public static class Run {103public static void main (String[] argv) {104System.exit(Integer.parseInt(argv[0]));105}106}107}108109110