Coverage Report - org.apache.maven.surefire.report.LegacyPojoStackTraceWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
LegacyPojoStackTraceWriter
57%
31/54
54%
13/24
3,5
 
 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  
 
 23  
 import java.io.PrintWriter;
 24  
 import java.io.StringWriter;
 25  
 import org.apache.maven.surefire.util.internal.StringUtils;
 26  
 
 27  
 /**
 28  
  * Write the trace out for a POJO test. Java 1.3 compatible.
 29  
  *
 30  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 31  
  * @noinspection ThrowableResultOfMethodCallIgnored
 32  
  */
 33  
 public class LegacyPojoStackTraceWriter
 34  
     implements StackTraceWriter
 35  
 {
 36  
     private final Throwable t;
 37  
 
 38  
     private final String testClass;
 39  
 
 40  
     private final String testMethod;
 41  
 
 42  
     public LegacyPojoStackTraceWriter( String testClass, String testMethod, Throwable t )
 43  2
     {
 44  2
         this.testClass = testClass;
 45  2
         this.testMethod = testMethod;
 46  2
         this.t = t;
 47  2
     }
 48  
 
 49  
     public String writeTraceToString()
 50  
     {
 51  2
         StringWriter w = new StringWriter();
 52  2
         if ( t != null )
 53  
         {
 54  2
             t.printStackTrace( new PrintWriter( w ) );
 55  2
             w.flush();
 56  
         }
 57  2
         return w.toString();
 58  
     }
 59  
 
 60  
     public String smartTrimmedStackTrace()
 61  
     {
 62  0
         StringBuffer result = new StringBuffer();
 63  0
         result.append( testClass );
 64  0
         result.append( "#" );
 65  0
         result.append( testMethod );
 66  0
         SafeThrowable throwable = getThrowable();
 67  0
         if ( throwable.getTarget() instanceof AssertionError )
 68  
         {
 69  0
             result.append( " " );
 70  0
             result.append( getTruncatedMessage( throwable.getMessage(), 77 - result.length() ) );
 71  
         }
 72  
         else
 73  
         {
 74  0
             Throwable target = throwable.getTarget();
 75  0
             if ( target != null )
 76  
             {
 77  0
                 result.append( " " );
 78  0
                 result.append( target.getClass().getSimpleName() );
 79  0
                 result.append( getTruncatedMessage( throwable.getMessage(), 77 - result.length() ) );
 80  
             }
 81  
         }
 82  0
         return result.toString();
 83  
     }
 84  
 
 85  
     private static String getTruncatedMessage( String msg, int i )
 86  
     {
 87  0
         if ( i < 0 )
 88  
         {
 89  0
             return "";
 90  
         }
 91  0
         if ( msg == null )
 92  
         {
 93  0
             return "";
 94  
         }
 95  0
         String substring = msg.substring( 0, Math.min( i, msg.length() ) );
 96  0
         if ( i < msg.length() )
 97  
         {
 98  0
             return " " + substring + "...";
 99  
         }
 100  
         else
 101  
         {
 102  0
             return " " + substring;
 103  
         }
 104  
     }
 105  
 
 106  
 
 107  
     public String writeTrimmedTraceToString()
 108  
     {
 109  2
         String text = writeTraceToString();
 110  
 
 111  2
         String marker = "at " + testClass + "." + testMethod;
 112  
 
 113  2
         String[] lines = StringUtils.split( text, "\n" );
 114  2
         int lastLine = lines.length - 1;
 115  2
         int causedByLine = -1;
 116  
         // skip first
 117  57
         for ( int i = 1; i < lines.length; i++ )
 118  
         {
 119  56
             String line = lines[i].trim();
 120  56
             if ( line.startsWith( marker ) )
 121  
             {
 122  2
                 lastLine = i;
 123  
             }
 124  54
             else if ( line.startsWith( "Caused by" ) )
 125  
             {
 126  1
                 causedByLine = i;
 127  1
                 break;
 128  
             }
 129  
         }
 130  
 
 131  2
         StringBuffer trace = new StringBuffer();
 132  7
         for ( int i = 0; i <= lastLine; i++ )
 133  
         {
 134  5
             trace.append( lines[i] );
 135  5
             trace.append( "\n" );
 136  
         }
 137  
 
 138  2
         if ( causedByLine != -1 )
 139  
         {
 140  7
             for ( int i = causedByLine; i < lines.length; i++ )
 141  
             {
 142  6
                 trace.append( lines[i] );
 143  6
                 trace.append( "\n" );
 144  
             }
 145  
         }
 146  2
         return trace.toString();
 147  
     }
 148  
 
 149  
     public SafeThrowable getThrowable()
 150  
     {
 151  0
         return new SafeThrowable( t );
 152  
     }
 153  
 }