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 import java.text.NumberFormat;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Locale;
25
26
27
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
85
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
128 for ( WrappedReportEntry entry : reportEntries )
129 {
130 entry.getStdout().free();
131 entry.getStdErr().free();
132 }
133
134 reportEntries.clear();
135 }
136
137 public int getCompletedCount()
138 {
139 return completedCount;
140 }
141
142 public int getErrors()
143 {
144 return errors;
145 }
146
147 public int getFailures()
148 {
149 return failures;
150 }
151
152 public int getSkipped()
153 {
154 return skipped;
155 }
156
157 private static final String TEST_SET_COMPLETED_PREFIX = "Tests run: ";
158
159 private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
160
161 private static final int MS_PER_SEC = 1000;
162
163 String elapsedTimeAsString( long runTime )
164 {
165 return numberFormat.format( (double) runTime / MS_PER_SEC );
166 }
167
168 public String getElapsedForTestSet()
169 {
170 return elapsedTimeAsString( elapsedForTestSet );
171 }
172
173 private void incrementCompletedCount()
174 {
175 completedCount += 1;
176 }
177
178 public String getTestSetSummary( WrappedReportEntry reportEntry )
179 {
180 StringBuilder buf = new StringBuilder();
181
182 buf.append( TEST_SET_COMPLETED_PREFIX );
183 buf.append( completedCount );
184 buf.append( ", Failures: " );
185 buf.append( failures );
186 buf.append( ", Errors: " );
187 buf.append( errors );
188 buf.append( ", Skipped: " );
189 buf.append( skipped );
190 buf.append( ", Time elapsed: " );
191 buf.append( reportEntry.elapsedTimeAsString() );
192 buf.append( " sec" );
193
194 if ( failures > 0 || errors > 0 )
195 {
196 buf.append( " <<< FAILURE!" );
197 }
198
199 buf.append( " - in " );
200 buf.append( reportEntry.getNameWithGroup() );
201
202 buf.append( "\n" );
203
204 return buf.toString();
205 }
206
207 public List<String> getTestResults()
208 {
209 List<String> result = new ArrayList<String>();
210 for ( WrappedReportEntry testResult : reportEntries )
211 {
212 if ( testResult.isErrorOrFailure() )
213 {
214 result.add( testResult.getOutput( trimStackTrace ) );
215 }
216 else if ( plainFormat && testResult.isSkipped() )
217 {
218 result.add( testResult.getName() + " skipped" );
219 }
220 else if ( plainFormat && testResult.isSucceeded() )
221 {
222 result.add( testResult.getElapsedTimeSummary() );
223 }
224 }
225 return result;
226 }
227
228 public List<WrappedReportEntry> getReportEntries()
229 {
230 return reportEntries;
231 }
232 }