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     public String getFullName() {
134         return getName() == null ? getSourceName() : getClassMethodName();
135     }
136 
137     @Override
138     public String getGroup() {
139         return original.getGroup();
140     }
141 
142     @Override
143     public StackTraceWriter getStackTraceWriter() {
144         return original.getStackTraceWriter();
145     }
146 
147     @Override
148     public String getMessage() {
149         return original.getMessage();
150     }
151 
152     public String getStackTrace(boolean trimStackTrace) {
153         StackTraceWriter w = original.getStackTraceWriter();
154         return w == null ? null : (trimStackTrace ? w.writeTrimmedTraceToString() : w.writeTraceToString());
155     }
156 
157     public String elapsedTimeAsString() {
158         return getElapsed() != null ? elapsedTimeFormat.format(new Object[] {getElapsed() / ONE_SECOND}) : null;
159     }
160 
161     String getReportSourceName() {
162         String sourceName = getSourceName();
163         String sourceText = getSourceText();
164         return isBlank(sourceText) ? sourceName : sourceText;
165     }
166 
167     String getReportSourceName(String suffix) {
168         return isBlank(suffix) ? getReportSourceName() : getReportSourceName() + "(" + suffix + ")";
169     }
170 
171     String getSourceName(String suffix) {
172         return isBlank(suffix) ? getSourceName() : getSourceName() + "(" + suffix + ")";
173     }
174 
175     String getReportName() {
176         String name = getName();
177         String nameText = getNameText();
178         return isBlank(nameText) ? name : nameText;
179     }
180 
181     public String getOutput(boolean trimStackTrace) {
182         String outputLine =
183                 getElapsedTimeSummary() + " <<< " + getReportEntryType().name() + "!";
184         String trimmedStackTrace = getStackTrace(trimStackTrace);
185         return trimmedStackTrace == null ? outputLine : outputLine + NL + trimmedStackTrace;
186     }
187 
188     public String getElapsedTimeVerbose() {
189         return "Time elapsed: " + (getElapsed() != null ? elapsedTimeAsString() : "(unknown)");
190     }
191 
192     public String getElapsedTimeSummary() {
193         return getFullName() + " -- " + getElapsedTimeVerbose();
194     }
195 
196     public boolean isErrorOrFailure() {
197         ReportEntryType thisType = getReportEntryType();
198         return ReportEntryType.FAILURE == thisType || ReportEntryType.ERROR == thisType;
199     }
200 
201     public boolean isSkipped() {
202         return ReportEntryType.SKIPPED == getReportEntryType();
203     }
204 
205     public boolean isSucceeded() {
206         return ReportEntryType.SUCCESS == getReportEntryType();
207     }
208 
209     @Override
210     public String getNameWithGroup() {
211         return original.getNameWithGroup();
212     }
213 
214     @Override
215     public String getReportNameWithGroup() {
216         String reportNameWithGroup = original.getReportNameWithGroup();
217 
218         if (isBlank(reportNameWithGroup)) {
219             return getNameWithGroup();
220         }
221 
222         return reportNameWithGroup;
223     }
224 
225     @Nonnull
226     @Override
227     public RunMode getRunMode() {
228         return original.getRunMode();
229     }
230 
231     @Override
232     public Long getTestRunId() {
233         return original.getTestRunId();
234     }
235 
236     @Override
237     public Map<String, String> getSystemProperties() {
238         return systemProperties;
239     }
240 }