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.commons.io.output.DeferredFileOutputStream;
23  import org.apache.maven.surefire.report.ReportEntry;
24  import org.apache.maven.surefire.report.StackTraceWriter;
25  
26  import java.text.NumberFormat;
27  import java.util.Locale;
28  
29  /**
30   * @author Kristian Rosenvold
31   */
32  public class WrappedReportEntry
33      implements ReportEntry
34  {
35      private final ReportEntry original;
36  
37      private final ReportEntryType reportEntryType;
38  
39      private final Integer elapsed;
40  
41      private final DeferredFileOutputStream stdout;
42  
43      private final DeferredFileOutputStream stdErr;
44  
45      private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
46  
47      private static final int MS_PER_SEC = 1000;
48  
49      static final String NL = System.getProperty( "line.separator" );
50  
51      public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed,
52                                 DeferredFileOutputStream stdout, DeferredFileOutputStream stdErr )
53      {
54          this.original = original;
55          this.reportEntryType = reportEntryType;
56          this.elapsed = estimatedElapsed;
57          this.stdout = stdout;
58          this.stdErr = stdErr;
59      }
60  
61      public Integer getElapsed()
62      {
63          return elapsed;
64      }
65  
66      public ReportEntryType getReportEntryType()
67      {
68          return reportEntryType;
69      }
70  
71      public DeferredFileOutputStream getStdout()
72      {
73          return stdout;
74      }
75  
76      public DeferredFileOutputStream getStdErr()
77      {
78          return stdErr;
79      }
80  
81      public String getSourceName()
82      {
83          return original.getSourceName();
84      }
85  
86      public String getName()
87      {
88          return original.getName();
89      }
90  
91      public String getGroup()
92      {
93          return original.getGroup();
94      }
95  
96      public StackTraceWriter getStackTraceWriter()
97      {
98          return original.getStackTraceWriter();
99      }
100 
101     public String getMessage()
102     {
103         return original.getMessage();
104     }
105 
106     public String getStackTrace( boolean trimStackTrace )
107     {
108         StackTraceWriter writer = original.getStackTraceWriter();
109         if ( writer == null )
110         {
111             return null;
112         }
113         return trimStackTrace ? writer.writeTrimmedTraceToString() : writer.writeTraceToString();
114     }
115 
116     public String elapsedTimeAsString()
117     {
118         return elapsedTimeAsString( getElapsed() );
119     }
120 
121     String elapsedTimeAsString( long runTime )
122     {
123         return numberFormat.format( (double) runTime / MS_PER_SEC );
124     }
125 
126     public String getReportName()
127     {
128         final int i = getName().lastIndexOf( "(" );
129         return i > 0 ? getName().substring( 0, i ) : getName();
130     }
131 
132     public String getReportName( String suffix )
133     {
134         return suffix != null && suffix.length() > 0 ? getReportName() + "(" + suffix + ")" : getReportName();
135     }
136 
137     public String getOutput( boolean trimStackTrace )
138     {
139         StringBuilder buf = new StringBuilder();
140 
141         buf.append( getElapsedTimeSummary() );
142 
143         buf.append( "  <<< " ).append( getReportEntryType().toString().toUpperCase() ).append( "!" ).append( NL );
144 
145         buf.append( getStackTrace( trimStackTrace ) );
146 
147         return buf.toString();
148     }
149 
150     public String getElapsedTimeSummary()
151     {
152         StringBuilder reportContent = new StringBuilder();
153         reportContent.append( getName() );
154         reportContent.append( "  Time elapsed: " );
155         reportContent.append( elapsedTimeAsString() );
156         reportContent.append( " sec" );
157 
158         return reportContent.toString();
159     }
160 
161     public boolean isErrorOrFailure()
162     {
163         ReportEntryType thisType = getReportEntryType();
164         return ReportEntryType.failure == thisType || ReportEntryType.error == thisType;
165     }
166 
167     public boolean isSkipped()
168     {
169         return ReportEntryType.skipped == getReportEntryType();
170     }
171 
172     public boolean isSucceeded()
173     {
174         return ReportEntryType.success == getReportEntryType();
175     }
176 
177     public String getNameWithGroup()
178     {
179         return original.getNameWithGroup();
180     }
181 }