1 | |
package org.apache.maven.plugin.failsafe; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.BufferedInputStream; |
23 | |
import java.io.File; |
24 | |
import java.io.FileInputStream; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.io.Reader; |
28 | |
import org.apache.maven.plugin.AbstractMojo; |
29 | |
import org.apache.maven.plugin.MojoExecutionException; |
30 | |
import org.apache.maven.plugin.MojoFailureException; |
31 | |
import org.apache.maven.plugin.surefire.SurefireHelper; |
32 | |
import org.apache.maven.plugin.surefire.SurefireReportParameters; |
33 | |
import org.apache.maven.surefire.failsafe.model.FailsafeSummary; |
34 | |
import org.apache.maven.surefire.failsafe.model.io.xpp3.FailsafeSummaryXpp3Reader; |
35 | |
import org.codehaus.plexus.util.IOUtil; |
36 | |
import org.codehaus.plexus.util.ReaderFactory; |
37 | |
import org.codehaus.plexus.util.StringUtils; |
38 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class VerifyMojo |
51 | |
extends AbstractMojo |
52 | |
implements SurefireReportParameters |
53 | |
{ |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private boolean skipTests; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private boolean skipITs; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
private boolean skipExec; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private boolean skip; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private boolean testFailureIgnore; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private File basedir; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private File testClassesDirectory; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private File reportsDirectory; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
private File summaryFile; |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
private File[] summaryFiles; |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
private Boolean failIfNoTests; |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
private String encoding; |
155 | |
|
156 | |
|
157 | |
public void execute() |
158 | |
throws MojoExecutionException, MojoFailureException |
159 | |
{ |
160 | 0 | if ( verifyParameters() ) |
161 | |
{ |
162 | 0 | getLog().info( |
163 | |
StringUtils.capitalizeFirstLetter( getPluginName() ) + " report directory: " + getReportsDirectory() ); |
164 | |
|
165 | |
int result; |
166 | |
try |
167 | |
{ |
168 | |
String encoding; |
169 | 0 | if ( StringUtils.isEmpty( this.encoding ) ) |
170 | |
{ |
171 | 0 | getLog().warn( |
172 | |
"File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING |
173 | |
+ ", i.e. build is platform dependent!" ); |
174 | 0 | encoding = ReaderFactory.FILE_ENCODING; |
175 | |
} |
176 | |
else |
177 | |
{ |
178 | 0 | encoding = this.encoding; |
179 | |
} |
180 | |
|
181 | |
final FailsafeSummary summary; |
182 | 0 | if ( !summaryFile.isFile() && summaryFiles != null ) |
183 | |
{ |
184 | 0 | summary = new FailsafeSummary(); |
185 | |
} |
186 | |
else |
187 | |
{ |
188 | 0 | summary = readSummary( encoding, summaryFile ); |
189 | |
} |
190 | 0 | if ( summaryFiles != null ) |
191 | |
{ |
192 | 0 | for ( int i = 0; i < summaryFiles.length; i++ ) |
193 | |
{ |
194 | 0 | summary.merge( readSummary( encoding, summaryFiles[i] ) ); |
195 | |
} |
196 | |
} |
197 | 0 | result = summary.getResult(); |
198 | |
} |
199 | 0 | catch ( IOException e ) |
200 | |
{ |
201 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
202 | |
} |
203 | 0 | catch ( XmlPullParserException e ) |
204 | |
{ |
205 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
206 | 0 | } |
207 | |
|
208 | 0 | SurefireHelper.reportExecution( this, result, getLog() ); |
209 | |
} |
210 | 0 | } |
211 | |
|
212 | |
private FailsafeSummary readSummary( String encoding, File summaryFile ) |
213 | |
throws IOException, XmlPullParserException |
214 | |
{ |
215 | 0 | FileInputStream fileInputStream = null; |
216 | 0 | BufferedInputStream bufferedInputStream = null; |
217 | 0 | Reader reader = null; |
218 | |
try |
219 | |
{ |
220 | 0 | fileInputStream = new FileInputStream( summaryFile ); |
221 | 0 | bufferedInputStream = new BufferedInputStream( fileInputStream ); |
222 | 0 | reader = new InputStreamReader( bufferedInputStream, encoding ); |
223 | 0 | FailsafeSummaryXpp3Reader xpp3Reader = new FailsafeSummaryXpp3Reader(); |
224 | 0 | return xpp3Reader.read( reader ); |
225 | |
} |
226 | |
finally |
227 | |
{ |
228 | 0 | IOUtil.close( reader ); |
229 | 0 | IOUtil.close( bufferedInputStream ); |
230 | 0 | IOUtil.close( fileInputStream ); |
231 | |
} |
232 | |
} |
233 | |
|
234 | |
protected boolean verifyParameters() |
235 | |
throws MojoFailureException |
236 | |
{ |
237 | 0 | if ( isSkip() || isSkipTests() || isSkipITs() || isSkipExec() ) |
238 | |
{ |
239 | 0 | getLog().info( "Tests are skipped." ); |
240 | 0 | return false; |
241 | |
} |
242 | |
|
243 | 0 | if ( !getTestClassesDirectory().exists() ) |
244 | |
{ |
245 | 0 | if ( getFailIfNoTests() != null && getFailIfNoTests().booleanValue() ) |
246 | |
{ |
247 | 0 | throw new MojoFailureException( "No tests to run!" ); |
248 | |
} |
249 | 0 | getLog().info( "No tests to run." ); |
250 | 0 | return false; |
251 | |
} |
252 | |
|
253 | 0 | return true; |
254 | |
} |
255 | |
|
256 | |
protected String getPluginName() |
257 | |
{ |
258 | 0 | return "failsafe"; |
259 | |
} |
260 | |
|
261 | |
protected String[] getDefaultIncludes() |
262 | |
{ |
263 | 0 | return null; |
264 | |
} |
265 | |
|
266 | |
public boolean isSkipTests() |
267 | |
{ |
268 | 0 | return skipTests; |
269 | |
} |
270 | |
|
271 | |
public void setSkipTests( boolean skipTests ) |
272 | |
{ |
273 | 0 | this.skipTests = skipTests; |
274 | 0 | } |
275 | |
|
276 | |
public boolean isSkipITs() |
277 | |
{ |
278 | 0 | return skipITs; |
279 | |
} |
280 | |
|
281 | |
public void setSkipITs( boolean skipITs ) |
282 | |
{ |
283 | 0 | this.skipITs = skipITs; |
284 | 0 | } |
285 | |
|
286 | |
public boolean isSkipExec() |
287 | |
{ |
288 | 0 | return skipExec; |
289 | |
} |
290 | |
|
291 | |
public void setSkipExec( boolean skipExec ) |
292 | |
{ |
293 | 0 | this.skipExec = skipExec; |
294 | 0 | } |
295 | |
|
296 | |
public boolean isSkip() |
297 | |
{ |
298 | 0 | return skip; |
299 | |
} |
300 | |
|
301 | |
public void setSkip( boolean skip ) |
302 | |
{ |
303 | 0 | this.skip = skip; |
304 | 0 | } |
305 | |
|
306 | |
public boolean isTestFailureIgnore() |
307 | |
{ |
308 | 0 | return testFailureIgnore; |
309 | |
} |
310 | |
|
311 | |
public void setTestFailureIgnore( boolean testFailureIgnore ) |
312 | |
{ |
313 | 0 | this.testFailureIgnore = testFailureIgnore; |
314 | 0 | } |
315 | |
|
316 | |
public File getBasedir() |
317 | |
{ |
318 | 0 | return basedir; |
319 | |
} |
320 | |
|
321 | |
public void setBasedir( File basedir ) |
322 | |
{ |
323 | 0 | this.basedir = basedir; |
324 | 0 | } |
325 | |
|
326 | |
public File getTestClassesDirectory() |
327 | |
{ |
328 | 0 | return testClassesDirectory; |
329 | |
} |
330 | |
|
331 | |
public void setTestClassesDirectory( File testClassesDirectory ) |
332 | |
{ |
333 | 0 | this.testClassesDirectory = testClassesDirectory; |
334 | 0 | } |
335 | |
|
336 | |
public File getReportsDirectory() |
337 | |
{ |
338 | 0 | return reportsDirectory; |
339 | |
} |
340 | |
|
341 | |
public void setReportsDirectory( File reportsDirectory ) |
342 | |
{ |
343 | 0 | this.reportsDirectory = reportsDirectory; |
344 | 0 | } |
345 | |
|
346 | |
public Boolean getFailIfNoTests() |
347 | |
{ |
348 | 0 | return failIfNoTests; |
349 | |
} |
350 | |
|
351 | |
public void setFailIfNoTests( Boolean failIfNoTests ) |
352 | |
{ |
353 | 0 | this.failIfNoTests = failIfNoTests; |
354 | 0 | } |
355 | |
|
356 | |
} |