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.report.ReportEntry;
23  import org.apache.maven.surefire.report.StackTraceWriter;
24  import org.apache.maven.surefire.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.util.internal.StringUtils.NL;
32  
33  /**
34   * @author Kristian Rosenvold
35   */
36  public class WrappedReportEntry
37      implements TestSetReportEntry
38  {
39      private final ReportEntry original;
40  
41      private final ReportEntryType reportEntryType;
42  
43      private final Integer elapsed;
44  
45      private final Utf8RecodingDeferredFileOutputStream stdout;
46  
47      private final Utf8RecodingDeferredFileOutputStream stdErr;
48  
49      private final Map<String, String> systemProperties;
50  
51      public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed,
52                                 Utf8RecodingDeferredFileOutputStream stdout,
53                                 Utf8RecodingDeferredFileOutputStream stdErr,
54                                 Map<String, String> systemProperties )
55      {
56          this.original = original;
57          this.reportEntryType = reportEntryType;
58          this.elapsed = estimatedElapsed;
59          this.stdout = stdout;
60          this.stdErr = stdErr;
61          this.systemProperties = unmodifiableMap( systemProperties );
62      }
63  
64      public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed,
65                                 Utf8RecodingDeferredFileOutputStream stdout,
66                                 Utf8RecodingDeferredFileOutputStream stdErr )
67      {
68          this( original, reportEntryType, estimatedElapsed, stdout, stdErr, Collections.<String, String>emptyMap() );
69      }
70  
71      @Override
72      public Integer getElapsed()
73      {
74          return elapsed;
75      }
76  
77      public ReportEntryType getReportEntryType()
78      {
79          return reportEntryType;
80      }
81  
82      public Utf8RecodingDeferredFileOutputStream getStdout()
83      {
84          return stdout;
85      }
86  
87      public Utf8RecodingDeferredFileOutputStream getStdErr()
88      {
89          return stdErr;
90      }
91  
92      @Override
93      public String getSourceName()
94      {
95          return original.getSourceName();
96      }
97  
98      @Override
99      public String getName()
100     {
101         return original.getName();
102     }
103 
104     public String getClassMethodName()
105     {
106         return getSourceName() + "." + getName();
107     }
108 
109     @Override
110     public String getGroup()
111     {
112         return original.getGroup();
113     }
114 
115     @Override
116     public StackTraceWriter getStackTraceWriter()
117     {
118         return original.getStackTraceWriter();
119     }
120 
121     @Override
122     public String getMessage()
123     {
124         return original.getMessage();
125     }
126 
127     public String getStackTrace( boolean trimStackTrace )
128     {
129         StackTraceWriter w = original.getStackTraceWriter();
130         return w == null ? null : ( trimStackTrace ? w.writeTrimmedTraceToString() : w.writeTraceToString() );
131     }
132 
133     public String elapsedTimeAsString()
134     {
135         return formatElapsedTime( getElapsed() );
136     }
137 
138     public String getReportName()
139     {
140         final int i = getName().lastIndexOf( "(" );
141         return i > 0 ? getName().substring( 0, i ) : getName();
142     }
143 
144     public String getReportName( String suffix )
145     {
146         return suffix != null && !suffix.isEmpty() ? getReportName() + "(" + suffix + ")" : getReportName();
147     }
148 
149     public String getOutput( boolean trimStackTrace )
150     {
151         String outputLine = getElapsedTimeSummary() + "  <<< " + getReportEntryType().name() + "!";
152         String trimmedStackTrace = getStackTrace( trimStackTrace );
153         return trimmedStackTrace == null ? outputLine : outputLine + NL + trimmedStackTrace;
154     }
155 
156     public String getElapsedTimeVerbose()
157     {
158         return "Time elapsed: " + elapsedTimeAsString() + " s";
159     }
160 
161     public String getElapsedTimeSummary()
162     {
163         return getName() + "  " + getElapsedTimeVerbose();
164     }
165 
166     public boolean isErrorOrFailure()
167     {
168         ReportEntryType thisType = getReportEntryType();
169         return ReportEntryType.FAILURE == thisType || ReportEntryType.ERROR == thisType;
170     }
171 
172     public boolean isSkipped()
173     {
174         return ReportEntryType.SKIPPED == getReportEntryType();
175     }
176 
177     public boolean isSucceeded()
178     {
179         return ReportEntryType.SUCCESS == getReportEntryType();
180     }
181 
182     @Override
183     public String getNameWithGroup()
184     {
185         return original.getNameWithGroup();
186     }
187 
188     @Override
189     public Map<String, String> getSystemProperties()
190     {
191         return systemProperties;
192     }
193 }