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 1098345 2011-05-01 14:56:14Z 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      // ----------------------------------------------------------------------
56      // Report interface
57      // ----------------------------------------------------------------------
58  
59  
60      protected AbstractReporter( boolean trimStackTrace )
61      {
62          this.trimStackTrace = trimStackTrace;
63      }
64  
65  
66      public void writeMessage( byte[] b, int off, int len )
67      {
68          // Keep quiet about console output
69          // Reporting is itching for a cleanup
70      }
71  
72  
73      public void testSetStarting( ReportEntry report )
74          throws ReporterException
75      {
76          testSetStartTime = System.currentTimeMillis();
77      }
78  
79      public void testSetCompleted( ReportEntry report )
80          throws ReporterException
81      {
82      }
83  
84      // ----------------------------------------------------------------------
85      // Test
86      // ----------------------------------------------------------------------
87  
88      public void testStarting( ReportEntry report )
89      {
90          startTime = System.currentTimeMillis();
91      }
92  
93      public void testSucceeded( ReportEntry report )
94      {
95          endTest();
96      }
97  
98      public void testSkipped( ReportEntry report )
99      {
100         ++skipped;
101 
102         endTest();
103     }
104 
105     public void testError( ReportEntry report, String stdOut, String stdErr )
106     {
107         ++errors;
108         endTest();
109     }
110 
111     public void testFailed( ReportEntry report, String stdOut, String stdErr )
112     {
113         ++failures;
114         endTest();
115     }
116 
117     private void endTest()
118     {
119         ++completedCount;
120 
121         endTime = System.currentTimeMillis();
122         // SUREFIRE-398 skipped tests call endTest without calling testStarting
123         // if startTime = 0, set it to endTime, so the diff will be 0
124         if ( startTime == 0 )
125         {
126             startTime = endTime;
127         }
128     }
129 
130     // ----------------------------------------------------------------------
131     // Counters
132     // ----------------------------------------------------------------------
133 
134     int getNumErrors()
135     {
136         return errors;
137     }
138 
139     int getNumSkipped()
140     {
141         return skipped;
142     }
143 
144     int getNumFailures()
145     {
146         return failures;
147     }
148 
149     int getNumTests()
150     {
151         return completedCount;
152     }
153 
154     // ----------------------------------------------------------------------
155     //
156     // ----------------------------------------------------------------------
157 
158     public void reset()
159     {
160         errors = 0;
161 
162         skipped = 0;
163 
164         failures = 0;
165 
166         completedCount = 0;
167 
168     }
169 
170     // ----------------------------------------------------------------------
171     //
172     // ----------------------------------------------------------------------
173 
174     String elapsedTimeAsString( long runTime )
175     {
176         return numberFormat.format( (double) runTime / MS_PER_SEC );
177     }
178 
179     /**
180      * Returns stacktrace as String.
181      *
182      * @param report ReportEntry object.
183      * @return stacktrace as string.
184      */
185     String getStackTrace( ReportEntry report )
186     {
187         StackTraceWriter writer = report.getStackTraceWriter();
188         if ( writer == null )
189         {
190             return null;
191         }
192         return trimStackTrace ? writer.writeTrimmedTraceToString() : writer.writeTraceToString();
193     }
194 
195     long getActualRunTime( ReportEntry reportEntry )
196     {
197         final Integer clientSpecifiedElapsed = reportEntry.getElapsed();
198         return clientSpecifiedElapsed != null ? clientSpecifiedElapsed.intValue() : endTime - startTime;
199     }
200 
201     void deleteIfExisting( File reportFile )
202     {
203         if ( reportFile.exists() )
204         {
205             //noinspection ResultOfMethodCallIgnored
206             reportFile.delete();
207         }
208     }
209 }