Coverage Report - org.apache.maven.surefire.booter.ForkingRunListener
 
Classes in this File Line Coverage Branch Coverage Complexity
ForkingRunListener
47%
56/119
30%
8/26
1,609
 
 1  
 package org.apache.maven.surefire.booter;
 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.util.Enumeration;
 24  
 import java.util.Properties;
 25  
 
 26  
 import org.apache.maven.surefire.report.ConsoleLogger;
 27  
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 28  
 import org.apache.maven.surefire.report.ReportEntry;
 29  
 import org.apache.maven.surefire.report.RunListener;
 30  
 import org.apache.maven.surefire.report.SafeThrowable;
 31  
 import org.apache.maven.surefire.report.StackTraceWriter;
 32  
 import org.apache.maven.surefire.util.internal.StringUtils;
 33  
 
 34  
 /**
 35  
  * Encodes the full output of the test run to the stdout stream.
 36  
  * <p/>
 37  
  * This class and the ForkClient contain the full definition of the
 38  
  * "wire-level" protocol used by the forked process. The protocol
 39  
  * is *not* part of any public api and may change without further
 40  
  * notice.
 41  
  * <p/>
 42  
  * This class is threadsafe.
 43  
  * <p/>
 44  
  * The synchronization in the underlying PrintStream (target instance)
 45  
  * is used to preserve thread safety of the output stream. To perform
 46  
  * multiple writes/prints for a single request, they must
 47  
  * synchronize on "target" variable in this class.
 48  
  *
 49  
  * @author Kristian Rosenvold
 50  
  */
 51  
 public class ForkingRunListener
 52  
     implements RunListener, ConsoleLogger, ConsoleOutputReceiver
 53  
 {
 54  
     public static final byte BOOTERCODE_TESTSET_STARTING = (byte) '1';
 55  
 
 56  
     public static final byte BOOTERCODE_TESTSET_COMPLETED = (byte) '2';
 57  
 
 58  
     public static final byte BOOTERCODE_STDOUT = (byte) '3';
 59  
 
 60  
     public static final byte BOOTERCODE_STDERR = (byte) '4';
 61  
 
 62  
     public static final byte BOOTERCODE_TEST_STARTING = (byte) '5';
 63  
 
 64  
     public static final byte BOOTERCODE_TEST_SUCCEEDED = (byte) '6';
 65  
 
 66  
     public static final byte BOOTERCODE_TEST_ERROR = (byte) '7';
 67  
 
 68  
     public static final byte BOOTERCODE_TEST_FAILED = (byte) '8';
 69  
 
 70  
     public static final byte BOOTERCODE_TEST_SKIPPED = (byte) '9';
 71  
 
 72  
     public static final byte BOOTERCODE_TEST_ASSUMPTIONFAILURE = (byte) 'G';
 73  
 
 74  
     public static final byte BOOTERCODE_CONSOLE = (byte) 'H';
 75  
 
 76  
     public static final byte BOOTERCODE_SYSPROPS = (byte) 'I';
 77  
 
 78  
     public static final byte BOOTERCODE_NEXT_TEST = (byte) 'N';
 79  
 
 80  
     public static final byte BOOTERCODE_ERROR = (byte) 'X';
 81  
 
 82  
     public static final byte BOOTERCODE_BYE = (byte) 'Z';
 83  
 
 84  
 
 85  
     private final PrintStream target;
 86  
 
 87  
     private final Integer testSetChannelId;
 88  
 
 89  
     private final boolean trimStackTraces;
 90  
 
 91  
     private final byte[] stdOutHeader;
 92  
 
 93  
     private final byte[] stdErrHeader;
 94  
 
 95  
     public ForkingRunListener( PrintStream target, int testSetChannelId, boolean trimStackTraces )
 96  1
     {
 97  1
         this.target = target;
 98  1
         this.testSetChannelId = testSetChannelId;
 99  1
         this.trimStackTraces = trimStackTraces;
 100  1
         stdOutHeader = createHeader( BOOTERCODE_STDOUT, testSetChannelId );
 101  1
         stdErrHeader = createHeader( BOOTERCODE_STDERR, testSetChannelId );
 102  1
         sendProps();
 103  1
     }
 104  
 
 105  
     public void testSetStarting( ReportEntry report )
 106  
     {
 107  0
         target.print( toString( BOOTERCODE_TESTSET_STARTING, report, testSetChannelId ) );
 108  0
     }
 109  
 
 110  
     public void testSetCompleted( ReportEntry report )
 111  
     {
 112  0
         target.print( toString( BOOTERCODE_TESTSET_COMPLETED, report, testSetChannelId ) );
 113  0
     }
 114  
 
 115  
     public void testStarting( ReportEntry report )
 116  
     {
 117  0
         target.print( toString( BOOTERCODE_TEST_STARTING, report, testSetChannelId ) );
 118  0
     }
 119  
 
 120  
     public void testSucceeded( ReportEntry report )
 121  
     {
 122  0
         target.print( toString( BOOTERCODE_TEST_SUCCEEDED, report, testSetChannelId ) );
 123  0
     }
 124  
 
 125  
     public void testAssumptionFailure( ReportEntry report )
 126  
     {
 127  0
         target.print( toString( BOOTERCODE_TEST_ASSUMPTIONFAILURE, report, testSetChannelId ) );
 128  0
     }
 129  
 
 130  
     public void testError( ReportEntry report )
 131  
     {
 132  0
         target.print( toString( BOOTERCODE_TEST_ERROR, report, testSetChannelId ) );
 133  0
     }
 134  
 
 135  
     public void testFailed( ReportEntry report )
 136  
     {
 137  0
         target.print( toString( BOOTERCODE_TEST_FAILED, report, testSetChannelId ) );
 138  0
     }
 139  
 
 140  
     public void testSkipped( ReportEntry report )
 141  
     {
 142  0
         target.print( toString( BOOTERCODE_TEST_SKIPPED, report, testSetChannelId ) );
 143  0
     }
 144  
 
 145  
     void sendProps()
 146  
     {
 147  1
         Properties systemProperties = System.getProperties();
 148  
 
 149  1
         if ( systemProperties != null )
 150  
         {
 151  1
             Enumeration propertyKeys = systemProperties.propertyNames();
 152  
 
 153  62
             while ( propertyKeys.hasMoreElements() )
 154  
             {
 155  61
                 String key = (String) propertyKeys.nextElement();
 156  
 
 157  61
                 String value = systemProperties.getProperty( key );
 158  
 
 159  61
                 if ( value == null )
 160  
                 {
 161  0
                     value = "null";
 162  
                 }
 163  61
                 target.print( toPropertyString( key, value ) );
 164  61
             }
 165  
         }
 166  1
     }
 167  
 
 168  
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
 169  
     {
 170  0
         byte[] header = stdout ? stdOutHeader : stdErrHeader;
 171  0
         byte[] content =
 172  
             new byte[buf.length * 3 + 1]; // Hex-escaping can be up to 3 times length of a regular byte.
 173  0
         int i = StringUtils.escapeBytesToPrintable( content, 0, buf, off, len );
 174  0
         content[i++] = (byte) '\n';
 175  
 
 176  0
         synchronized ( target ) // See notes about synhronization/thread safety in class javadoc
 177  
         {
 178  0
             target.write( header, 0, header.length );
 179  0
             target.write( content, 0, i );
 180  0
         }
 181  0
     }
 182  
 
 183  
     public static byte[] createHeader( byte booterCode, int testSetChannel )
 184  
     {
 185  2
         byte[] header = new byte[7];
 186  2
         header[0] = booterCode;
 187  2
         header[1] = (byte) ',';
 188  2
         header[6] = (byte) ',';
 189  
 
 190  2
         int i = testSetChannel;
 191  2
         int charPos = 6;
 192  2
         int radix = 1 << 4;
 193  2
         int mask = radix - 1;
 194  
         do
 195  
         {
 196  2
             header[--charPos] = (byte) digits[i & mask];
 197  2
             i >>>= 4;
 198  
         }
 199  2
         while ( i != 0 );
 200  
 
 201  8
         while ( charPos > 2 )
 202  
         {
 203  6
             header[--charPos] = (byte) '0';
 204  
         }
 205  2
         return header;
 206  
     }
 207  
 
 208  1
     private final static char[] digits =
 209  
         { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
 210  
             'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
 211  
 
 212  
 
 213  
     public void info( String message )
 214  
     {
 215  2
         if ( message == null )
 216  
         {
 217  0
             return;
 218  
         }
 219  
 
 220  2
         StringBuilder sb = new StringBuilder( 7 + message.length() * 5 );
 221  2
         append( sb, BOOTERCODE_CONSOLE );comma( sb );
 222  2
         append( sb, Integer.toHexString( testSetChannelId ) );comma( sb );
 223  2
         StringUtils.escapeToPrintable( sb, message );
 224  
 
 225  2
         sb.append( '\n' );
 226  2
         target.print( sb.toString() );
 227  2
     }
 228  
 
 229  
     private String toPropertyString( String key, String value )
 230  
     {
 231  61
         StringBuilder stringBuilder = new StringBuilder();
 232  
 
 233  61
         append( stringBuilder, BOOTERCODE_SYSPROPS );comma( stringBuilder );
 234  61
         append( stringBuilder, Integer.toHexString( testSetChannelId ) );comma( stringBuilder );
 235  
 
 236  61
         StringUtils.escapeToPrintable( stringBuilder, key );
 237  61
         comma( stringBuilder );
 238  61
         StringUtils.escapeToPrintable( stringBuilder, value );
 239  61
         stringBuilder.append( "\n" );
 240  61
         return stringBuilder.toString();
 241  
     }
 242  
 
 243  
     private String toString( byte operationCode, ReportEntry reportEntry, Integer testSetChannelId )
 244  
     {
 245  0
         StringBuilder stringBuilder = new StringBuilder();
 246  0
         append( stringBuilder, operationCode ); comma( stringBuilder );
 247  0
         append( stringBuilder, Integer.toHexString( testSetChannelId ) );comma( stringBuilder );
 248  
 
 249  0
         nullableEncoding( stringBuilder, reportEntry.getSourceName() );
 250  0
         comma( stringBuilder );
 251  0
         nullableEncoding( stringBuilder, reportEntry.getName() );
 252  0
         comma( stringBuilder );
 253  0
         nullableEncoding( stringBuilder, reportEntry.getGroup() );
 254  0
         comma( stringBuilder );
 255  0
         nullableEncoding( stringBuilder, reportEntry.getMessage() );
 256  0
         comma( stringBuilder );
 257  0
         nullableEncoding( stringBuilder, reportEntry.getElapsed() );
 258  0
         encode( stringBuilder, reportEntry.getStackTraceWriter() );
 259  0
         stringBuilder.append( "\n" );
 260  0
         return stringBuilder.toString();
 261  
     }
 262  
 
 263  
     private static void comma( StringBuilder stringBuilder )
 264  
     {
 265  187
         stringBuilder.append( "," );
 266  187
     }
 267  
 
 268  
     private ForkingRunListener append( StringBuilder stringBuilder, String message )
 269  
     {
 270  63
         stringBuilder.append( encode( message ) );
 271  63
         return this;
 272  
     }
 273  
 
 274  
     private ForkingRunListener append( StringBuilder stringBuilder, byte b )
 275  
     {
 276  63
         stringBuilder.append( (char) b );
 277  63
         return this;
 278  
     }
 279  
 
 280  
     private void nullableEncoding( StringBuilder stringBuilder, Integer source )
 281  
     {
 282  0
         if ( source == null )
 283  
         {
 284  0
             stringBuilder.append( "null" );
 285  
         }
 286  
         else
 287  
         {
 288  0
             stringBuilder.append( source.toString() );
 289  
         }
 290  0
     }
 291  
 
 292  
     private String encode( String source )
 293  
     {
 294  63
         return source;
 295  
     }
 296  
 
 297  
 
 298  
     private static void nullableEncoding( StringBuilder stringBuilder, String source )
 299  
     {
 300  0
         if ( source == null || source.length() == 0 )
 301  
         {
 302  0
             stringBuilder.append( "null" );
 303  
         }
 304  
         else
 305  
         {
 306  0
             StringUtils.escapeToPrintable( stringBuilder, source );
 307  
         }
 308  0
     }
 309  
 
 310  
     private void encode( StringBuilder stringBuilder, StackTraceWriter stackTraceWriter )
 311  
     {
 312  0
         encode( stringBuilder, stackTraceWriter, trimStackTraces );
 313  0
     }
 314  
 
 315  
     public static void encode( StringBuilder stringBuilder, StackTraceWriter stackTraceWriter, boolean trimStackTraces )
 316  
     {
 317  0
         if ( stackTraceWriter != null )
 318  
         {
 319  0
             comma( stringBuilder );
 320  
             //noinspection ThrowableResultOfMethodCallIgnored
 321  0
             final SafeThrowable throwable = stackTraceWriter.getThrowable();
 322  0
             if ( throwable != null )
 323  
             {
 324  0
                 String message = throwable.getLocalizedMessage();
 325  0
                 nullableEncoding( stringBuilder, message );
 326  
             }
 327  0
             comma( stringBuilder );
 328  0
             nullableEncoding( stringBuilder, stackTraceWriter.smartTrimmedStackTrace() );
 329  0
             comma( stringBuilder );
 330  0
             nullableEncoding( stringBuilder, trimStackTraces
 331  
                 ? stackTraceWriter.writeTrimmedTraceToString()
 332  
                 : stackTraceWriter.writeTraceToString() );
 333  
         }
 334  0
     }
 335  
 }