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
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.Disabled;
29 import org.junit.jupiter.api.Test;
30 import org.junitpioneer.jupiter.SetSystemProperty;
31
32
33
34
35
36 @SetSystemProperty(key = "org.apache.commons.exec.lenient", value = "false")
37 @SetSystemProperty(key = "org.apache.commons.exec.debug", value = "true")
38 public class LogOutputStreamTest {
39
40 private static final class SystemLogOutputStream extends LogOutputStream {
41
42 StringBuffer output = new StringBuffer();
43
44 private SystemLogOutputStream(final int level) {
45 super(level);
46 }
47
48 private SystemLogOutputStream(final int level, final Charset charset) {
49 super(level, charset);
50 }
51
52 private String getOutput() {
53 return output.toString();
54 }
55
56 @Override
57 protected void processLine(final String line, final int level) {
58 System.out.println(line);
59 output.append(line);
60 }
61 }
62
63 private final Executor exec = DefaultExecutor.builder().get();
64 private final File testDir = new File("src/test/scripts");
65 private OutputStream systemOut;
66
67 private final File environmentScript = TestUtil.resolveScriptForOS(testDir + "/environment");
68
69 private final File utf8CharacterScript = TestUtil.resolveScriptForOS(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 public void testStdout() throws Exception {
80 this.systemOut = new SystemLogOutputStream(1);
81 this.exec.setStreamHandler(new PumpStreamHandler(systemOut, systemOut));
82
83 final CommandLine cl = new CommandLine(environmentScript);
84 final int exitValue = exec.execute(cl);
85 assertFalse(exec.isFailure(exitValue));
86 }
87
88 @Test
89 @Disabled("The file utf8CharacterScript is missing from the repository and is not in its history")
90 public void testStdoutWithUTF8Characters() throws Exception {
91 this.systemOut = new SystemLogOutputStream(1, StandardCharsets.UTF_8);
92 this.exec.setStreamHandler(new PumpStreamHandler(systemOut, systemOut));
93
94 final CommandLine cl = new CommandLine(utf8CharacterScript);
95 final int exitValue = exec.execute(cl);
96 assertFalse(exec.isFailure(exitValue));
97 assertEquals("This string contains UTF-8 characters like the see no evil monkey \uD83D\uDE48 and the right single quotation mark \u2019",
98 ((SystemLogOutputStream) systemOut).getOutput());
99 }
100
101 }