View Javadoc

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      {
44          this.testClass = testClass;
45          this.testMethod = testMethod;
46          this.t = t;
47      }
48  
49      public String writeTraceToString()
50      {
51          StringWriter w = new StringWriter();
52          if ( t != null )
53          {
54              t.printStackTrace( new PrintWriter( w ) );
55              w.flush();
56          }
57          return w.toString();
58      }
59  
60      public String writeTrimmedTraceToString()
61      {
62          String text = writeTraceToString();
63  
64          String marker = "at " + testClass + "." + testMethod;
65  
66          String[] lines = StringUtils.split( text, "\n" );
67          int lastLine = lines.length - 1;
68          int causedByLine = -1;
69          // skip first
70          for ( int i = 1; i < lines.length; i++ )
71          {
72              String line = lines[i].trim();
73              if ( line.startsWith( marker ) )
74              {
75                  lastLine = i;
76              }
77              else if ( line.startsWith( "Caused by" ) )
78              {
79                  causedByLine = i;
80                  break;
81              }
82          }
83  
84          StringBuffer trace = new StringBuffer();
85          for ( int i = 0; i <= lastLine; i++ )
86          {
87              trace.append( lines[i] );
88              trace.append( "\n" );
89          }
90  
91          if ( causedByLine != -1 )
92          {
93              for ( int i = causedByLine; i < lines.length; i++ )
94              {
95                  trace.append( lines[i] );
96                  trace.append( "\n" );
97              }
98          }
99          return trace.toString();
100     }
101 
102     public Throwable getThrowable()
103     {
104         return t;
105     }
106 }