Coverage Report - org.apache.maven.surefire.report.ConsoleOutputCapture
 
Classes in this File Line Coverage Branch Coverage Complexity
ConsoleOutputCapture
0%
0/5
N/A
1.25
ConsoleOutputCapture$ForwardingPrintStream
0%
0/24
0%
0/2
1.25
 
 1  
 package org.apache.maven.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.ByteArrayOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.PrintStream;
 25  
 import org.apache.maven.surefire.util.internal.ByteBuffer;
 26  
 
 27  
 /**
 28  
  * Deals with system.out/err.
 29  
  * <p/>
 30  
  */
 31  0
 public class ConsoleOutputCapture
 32  
 {
 33  
     public static void startCapture( ConsoleOutputReceiver target )
 34  
     {
 35  0
         System.setOut( new ForwardingPrintStream( true, target ) );
 36  
 
 37  0
         System.setErr( new ForwardingPrintStream( false, target ) );
 38  0
     }
 39  
 
 40  0
     private static class ForwardingPrintStream
 41  
         extends PrintStream
 42  
     {
 43  
         private final boolean isStdout;
 44  
 
 45  
         private final ConsoleOutputReceiver target;
 46  
 
 47  
         ForwardingPrintStream( boolean stdout, ConsoleOutputReceiver target )
 48  
         {
 49  0
             super( new ByteArrayOutputStream() );
 50  0
             isStdout = stdout;
 51  0
             this.target = target;
 52  0
         }
 53  
 
 54  
         public void write( byte[] buf, int off, int len )
 55  
         {
 56  0
             target.writeTestOutput( buf, off, len, isStdout );
 57  0
         }
 58  
 
 59  
         public void write( byte[] b )
 60  
             throws IOException
 61  
         {
 62  0
             target.writeTestOutput( b, 0, b.length, isStdout );
 63  0
         }
 64  
 
 65  
         public void write( int b )
 66  
         {
 67  0
             byte[] buf = new byte[1];
 68  0
             buf[0] = (byte) b;
 69  
             try
 70  
             {
 71  0
                 write( buf );
 72  
             }
 73  0
             catch ( IOException e )
 74  
             {
 75  0
                 setError();
 76  0
             }
 77  0
         }
 78  
 
 79  0
         static final byte[] newline = new byte[]{ (byte) '\n' };
 80  
 
 81  
         public void println( String s )
 82  
         {
 83  0
             if (s == null){
 84  0
                 s = "null"; // Shamelessy taken from super.print
 85  
             }
 86  0
             final byte[] bytes = s.getBytes();
 87  0
             final byte[] join = ByteBuffer.join( bytes, 0, bytes.length, newline, 0, 1 );
 88  0
             target.writeTestOutput( join, 0, join.length, isStdout );
 89  0
         }
 90  
 
 91  
         public void close()
 92  
         {
 93  0
         }
 94  
 
 95  
         public void flush()
 96  
         {
 97  0
         }
 98  
     }
 99  
 
 100  
 }