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