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