Skip to content

Commit 1dac10b

Browse files
Jonathan Paul CookJonathan Paul Cook
authored andcommitted
BAEL-2541 - Guide to ProcessBuilder API
- Update test commands to work on windows
1 parent f14c3fb commit 1dac10b

1 file changed

Lines changed: 181 additions & 159 deletions

File tree

Lines changed: 181 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,181 @@
1-
package com.baeldung.processbuilder;
2-
3-
import static org.hamcrest.Matchers.contains;
4-
import static org.hamcrest.Matchers.containsString;
5-
import static org.hamcrest.Matchers.empty;
6-
import static org.hamcrest.Matchers.hasItem;
7-
import static org.hamcrest.Matchers.is;
8-
import static org.hamcrest.Matchers.not;
9-
import static org.junit.Assert.assertEquals;
10-
import static org.junit.Assert.assertThat;
11-
12-
import java.io.BufferedReader;
13-
import java.io.File;
14-
import java.io.IOException;
15-
import java.io.InputStream;
16-
import java.io.InputStreamReader;
17-
import java.lang.ProcessBuilder.Redirect;
18-
import java.nio.file.Files;
19-
import java.util.Arrays;
20-
import java.util.List;
21-
import java.util.Map;
22-
import java.util.concurrent.ExecutionException;
23-
import java.util.stream.Collectors;
24-
25-
import org.junit.Rule;
26-
import org.junit.Test;
27-
import org.junit.rules.TemporaryFolder;
28-
29-
public class ProcessBuilderUnitTest {
30-
31-
@Rule
32-
public TemporaryFolder tempFolder = new TemporaryFolder();
33-
34-
@Test
35-
public void givenProcessBuilder_whenInvokeStart_thenSuccess() throws IOException, InterruptedException, ExecutionException {
36-
ProcessBuilder processBuilder = new ProcessBuilder("java", "-version");
37-
processBuilder.redirectErrorStream(true);
38-
39-
Process process = processBuilder.start();
40-
41-
List<String> results = readOutput(process.getInputStream());
42-
assertThat("Results should not be empty", results, is(not(empty())));
43-
assertThat("Results should contain java version: ", results, hasItem(containsString("java version")));
44-
45-
int exitCode = process.waitFor();
46-
assertEquals("No errors should be detected", 0, exitCode);
47-
}
48-
49-
@Test
50-
public void givenProcessBuilder_whenModifyEnvironment_thenSuccess() throws IOException, InterruptedException {
51-
ProcessBuilder processBuilder = new ProcessBuilder();
52-
Map<String, String> environment = processBuilder.environment();
53-
environment.forEach((key, value) -> System.out.println(key + value));
54-
55-
environment.put("GREETING", "Hola Mundo");
56-
57-
processBuilder.command("/bin/bash", "-c", "echo $GREETING");
58-
Process process = processBuilder.start();
59-
60-
List<String> results = readOutput(process.getInputStream());
61-
assertThat("Results should not be empty", results, is(not(empty())));
62-
assertThat("Results should contain a greeting ", results, hasItem(containsString("Hola Mundo")));
63-
64-
int exitCode = process.waitFor();
65-
assertEquals("No errors should be detected", 0, exitCode);
66-
}
67-
68-
@Test
69-
public void givenProcessBuilder_whenModifyWorkingDir_thenSuccess() throws IOException, InterruptedException {
70-
ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", "-c", "ls");
71-
72-
processBuilder.directory(new File("src"));
73-
Process process = processBuilder.start();
74-
75-
List<String> results = readOutput(process.getInputStream());
76-
assertThat("Results should not be empty", results, is(not(empty())));
77-
assertThat("Results should contain directory listing: ", results, contains("main", "test"));
78-
79-
int exitCode = process.waitFor();
80-
assertEquals("No errors should be detected", 0, exitCode);
81-
}
82-
83-
@Test
84-
public void givenProcessBuilder_whenRedirectStandardOutput_thenSuccessWriting() throws IOException, InterruptedException {
85-
ProcessBuilder processBuilder = new ProcessBuilder("java", "-version");
86-
87-
processBuilder.redirectErrorStream(true);
88-
File log = tempFolder.newFile("java-version.log");
89-
processBuilder.redirectOutput(log);
90-
91-
Process process = processBuilder.start();
92-
93-
assertEquals("If redirected, should be -1 ", -1, process.getInputStream()
94-
.read());
95-
int exitCode = process.waitFor();
96-
assertEquals("No errors should be detected", 0, exitCode);
97-
98-
List<String> lines = Files.lines(log.toPath())
99-
.collect(Collectors.toList());
100-
101-
assertThat("Results should not be empty", lines, is(not(empty())));
102-
assertThat("Results should contain java version: ", lines, hasItem(containsString("java version")));
103-
}
104-
105-
@Test
106-
public void givenProcessBuilder_whenRedirectStandardOutput_thenSuccessAppending() throws IOException, InterruptedException {
107-
ProcessBuilder processBuilder = new ProcessBuilder("java", "-version");
108-
109-
File log = tempFolder.newFile("java-version-append.log");
110-
processBuilder.redirectErrorStream(true);
111-
processBuilder.redirectOutput(Redirect.appendTo(log));
112-
113-
Process process = processBuilder.start();
114-
115-
assertEquals("If redirected output, should be -1 ", -1, process.getInputStream()
116-
.read());
117-
118-
int exitCode = process.waitFor();
119-
assertEquals("No errors should be detected", 0, exitCode);
120-
121-
List<String> lines = Files.lines(log.toPath())
122-
.collect(Collectors.toList());
123-
124-
assertThat("Results should not be empty", lines, is(not(empty())));
125-
assertThat("Results should contain java version: ", lines, hasItem(containsString("java version")));
126-
}
127-
128-
@Test
129-
public void givenProcessBuilder_whenStartingPipeline_thenSuccess() throws IOException, InterruptedException {
130-
List<ProcessBuilder> builders = Arrays.asList(
131-
new ProcessBuilder("find", "src", "-name", "*.java", "-type", "f"),
132-
new ProcessBuilder("wc", "-l"));
133-
134-
List<Process> processes = ProcessBuilder.startPipeline(builders);
135-
Process last = processes.get(processes.size() - 1);
136-
137-
List<String> output = readOutput(last.getInputStream());
138-
assertThat("Results should not be empty", output, is(not(empty())));
139-
}
140-
141-
@Test
142-
public void givenProcessBuilder_whenInheritIO_thenSuccess() throws IOException, InterruptedException {
143-
ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", "-c", "echo hello");
144-
145-
processBuilder.inheritIO();
146-
Process process = processBuilder.start();
147-
148-
int exitCode = process.waitFor();
149-
assertEquals("No errors should be detected", 0, exitCode);
150-
}
151-
152-
private List<String> readOutput(InputStream inputStream) throws IOException {
153-
try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) {
154-
return output.lines()
155-
.collect(Collectors.toList());
156-
}
157-
}
158-
159-
}
1+
package com.baeldung.processbuilder;
2+
3+
import static org.hamcrest.Matchers.contains;
4+
import static org.hamcrest.Matchers.containsString;
5+
import static org.hamcrest.Matchers.empty;
6+
import static org.hamcrest.Matchers.hasItem;
7+
import static org.hamcrest.Matchers.hasItems;
8+
import static org.hamcrest.Matchers.is;
9+
import static org.hamcrest.Matchers.not;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertThat;
12+
13+
import java.io.BufferedReader;
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.InputStreamReader;
18+
import java.lang.ProcessBuilder.Redirect;
19+
import java.nio.file.Files;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.concurrent.ExecutionException;
24+
import java.util.stream.Collectors;
25+
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.rules.TemporaryFolder;
29+
30+
public class ProcessBuilderUnitTest {
31+
32+
@Rule
33+
public TemporaryFolder tempFolder = new TemporaryFolder();
34+
35+
@Test
36+
public void givenProcessBuilder_whenInvokeStart_thenSuccess() throws IOException, InterruptedException, ExecutionException {
37+
ProcessBuilder processBuilder = new ProcessBuilder("java", "-version");
38+
processBuilder.redirectErrorStream(true);
39+
40+
Process process = processBuilder.start();
41+
42+
List<String> results = readOutput(process.getInputStream());
43+
assertThat("Results should not be empty", results, is(not(empty())));
44+
assertThat("Results should contain java version: ", results, hasItem(containsString("java version")));
45+
46+
int exitCode = process.waitFor();
47+
assertEquals("No errors should be detected", 0, exitCode);
48+
}
49+
50+
@Test
51+
public void givenProcessBuilder_whenModifyEnvironment_thenSuccess() throws IOException, InterruptedException {
52+
ProcessBuilder processBuilder = new ProcessBuilder();
53+
Map<String, String> environment = processBuilder.environment();
54+
environment.forEach((key, value) -> System.out.println(key + value));
55+
56+
environment.put("GREETING", "Hola Mundo");
57+
58+
List<String> command = getGreetingCommand();
59+
processBuilder.command(command);
60+
Process process = processBuilder.start();
61+
62+
List<String> results = readOutput(process.getInputStream());
63+
assertThat("Results should not be empty", results, is(not(empty())));
64+
assertThat("Results should contain a greeting ", results, hasItem(containsString("Hola Mundo")));
65+
66+
int exitCode = process.waitFor();
67+
assertEquals("No errors should be detected", 0, exitCode);
68+
}
69+
70+
@Test
71+
public void givenProcessBuilder_whenModifyWorkingDir_thenSuccess() throws IOException, InterruptedException {
72+
List<String> command = getDirectoryListingCommand();
73+
ProcessBuilder processBuilder = new ProcessBuilder(command);
74+
75+
processBuilder.directory(new File("src"));
76+
Process process = processBuilder.start();
77+
78+
List<String> results = readOutput(process.getInputStream());
79+
assertThat("Results should not be empty", results, is(not(empty())));
80+
assertThat("Results should contain directory listing: ", results, hasItems(containsString("main"), containsString("test")));
81+
82+
int exitCode = process.waitFor();
83+
assertEquals("No errors should be detected", 0, exitCode);
84+
}
85+
86+
private List<String> getDirectoryListingCommand() {
87+
return isWindows() ? Arrays.asList("cmd.exe", "/c", "dir") : Arrays.asList("/bin/sh", "-c", "ls");
88+
}
89+
90+
private List<String> getGreetingCommand() {
91+
return isWindows() ? Arrays.asList("cmd.exe", "/c", "echo %GREETING%") : Arrays.asList("/bin/bash", "-c", "echo $GREETING");
92+
}
93+
94+
private List<String> getEchoCommand() {
95+
return isWindows() ? Arrays.asList("cmd.exe", "/c", "echo hello") : Arrays.asList("/bin/sh", "-c", "echo hello");
96+
}
97+
98+
private boolean isWindows() {
99+
return System.getProperty("os.name")
100+
.toLowerCase()
101+
.startsWith("windows");
102+
}
103+
104+
@Test
105+
public void givenProcessBuilder_whenRedirectStandardOutput_thenSuccessWriting() throws IOException, InterruptedException {
106+
ProcessBuilder processBuilder = new ProcessBuilder("java", "-version");
107+
108+
processBuilder.redirectErrorStream(true);
109+
File log = tempFolder.newFile("java-version.log");
110+
processBuilder.redirectOutput(log);
111+
112+
Process process = processBuilder.start();
113+
114+
assertEquals("If redirected, should be -1 ", -1, process.getInputStream()
115+
.read());
116+
int exitCode = process.waitFor();
117+
assertEquals("No errors should be detected", 0, exitCode);
118+
119+
List<String> lines = Files.lines(log.toPath())
120+
.collect(Collectors.toList());
121+
122+
assertThat("Results should not be empty", lines, is(not(empty())));
123+
assertThat("Results should contain java version: ", lines, hasItem(containsString("java version")));
124+
}
125+
126+
@Test
127+
public void givenProcessBuilder_whenRedirectStandardOutput_thenSuccessAppending() throws IOException, InterruptedException {
128+
ProcessBuilder processBuilder = new ProcessBuilder("java", "-version");
129+
130+
File log = tempFolder.newFile("java-version-append.log");
131+
processBuilder.redirectErrorStream(true);
132+
processBuilder.redirectOutput(Redirect.appendTo(log));
133+
134+
Process process = processBuilder.start();
135+
136+
assertEquals("If redirected output, should be -1 ", -1, process.getInputStream()
137+
.read());
138+
139+
int exitCode = process.waitFor();
140+
assertEquals("No errors should be detected", 0, exitCode);
141+
142+
List<String> lines = Files.lines(log.toPath())
143+
.collect(Collectors.toList());
144+
145+
assertThat("Results should not be empty", lines, is(not(empty())));
146+
assertThat("Results should contain java version: ", lines, hasItem(containsString("java version")));
147+
}
148+
149+
/* @Test
150+
public void givenProcessBuilder_whenStartingPipeline_thenSuccess() throws IOException, InterruptedException {
151+
List<ProcessBuilder> builders = Arrays.asList(
152+
new ProcessBuilder("find", "src", "-name", "*.java", "-type", "f"),
153+
new ProcessBuilder("wc", "-l"));
154+
155+
List<Process> processes = ProcessBuilder.startPipeline(builders);
156+
Process last = processes.get(processes.size() - 1);
157+
158+
List<String> output = readOutput(last.getInputStream());
159+
assertThat("Results should not be empty", output, is(not(empty())));
160+
}*/
161+
162+
@Test
163+
public void givenProcessBuilder_whenInheritIO_thenSuccess() throws IOException, InterruptedException {
164+
List<String> command = getEchoCommand();
165+
ProcessBuilder processBuilder = new ProcessBuilder(command);
166+
167+
processBuilder.inheritIO();
168+
Process process = processBuilder.start();
169+
170+
int exitCode = process.waitFor();
171+
assertEquals("No errors should be detected", 0, exitCode);
172+
}
173+
174+
private List<String> readOutput(InputStream inputStream) throws IOException {
175+
try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) {
176+
return output.lines()
177+
.collect(Collectors.toList());
178+
}
179+
}
180+
181+
}

0 commit comments

Comments
 (0)