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