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 java.io.PrintWriter;
24 import java.io.StringWriter;
25
26
27
28
29
30
31 public class PojoStackTraceWriter
32 implements StackTraceWriter
33 {
34 private final Throwable t;
35
36 private final String testClass;
37
38 private final String testMethod;
39
40 public PojoStackTraceWriter( String testClass, String testMethod, Throwable t )
41 {
42 this.testClass = testClass;
43 this.testMethod = testMethod;
44 this.t = t;
45 }
46
47 public String writeTraceToString()
48 {
49 if ( t != null )
50 {
51 StringWriter w = new StringWriter();
52 PrintWriter stackTrace = new PrintWriter( w );
53 t.printStackTrace( stackTrace );
54 stackTrace.close();
55 w.flush();
56 StringBuffer builder = w.getBuffer();
57 if ( isMultiLineExceptionMessage( t ) )
58 {
59
60 String exc = t.getClass().getName() + ": ";
61 if ( builder.toString().startsWith( exc ) )
62 {
63 builder.insert( exc.length(), '\n' );
64 }
65 }
66 return builder.toString();
67 }
68 return "";
69 }
70
71 public String smartTrimmedStackTrace()
72 {
73 return t == null ? "" : new SmartStackTraceParser( testClass, t, testMethod ).getString();
74 }
75
76 public String writeTrimmedTraceToString()
77 {
78 return t == null ? "" : SmartStackTraceParser.stackTraceWithFocusOnClassAsString( t, testClass );
79 }
80
81 public SafeThrowable getThrowable()
82 {
83 return t == null ? null : new SafeThrowable( t );
84 }
85
86 private static boolean isMultiLineExceptionMessage( Throwable t )
87 {
88 String msg = t.getLocalizedMessage();
89 if ( msg != null )
90 {
91 int countNewLines = 0;
92 for ( int i = 0, length = msg.length(); i < length; i++ )
93 {
94 if ( msg.charAt( i ) == '\n' )
95 {
96 if ( ++countNewLines == 2 )
97 {
98 break;
99 }
100 }
101 }
102 return countNewLines > 1 || countNewLines == 1 && !msg.trim().endsWith( "\n" );
103 }
104 return false;
105 }
106 }