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