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 PojoStackTraceWriter
34 implements StackTraceWriter
35 {
36 private final Throwable t;
37
38 private final String testClass;
39
40 private 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 if ( t != null )
52 {
53 StringWriter w = new StringWriter();
54 PrintWriter stackTrace = new PrintWriter( w );
55 try
56 {
57 t.printStackTrace( stackTrace );
58 stackTrace.flush();
59 }
60 finally
61 {
62 stackTrace.close();
63 }
64 StringBuffer builder = w.getBuffer();
65 if ( isMultiLineExceptionMessage( t ) )
66 {
67
68 String exc = t.getClass().getName() + ": ";
69 if ( StringUtils.startsWith( builder, exc ) )
70 {
71 builder.insert( exc.length(), '\n' );
72 }
73 }
74 return builder.toString();
75 }
76 return "";
77 }
78
79 public String smartTrimmedStackTrace()
80 {
81 return t == null ? "" : new SmartStackTraceParser( testClass, t, testMethod ).getString();
82 }
83
84 public String writeTrimmedTraceToString()
85 {
86 return t == null ? "" : SmartStackTraceParser.stackTraceWithFocusOnClassAsString( t, testClass );
87 }
88
89 public SafeThrowable getThrowable()
90 {
91 return t == null ? null : new SafeThrowable( t );
92 }
93
94 private static boolean isMultiLineExceptionMessage( Throwable t )
95 {
96 String msg = t.getLocalizedMessage();
97 if ( msg != null )
98 {
99 int countNewLines = 0;
100 for ( int i = 0, length = msg.length(); i < length; i++ )
101 {
102 if ( msg.charAt( i ) == '\n' )
103 {
104 if ( ++countNewLines == 2 )
105 {
106 break;
107 }
108 }
109 }
110 return countNewLines > 1 || countNewLines == 1 && !msg.trim().endsWith( "\n" );
111 }
112 return false;
113 }
114 }