Path: blob/master/test/jdk/java/lang/ProcessBuilder/DestroyTest.java
41149 views
/*1* Copyright (c) 2012, 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 424489626* @summary Test for the various platform specific implementations of27* destroyForcibly.28*/2930import java.io.BufferedReader;31import java.io.BufferedWriter;32import java.io.File;33import java.io.FileWriter;34import java.io.IOException;35import java.io.InputStreamReader;3637abstract class ProcessTest implements Runnable {38ProcessBuilder bldr;39Process p;4041public void run() {42try {43String line;44BufferedReader is =45new BufferedReader(new InputStreamReader(p.getInputStream()));46while ((line = is.readLine()) != null)47System.err.println("ProcessTrap: " + line);48} catch(IOException e) {49if (!e.getMessage().matches("[Ss]tream [Cc]losed")) {50throw new RuntimeException(e);51}52}53}5455public void runTest() throws Exception {56// The destroy() method is not tested because57// the process streams are closed by the destroy() call.58// After a destroy() call, the process terminates with a59// SIGPIPE even if it was trapping SIGTERM.60// So skip the trap test and go straight to destroyForcibly().61p.destroyForcibly();62p.waitFor();63if (p.isAlive())64throw new RuntimeException("Problem terminating the process.");65}66}6768class UnixTest extends ProcessTest {69public UnixTest(File script) throws IOException {70script.deleteOnExit();71createScript(script);72bldr = new ProcessBuilder(script.getCanonicalPath());73bldr.redirectErrorStream(true);74bldr.directory(new File("."));75p = bldr.start();76}7778void createScript(File processTrapScript) throws IOException {79processTrapScript.deleteOnExit();80try (FileWriter fstream = new FileWriter(processTrapScript);81BufferedWriter out = new BufferedWriter(fstream)) {82out.write("#!/bin/bash\n" +83"echo \\\"ProcessTrap.sh started\\\"\n" +84"while :\n" +85"do\n" +86" sleep 1;\n" +87"done\n");88}89processTrapScript.setExecutable(true, true);90}9192}9394class WindowsTest extends ProcessTest {95public WindowsTest() throws IOException {96bldr = new ProcessBuilder("ftp");97bldr.redirectErrorStream(true);98bldr.directory(new File("."));99p = bldr.start();100}101102}103104public class DestroyTest {105106public static ProcessTest getTest() throws Exception {107String osName = System.getProperty("os.name");108if (osName.startsWith("Windows")) {109return new WindowsTest();110} else {111File userDir = new File(System.getProperty("user.dir", "."));112File tempFile = File.createTempFile("ProcessTrap-", ".sh", userDir);113if (osName.startsWith("Linux")114|| osName.startsWith("Mac OS")115|| osName.equals("AIX")) {116return new UnixTest(tempFile);117}118}119return null;120}121122public static void main(String args[]) throws Exception {123ProcessTest test = getTest();124if (test == null) {125throw new RuntimeException("Unrecognised OS");126} else {127new Thread(test).start();128Thread.sleep(1000);129test.runTest();130}131}132}133134135136