1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.surefire.report;
20  
21  import java.io.PrintWriter;
22  import java.io.StringWriter;
23  import java.util.Objects;
24  
25  import org.apache.maven.surefire.api.report.SafeThrowable;
26  import org.apache.maven.surefire.api.report.StackTraceWriter;
27  import org.apache.maven.surefire.api.util.internal.StringUtils;
28  
29  
30  
31  
32  
33  
34  public class PojoStackTraceWriter implements StackTraceWriter {
35      private final Throwable t;
36  
37      private final String testClass;
38  
39      private final String testMethod;
40  
41      public PojoStackTraceWriter(String testClass, String testMethod, Throwable t) {
42          this.testClass = testClass;
43          this.testMethod = testMethod;
44          this.t = t;
45      }
46  
47      @Override
48      public String writeTraceToString() {
49          if (t != null) {
50              StringWriter w = new StringWriter();
51              try (PrintWriter stackTrace = new PrintWriter(w)) {
52                  t.printStackTrace(stackTrace);
53              }
54              StringBuffer builder = w.getBuffer();
55              if (isMultiLineExceptionMessage(t)) {
56                  
57                  String exc = t.getClass().getName() + ": ";
58                  if (StringUtils.startsWith(builder, exc)) {
59                      builder.insert(exc.length(), '\n');
60                  }
61              }
62              return builder.toString();
63          }
64          return "";
65      }
66  
67      @Override
68      public String smartTrimmedStackTrace() {
69          return t == null ? "" : new SmartStackTraceParser(testClass, t, testMethod).getString();
70      }
71  
72      @Override
73      public String writeTrimmedTraceToString() {
74          return t == null ? "" : SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t, testClass);
75      }
76  
77      @Override
78      public SafeThrowable getThrowable() {
79          return t == null ? null : new SafeThrowable(t);
80      }
81  
82      private static boolean isMultiLineExceptionMessage(Throwable t) {
83          String msg = t.getLocalizedMessage();
84          if (msg != null) {
85              int countNewLines = 0;
86              for (int i = 0, length = msg.length(); i < length; i++) {
87                  if (msg.charAt(i) == '\n') {
88                      if (++countNewLines == 2) {
89                          break;
90                      }
91                  }
92              }
93              return countNewLines > 1 || countNewLines == 1 && !msg.trim().endsWith("\n");
94          }
95          return false;
96      }
97  
98      @Override
99      public boolean equals(Object o) {
100         if (this == o) {
101             return true;
102         }
103         if (o == null || getClass() != o.getClass()) {
104             return false;
105         }
106         PojoStackTraceWriter that = (PojoStackTraceWriter) o;
107         return Objects.equals(t, that.t)
108                 && Objects.equals(testClass, that.testClass)
109                 && Objects.equals(testMethod, that.testMethod);
110     }
111 
112     @Override
113     public int hashCode() {
114         return Objects.hash(t, testClass, testMethod);
115     }
116 }