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.plugin.surefire.report;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.text.MessageFormat;
24  import java.util.Collections;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import org.apache.maven.surefire.api.report.ReportEntry;
29  import org.apache.maven.surefire.api.report.RunMode;
30  import org.apache.maven.surefire.api.report.StackTraceWriter;
31  import org.apache.maven.surefire.api.report.TestSetReportEntry;
32  
33  import static java.util.Collections.unmodifiableMap;
34  import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
35  import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank;
36  
37  /**
38   * @author Kristian Rosenvold
39   */
40  public class WrappedReportEntry implements TestSetReportEntry {
41      private static final float ONE_SECOND = 1000.0f;
42  
43      private final ReportEntry original;
44  
45      private final ReportEntryType reportEntryType;
46  
47      private final Integer elapsed;
48  
49      private final Utf8RecodingDeferredFileOutputStream stdout;
50  
51      private final Utf8RecodingDeferredFileOutputStream stdErr;
52  
53      private final Map<String, String> systemProperties;
54  
55      /*
56       * Rationale: The idea is to always display four digits for visually consistent output
57       * Important: Keep in sync with maven-surefire-report-plugin/src/main/resources/surefire-report.properties
58       */
59      private final MessageFormat elapsedTimeFormat = new MessageFormat(
60              "{0,choice,0#0|0.0<{0,number,0.000}|10#{0,number,0.00}|100#{0,number,0.0}|1000#{0,number,0}} s",
61              Locale.ROOT);
62  
63      public WrappedReportEntry(
64              ReportEntry original,
65              ReportEntryType reportEntryType,
66              Integer estimatedElapsed,
67              Utf8RecodingDeferredFileOutputStream stdout,
68              Utf8RecodingDeferredFileOutputStream stdErr,
69              Map<String, String> systemProperties) {
70          this.original = original;
71          this.reportEntryType = reportEntryType;
72          this.elapsed = estimatedElapsed;
73          this.stdout = stdout;
74          this.stdErr = stdErr;
75          this.systemProperties = unmodifiableMap(systemProperties);
76      }
77  
78      public WrappedReportEntry(
79              ReportEntry original,
80              ReportEntryType reportEntryType,
81              Integer estimatedElapsed,
82              Utf8RecodingDeferredFileOutputStream stdout,
83              Utf8RecodingDeferredFileOutputStream stdErr) {
84          this(original, reportEntryType, estimatedElapsed, stdout, stdErr, Collections.<String, String>emptyMap());
85      }
86  
87      @Override
88      public Integer getElapsed() {
89          return elapsed;
90      }
91  
92      @Override
93      public int getElapsed(int fallback) {
94          return elapsed == null ? fallback : elapsed;
95      }
96  
97      public ReportEntryType getReportEntryType() {
98          return reportEntryType;
99      }
100 
101     public Utf8RecodingDeferredFileOutputStream getStdout() {
102         return stdout;
103     }
104 
105     public Utf8RecodingDeferredFileOutputStream getStdErr() {
106         return stdErr;
107     }
108 
109     @Override
110     public String getSourceName() {
111         return original.getSourceName();
112     }
113 
114     @Override
115     public String getSourceText() {
116         return original.getSourceText();
117     }
118 
119     @Override
120     public String getName() {
121         return original.getName();
122     }
123 
124     @Override
125     public String getNameText() {
126         return original.getNameText();
127     }
128 
129     public String getClassMethodName() {
130         return original.getSourceName() + "." + original.getName();
131     }
132 
133     @Override
134     public String getGroup() {
135         return original.getGroup();
136     }
137 
138     @Override
139     public StackTraceWriter getStackTraceWriter() {
140         return original.getStackTraceWriter();
141     }
142 
143     @Override
144     public String getMessage() {
145         return original.getMessage();
146     }
147 
148     public String getStackTrace(boolean trimStackTrace) {
149         StackTraceWriter w = original.getStackTraceWriter();
150         return w == null ? null : (trimStackTrace ? w.writeTrimmedTraceToString() : w.writeTraceToString());
151     }
152 
153     public String elapsedTimeAsString() {
154         return getElapsed() != null ? elapsedTimeFormat.format(new Object[] {getElapsed() / ONE_SECOND}) : null;
155     }
156 
157     String getReportSourceName() {
158         String sourceName = getSourceName();
159         String sourceText = getSourceText();
160         return isBlank(sourceText) ? sourceName : sourceText;
161     }
162 
163     String getReportSourceName(String suffix) {
164         return isBlank(suffix) ? getReportSourceName() : getReportSourceName() + "(" + suffix + ")";
165     }
166 
167     String getSourceName(String suffix) {
168         return isBlank(suffix) ? getSourceName() : getSourceName() + "(" + suffix + ")";
169     }
170 
171     String getReportName() {
172         String name = getName();
173         String nameText = getNameText();
174         return isBlank(nameText) ? name : nameText;
175     }
176 
177     public String getOutput(boolean trimStackTrace) {
178         String outputLine =
179                 getElapsedTimeSummary() + " <<< " + getReportEntryType().name() + "!";
180         String trimmedStackTrace = getStackTrace(trimStackTrace);
181         return trimmedStackTrace == null ? outputLine : outputLine + NL + trimmedStackTrace;
182     }
183 
184     public String getElapsedTimeVerbose() {
185         return "Time elapsed: " + (getElapsed() != null ? elapsedTimeAsString() : "(unknown)");
186     }
187 
188     public String getElapsedTimeSummary() {
189         String description = getName() == null ? getSourceName() : getClassMethodName();
190         return description + " -- " + getElapsedTimeVerbose();
191     }
192 
193     public boolean isErrorOrFailure() {
194         ReportEntryType thisType = getReportEntryType();
195         return ReportEntryType.FAILURE == thisType || ReportEntryType.ERROR == thisType;
196     }
197 
198     public boolean isSkipped() {
199         return ReportEntryType.SKIPPED == getReportEntryType();
200     }
201 
202     public boolean isSucceeded() {
203         return ReportEntryType.SUCCESS == getReportEntryType();
204     }
205 
206     @Override
207     public String getNameWithGroup() {
208         return original.getNameWithGroup();
209     }
210 
211     @Override
212     public String getReportNameWithGroup() {
213         String reportNameWithGroup = original.getReportNameWithGroup();
214 
215         if (isBlank(reportNameWithGroup)) {
216             return getNameWithGroup();
217         }
218 
219         return reportNameWithGroup;
220     }
221 
222     @Nonnull
223     @Override
224     public RunMode getRunMode() {
225         return original.getRunMode();
226     }
227 
228     @Override
229     public Long getTestRunId() {
230         return original.getTestRunId();
231     }
232 
233     @Override
234     public Map<String, String> getSystemProperties() {
235         return systemProperties;
236     }
237 }