Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Runtime/exec/ExitValue.java
47209 views
/*1* Copyright (c) 2002, 2007, 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[] { "/bin/sh", "-c", posixShellProgram },71expectedExitValue);72}7374final static int EXIT_CODE = 5;7576public static void main(String[] args) throws Exception {7778String java = join(File.separator, new String []79{ System.getProperty("java.home"), "bin", "java" });8081checkExitValue(new String[]82{ java,83"-classpath", System.getProperty("test.classes", "."),84"ExitValue$Run", String.valueOf(EXIT_CODE)85}, EXIT_CODE);8687checkExitValue(new String[] { "/bin/true" }, 0);8889checkPosixShellExitValue("exit", 0);9091checkPosixShellExitValue("exit 7", 7);9293if (new File("/bin/kill").exists()) {94int sigoffset =95System.getProperty("os.name").equals("SunOS") ? 0 : 128;96checkPosixShellExitValue("/bin/kill -9 $$", sigoffset+9);97}98}99100public static class Run {101public static void main (String[] argv) {102System.exit(Integer.parseInt(argv[0]));103}104}105}106107108