Merge pull request #11502 from hkhan/BAEL-44334-enhance-java-process-test

[BAEL-44334] Enhance the process test to shutdown ExecutorService
This commit is contained in:
kwoyke 2021-11-26 09:26:59 +01:00 committed by GitHub
commit 91e38a4500
1 changed files with 37 additions and 6 deletions

View File

@ -1,12 +1,23 @@
package com.baeldung.java.shell; package com.baeldung.shell;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.*; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; import java.util.function.Consumer;
import static org.junit.Assert.assertEquals;
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");
@ -29,6 +40,18 @@ public class JavaProcessUnitIntegrationTest {
private String homeDirectory = System.getProperty("user.home"); private String homeDirectory = System.getProperty("user.home");
private ExecutorService executorService;
@Before
public void setUp() {
executorService = Executors.newSingleThreadExecutor();
}
@After
public void tearDown() {
executorService.shutdown();
}
@Test @Test
public void givenProcess_whenCreatingViaRuntime_shouldSucceed() throws Exception { public void givenProcess_whenCreatingViaRuntime_shouldSucceed() throws Exception {
Process process; Process process;
@ -38,9 +61,13 @@ public class JavaProcessUnitIntegrationTest {
process = Runtime.getRuntime().exec(String.format("sh -c ls %s", homeDirectory)); process = Runtime.getRuntime().exec(String.format("sh -c ls %s", homeDirectory));
} }
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer); StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
Executors.newSingleThreadExecutor().submit(streamGobbler);
Future<?> future = executorService.submit(streamGobbler);
int exitCode = process.waitFor(); int exitCode = process.waitFor();
Assert.assertEquals(0, exitCode);
// verify the stream output from the process
assertDoesNotThrow(() -> future.get(10, TimeUnit.SECONDS));
assertEquals(0, exitCode);
} }
@Test @Test
@ -54,8 +81,12 @@ public class JavaProcessUnitIntegrationTest {
builder.directory(new File(homeDirectory)); builder.directory(new File(homeDirectory));
Process process = builder.start(); Process process = builder.start();
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer); StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), consumer);
Executors.newSingleThreadExecutor().submit(streamGobbler);
Future<?> future = executorService.submit(streamGobbler);
int exitCode = process.waitFor(); int exitCode = process.waitFor();
Assert.assertEquals(0, exitCode);
// verify the stream output from the process
assertDoesNotThrow(() -> future.get(10, TimeUnit.SECONDS));
assertEquals(0, exitCode);
} }
} }