View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Write the trace out for a POJO test.
31   *
32   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
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                  // SUREFIRE-986
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 }