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 | |
import org.apache.maven.surefire.util.internal.StringUtils; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class LegacyPojoStackTraceWriter |
34 | |
implements StackTraceWriter |
35 | |
{ |
36 | |
private final Throwable t; |
37 | |
|
38 | |
private final String testClass; |
39 | |
|
40 | |
private final String testMethod; |
41 | |
|
42 | |
public LegacyPojoStackTraceWriter( String testClass, String testMethod, Throwable t ) |
43 | 2 | { |
44 | 2 | this.testClass = testClass; |
45 | 2 | this.testMethod = testMethod; |
46 | 2 | this.t = t; |
47 | 2 | } |
48 | |
|
49 | |
public String writeTraceToString() |
50 | |
{ |
51 | 2 | StringWriter w = new StringWriter(); |
52 | 2 | if ( t != null ) |
53 | |
{ |
54 | 2 | t.printStackTrace( new PrintWriter( w ) ); |
55 | 2 | w.flush(); |
56 | |
} |
57 | 2 | return w.toString(); |
58 | |
} |
59 | |
|
60 | |
public String smartTrimmedStackTrace() |
61 | |
{ |
62 | 0 | StringBuffer result = new StringBuffer(); |
63 | 0 | result.append( testClass ); |
64 | 0 | result.append( "#" ); |
65 | 0 | result.append( testMethod ); |
66 | 0 | SafeThrowable throwable = getThrowable(); |
67 | 0 | if ( throwable.getTarget() instanceof AssertionError ) |
68 | |
{ |
69 | 0 | result.append( " " ); |
70 | 0 | result.append( getTruncatedMessage( throwable.getMessage(), 77 - result.length() ) ); |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | 0 | Throwable target = throwable.getTarget(); |
75 | 0 | if ( target != null ) |
76 | |
{ |
77 | 0 | result.append( " " ); |
78 | 0 | result.append( target.getClass().getSimpleName() ); |
79 | 0 | result.append( getTruncatedMessage( throwable.getMessage(), 77 - result.length() ) ); |
80 | |
} |
81 | |
} |
82 | 0 | return result.toString(); |
83 | |
} |
84 | |
|
85 | |
private static String getTruncatedMessage( String msg, int i ) |
86 | |
{ |
87 | 0 | if ( i < 0 ) |
88 | |
{ |
89 | 0 | return ""; |
90 | |
} |
91 | 0 | if ( msg == null ) |
92 | |
{ |
93 | 0 | return ""; |
94 | |
} |
95 | 0 | String substring = msg.substring( 0, Math.min( i, msg.length() ) ); |
96 | 0 | if ( i < msg.length() ) |
97 | |
{ |
98 | 0 | return " " + substring + "..."; |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 0 | return " " + substring; |
103 | |
} |
104 | |
} |
105 | |
|
106 | |
|
107 | |
public String writeTrimmedTraceToString() |
108 | |
{ |
109 | 2 | String text = writeTraceToString(); |
110 | |
|
111 | 2 | String marker = "at " + testClass + "." + testMethod; |
112 | |
|
113 | 2 | String[] lines = StringUtils.split( text, "\n" ); |
114 | 2 | int lastLine = lines.length - 1; |
115 | 2 | int causedByLine = -1; |
116 | |
|
117 | 57 | for ( int i = 1; i < lines.length; i++ ) |
118 | |
{ |
119 | 56 | String line = lines[i].trim(); |
120 | 56 | if ( line.startsWith( marker ) ) |
121 | |
{ |
122 | 2 | lastLine = i; |
123 | |
} |
124 | 54 | else if ( line.startsWith( "Caused by" ) ) |
125 | |
{ |
126 | 1 | causedByLine = i; |
127 | 1 | break; |
128 | |
} |
129 | |
} |
130 | |
|
131 | 2 | StringBuffer trace = new StringBuffer(); |
132 | 7 | for ( int i = 0; i <= lastLine; i++ ) |
133 | |
{ |
134 | 5 | trace.append( lines[i] ); |
135 | 5 | trace.append( "\n" ); |
136 | |
} |
137 | |
|
138 | 2 | if ( causedByLine != -1 ) |
139 | |
{ |
140 | 7 | for ( int i = causedByLine; i < lines.length; i++ ) |
141 | |
{ |
142 | 6 | trace.append( lines[i] ); |
143 | 6 | trace.append( "\n" ); |
144 | |
} |
145 | |
} |
146 | 2 | return trace.toString(); |
147 | |
} |
148 | |
|
149 | |
public SafeThrowable getThrowable() |
150 | |
{ |
151 | 0 | return new SafeThrowable( t ); |
152 | |
} |
153 | |
} |