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