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