1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.api.report;
20
21 import java.io.PrintWriter;
22 import java.io.StringWriter;
23
24 import org.apache.maven.surefire.api.util.internal.StringUtils;
25
26 import static org.apache.maven.surefire.shared.utils.StringUtils.isNotEmpty;
27
28
29
30
31
32
33 public class LegacyPojoStackTraceWriter implements StackTraceWriter {
34 private final Throwable t;
35
36 private final String testClass;
37
38 private final String testMethod;
39
40 public LegacyPojoStackTraceWriter(String testClass, String testMethod, Throwable t) {
41 this.testClass = testClass;
42 this.testMethod = testMethod;
43 this.t = t;
44 }
45
46 @Override
47 public String writeTraceToString() {
48 if (t != null) {
49 StringWriter w = new StringWriter();
50 try (PrintWriter stackTrace = new PrintWriter(w)) {
51 t.printStackTrace(stackTrace);
52 }
53 StringBuffer builder = w.getBuffer();
54 if (isMultiLineExceptionMessage(t)) {
55
56 String exc = t.getClass().getName() + ": ";
57 if (StringUtils.startsWith(builder, exc)) {
58 builder.insert(exc.length(), '\n');
59 }
60 }
61 return builder.toString();
62 }
63 return "";
64 }
65
66 @Override
67 public String smartTrimmedStackTrace() {
68 StringBuilder result = new StringBuilder();
69 result.append(testClass);
70 result.append("#");
71 result.append(testMethod);
72 SafeThrowable throwable = getThrowable();
73 Throwable target = throwable.getTarget();
74 if (target != null) {
75 if (!(target instanceof AssertionError)) {
76 result.append(' ').append(target.getClass().getSimpleName());
77 }
78 final String msg = throwable.getMessage();
79 if (isNotEmpty(msg)) {
80 result.append(' ').append(msg);
81 }
82 }
83 return result.toString();
84 }
85
86 private static boolean isMultiLineExceptionMessage(Throwable t) {
87 String msg = t.getLocalizedMessage();
88 if (msg != null) {
89 int countNewLines = 0;
90 for (int i = 0, length = msg.length(); i < length; i++) {
91 if (msg.charAt(i) == '\n') {
92 if (++countNewLines == 2) {
93 break;
94 }
95 }
96 }
97 return countNewLines > 1 || countNewLines == 1 && !msg.trim().endsWith("\n");
98 }
99 return false;
100 }
101
102 @Override
103 public String writeTrimmedTraceToString() {
104 String text = writeTraceToString();
105
106 String marker = "at " + testClass + "." + testMethod;
107
108 String[] lines = StringUtils.split(text, "\n");
109 int lastLine = lines.length - 1;
110 int causedByLine = -1;
111
112 for (int i = 1; i < lines.length; i++) {
113 String line = lines[i].trim();
114 if (line.startsWith(marker)) {
115 lastLine = i;
116 } else if (line.startsWith("Caused by")) {
117 causedByLine = i;
118 break;
119 }
120 }
121
122 StringBuilder trace = new StringBuilder();
123 for (int i = 0; i <= lastLine; i++) {
124 trace.append(lines[i]);
125 trace.append("\n");
126 }
127
128 if (causedByLine != -1) {
129 for (int i = causedByLine; i < lines.length; i++) {
130 trace.append(lines[i]);
131 trace.append("\n");
132 }
133 }
134 return trace.toString();
135 }
136
137 @Override
138 public SafeThrowable getThrowable() {
139 return new SafeThrowable(t);
140 }
141 }