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