View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Test the LogOutputStream.
37   */
38  // turn on debug mode and throw an exception for each encountered problem
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  }