Coverage Report - org.apache.maven.surefire.report.PojoStackTraceWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
PojoStackTraceWriter
0%
0/32
0%
0/14
2.75
 
 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 org.apache.maven.surefire.util.internal.StringUtils;
 24  
 
 25  
 import java.io.PrintWriter;
 26  
 import java.io.StringWriter;
 27  
 
 28  
 /**
 29  
  * Write the trace out for a POJO test.
 30  
  *
 31  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 32  
  */
 33  
 public class PojoStackTraceWriter
 34  
     implements StackTraceWriter
 35  
 {
 36  
     private final Throwable t;
 37  
 
 38  
     protected final String testClass;
 39  
 
 40  
     protected final String testMethod;
 41  
 
 42  
     public PojoStackTraceWriter( String testClass, String testMethod, Throwable t )
 43  0
     {
 44  0
         this.testClass = testClass;
 45  0
         this.testMethod = testMethod;
 46  0
         this.t = t;
 47  0
     }
 48  
 
 49  
     public String writeTraceToString()
 50  
     {
 51  0
         StringWriter w = new StringWriter();
 52  0
         if ( t != null )
 53  
         {
 54  0
             t.printStackTrace( new PrintWriter( w ) );
 55  0
             w.flush();
 56  
         }
 57  0
         return w.toString();
 58  
     }
 59  
 
 60  
     public String writeTrimmedTraceToString()
 61  
     {
 62  0
         String text = writeTraceToString();
 63  
 
 64  0
         String marker = "at " + testClass + "." + testMethod;
 65  
 
 66  0
         String[] lines = StringUtils.split( text, "\n" );
 67  0
         int lastLine = lines.length - 1;
 68  0
         int causedByLine = -1;
 69  
         // skip first
 70  0
         for ( int i = 1; i < lines.length; i++ )
 71  
         {
 72  0
             String line = lines[i].trim();
 73  0
             if ( line.startsWith( marker ) )
 74  
             {
 75  0
                 lastLine = i;
 76  
             }
 77  0
             else if ( line.startsWith( "Caused by" ) )
 78  
             {
 79  0
                 causedByLine = i;
 80  0
                 break;
 81  
             }
 82  
         }
 83  
 
 84  0
         StringBuffer trace = new StringBuffer();
 85  0
         for ( int i = 0; i <= lastLine; i++ )
 86  
         {
 87  0
             trace.append( lines[i] );
 88  0
             trace.append( "\n" );
 89  
         }
 90  
 
 91  0
         if ( causedByLine != -1 )
 92  
         {
 93  0
             for ( int i = causedByLine; i < lines.length; i++ )
 94  
             {
 95  0
                 trace.append( lines[i] );
 96  0
                 trace.append( "\n" );
 97  
             }
 98  
         }
 99  0
         return trace.toString();
 100  
     }
 101  
 
 102  
     public Throwable getThrowable()
 103  
     {
 104  0
         return t;
 105  
     }
 106  
 }