11package com .baeldung .processbuilder ;
22
3- import static org .hamcrest .Matchers .contains ;
43import static org .hamcrest .Matchers .containsString ;
54import static org .hamcrest .Matchers .empty ;
65import static org .hamcrest .Matchers .hasItem ;
@@ -83,24 +82,6 @@ public void givenProcessBuilder_whenModifyWorkingDir_thenSuccess() throws IOExce
8382 assertEquals ("No errors should be detected" , 0 , exitCode );
8483 }
8584
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-
10485 @ Test
10586 public void givenProcessBuilder_whenRedirectStandardOutput_thenSuccessWriting () throws IOException , InterruptedException {
10687 ProcessBuilder processBuilder = new ProcessBuilder ("java" , "-version" );
@@ -146,18 +127,20 @@ public void givenProcessBuilder_whenRedirectStandardOutput_thenSuccessAppending(
146127 assertThat ("Results should contain java version: " , lines , hasItem (containsString ("java version" )));
147128 }
148129
149- /* @Test
130+ @ Test
150131 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"));
132+ if (!isWindows ()) {
133+ List <ProcessBuilder > builders = Arrays .asList (
134+ new ProcessBuilder ("find" , "src" , "-name" , "*.java" , "-type" , "f" ),
135+ new ProcessBuilder ("wc" , "-l" ));
154136
155- List<Process> processes = ProcessBuilder.startPipeline(builders);
156- Process last = processes.get(processes.size() - 1);
137+ List <Process > processes = ProcessBuilder .startPipeline (builders );
138+ Process last = processes .get (processes .size () - 1 );
157139
158- List<String> output = readOutput(last.getInputStream());
159- assertThat("Results should not be empty", output, is(not(empty())));
160- }*/
140+ List <String > output = readOutput (last .getInputStream ());
141+ assertThat ("Results should not be empty" , output , is (not (empty ())));
142+ }
143+ }
161144
162145 @ Test
163146 public void givenProcessBuilder_whenInheritIO_thenSuccess () throws IOException , InterruptedException {
@@ -178,4 +161,22 @@ private List<String> readOutput(InputStream inputStream) throws IOException {
178161 }
179162 }
180163
164+ private List <String > getDirectoryListingCommand () {
165+ return isWindows () ? Arrays .asList ("cmd.exe" , "/c" , "dir" ) : Arrays .asList ("/bin/sh" , "-c" , "ls" );
166+ }
167+
168+ private List <String > getGreetingCommand () {
169+ return isWindows () ? Arrays .asList ("cmd.exe" , "/c" , "echo %GREETING%" ) : Arrays .asList ("/bin/bash" , "-c" , "echo $GREETING" );
170+ }
171+
172+ private List <String > getEchoCommand () {
173+ return isWindows () ? Arrays .asList ("cmd.exe" , "/c" , "echo hello" ) : Arrays .asList ("/bin/sh" , "-c" , "echo hello" );
174+ }
175+
176+ private boolean isWindows () {
177+ return System .getProperty ("os.name" )
178+ .toLowerCase ()
179+ .startsWith ("windows" );
180+ }
181+
181182}
0 commit comments