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 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.5 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 static final int MAX_LINE_LENGTH = 77;
37  
38      private final Throwable t;
39  
40      private final String testClass;
41  
42      private final String testMethod;
43  
44      public LegacyPojoStackTraceWriter( String testClass, String testMethod, Throwable t )
45      {
46          this.testClass = testClass;
47          this.testMethod = testMethod;
48          this.t = t;
49      }
50  
51      public String writeTraceToString()
52      {
53          if ( t != null )
54          {
55              StringWriter w = new StringWriter();
56              PrintWriter stackTrace = new PrintWriter( w );
57              t.printStackTrace( stackTrace );
58              stackTrace.close();
59              w.flush();
60              StringBuffer builder = w.getBuffer();
61              if ( isMultiLineExceptionMessage( t ) )
62              {
63                  // SUREFIRE-986
64                  String exc = t.getClass().getName() + ": ";
65                  if ( builder.toString().startsWith( exc ) )
66                  {
67                      builder.insert( exc.length(), '\n' );
68                  }
69              }
70              return builder.toString();
71          }
72          return "";
73      }
74  
75      public String smartTrimmedStackTrace()
76      {
77          StringBuilder result = new StringBuilder();
78          result.append( testClass );
79          result.append( "#" );
80          result.append( testMethod );
81          SafeThrowable throwable = getThrowable();
82          if ( throwable.getTarget() instanceof AssertionError )
83          {
84              result.append( " " );
85              result.append( getTruncatedMessage( throwable.getMessage(), MAX_LINE_LENGTH - result.length() ) );
86          }
87          else
88          {
89              Throwable target = throwable.getTarget();
90              if ( target != null )
91              {
92                  result.append( " " );
93                  result.append( target.getClass().getSimpleName() );
94                  result.append( getTruncatedMessage( throwable.getMessage(), MAX_LINE_LENGTH - result.length() ) );
95              }
96          }
97          return result.toString();
98      }
99  
100     private static boolean isMultiLineExceptionMessage( Throwable t )
101     {
102         String msg = t.getLocalizedMessage();
103         if ( msg != null )
104         {
105             int countNewLines = 0;
106             for ( int i = 0, length = msg.length(); i < length; i++ )
107             {
108                 if ( msg.charAt( i ) == '\n' )
109                 {
110                     if ( ++countNewLines == 2 )
111                     {
112                         break;
113                     }
114                 }
115             }
116             return countNewLines > 1 || countNewLines == 1 && !msg.trim().endsWith( "\n" );
117         }
118         return false;
119     }
120 
121     private static String getTruncatedMessage( String msg, int i )
122     {
123         if ( i < 0 )
124         {
125             return "";
126         }
127         if ( msg == null )
128         {
129             return "";
130         }
131         String substring = msg.substring( 0, Math.min( i, msg.length() ) );
132         if ( i < msg.length() )
133         {
134             return " " + substring + "...";
135         }
136         else
137         {
138             return " " + substring;
139         }
140     }
141 
142 
143     public String writeTrimmedTraceToString()
144     {
145         String text = writeTraceToString();
146 
147         String marker = "at " + testClass + "." + testMethod;
148 
149         String[] lines = StringUtils.split( text, "\n" );
150         int lastLine = lines.length - 1;
151         int causedByLine = -1;
152         // skip first
153         for ( int i = 1; i < lines.length; i++ )
154         {
155             String line = lines[i].trim();
156             if ( line.startsWith( marker ) )
157             {
158                 lastLine = i;
159             }
160             else if ( line.startsWith( "Caused by" ) )
161             {
162                 causedByLine = i;
163                 break;
164             }
165         }
166 
167         StringBuilder trace = new StringBuilder();
168         for ( int i = 0; i <= lastLine; i++ )
169         {
170             trace.append( lines[i] );
171             trace.append( "\n" );
172         }
173 
174         if ( causedByLine != -1 )
175         {
176             for ( int i = causedByLine; i < lines.length; i++ )
177             {
178                 trace.append( lines[i] );
179                 trace.append( "\n" );
180             }
181         }
182         return trace.toString();
183     }
184 
185     public SafeThrowable getThrowable()
186     {
187         return new SafeThrowable( t );
188     }
189 }