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.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   * Write the trace out for a POJO test. Java 1.5 compatible.
30   *
31   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
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                  // SUREFIRE-986
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         // skip first
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 }