|
| 1 | +package com.baeldung.java9.process; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.junit.jupiter.api.Assertions.*; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.lang.String; |
| 11 | +import java.util.Optional; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import java.lang.Integer; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +class ProcessUnderstandingTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void givenSourceProgram_whenExecutedFromAnotherProgram_thenSourceProgramOutput3() throws IOException { |
| 21 | + Process process = Runtime.getRuntime() |
| 22 | + .exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\OutputStreamExample.java"); |
| 23 | + process = Runtime.getRuntime() |
| 24 | + .exec("java -cp src/main/java com.baeldung.java9.process.OutputStreamExample"); |
| 25 | + BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 26 | + int value = Integer.parseInt(output.readLine()); |
| 27 | + assertEquals(3, value); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenSourceProgram_whenReadingInputStream_thenFirstLineEquals3() throws IOException { |
| 32 | + Process process = Runtime.getRuntime() |
| 33 | + .exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\OutputStreamExample.java"); |
| 34 | + process = Runtime.getRuntime() |
| 35 | + .exec("java -cp src/main/java com.baeldung.java9.process.OutputStreamExample"); |
| 36 | + BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 37 | + int value = Integer.parseInt(output.readLine()); |
| 38 | + assertEquals(3, value); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenSubProcess_whenEncounteringError_thenErrorStreamNotNull() throws IOException { |
| 43 | + Process process = Runtime.getRuntime() |
| 44 | + .exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\ProcessCompilationError.java"); |
| 45 | + BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream())); |
| 46 | + String errorString = error.readLine(); |
| 47 | + assertNotNull(errorString); |
| 48 | + } |
| 49 | + |
| 50 | + //@Test - windows specific |
| 51 | + public void givenSubProcess_thenStartSuccessIsAlive() throws IOException { |
| 52 | + ProcessBuilder builder = new ProcessBuilder("notepad.exe"); |
| 53 | + assertTrue(builder.start().isAlive()); |
| 54 | + } |
| 55 | + |
| 56 | + //@Test - windows specific |
| 57 | + public void givenSubProcess_whenDestroying_thenProcessNotAlive() throws IOException, InterruptedException { |
| 58 | + ProcessBuilder builder = new ProcessBuilder("notepad.exe"); |
| 59 | + Process process = builder.start(); |
| 60 | + Thread.sleep(10000); |
| 61 | + process.destroy(); |
| 62 | + assertFalse(process.isAlive()); |
| 63 | + } |
| 64 | + |
| 65 | + //@Test - windows specific |
| 66 | + public void givenSubProcess_whenAlive_thenDestroyForcibly() throws IOException, InterruptedException { |
| 67 | + ProcessBuilder builder = new ProcessBuilder("notepad.exe"); |
| 68 | + Process process = builder.start(); |
| 69 | + Thread.sleep(10000); |
| 70 | + process.destroy(); |
| 71 | + if (process.isAlive()) { |
| 72 | + process.destroyForcibly(); |
| 73 | + } |
| 74 | + assertFalse(process.isAlive()); |
| 75 | + } |
| 76 | + |
| 77 | + //@Test - windows specific |
| 78 | + public void givenSubProcess_checkAlive() throws IOException, InterruptedException { |
| 79 | + ProcessBuilder builder = new ProcessBuilder("notepad.exe"); |
| 80 | + Process process = builder.start(); |
| 81 | + Thread.sleep(10000); |
| 82 | + process.destroy(); |
| 83 | + assertFalse(process.isAlive()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenProcessNotCreated_fromWithinJavaApplicationDestroying_thenProcessNotAlive() { |
| 88 | + Optional<ProcessHandle> optionalProcessHandle = ProcessHandle.of(5232); |
| 89 | + ProcessHandle processHandle = optionalProcessHandle.get(); |
| 90 | + processHandle.destroy(); |
| 91 | + assertFalse(processHandle.isAlive()); |
| 92 | + } |
| 93 | + |
| 94 | + //@Test - windows specific |
| 95 | + public void givenSubProcess_whenCurrentThreadWaitsIndefinitelyuntilSubProcessEnds_thenProcessWaitForReturnsGrt0() throws IOException, InterruptedException { |
| 96 | + ProcessBuilder builder = new ProcessBuilder("notepad.exe"); |
| 97 | + Process process = builder.start(); |
| 98 | + assertThat(process.waitFor() >= 0); |
| 99 | + } |
| 100 | + |
| 101 | + //@Test - windows specific |
| 102 | + public void givenSubProcess_whenCurrentThreadWaitsAndSubProcessNotTerminated_thenProcessWaitForReturnsFalse() throws IOException, InterruptedException { |
| 103 | + ProcessBuilder builder = new ProcessBuilder("notepad.exe"); |
| 104 | + Process process = builder.start(); |
| 105 | + assertFalse(process.waitFor(1, TimeUnit.SECONDS)); |
| 106 | + } |
| 107 | + |
| 108 | + //@Test - windows specific |
| 109 | + public void givenSubProcess_whenCurrentThreadWillNotWaitIndefinitelyforSubProcessToEnd_thenProcessExitValueReturnsGrt0() throws IOException { |
| 110 | + ProcessBuilder builder = new ProcessBuilder("notepad.exe"); |
| 111 | + Process process = builder.start(); |
| 112 | + assertThat(process.exitValue() >= 0); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void givenRunningProcesses_whenFilterOnProcessIdRange_thenGetSelectedProcessPid() { |
| 117 | + assertThat(((int) ProcessHandle.allProcesses() |
| 118 | + .filter(ph -> (ph.pid() > 10000 && ph.pid() < 50000)) |
| 119 | + .count()) > 0); |
| 120 | + } |
| 121 | +} |
0 commit comments