1 /*
   2  * Copyright (c) 2014, 2017, 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 static org.testng.Assert.assertEquals;
  25 import static org.testng.Assert.assertFalse;
  26 import static org.testng.Assert.assertTrue;
  27 import static org.testng.Assert.fail;
  28 
  29 import java.io.IOException;
  30 import java.util.List;
  31 import java.util.Optional;
  32 import java.util.stream.Collectors;
  33 
  34 import org.testng.TestNG;
  35 import org.testng.annotations.Test;
  36 
  37 /*
  38  * @test
  39  * @library /test/lib
  40  * @modules java.base/jdk.internal.misc
  41  *          jdk.management
  42  * @build jdk.test.lib.Utils
  43  *        jdk.test.lib.Asserts
  44  *        jdk.test.lib.JDKToolFinder
  45  *        jdk.test.lib.JDKToolLauncher
  46  *        jdk.test.lib.Platform
  47  *        jdk.test.lib.process.*
  48  * @run testng Basic
  49  * @summary Basic tests for ProcessHandler
  50  * @author Roger Riggs
  51  */
  52 public class Basic {
  53     /**
  54      * Tests of ProcessHandle.current.
  55      */
  56     @Test
  57     public static void test1() {
  58         try {
  59             ProcessHandle self = ProcessHandle.current();
  60             ProcessHandle self1 = ProcessHandle.current();
  61             assertEquals(self, self1); //, "get pid twice should be same %d: %d");
  62         } finally {
  63             // Cleanup any left over processes
  64             ProcessHandle.current().children().forEach(ProcessHandle::destroy);
  65         }
  66     }
  67 
  68     /**
  69      * Tests of ProcessHandle.get.
  70      */
  71     @Test
  72     public static void test2() {
  73         try {
  74             ProcessHandle self = ProcessHandle.current();
  75             long pid = self.pid();       // known native process id
  76             Optional<ProcessHandle> self1 = ProcessHandle.of(pid);
  77             assertEquals(self1.get(), self,
  78                     "ProcessHandle.of(x.pid()) should be equal pid() %d: %d");
  79 
  80             Optional<ProcessHandle> ph = ProcessHandle.of(pid);
  81             assertEquals(pid, ph.get().pid());
  82         } finally {
  83             // Cleanup any left over processes
  84             ProcessHandle.current().children().forEach(ProcessHandle::destroy);
  85         }
  86     }
  87 
  88     @Test
  89     public static void test3() {
  90         // Test can get parent of current
  91         ProcessHandle ph = ProcessHandle.current();
  92         try {
  93             Optional<ProcessHandle> pph = ph.parent();
  94             assertTrue(pph.isPresent(), "Current has a Parent");
  95         } finally {
  96             // Cleanup any left over processes
  97             ProcessHandle.current().children().forEach(ProcessHandle::destroy);
  98         }
  99     }
 100 
 101     @Test
 102     public static void test4() {
 103         try {
 104             Process p = new ProcessBuilder("sleep", "0").start();
 105             p.waitFor();
 106 
 107             long deadPid = p.pid();
 108             p = null;               // Forget the process
 109 
 110             Optional<ProcessHandle> t = ProcessHandle.of(deadPid);
 111             assertFalse(t.isPresent(), "Handle created for invalid pid:" + t);
 112         } catch (IOException | InterruptedException ex) {
 113             fail("Unexpected exception", ex);
 114         } finally {
 115             // Cleanup any left over processes
 116             ProcessHandle.current().children().forEach(ProcessHandle::destroy);
 117         }
 118     }
 119 
 120     @Test
 121     public static void test5() {
 122         // Always contains itself.
 123         ProcessHandle current = ProcessHandle.current();
 124         List<ProcessHandle> list = ProcessHandle.allProcesses().collect(Collectors.toList());
 125         if (!list.stream()
 126                 .anyMatch(ph -> ph.equals(ProcessHandle.current()))) {
 127             System.out.printf("current: %s%n", current);
 128             System.out.printf("all processes.size: %d%n", list.size());
 129             list.forEach(p -> ProcessUtil.printProcess(p, "   allProcesses: "));
 130             fail("current process not found in all processes");
 131         }
 132     }
 133 
 134     @Test(expectedExceptions = IllegalStateException.class)
 135     public static void test6() {
 136         ProcessHandle.current().onExit();
 137     }
 138 
 139     @Test(expectedExceptions = IllegalStateException.class)
 140     public static void test7() {
 141         ProcessHandle.current().destroyForcibly();
 142     }
 143 
 144     // Main can be used to run the tests from the command line with only testng.jar.
 145     @SuppressWarnings("raw_types")
 146     public static void main(String[] args) {
 147         Class<?>[] testclass = {TreeTest.class};
 148         TestNG testng = new TestNG();
 149         testng.setTestClasses(testclass);
 150         testng.run();
 151     }
 152 
 153 }