Path: blob/master/test/jdk/java/lang/ProcessHandle/Basic.java
41149 views
/*1* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import static org.testng.Assert.assertEquals;24import static org.testng.Assert.assertFalse;25import static org.testng.Assert.assertTrue;26import static org.testng.Assert.fail;2728import java.io.IOException;29import java.util.List;30import java.util.Optional;31import java.util.stream.Collectors;3233import org.testng.TestNG;34import org.testng.annotations.Test;3536/*37* @test38* @library /test/lib39* @modules java.base/jdk.internal.misc40* jdk.management41* @build jdk.test.lib.Utils42* jdk.test.lib.Asserts43* jdk.test.lib.JDKToolFinder44* jdk.test.lib.JDKToolLauncher45* jdk.test.lib.Platform46* jdk.test.lib.process.*47* @run testng Basic48* @summary Basic tests for ProcessHandler49* @author Roger Riggs50*/51public class Basic {52/**53* Tests of ProcessHandle.current.54*/55@Test56public static void test1() {57try {58ProcessHandle self = ProcessHandle.current();59ProcessHandle self1 = ProcessHandle.current();60assertEquals(self, self1); //, "get pid twice should be same %d: %d");61} finally {62// Cleanup any left over processes63ProcessHandle.current().children().forEach(ProcessHandle::destroy);64}65}6667/**68* Tests of ProcessHandle.get.69*/70@Test71public static void test2() {72try {73ProcessHandle self = ProcessHandle.current();74long pid = self.pid(); // known native process id75Optional<ProcessHandle> self1 = ProcessHandle.of(pid);76assertEquals(self1.get(), self,77"ProcessHandle.of(x.pid()) should be equal pid() %d: %d");7879Optional<ProcessHandle> ph = ProcessHandle.of(pid);80assertEquals(pid, ph.get().pid());81} finally {82// Cleanup any left over processes83ProcessHandle.current().children().forEach(ProcessHandle::destroy);84}85}8687@Test88public static void test3() {89// Test can get parent of current90ProcessHandle ph = ProcessHandle.current();91try {92Optional<ProcessHandle> pph = ph.parent();93assertTrue(pph.isPresent(), "Current has a Parent");94} finally {95// Cleanup any left over processes96ProcessHandle.current().children().forEach(ProcessHandle::destroy);97}98}99100@Test101public static void test4() {102try {103Process p = new ProcessBuilder("sleep", "0").start();104p.waitFor();105106long deadPid = p.pid();107p = null; // Forget the process108109Optional<ProcessHandle> t = ProcessHandle.of(deadPid);110assertFalse(t.isPresent(), "Handle created for invalid pid:" + t);111} catch (IOException | InterruptedException ex) {112fail("Unexpected exception", ex);113} finally {114// Cleanup any left over processes115ProcessHandle.current().children().forEach(ProcessHandle::destroy);116}117}118119@Test120public static void test5() {121// Always contains itself.122ProcessHandle current = ProcessHandle.current();123List<ProcessHandle> list = ProcessHandle.allProcesses().collect(Collectors.toList());124if (!list.stream()125.anyMatch(ph -> ph.equals(ProcessHandle.current()))) {126System.out.printf("current: %s%n", current);127System.out.printf("all processes.size: %d%n", list.size());128list.forEach(p -> ProcessUtil.printProcess(p, " allProcesses: "));129fail("current process not found in all processes");130}131}132133@Test(expectedExceptions = IllegalStateException.class)134public static void test6() {135ProcessHandle.current().onExit();136}137138@Test(expectedExceptions = IllegalStateException.class)139public static void test7() {140ProcessHandle.current().destroyForcibly();141}142143// Main can be used to run the tests from the command line with only testng.jar.144@SuppressWarnings("raw_types")145public static void main(String[] args) {146Class<?>[] testclass = {TreeTest.class};147TestNG testng = new TestNG();148testng.setTestClasses(testclass);149testng.run();150}151152}153154155