View Javadoc

1   package org.apache.maven.plugin.surefire.report;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import java.text.NumberFormat;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Locale;
25  
26  /**
27   * Maintains per-thread test result state. Not thread safe.
28   */
29  public class TestSetStats
30  {
31      private final boolean trimStackTrace;
32  
33      private final boolean plainFormat;
34  
35      private long testSetStartAt;
36  
37      private long testStartAt;
38  
39      private int completedCount;
40  
41      private int errors;
42  
43      private int failures;
44  
45      private int skipped;
46  
47      private long lastStartAt;
48  
49      private long elapsedForTestSet;
50  
51      private final List<WrappedReportEntry> reportEntries = new ArrayList<WrappedReportEntry>();
52  
53      public TestSetStats( boolean trimStackTrace, boolean plainFormat )
54      {
55          this.trimStackTrace = trimStackTrace;
56          this.plainFormat = plainFormat;
57      }
58  
59      public int getElapsedSinceTestSetStart()
60      {
61          return (int) ( System.currentTimeMillis() - testSetStartAt );
62      }
63  
64      public int getElapsedSinceLastStart()
65      {
66          return (int) ( System.currentTimeMillis() - lastStartAt );
67      }
68  
69      public void testSetStart()
70      {
71          lastStartAt = testSetStartAt = System.currentTimeMillis();
72      }
73  
74      public void testStart()
75      {
76          lastStartAt = testStartAt = System.currentTimeMillis();
77      }
78  
79      private long finishTest( WrappedReportEntry reportEntry )
80      {
81          reportEntries.add( reportEntry );
82          incrementCompletedCount();
83          long testEndAt = System.currentTimeMillis();
84          // SUREFIRE-398 skipped tests call endTest without calling testStarting
85          // if startTime = 0, set it to endTime, so the diff will be 0
86          if ( testStartAt == 0 )
87          {
88              testStartAt = testEndAt;
89          }
90          long elapsedForThis = reportEntry.getElapsed() != null ? reportEntry.getElapsed() : testEndAt - testStartAt;
91          elapsedForTestSet += elapsedForThis;
92          return elapsedForThis;
93      }
94  
95      public void testSucceeded( WrappedReportEntry reportEntry )
96      {
97          finishTest( reportEntry );
98      }
99  
100 
101     public void testError( WrappedReportEntry reportEntry )
102     {
103         errors += 1;
104         finishTest( reportEntry );
105 
106     }
107 
108     public void testFailure( WrappedReportEntry reportEntry )
109     {
110         failures += 1;
111         finishTest( reportEntry );
112     }
113 
114     public void testSkipped( WrappedReportEntry reportEntry )
115     {
116         skipped += 1;
117         finishTest( reportEntry );
118     }
119 
120     public void reset()
121     {
122         completedCount = 0;
123         errors = 0;
124         failures = 0;
125         skipped = 0;
126         elapsedForTestSet = 0;
127         reportEntries.clear();
128     }
129 
130     public int getCompletedCount()
131     {
132         return completedCount;
133     }
134 
135     public int getErrors()
136     {
137         return errors;
138     }
139 
140     public int getFailures()
141     {
142         return failures;
143     }
144 
145     public int getSkipped()
146     {
147         return skipped;
148     }
149 
150     private static final String TEST_SET_COMPLETED_PREFIX = "Tests run: ";
151 
152     private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
153 
154     private static final int MS_PER_SEC = 1000;
155 
156     String elapsedTimeAsString( long runTime )
157     {
158         return numberFormat.format( (double) runTime / MS_PER_SEC );
159     }
160 
161     public String getElapsedForTestSet()
162     {
163         return elapsedTimeAsString( elapsedForTestSet );
164     }
165 
166     private void incrementCompletedCount()
167     {
168         completedCount += 1;
169     }
170 
171     public String getTestSetSummary( WrappedReportEntry reportEntry )
172     {
173         StringBuilder buf = new StringBuilder();
174 
175         buf.append( TEST_SET_COMPLETED_PREFIX );
176         buf.append( completedCount );
177         buf.append( ", Failures: " );
178         buf.append( failures );
179         buf.append( ", Errors: " );
180         buf.append( errors );
181         buf.append( ", Skipped: " );
182         buf.append( skipped );
183         buf.append( ", Time elapsed: " );
184         buf.append( reportEntry.elapsedTimeAsString() );
185         buf.append( " sec" );
186 
187         if ( failures > 0 || errors > 0 )
188         {
189             buf.append( " <<< FAILURE!" );
190         }
191         
192         buf.append( " - in " );
193         buf.append( reportEntry.getNameWithGroup() );
194 
195         buf.append( "\n" );
196 
197         return buf.toString();
198     }
199 
200     public List<String> getTestResults()
201     {
202         List<String> result = new ArrayList<String>();
203         for ( WrappedReportEntry testResult : reportEntries )
204         {
205             if ( testResult.isErrorOrFailure() )
206             {
207                 result.add( testResult.getOutput( trimStackTrace ) );
208             }
209             else if ( plainFormat && testResult.isSkipped() )
210             {
211                 result.add( testResult.getName() + " skipped" );
212             }
213             else if ( plainFormat && testResult.isSucceeded() )
214             {
215                 result.add( testResult.getElapsedTimeSummary() );
216             }
217         }
218         return result;
219     }
220 
221     public List<WrappedReportEntry> getReportEntries()
222     {
223         return reportEntries;
224     }
225 }