/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.*; public class ProcessKillTest implements Runnable { private Process p; public ProcessKillTest() throws Exception { ProcessBuilder bldr; if(isOS("win")) bldr = new ProcessBuilder("ping", "127.0.0.1"); else bldr = new ProcessBuilder("./ProcessKillTest.sh"); bldr.redirectErrorStream(true); bldr.directory(new File(".")); p = bldr.start(); System.out.println(p.toString()); } public Process getProc() { return p; } public void killProc(boolean force) throws Exception { if(force) { p.destroyForcibly(); p.waitFor(); // System.out.println(p.exitValue()); if( (isOS("sol") && p.exitValue() != 9) || (isOS("lin") && p.exitValue() != 137) ) throw new RuntimeException("Test failed: wrong exit value"); } else { p.destroy(); } } public boolean isAlive() { return p.isAlive(); } public void run() { try { String line; BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = is.readLine()) != null) System.out.println(line); } catch(IOException e) { if(!e.getMessage().equals("Stream closed")) { throw new RuntimeException(e); } } } public static boolean isOS(String os) { return System.getProperty("os.name").toLowerCase().indexOf(os) == 0; } public static void testWin(ProcessKillTest test) throws Exception { if(!test.isAlive()) { throw new RuntimeException( "Test failed: Process exited prematurely"); } test.killProc(false); test.getProc().waitFor(); // System.out.println(test.getProc().exitValue()); if(test.isAlive()) throw new RuntimeException( "Test failed: hasn't been killed, please terminate " + test.getProc().toString() + " manually"); System.out.println("Test passed"); } public static void testTheRest(ProcessKillTest test) throws Exception { test.killProc(false); if(!test.isAlive()) { throw new RuntimeException( "Test failed: Process exited prematurely"); } else { test.killProc(true); if(test.isAlive()) { throw new RuntimeException( "Test failed: Process hasn't been killed"); } System.out.println("Test passed"); } } public static void main(String args[]) throws Exception { ProcessKillTest test = new ProcessKillTest(); new Thread(test).start(); Thread.sleep(1000); if(isOS("win")) testWin(test); else testTheRest(test); } }