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