forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-654: Java 9 Process API Improvements (eugenp#1235)
* code for BAEL-654: Java 9 Process API Improvements * incorporate review
- Loading branch information
1 parent
afee31e
commit 413bddc
Showing
2 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
core-java-9/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.baeldung.java9.process; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Created by sanaulla on 2/23/2017. | ||
*/ | ||
|
||
public class ProcessAPIEnhancementsTest { | ||
|
||
Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsTest.class); | ||
|
||
@Test | ||
public void givenCurrentProcess_whenInvokeGetInfo_thenSuccess() throws IOException { | ||
ProcessHandle processHandle = ProcessHandle.current(); | ||
ProcessHandle.Info processInfo = processHandle.info(); | ||
assertNotNull(processHandle.getPid()); | ||
assertEquals(false, processInfo.arguments() | ||
.isPresent()); | ||
assertEquals(true, processInfo.command() | ||
.isPresent()); | ||
assertTrue(processInfo.command() | ||
.get() | ||
.contains("java")); | ||
|
||
assertEquals(true, processInfo.startInstant() | ||
.isPresent()); | ||
assertEquals(true, processInfo.totalCpuDuration() | ||
.isPresent()); | ||
assertEquals(true, processInfo.user() | ||
.isPresent()); | ||
} | ||
|
||
@Test | ||
public void givenSpawnProcess_whenInvokeGetInfo_thenSuccess() throws IOException { | ||
|
||
String javaCmd = ProcessUtils.getJavaCmd() | ||
.getAbsolutePath(); | ||
ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version"); | ||
Process process = processBuilder.inheritIO() | ||
.start(); | ||
ProcessHandle processHandle = process.toHandle(); | ||
ProcessHandle.Info processInfo = processHandle.info(); | ||
assertNotNull(processHandle.getPid()); | ||
assertEquals(false, processInfo.arguments() | ||
.isPresent()); | ||
assertEquals(true, processInfo.command() | ||
.isPresent()); | ||
assertTrue(processInfo.command() | ||
.get() | ||
.contains("java")); | ||
assertEquals(true, processInfo.startInstant() | ||
.isPresent()); | ||
assertEquals(true, processInfo.totalCpuDuration() | ||
.isPresent()); | ||
assertEquals(true, processInfo.user() | ||
.isPresent()); | ||
} | ||
|
||
@Test | ||
public void givenLiveProcesses_whenInvokeGetInfo_thenSuccess() { | ||
Stream<ProcessHandle> liveProcesses = ProcessHandle.allProcesses(); | ||
liveProcesses.filter(ProcessHandle::isAlive) | ||
.forEach(ph -> { | ||
assertNotNull(ph.getPid()); | ||
assertEquals(true, ph.info() | ||
.command() | ||
.isPresent()); | ||
assertEquals(true, ph.info() | ||
.startInstant() | ||
.isPresent()); | ||
assertEquals(true, ph.info() | ||
.totalCpuDuration() | ||
.isPresent()); | ||
assertEquals(true, ph.info() | ||
.user() | ||
.isPresent()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void givenProcess_whenGetChildProcess_thenSuccess() throws IOException { | ||
int childProcessCount = 5; | ||
for (int i = 0; i < childProcessCount; i++) { | ||
String javaCmd = ProcessUtils.getJavaCmd() | ||
.getAbsolutePath(); | ||
ProcessBuilder processBuilder | ||
= new ProcessBuilder(javaCmd, "-version"); | ||
processBuilder.inheritIO().start(); | ||
} | ||
|
||
Stream<ProcessHandle> children = ProcessHandle.current() | ||
.children(); | ||
children.filter(ProcessHandle::isAlive) | ||
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info() | ||
.command())); | ||
Stream<ProcessHandle> descendants = ProcessHandle.current() | ||
.descendants(); | ||
descendants.filter(ProcessHandle::isAlive) | ||
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info() | ||
.command())); | ||
} | ||
|
||
@Test | ||
public void givenProcess_whenAddExitCallback_thenSuccess() throws Exception { | ||
String javaCmd = ProcessUtils.getJavaCmd() | ||
.getAbsolutePath(); | ||
ProcessBuilder processBuilder | ||
= new ProcessBuilder(javaCmd, "-version"); | ||
Process process = processBuilder.inheritIO() | ||
.start(); | ||
ProcessHandle processHandle = process.toHandle(); | ||
|
||
log.info("PID: {} has started", processHandle.getPid()); | ||
CompletableFuture<ProcessHandle> onProcessExit = processHandle.onExit(); | ||
onProcessExit.get(); | ||
assertEquals(false, processHandle.isAlive()); | ||
onProcessExit.thenAccept(ph -> { | ||
log.info("PID: {} has stopped", ph.getPid()); | ||
}); | ||
} | ||
|
||
} |