View Javadoc

1   package org.apache.maven.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.io.File;
23  import java.text.NumberFormat;
24  import java.util.Locale;
25  
26  /**
27   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
28   * @version $Id: AbstractReporter.java 1081527 2011-03-14 19:31:34Z krosenvold $
29   */
30  public abstract class AbstractReporter
31      implements Reporter
32  {
33      int completedCount;
34  
35      int errors;
36  
37      int failures;
38  
39      private long startTime;
40  
41      private long endTime;
42  
43      private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
44  
45      static final String NL = System.getProperty( "line.separator" );
46  
47      private static final int MS_PER_SEC = 1000;
48  
49      long testSetStartTime;
50  
51      int skipped;
52  
53      private final boolean trimStackTrace;
54  
55      private final ReporterConfiguration reporterConfiguration;
56  
57      // ----------------------------------------------------------------------
58      // Report interface
59      // ----------------------------------------------------------------------
60  
61  
62      AbstractReporter( ReporterConfiguration reporterConfiguration )
63      {
64          this.reporterConfiguration = reporterConfiguration;
65          this.trimStackTrace = reporterConfiguration.isTrimStackTrace().booleanValue();
66      }
67  
68      boolean isTimedOut()
69      {
70          return reporterConfiguration.isTimedOut();
71      }
72  
73      public void writeFooter( String footer )
74      {
75          writeMessage( footer );
76      }
77  
78      public void runStarting()
79      {
80      }
81  
82      public void runCompleted()
83      {
84      }
85  
86      public void testSetStarting( ReportEntry report )
87          throws ReporterException
88      {
89          testSetStartTime = System.currentTimeMillis();
90      }
91  
92      public void testSetCompleted( ReportEntry report )
93          throws ReporterException
94      {
95      }
96  
97      // ----------------------------------------------------------------------
98      // Test
99      // ----------------------------------------------------------------------
100 
101     public void testStarting( ReportEntry report )
102     {
103         startTime = System.currentTimeMillis();
104     }
105 
106     public void testSucceeded( ReportEntry report )
107     {
108         endTest();
109     }
110 
111     public void testSkipped( ReportEntry report )
112     {
113         ++skipped;
114 
115         endTest();
116     }
117 
118     public void testError( ReportEntry report, String stdOut, String stdErr )
119     {
120         ++errors;
121         endTest();
122     }
123 
124     public void testFailed( ReportEntry report, String stdOut, String stdErr )
125     {
126         ++failures;
127         endTest();
128     }
129 
130     private void endTest()
131     {
132         ++completedCount;
133 
134         endTime = System.currentTimeMillis();
135         // SUREFIRE-398 skipped tests call endTest without calling testStarting
136         // if startTime = 0, set it to endTime, so the diff will be 0
137         if ( startTime == 0 )
138         {
139             startTime = endTime;
140         }
141     }
142 
143     // ----------------------------------------------------------------------
144     // Counters
145     // ----------------------------------------------------------------------
146 
147     int getNumErrors()
148     {
149         return errors;
150     }
151 
152     int getNumSkipped()
153     {
154         return skipped;
155     }
156 
157     int getNumFailures()
158     {
159         return failures;
160     }
161 
162     int getNumTests()
163     {
164         return completedCount;
165     }
166 
167     // ----------------------------------------------------------------------
168     //
169     // ----------------------------------------------------------------------
170 
171     public void reset()
172     {
173         errors = 0;
174 
175         skipped = 0;
176 
177         failures = 0;
178 
179         completedCount = 0;
180 
181     }
182 
183     // ----------------------------------------------------------------------
184     //
185     // ----------------------------------------------------------------------
186 
187     String elapsedTimeAsString( long runTime )
188     {
189         return numberFormat.format( (double) runTime / MS_PER_SEC );
190     }
191 
192     /**
193      * Returns stacktrace as String.
194      *
195      * @param report ReportEntry object.
196      * @return stacktrace as string.
197      */
198     String getStackTrace( ReportEntry report )
199     {
200         StackTraceWriter writer = report.getStackTraceWriter();
201         if ( writer == null )
202         {
203             return null;
204         }
205         return trimStackTrace ? writer.writeTrimmedTraceToString() : writer.writeTraceToString();
206     }
207 
208     long getActualRunTime( ReportEntry reportEntry )
209     {
210         final Integer clientSpecifiedElapsed = reportEntry.getElapsed();
211         return clientSpecifiedElapsed != null ? clientSpecifiedElapsed.intValue() : endTime - startTime;
212     }
213 
214     // @deprecated dont use.  TODO remove for 2.7.2
215     public void testError( ReportEntry report )
216     {
217     }
218 
219     // @deprecated dont use.  TODO remove for 2.7.2
220     public void testFailed( ReportEntry report )
221     {
222     }
223 
224     // @deprecated dont use.  TODO remove for 2.7.2
225     public void testAssumptionFailure( ReportEntry report )
226     {
227     }
228 
229     void deleteIfExisting( File reportFile )
230     {
231         if ( reportFile.exists() )
232         {
233             //noinspection ResultOfMethodCallIgnored
234             reportFile.delete();
235         }
236     }
237 }