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    *      http://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  
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   * Test the LogOutputStream.
34   */
35  // turn on debug mode and throw an exception for each encountered problem
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 }