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