Added Java Process unit test

This commit is contained in:
Christian Rädel 2016-08-18 01:47:23 +02:00
parent 9450837585
commit e3fa46a9c7
1 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,67 @@
package org.baeldung.java.shell;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
public class JavaProcessUnitTest {
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
private static class StreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}
@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(consumer);
}
}
private Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
Assert.assertNotNull(s);
}
};
private String homeDirectory = System.getProperty("user.home");
@Test
public void givenProcess_whenCreatingViaRuntime_shouldSucceed() throws Exception {
Process process;
if (IS_WINDOWS) {
process = Runtime.getRuntime().exec(String.format("cmd.exe /c dir %s", homeDirectory));
} else {
process = Runtime.getRuntime().exec(String.format("sh -c ls %s", homeDirectory));
}
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
Assert.assertEquals(0, exitCode);
}
@Test
public void givenProcess_whenCreatingViaProcessBuilder_shouldSucceed() throws Exception {
ProcessBuilder builder = new ProcessBuilder();
if (IS_WINDOWS) {
builder.command("cmd.exe", "/c", "dir");
builder.inheritIO();
} else {
builder.command("sh", "-c", "ls");
}
builder.directory(new File(homeDirectory));
Process process = builder.start();
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
Assert.assertEquals(0, exitCode);
}
}