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