View Javadoc
1   package org.apache.maven.plugin.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.PrintStream;
23  import java.nio.CharBuffer;
24  import java.nio.charset.CharacterCodingException;
25  import java.nio.charset.Charset;
26  
27  import org.apache.maven.surefire.report.ReportEntry;
28  
29  import static java.nio.ByteBuffer.wrap;
30  import static java.nio.charset.Charset.defaultCharset;
31  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * Outputs test system out/system err directly to the console
35   * <br>
36   * Just a step on the road to getting the separation of reporting concerns
37   * operating properly.
38   *
39   * @author Kristian Rosenvold
40   */
41  public class DirectConsoleOutput
42      implements TestcycleConsoleOutputReceiver
43  {
44      private static final Charset STANDARD_CHARSET = defaultCharset();
45  
46      private final PrintStream sout;
47  
48      private final PrintStream serr;
49  
50      public DirectConsoleOutput( PrintStream sout, PrintStream serr )
51      {
52          this.sout = requireNonNull( sout );
53          this.serr = requireNonNull( serr );
54      }
55  
56      @Override
57      public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
58      {
59          PrintStream stream = stdout ? sout : serr;
60          //noinspection SynchronizationOnLocalVariableOrMethodParameter
61          synchronized ( stream )
62          {
63              try
64              {
65                  CharBuffer decode = STANDARD_CHARSET.newDecoder().decode( wrap( buf, off, len ) );
66                  stream.append( decode );
67              }
68              catch ( CharacterCodingException e )
69              {
70                  stream.write( buf, off, len );
71              }
72          }
73      }
74  
75      @Override
76      public void testSetStarting( ReportEntry reportEntry )
77      {
78      }
79  
80      @Override
81      public void testSetCompleted( ReportEntry report )
82      {
83      }
84  
85      @Override
86      public void close()
87      {
88      }
89  }