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