Skip to content

Commit 413bddc

Browse files
sanaulla123maibin
authored andcommitted
BAEL-654: Java 9 Process API Improvements (eugenp#1235)
* code for BAEL-654: Java 9 Process API Improvements * incorporate review
1 parent afee31e commit 413bddc

2 files changed

Lines changed: 140 additions & 2 deletions

File tree

‎core-java-9/pom.xml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<artifactId>slf4j-api</artifactId>
2222
<version>${org.slf4j.version}</version>
2323
</dependency>
24+
<dependency>
25+
<groupId>ch.qos.logback</groupId>
26+
<artifactId>logback-classic</artifactId>
27+
<version>${ch.qos.logback.version}</version>
28+
</dependency>
2429

2530
<dependency>
2631
<groupId>org.hamcrest</groupId>
@@ -76,9 +81,9 @@
7681

7782
<!-- logging -->
7883
<org.slf4j.version>1.7.21</org.slf4j.version>
79-
84+
<ch.qos.logback.version>1.2.1</ch.qos.logback.version>
8085
<!-- maven plugins -->
81-
<maven-compiler-plugin.version>3.6-jigsaw-SNAPSHOT</maven-compiler-plugin.version>
86+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
8287
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
8388

8489
<!-- testing -->
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.baeldung.java9.process;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.IOException;
8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.stream.Stream;
10+
11+
import org.junit.Test;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
/**
16+
* Created by sanaulla on 2/23/2017.
17+
*/
18+
19+
public class ProcessAPIEnhancementsTest {
20+
21+
Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsTest.class);
22+
23+
@Test
24+
public void givenCurrentProcess_whenInvokeGetInfo_thenSuccess() throws IOException {
25+
ProcessHandle processHandle = ProcessHandle.current();
26+
ProcessHandle.Info processInfo = processHandle.info();
27+
assertNotNull(processHandle.getPid());
28+
assertEquals(false, processInfo.arguments()
29+
.isPresent());
30+
assertEquals(true, processInfo.command()
31+
.isPresent());
32+
assertTrue(processInfo.command()
33+
.get()
34+
.contains("java"));
35+
36+
assertEquals(true, processInfo.startInstant()
37+
.isPresent());
38+
assertEquals(true, processInfo.totalCpuDuration()
39+
.isPresent());
40+
assertEquals(true, processInfo.user()
41+
.isPresent());
42+
}
43+
44+
@Test
45+
public void givenSpawnProcess_whenInvokeGetInfo_thenSuccess() throws IOException {
46+
47+
String javaCmd = ProcessUtils.getJavaCmd()
48+
.getAbsolutePath();
49+
ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version");
50+
Process process = processBuilder.inheritIO()
51+
.start();
52+
ProcessHandle processHandle = process.toHandle();
53+
ProcessHandle.Info processInfo = processHandle.info();
54+
assertNotNull(processHandle.getPid());
55+
assertEquals(false, processInfo.arguments()
56+
.isPresent());
57+
assertEquals(true, processInfo.command()
58+
.isPresent());
59+
assertTrue(processInfo.command()
60+
.get()
61+
.contains("java"));
62+
assertEquals(true, processInfo.startInstant()
63+
.isPresent());
64+
assertEquals(true, processInfo.totalCpuDuration()
65+
.isPresent());
66+
assertEquals(true, processInfo.user()
67+
.isPresent());
68+
}
69+
70+
@Test
71+
public void givenLiveProcesses_whenInvokeGetInfo_thenSuccess() {
72+
Stream<ProcessHandle> liveProcesses = ProcessHandle.allProcesses();
73+
liveProcesses.filter(ProcessHandle::isAlive)
74+
.forEach(ph -> {
75+
assertNotNull(ph.getPid());
76+
assertEquals(true, ph.info()
77+
.command()
78+
.isPresent());
79+
assertEquals(true, ph.info()
80+
.startInstant()
81+
.isPresent());
82+
assertEquals(true, ph.info()
83+
.totalCpuDuration()
84+
.isPresent());
85+
assertEquals(true, ph.info()
86+
.user()
87+
.isPresent());
88+
});
89+
}
90+
91+
@Test
92+
public void givenProcess_whenGetChildProcess_thenSuccess() throws IOException {
93+
int childProcessCount = 5;
94+
for (int i = 0; i < childProcessCount; i++) {
95+
String javaCmd = ProcessUtils.getJavaCmd()
96+
.getAbsolutePath();
97+
ProcessBuilder processBuilder
98+
= new ProcessBuilder(javaCmd, "-version");
99+
processBuilder.inheritIO().start();
100+
}
101+
102+
Stream<ProcessHandle> children = ProcessHandle.current()
103+
.children();
104+
children.filter(ProcessHandle::isAlive)
105+
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info()
106+
.command()));
107+
Stream<ProcessHandle> descendants = ProcessHandle.current()
108+
.descendants();
109+
descendants.filter(ProcessHandle::isAlive)
110+
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info()
111+
.command()));
112+
}
113+
114+
@Test
115+
public void givenProcess_whenAddExitCallback_thenSuccess() throws Exception {
116+
String javaCmd = ProcessUtils.getJavaCmd()
117+
.getAbsolutePath();
118+
ProcessBuilder processBuilder
119+
= new ProcessBuilder(javaCmd, "-version");
120+
Process process = processBuilder.inheritIO()
121+
.start();
122+
ProcessHandle processHandle = process.toHandle();
123+
124+
log.info("PID: {} has started", processHandle.getPid());
125+
CompletableFuture<ProcessHandle> onProcessExit = processHandle.onExit();
126+
onProcessExit.get();
127+
assertEquals(false, processHandle.isAlive());
128+
onProcessExit.thenAccept(ph -> {
129+
log.info("PID: {} has stopped", ph.getPid());
130+
});
131+
}
132+
133+
}

0 commit comments

Comments
 (0)