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 | |
protected final String testClass; |
39 | |
|
40 | |
protected final String testMethod; |
41 | |
|
42 | |
public PojoStackTraceWriter( String testClass, String testMethod, Throwable t ) |
43 | 0 | { |
44 | 0 | this.testClass = testClass; |
45 | 0 | this.testMethod = testMethod; |
46 | 0 | this.t = t; |
47 | 0 | } |
48 | |
|
49 | |
public String writeTraceToString() |
50 | |
{ |
51 | 0 | StringWriter w = new StringWriter(); |
52 | 0 | if ( t != null ) |
53 | |
{ |
54 | 0 | t.printStackTrace( new PrintWriter( w ) ); |
55 | 0 | w.flush(); |
56 | |
} |
57 | 0 | return w.toString(); |
58 | |
} |
59 | |
|
60 | |
public String writeTrimmedTraceToString() |
61 | |
{ |
62 | 0 | String text = writeTraceToString(); |
63 | |
|
64 | 0 | String marker = "at " + testClass + "." + testMethod; |
65 | |
|
66 | 0 | String[] lines = StringUtils.split( text, "\n" ); |
67 | 0 | int lastLine = lines.length - 1; |
68 | 0 | int causedByLine = -1; |
69 | |
|
70 | 0 | for ( int i = 1; i < lines.length; i++ ) |
71 | |
{ |
72 | 0 | String line = lines[i].trim(); |
73 | 0 | if ( line.startsWith( marker ) ) |
74 | |
{ |
75 | 0 | lastLine = i; |
76 | |
} |
77 | 0 | else if ( line.startsWith( "Caused by" ) ) |
78 | |
{ |
79 | 0 | causedByLine = i; |
80 | 0 | break; |
81 | |
} |
82 | |
} |
83 | |
|
84 | 0 | StringBuffer trace = new StringBuffer(); |
85 | 0 | for ( int i = 0; i <= lastLine; i++ ) |
86 | |
{ |
87 | 0 | trace.append( lines[i] ); |
88 | 0 | trace.append( "\n" ); |
89 | |
} |
90 | |
|
91 | 0 | if ( causedByLine != -1 ) |
92 | |
{ |
93 | 0 | for ( int i = causedByLine; i < lines.length; i++ ) |
94 | |
{ |
95 | 0 | trace.append( lines[i] ); |
96 | 0 | trace.append( "\n" ); |
97 | |
} |
98 | |
} |
99 | 0 | return trace.toString(); |
100 | |
} |
101 | |
|
102 | |
public Throwable getThrowable() |
103 | |
{ |
104 | 0 | return t; |
105 | |
} |
106 | |
} |