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.ArrayList;
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Queue;
28  import java.util.concurrent.ConcurrentLinkedQueue;
29  
30  /**
31   * Maintains per-thread test result state. Not thread safe.
32   */
33  public class TestSetStats
34  {
35      private final Queue<WrappedReportEntry> reportEntries = new ConcurrentLinkedQueue<WrappedReportEntry>();
36  
37      private final boolean trimStackTrace;
38  
39      private final boolean plainFormat;
40  
41      private long testSetStartAt;
42  
43      private long testStartAt;
44  
45      private int completedCount;
46  
47      private int errors;
48  
49      private int failures;
50  
51      private int skipped;
52  
53      private long lastStartAt;
54  
55      private long elapsedForTestSet;
56  
57      public TestSetStats( boolean trimStackTrace, boolean plainFormat )
58      {
59          this.trimStackTrace = trimStackTrace;
60          this.plainFormat = plainFormat;
61      }
62  
63      public int getElapsedSinceTestSetStart()
64      {
65          if ( testSetStartAt > 0 )
66          {
67              return (int) ( System.currentTimeMillis() - testSetStartAt );
68          }
69          else
70          {
71              return 0;
72          }
73      }
74  
75      public int getElapsedSinceLastStart()
76      {
77          if ( lastStartAt > 0 )
78          {
79              return (int) ( System.currentTimeMillis() - lastStartAt );
80          }
81          else
82          {
83              return 0;
84          }
85      }
86  
87      public void testSetStart()
88      {
89          testSetStartAt = System.currentTimeMillis();
90          lastStartAt = testSetStartAt;
91      }
92  
93      public void testStart()
94      {
95          testStartAt = System.currentTimeMillis();
96          lastStartAt = testStartAt;
97      }
98  
99      private long finishTest( WrappedReportEntry reportEntry )
100     {
101         reportEntries.add( reportEntry );
102         incrementCompletedCount();
103         long testEndAt = System.currentTimeMillis();
104         // SUREFIRE-398 skipped tests call endTest without calling testStarting
105         // if startTime = 0, set it to endTime, so the diff will be 0
106         if ( testStartAt == 0 )
107         {
108             testStartAt = testEndAt;
109         }
110         long elapsedForThis = reportEntry.getElapsed() != null ? reportEntry.getElapsed() : testEndAt - testStartAt;
111         elapsedForTestSet += elapsedForThis;
112         return elapsedForThis;
113     }
114 
115     public void testSucceeded( WrappedReportEntry reportEntry )
116     {
117         finishTest( reportEntry );
118     }
119 
120 
121     public void testError( WrappedReportEntry reportEntry )
122     {
123         errors += 1;
124         finishTest( reportEntry );
125 
126     }
127 
128     public void testFailure( WrappedReportEntry reportEntry )
129     {
130         failures += 1;
131         finishTest( reportEntry );
132     }
133 
134     public void testSkipped( WrappedReportEntry reportEntry )
135     {
136         skipped += 1;
137         finishTest( reportEntry );
138     }
139 
140     public void reset()
141     {
142         completedCount = 0;
143         errors = 0;
144         failures = 0;
145         skipped = 0;
146         elapsedForTestSet = 0;
147 
148         for ( WrappedReportEntry entry : reportEntries )
149         {
150             entry.getStdout().free();
151             entry.getStdErr().free();
152         }
153 
154         reportEntries.clear();
155     }
156 
157     public int getCompletedCount()
158     {
159         return completedCount;
160     }
161 
162     public int getErrors()
163     {
164         return errors;
165     }
166 
167     public int getFailures()
168     {
169         return failures;
170     }
171 
172     public int getSkipped()
173     {
174         return skipped;
175     }
176 
177     public String elapsedTimeAsString( long runTime )
178     {
179         return numberFormat.format( (double) runTime / MS_PER_SEC );
180     }
181 
182     private static final String TEST_SET_COMPLETED_PREFIX = "Tests run: ";
183 
184     private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
185 
186     private static final int MS_PER_SEC = 1000;
187 
188     public String getElapsedForTestSet()
189     {
190         return elapsedTimeAsString( elapsedForTestSet );
191     }
192 
193     private void incrementCompletedCount()
194     {
195         completedCount += 1;
196     }
197 
198     public String getTestSetSummary( WrappedReportEntry reportEntry )
199     {
200         StringBuilder buf = new StringBuilder();
201 
202         buf.append( TEST_SET_COMPLETED_PREFIX );
203         buf.append( completedCount );
204         buf.append( ", Failures: " );
205         buf.append( failures );
206         buf.append( ", Errors: " );
207         buf.append( errors );
208         buf.append( ", Skipped: " );
209         buf.append( skipped );
210         buf.append( ", Time elapsed: " );
211         buf.append( reportEntry.elapsedTimeAsString() );
212         buf.append( " sec" );
213 
214         if ( failures > 0 || errors > 0 )
215         {
216             buf.append( " <<< FAILURE!" );
217         }
218 
219         buf.append( " - in " );
220         buf.append( reportEntry.getNameWithGroup() );
221 
222         buf.append( "\n" );
223 
224         return buf.toString();
225     }
226 
227     public List<String> getTestResults()
228     {
229         List<String> result = new ArrayList<String>();
230         for ( WrappedReportEntry testResult : reportEntries )
231         {
232             if ( testResult.isErrorOrFailure() )
233             {
234                 result.add( testResult.getOutput( trimStackTrace ) );
235             }
236             else if ( plainFormat && testResult.isSkipped() )
237             {
238                 result.add( testResult.getName() + " skipped" );
239             }
240             else if ( plainFormat && testResult.isSucceeded() )
241             {
242                 result.add( testResult.getElapsedTimeSummary() );
243             }
244         }
245         return result;
246     }
247 
248     public Collection<WrappedReportEntry> getReportEntries()
249     {
250         return reportEntries;
251     }
252 }