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