[Java-5762]: Using Pipes in Runtime.exec() Command (#12941)

Co-authored-by: Harpal Singh <harpal.singh@kaleyra.com>
This commit is contained in:
Harry9656 2022-11-23 18:31:21 +01:00 committed by GitHub
parent 66fb7ea877
commit 0ecc5b2c11
3 changed files with 74 additions and 49 deletions

View File

@ -0,0 +1,21 @@
package com.baeldung.shell;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.function.Consumer;
public class StreamGobbler implements Runnable {
private final InputStream inputStream;
private final 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);
}
}

View File

@ -5,10 +5,7 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -21,21 +18,6 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
public class JavaProcessUnitIntegrationTest { public class JavaProcessUnitIntegrationTest {
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows"); 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 = Assert::assertNotNull; private Consumer<String> consumer = Assert::assertNotNull;
private String homeDirectory = System.getProperty("user.home"); private String homeDirectory = System.getProperty("user.home");
@ -58,7 +40,7 @@ public class JavaProcessUnitIntegrationTest {
if (IS_WINDOWS) { if (IS_WINDOWS) {
process = Runtime.getRuntime().exec(String.format("cmd.exe /c dir %s", homeDirectory)); process = Runtime.getRuntime().exec(String.format("cmd.exe /c dir %s", homeDirectory));
} else { } else {
process = Runtime.getRuntime().exec(String.format("sh -c ls %s", homeDirectory)); process = Runtime.getRuntime().exec(String.format("/bin/sh -c ls %s", homeDirectory));
} }
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer); StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
@ -70,13 +52,34 @@ public class JavaProcessUnitIntegrationTest {
assertEquals(0, exitCode); assertEquals(0, exitCode);
} }
@Test
public void shouldExecuteCommandWithPipesWithRuntimeProcess(){
Process process;
try {
if (IS_WINDOWS) {
process = Runtime.getRuntime().exec(String.format("cmd.exe /c dir %s | findstr \"Desktop\"", homeDirectory));
} else {
process = Runtime.getRuntime().exec(String.format("/bin/sh -c ls %s | grep \"Desktop\"", homeDirectory));
}
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
Future<?> future = executorService.submit(streamGobbler);
int exitCode = process.waitFor();
// verify the stream output from the process
assertDoesNotThrow(() -> future.get(10, TimeUnit.SECONDS));
assertEquals(0, exitCode);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test @Test
public void givenProcess_whenCreatingViaProcessBuilder_shouldSucceed() throws Exception { public void givenProcess_whenCreatingViaProcessBuilder_shouldSucceed() throws Exception {
ProcessBuilder builder = new ProcessBuilder(); ProcessBuilder builder = new ProcessBuilder();
if (IS_WINDOWS) { if (IS_WINDOWS) {
builder.command("cmd.exe", "/c", "dir"); builder.command("cmd.exe", "/c", "dir");
} else { } else {
builder.command("sh", "-c", "ls"); builder.command("/bin/sh", "-c", "ls");
} }
builder.directory(new File(homeDirectory)); builder.directory(new File(homeDirectory));
Process process = builder.start(); Process process = builder.start();

View File

@ -1,15 +1,41 @@
package com.baeldung.system.exit; package com.baeldung.system.exit;
import static org.junit.Assert.assertEquals;
import java.security.Permission;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.security.Permission;
import static org.junit.Assert.assertEquals;
@Ignore("This test is ignored because it tests deprecated code")
public class SystemExitUnitTest { public class SystemExitUnitTest {
private SecurityManager securityManager;
private SystemExitExample example;
@Before
public void setUp() {
example = new SystemExitExample();
securityManager = System.getSecurityManager();
System.setSecurityManager(new NoExitSecurityManager());
}
@After
public void tearDown() throws Exception {
System.setSecurityManager(securityManager);
}
@Test
public void testExit() throws Exception {
try {
example.readFile();
} catch (ExitException e) {
assertEquals("Exit status", 2, e.status);
}
}
protected static class ExitException extends SecurityException { protected static class ExitException extends SecurityException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -35,29 +61,4 @@ public class SystemExitUnitTest {
throw new ExitException(status); throw new ExitException(status);
} }
} }
private SecurityManager securityManager;
private SystemExitExample example;
@Before
public void setUp() throws Exception {
example = new SystemExitExample();
securityManager = System.getSecurityManager();
System.setSecurityManager(new NoExitSecurityManager());
}
@After
public void tearDown() throws Exception {
System.setSecurityManager(securityManager);
}
@Test
public void testExit() throws Exception {
try {
example.readFile();
} catch (ExitException e) {
assertEquals("Exit status", 2, e.status);
}
}
} }