1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 
  26 public class ProcessKillTest implements Runnable {
  27 
  28     private Process p;
  29 
  30     public ProcessKillTest() throws Exception {
  31         ProcessBuilder bldr;
  32         if(isOS("win"))
  33             bldr = new ProcessBuilder("ping", "127.0.0.1");
  34         else
  35             bldr = new ProcessBuilder("./ProcessKillTest.sh");
  36         bldr.redirectErrorStream(true);
  37         bldr.directory(new File("."));
  38         p = bldr.start();
  39         System.out.println(p.toString());
  40     }
  41 
  42     public Process getProc() {
  43         return p;
  44     }
  45 
  46     public void killProc(boolean force) throws Exception {
  47         if(force) {
  48             p.destroyForcibly();
  49             p.waitFor();
  50             // System.out.println(p.exitValue());
  51             if( (isOS("sol") && p.exitValue() != 9) ||
  52                 (isOS("lin") && p.exitValue() != 137) )
  53                 throw new RuntimeException("Test failed: wrong exit value");
  54         } else {
  55             p.destroy();
  56         }
  57     }
  58 
  59     public boolean isAlive() {
  60         return p.isAlive();
  61     }
  62 
  63     public void run() {
  64         try {
  65             String line;
  66             BufferedReader is =
  67                 new BufferedReader(new InputStreamReader(p.getInputStream()));
  68             while ((line = is.readLine()) != null)
  69                 System.out.println(line);
  70         } catch(IOException e) {
  71             if(!e.getMessage().equals("Stream closed")) {
  72                 throw new RuntimeException(e);
  73             }
  74         }
  75     }
  76 
  77     public static boolean isOS(String os) {
  78         return System.getProperty("os.name").toLowerCase().indexOf(os) == 0;
  79     }
  80 
  81     public static void testWin(ProcessKillTest test) throws Exception {
  82         if(!test.isAlive()) {
  83             throw new RuntimeException(
  84                 "Test failed: Process exited prematurely");
  85         }
  86         test.killProc(false);
  87         test.getProc().waitFor();
  88         // System.out.println(test.getProc().exitValue());
  89 
  90         if(test.isAlive())
  91             throw new RuntimeException(
  92                 "Test failed: hasn't been killed, please terminate " +
  93                 test.getProc().toString() + " manually");
  94         System.out.println("Test passed");
  95     }
  96 
  97     public static void testTheRest(ProcessKillTest test) throws Exception {
  98         test.killProc(false);
  99         if(!test.isAlive()) {
 100             throw new RuntimeException(
 101                 "Test failed: Process exited prematurely");
 102         } else {
 103             test.killProc(true);
 104             if(test.isAlive()) {
 105                 throw new RuntimeException(
 106                     "Test failed: Process hasn't been killed");
 107             }
 108             System.out.println("Test passed");
 109         }
 110     }
 111 
 112     public static void main(String args[]) throws Exception {
 113         ProcessKillTest test = new ProcessKillTest();
 114         new Thread(test).start();
 115         Thread.sleep(1000);
 116 
 117         if(isOS("win"))
 118             testWin(test);
 119         else
 120             testTheRest(test);
 121     }
 122 }