1 package org.apache.maven.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.io.File;
23 import java.text.NumberFormat;
24 import java.util.Locale;
25
26
27
28
29
30 public abstract class AbstractReporter
31 implements Reporter
32 {
33 int completedCount;
34
35 int errors;
36
37 int failures;
38
39 private long startTime;
40
41 private long endTime;
42
43 private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
44
45 static final String NL = System.getProperty( "line.separator" );
46
47 private static final int MS_PER_SEC = 1000;
48
49 long testSetStartTime;
50
51 int skipped;
52
53 private final boolean trimStackTrace;
54
55 private final ReporterConfiguration reporterConfiguration;
56
57
58
59
60
61
62 AbstractReporter( ReporterConfiguration reporterConfiguration )
63 {
64 this.reporterConfiguration = reporterConfiguration;
65 this.trimStackTrace = reporterConfiguration.isTrimStackTrace().booleanValue();
66 }
67
68 boolean isTimedOut()
69 {
70 return reporterConfiguration.isTimedOut();
71 }
72
73 public void writeFooter( String footer )
74 {
75 writeMessage( footer );
76 }
77
78 public void runStarting()
79 {
80 }
81
82 public void runCompleted()
83 {
84 }
85
86 public void testSetStarting( ReportEntry report )
87 throws ReporterException
88 {
89 testSetStartTime = System.currentTimeMillis();
90 }
91
92 public void testSetCompleted( ReportEntry report )
93 throws ReporterException
94 {
95 }
96
97
98
99
100
101 public void testStarting( ReportEntry report )
102 {
103 startTime = System.currentTimeMillis();
104 }
105
106 public void testSucceeded( ReportEntry report )
107 {
108 endTest();
109 }
110
111 public void testSkipped( ReportEntry report )
112 {
113 ++skipped;
114
115 endTest();
116 }
117
118 public void testError( ReportEntry report, String stdOut, String stdErr )
119 {
120 ++errors;
121 endTest();
122 }
123
124 public void testFailed( ReportEntry report, String stdOut, String stdErr )
125 {
126 ++failures;
127 endTest();
128 }
129
130 private void endTest()
131 {
132 ++completedCount;
133
134 endTime = System.currentTimeMillis();
135
136
137 if ( startTime == 0 )
138 {
139 startTime = endTime;
140 }
141 }
142
143
144
145
146
147 int getNumErrors()
148 {
149 return errors;
150 }
151
152 int getNumSkipped()
153 {
154 return skipped;
155 }
156
157 int getNumFailures()
158 {
159 return failures;
160 }
161
162 int getNumTests()
163 {
164 return completedCount;
165 }
166
167
168
169
170
171 public void reset()
172 {
173 errors = 0;
174
175 skipped = 0;
176
177 failures = 0;
178
179 completedCount = 0;
180
181 }
182
183
184
185
186
187 String elapsedTimeAsString( long runTime )
188 {
189 return numberFormat.format( (double) runTime / MS_PER_SEC );
190 }
191
192
193
194
195
196
197
198 String getStackTrace( ReportEntry report )
199 {
200 StackTraceWriter writer = report.getStackTraceWriter();
201 if ( writer == null )
202 {
203 return null;
204 }
205 return trimStackTrace ? writer.writeTrimmedTraceToString() : writer.writeTraceToString();
206 }
207
208 long getActualRunTime( ReportEntry reportEntry )
209 {
210 final Integer clientSpecifiedElapsed = reportEntry.getElapsed();
211 return clientSpecifiedElapsed != null ? clientSpecifiedElapsed.intValue() : endTime - startTime;
212 }
213
214
215 public void testError( ReportEntry report )
216 {
217 }
218
219
220 public void testFailed( ReportEntry report )
221 {
222 }
223
224
225 public void testAssumptionFailure( ReportEntry report )
226 {
227 }
228
229 void deleteIfExisting( File reportFile )
230 {
231 if ( reportFile.exists() )
232 {
233
234 reportFile.delete();
235 }
236 }
237 }