1 package org.apache.maven.plugin.surefire.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
105
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 }