View Javadoc
1   package org.apache.maven.plugin.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.api.report.ReportEntry;
23  import org.apache.maven.surefire.api.report.RunMode;
24  import org.apache.maven.surefire.api.report.StackTraceWriter;
25  import org.apache.maven.surefire.api.report.TestSetReportEntry;
26  
27  import javax.annotation.Nonnull;
28  import java.util.Collections;
29  import java.util.Map;
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
40      implements TestSetReportEntry
41  {
42      private final ReportEntry original;
43  
44      private final ReportEntryType reportEntryType;
45  
46      private final Integer elapsed;
47  
48      private final Utf8RecodingDeferredFileOutputStream stdout;
49  
50      private final Utf8RecodingDeferredFileOutputStream stdErr;
51  
52      private final Map<String, String> systemProperties;
53  
54      public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed,
55                                 Utf8RecodingDeferredFileOutputStream stdout,
56                                 Utf8RecodingDeferredFileOutputStream stdErr,
57                                 Map<String, String> systemProperties )
58      {
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( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed,
68                                 Utf8RecodingDeferredFileOutputStream stdout,
69                                 Utf8RecodingDeferredFileOutputStream stdErr )
70      {
71          this( original, reportEntryType, estimatedElapsed, stdout, stdErr, Collections.<String, String>emptyMap() );
72      }
73  
74      @Override
75      public Integer getElapsed()
76      {
77          return elapsed;
78      }
79  
80      @Override
81      public int getElapsed( int fallback )
82      {
83          return elapsed == null ? fallback : elapsed;
84      }
85  
86      public ReportEntryType getReportEntryType()
87      {
88          return reportEntryType;
89      }
90  
91      public Utf8RecodingDeferredFileOutputStream getStdout()
92      {
93          return stdout;
94      }
95  
96      public Utf8RecodingDeferredFileOutputStream getStdErr()
97      {
98          return stdErr;
99      }
100 
101     @Override
102     public String getSourceName()
103     {
104         return original.getSourceName();
105     }
106 
107     @Override
108     public String getSourceText()
109     {
110         return original.getSourceText();
111     }
112 
113     @Override
114     public String getName()
115     {
116         return original.getName();
117     }
118 
119     @Override
120     public String getNameText()
121     {
122         return original.getNameText();
123     }
124 
125     public String getClassMethodName()
126     {
127         return original.getSourceName() + "." + original.getName();
128     }
129 
130     @Override
131     public String getGroup()
132     {
133         return original.getGroup();
134     }
135 
136     @Override
137     public StackTraceWriter getStackTraceWriter()
138     {
139         return original.getStackTraceWriter();
140     }
141 
142     @Override
143     public String getMessage()
144     {
145         return original.getMessage();
146     }
147 
148     public String getStackTrace( boolean trimStackTrace )
149     {
150         StackTraceWriter w = original.getStackTraceWriter();
151         return w == null ? null : ( trimStackTrace ? w.writeTrimmedTraceToString() : w.writeTraceToString() );
152     }
153 
154     public String elapsedTimeAsString()
155     {
156         return formatElapsedTime( getElapsed() );
157     }
158 
159     String getReportSourceName()
160     {
161         String sourceName = getSourceName();
162         String sourceText = getSourceText();
163         return isBlank( sourceText ) ? sourceName : sourceText;
164     }
165 
166     String getReportSourceName( String suffix )
167     {
168         return isBlank( suffix ) ? getReportSourceName() : getReportSourceName() + "(" + suffix + ")";
169     }
170 
171     String getSourceName( String suffix )
172     {
173         return isBlank( suffix ) ? getSourceName() : getSourceName() + "(" + suffix + ")";
174     }
175 
176     String getReportName()
177     {
178         String name = getName();
179         String nameText = getNameText();
180         return isBlank( nameText ) ? name : nameText;
181     }
182 
183     public String getOutput( boolean trimStackTrace )
184     {
185         String outputLine = getElapsedTimeSummary() + "  <<< " + getReportEntryType().name() + "!";
186         String trimmedStackTrace = getStackTrace( trimStackTrace );
187         return trimmedStackTrace == null ? outputLine : outputLine + NL + trimmedStackTrace;
188     }
189 
190     public String getElapsedTimeVerbose()
191     {
192         return "Time elapsed: " + elapsedTimeAsString() + " s";
193     }
194 
195     public String getElapsedTimeSummary()
196     {
197         String description = getName() == null ? getSourceName() : getClassMethodName();
198         return description + "  " + getElapsedTimeVerbose();
199     }
200 
201     public boolean isErrorOrFailure()
202     {
203         ReportEntryType thisType = getReportEntryType();
204         return ReportEntryType.FAILURE == thisType || ReportEntryType.ERROR == thisType;
205     }
206 
207     public boolean isSkipped()
208     {
209         return ReportEntryType.SKIPPED == getReportEntryType();
210     }
211 
212     public boolean isSucceeded()
213     {
214         return ReportEntryType.SUCCESS == getReportEntryType();
215     }
216 
217     @Override
218     public String getNameWithGroup()
219     {
220         return original.getNameWithGroup();
221     }
222 
223     @Override
224     public String getReportNameWithGroup()
225     {
226         String reportNameWithGroup = original.getReportNameWithGroup();
227 
228         if ( isBlank ( reportNameWithGroup ) )
229         {
230             return getNameWithGroup();
231         }
232 
233         return reportNameWithGroup;
234     }
235 
236     @Nonnull
237     @Override
238     public RunMode getRunMode()
239     {
240         return original.getRunMode();
241     }
242 
243     @Override
244     public Long getTestRunId()
245     {
246         return original.getTestRunId();
247     }
248 
249     @Override
250     public Map<String, String> getSystemProperties()
251     {
252         return systemProperties;
253     }
254 }