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 org.apache.maven.execution.MavenSession;
23 import org.apache.maven.plugin.AbstractMojo;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugin.failsafe.util.FailsafeSummaryXmlUtils;
27 import org.apache.maven.plugin.surefire.SurefireHelper;
28 import org.apache.maven.plugin.surefire.SurefireReportParameters;
29 import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
30 import org.apache.maven.plugins.annotations.Component;
31 import org.apache.maven.plugins.annotations.LifecyclePhase;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.surefire.cli.CommandLineOption;
35 import org.apache.maven.surefire.suite.RunResult;
36 import org.codehaus.plexus.logging.Logger;
37
38 import java.io.File;
39 import java.util.Collection;
40
41 import static org.apache.maven.plugin.surefire.SurefireHelper.reportExecution;
42 import static org.apache.maven.shared.utils.StringUtils.capitalizeFirstLetter;
43 import static org.apache.maven.surefire.suite.RunResult.noTestsRun;
44
45
46
47
48
49
50
51 @SuppressWarnings( "unused" )
52 @Mojo( name = "verify", defaultPhase = LifecyclePhase.VERIFY, requiresProject = true, threadSafe = true )
53 public class VerifyMojo
54 extends AbstractMojo
55 implements SurefireReportParameters
56 {
57
58
59
60
61
62
63
64 @Parameter( property = "skipTests" )
65 private boolean skipTests;
66
67
68
69
70
71
72
73 @Parameter( property = "skipITs" )
74 private boolean skipITs;
75
76
77
78
79
80
81
82 @Deprecated
83 @Parameter( property = "maven.test.skip.exec" )
84 private boolean skipExec;
85
86
87
88
89
90
91 @Parameter( property = "maven.test.skip", defaultValue = "false" )
92 private boolean skip;
93
94
95
96
97
98 @Parameter( property = "maven.test.failure.ignore", defaultValue = "false" )
99 private boolean testFailureIgnore;
100
101
102
103
104
105 @Parameter( defaultValue = "${basedir}" )
106 private File basedir;
107
108
109
110
111
112 @Parameter( defaultValue = "${project.build.testOutputDirectory}" )
113 private File testClassesDirectory;
114
115
116
117
118 @Parameter( defaultValue = "${project.build.directory}/failsafe-reports" )
119 private File reportsDirectory;
120
121
122
123
124 @Parameter( defaultValue = "${project.build.directory}/failsafe-reports/failsafe-summary.xml", required = true )
125 private File summaryFile;
126
127
128
129
130
131 @Parameter
132 private File[] summaryFiles;
133
134
135
136
137
138
139 @Parameter( property = "failIfNoTests" )
140 private Boolean failIfNoTests;
141
142
143
144
145
146
147
148 @Parameter( property = "encoding", defaultValue = "${project.reporting.outputEncoding}" )
149 private String encoding;
150
151
152
153
154 @Component
155 private MavenSession session;
156
157 @Component
158 private Logger logger;
159
160 private Collection<CommandLineOption> cli;
161
162 private volatile PluginConsoleLogger consoleLogger;
163
164 @Override
165 public void execute()
166 throws MojoExecutionException, MojoFailureException
167 {
168 cli = commandLineOptions();
169 if ( verifyParameters() )
170 {
171 logDebugOrCliShowErrors( capitalizeFirstLetter( getPluginName() )
172 + " report directory: " + getReportsDirectory() );
173
174 RunResult summary;
175 try
176 {
177 summary = existsSummaryFile() ? readSummary( summaryFile ) : noTestsRun();
178
179 if ( existsSummaryFiles() )
180 {
181 for ( final File summaryFile : summaryFiles )
182 {
183 summary = summary.aggregate( readSummary( summaryFile ) );
184 }
185 }
186 }
187 catch ( Exception e )
188 {
189 throw new MojoExecutionException( e.getMessage(), e );
190 }
191
192 reportExecution( this, summary, getConsoleLogger(), null );
193 }
194 }
195
196 private PluginConsoleLogger getConsoleLogger()
197 {
198 if ( consoleLogger == null )
199 {
200 synchronized ( this )
201 {
202 if ( consoleLogger == null )
203 {
204 consoleLogger = new PluginConsoleLogger( logger );
205 }
206 }
207 }
208 return consoleLogger;
209 }
210
211 private RunResult readSummary( File summaryFile ) throws Exception
212 {
213 return FailsafeSummaryXmlUtils.toRunResult( summaryFile );
214 }
215
216 protected boolean verifyParameters()
217 throws MojoFailureException
218 {
219 if ( isSkip() || isSkipTests() || isSkipITs() || isSkipExec() )
220 {
221 getConsoleLogger().info( "Tests are skipped." );
222 return false;
223 }
224
225 if ( !getTestClassesDirectory().exists() )
226 {
227 if ( getFailIfNoTests() != null && getFailIfNoTests() )
228 {
229 throw new MojoFailureException( "No tests to run!" );
230 }
231 }
232
233 if ( !existsSummary() )
234 {
235 getConsoleLogger().info( "No tests to run." );
236 return false;
237 }
238
239 return true;
240 }
241
242 protected String getPluginName()
243 {
244 return "failsafe";
245 }
246
247 protected String[] getDefaultIncludes()
248 {
249 return null;
250 }
251
252 @Override
253 public boolean isSkipTests()
254 {
255 return skipTests;
256 }
257
258 @Override
259 public void setSkipTests( boolean skipTests )
260 {
261 this.skipTests = skipTests;
262 }
263
264 public boolean isSkipITs()
265 {
266 return skipITs;
267 }
268
269 public void setSkipITs( boolean skipITs )
270 {
271 this.skipITs = skipITs;
272 }
273
274 @Override
275 @Deprecated
276 public boolean isSkipExec()
277 {
278 return skipExec;
279 }
280
281 @Override
282 @Deprecated
283 public void setSkipExec( boolean skipExec )
284 {
285 this.skipExec = skipExec;
286 }
287
288 @Override
289 public boolean isSkip()
290 {
291 return skip;
292 }
293
294 @Override
295 public void setSkip( boolean skip )
296 {
297 this.skip = skip;
298 }
299
300 @Override
301 public boolean isTestFailureIgnore()
302 {
303 return testFailureIgnore;
304 }
305
306 @Override
307 public void setTestFailureIgnore( boolean testFailureIgnore )
308 {
309 this.testFailureIgnore = testFailureIgnore;
310 }
311
312 @Override
313 public File getBasedir()
314 {
315 return basedir;
316 }
317
318 @Override
319 public void setBasedir( File basedir )
320 {
321 this.basedir = basedir;
322 }
323
324 @Override
325 public File getTestClassesDirectory()
326 {
327 return testClassesDirectory;
328 }
329
330 @Override
331 public void setTestClassesDirectory( File testClassesDirectory )
332 {
333 this.testClassesDirectory = testClassesDirectory;
334 }
335
336 @Override
337 public File getReportsDirectory()
338 {
339 return reportsDirectory;
340 }
341
342 @Override
343 public void setReportsDirectory( File reportsDirectory )
344 {
345 this.reportsDirectory = reportsDirectory;
346 }
347
348 @Override
349 public Boolean getFailIfNoTests()
350 {
351 return failIfNoTests;
352 }
353
354 @Override
355 public void setFailIfNoTests( boolean failIfNoTests )
356 {
357 this.failIfNoTests = failIfNoTests;
358 }
359
360 private boolean existsSummaryFile()
361 {
362 return summaryFile != null && summaryFile.isFile();
363 }
364
365 private boolean existsSummaryFiles()
366 {
367 return summaryFiles != null && summaryFiles.length != 0;
368 }
369
370 private boolean existsSummary()
371 {
372 return existsSummaryFile() || existsSummaryFiles();
373 }
374
375 private Collection<CommandLineOption> commandLineOptions()
376 {
377 return SurefireHelper.commandLineOptions( session, getConsoleLogger() );
378 }
379
380 private void logDebugOrCliShowErrors( String s )
381 {
382 SurefireHelper.logDebugOrCliShowErrors( s, getConsoleLogger(), cli );
383 }
384
385 }