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