1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.exec;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23
24 import java.io.File;
25 import java.io.OutputStream;
26 import java.nio.charset.Charset;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Path;
29
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.Disabled;
32 import org.junit.jupiter.api.Test;
33 import org.junitpioneer.jupiter.SetSystemProperty;
34
35
36
37
38
39 @SetSystemProperty(key = "org.apache.commons.exec.lenient", value = "false")
40 @SetSystemProperty(key = "org.apache.commons.exec.debug", value = "true")
41 class LogOutputStreamTest {
42
43 private static final class TestLogOutputStream extends LogOutputStream {
44
45 private final StringBuffer output = new StringBuffer();
46
47 private TestLogOutputStream(final int level) {
48 super(level);
49 }
50
51 private TestLogOutputStream(final int level, final Charset charset) {
52 super(level, charset);
53 }
54
55 private String getOutput() {
56 return output.toString();
57 }
58
59 @Override
60 protected void processLine(final String line, final int level) {
61 output.append(line);
62 }
63 }
64
65 private final Executor exec = DefaultExecutor.builder().get();
66 private final File testDir = new File("src/test/scripts");
67 private OutputStream systemOut;
68 private final Path environmentScript = TestUtil.resolveScriptPathForOS(testDir + "/environment");
69 private final Path utf8CharacterScript = TestUtil.resolveScriptPathForOS(testDir + "/utf8Characters");
70
71 @AfterEach
72 public void tearDown() throws Exception {
73 if (this.systemOut != null) {
74 this.systemOut.close();
75 }
76 }
77
78 @Test
79 void testStdout() throws Exception {
80 this.systemOut = new TestLogOutputStream(1);
81 this.exec.setStreamHandler(new PumpStreamHandler(systemOut, systemOut));
82 final CommandLine cl = new CommandLine(environmentScript);
83 final int exitValue = exec.execute(cl);
84 assertFalse(exec.isFailure(exitValue));
85 }
86
87 @Test
88 @Disabled("The file utf8CharacterScript is missing from the repository and is not in its history")
89 void testStdoutWithUTF8Characters() throws Exception {
90 this.systemOut = new TestLogOutputStream(1, StandardCharsets.UTF_8);
91 this.exec.setStreamHandler(new PumpStreamHandler(systemOut, systemOut));
92 final CommandLine cl = new CommandLine(utf8CharacterScript);
93 final int exitValue = exec.execute(cl);
94 assertFalse(exec.isFailure(exitValue));
95 assertEquals("This string contains UTF-8 characters like the see no evil monkey \uD83D\uDE48 and the right single quotation mark \u2019",
96 ((TestLogOutputStream) systemOut).getOutput());
97 }
98
99 }