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