1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.nuxeo.build.maven;
18
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugin.surefire.SurefireHelper;
31 import org.apache.maven.plugin.surefire.SurefireReportParameters;
32 import org.apache.maven.plugins.annotations.LifecyclePhase;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.plugins.annotations.ResolutionScope;
36 import org.apache.maven.plugins.surefire.report.ReportTestSuite;
37 import org.apache.maven.plugins.surefire.report.SurefireReportParser;
38 import org.apache.maven.reporting.MavenReportException;
39 import org.apache.maven.surefire.suite.RunResult;
40 import org.codehaus.plexus.util.StringUtils;
41
42
43
44
45
46
47
48
49 @Mojo(name = "verify", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true,
50 requiresProject = true, requiresDependencyResolution = ResolutionScope.TEST)
51 public class VerifyMojo extends IntegrationTestMojo implements SurefireReportParameters {
52
53
54
55
56
57
58 @Parameter
59 protected File[] summaryFiles;
60
61
62
63
64
65
66
67
68
69 @Parameter(property = "createSummary", defaultValue = "false")
70 private boolean createSummary;
71
72
73
74
75
76
77
78 @Parameter(defaultValue = "${project.build.directory}/nxtools-reports")
79 private File reportsDirectory;
80
81
82
83
84
85
86 @Parameter(property = "skipTests")
87 protected boolean skipTests;
88
89 @Override
90 public String getEncoding() {
91 if (StringUtils.isEmpty(reportingEncoding)) {
92 reportingEncoding = super.getEncoding();
93 }
94 return reportingEncoding;
95 }
96
97
98
99
100
101
102
103 @SuppressWarnings("hiding")
104 @Parameter(property = "maven.test.failure.ignore", defaultValue = "false")
105 protected boolean testFailureIgnore;
106
107
108
109
110
111
112 @Parameter(property = "failIfNoTests")
113 protected Boolean failIfNoTests;
114
115 @Override
116 public void execute() throws MojoExecutionException, MojoFailureException {
117 if (isSkipITs() || isSkipTests()) {
118 getLog().info("Tests are skipped.");
119 return;
120 }
121
122 RunResult summary = RunResult.noTestsRun();
123 if (createSummary) {
124 List<File> reportsDirectories = Collections.singletonList(reportsDirectory);
125 SurefireReportParser parser = new SurefireReportParser(reportsDirectories, Locale.getDefault());
126 try {
127 List<ReportTestSuite> reports = parser.parseXMLReportFiles();
128 if (reports.isEmpty()) {
129 getLog().info("No report found in " + reportsDirectory);
130 }
131 int total = 0, errors = 0, failures = 0, skipped = 0, flakes = 0;
132 for (ReportTestSuite report : reports) {
133 total += report.getNumberOfTests();
134 errors += report.getNumberOfErrors();
135 failures += report.getNumberOfFailures();
136 skipped += report.getNumberOfSkipped();
137 flakes += report.getNumberOfFlakes();
138 }
139 summary = new RunResult(total, errors, failures, skipped, flakes);
140 } catch (MavenReportException e) {
141 getLog().error("Could not parse reports from " + reportsDirectory, e);
142 throw new MojoExecutionException(e.getMessage(), e);
143 }
144 }
145
146 try {
147 if (summaryFile.isFile()) {
148 summary = summary.aggregate(readSummary(getEncoding(), summaryFile));
149 }
150 if (summaryFiles != null) {
151 for (File file : summaryFiles) {
152 summary = summary.aggregate(readSummary(getEncoding(), file));
153 }
154 }
155 if (createSummary) {
156 writeSummary(summary);
157 }
158 } catch (IOException e) {
159 throw new MojoExecutionException(e.getMessage(), e);
160 }
161 SurefireHelper.reportExecution(this, summary, getLog());
162 }
163
164 private RunResult readSummary(String sumEncoding, File sumFile) throws IOException {
165 try (InputStream in = new BufferedInputStream(new FileInputStream(sumFile))) {
166 return RunResult.fromInputStream(in, sumEncoding);
167 }
168 }
169
170
171
172
173 public boolean isSkipITs() {
174 return skipITs;
175 }
176
177
178
179
180 public void setSkipITs(boolean skipITs) {
181 this.skipITs = skipITs;
182 }
183
184 @Override
185 public boolean isSkipTests() {
186 return skipTests;
187 }
188
189 @Override
190 public void setSkipTests(boolean skipTests) {
191 this.skipTests = skipTests;
192 }
193
194
195
196
197 @Override
198 @Deprecated
199 public boolean isSkipExec() {
200 return isSkipITs() || isSkipTests();
201 }
202
203
204
205
206 @Override
207 @Deprecated
208 public void setSkipExec(boolean skipExec) {
209 throw new UnsupportedOperationException();
210 }
211
212
213
214
215 @Override
216 @Deprecated
217 public boolean isSkip() {
218 return isSkipITs() || isSkipTests();
219 }
220
221
222
223
224 @Override
225 @Deprecated
226 public void setSkip(boolean skip) {
227 throw new UnsupportedOperationException();
228 }
229
230 @Override
231 public boolean isTestFailureIgnore() {
232 return testFailureIgnore;
233 }
234
235 @Override
236 public void setTestFailureIgnore(boolean testFailureIgnore) {
237 this.testFailureIgnore = testFailureIgnore;
238 }
239
240 @Override
241 public File getBasedir() {
242 return project.getBasedir();
243 }
244
245 @Override
246 public void setBasedir(File basedir) {
247 throw new UnsupportedOperationException();
248 }
249
250 @Override
251 public File getTestClassesDirectory() {
252 return new File(project.getBuild().getTestOutputDirectory());
253 }
254
255 @Override
256 public void setTestClassesDirectory(File testClassesDirectory) {
257 throw new UnsupportedOperationException();
258 }
259
260 @Override
261 public File getReportsDirectory() {
262 return summaryFile.getParentFile();
263 }
264
265 @Override
266 public void setReportsDirectory(File reportsDirectory) {
267 throw new UnsupportedOperationException();
268 }
269
270 @Override
271 public Boolean getFailIfNoTests() {
272 return failIfNoTests;
273 }
274
275 @Override
276 public void setFailIfNoTests(boolean failIfNoTests) {
277 throw new UnsupportedOperationException();
278 }
279
280 }