47
47
import java.util.Collections;
48
48
import java.util.List;
49
49
import java.util.ListIterator;
51
50
import java.util.concurrent.ConcurrentHashMap;
52
import java.util.concurrent.locks.Lock;
53
import java.util.concurrent.locks.ReadWriteLock;
54
import java.util.concurrent.locks.ReentrantReadWriteLock;
51
import java.util.concurrent.ConcurrentMap;
56
import org.apache.commons.lang.StringUtils;
57
import org.apache.maven.artifact.versioning.ComparableVersion;
58
53
import org.apache.maven.plugin.MojoFailureException;
59
54
import org.apache.maven.project.MavenProject;
60
55
import org.apache.tools.ant.DirectoryScanner;
68
63
* @author Kohsuke Kawaguchi
70
65
public class SurefireArchiver extends MavenReporter {
71
private TestResult result;
66
private transient TestResult result;
74
69
* Store the filesets here as we want to track ignores between multiple runs of this class<br/>
75
70
* Note: Because this class can be run with different mojo goals with different path settings,
76
71
* we track multiple {@link FileSet}s for each encountered <tt>reportsDir</tt>
78
private transient Map<File, FileSet> fileSets = new ConcurrentHashMap<File,FileSet>();// Collections.synchronizedMap(new HashMap<File, FileSet>());
80
private final ReadWriteLock fileSetsReadWriteLock = new ReentrantReadWriteLock();
81
private final Lock fileSetsReadLock = fileSetsReadWriteLock.readLock();
82
private Lock fileSetsWriteLock = fileSetsReadWriteLock.writeLock();
73
private transient ConcurrentMap<File, FileSet> fileSets = new ConcurrentHashMap<File,FileSet>();
84
75
public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
85
76
if (isSurefireTest(mojo)) {
88
79
// so that we can record this as yellow.
89
80
// note that because of the way Maven works, just updating system property at this point is too late
90
81
XmlPlexusConfiguration c = (XmlPlexusConfiguration) mojo.configuration.getChild("testFailureIgnore");
91
if(c!=null && c.getValue().equals("${maven.test.failure.ignore}") && System.getProperty("maven.test.failure.ignore")==null) {
92
if (maven3orLater( build.getMavenBuildInformation().getMavenVersion() )) {
82
if(c!=null && c.getValue() != null && c.getValue().equals("${maven.test.failure.ignore}") && System.getProperty("maven.test.failure.ignore")==null) {
83
if (build.getMavenBuildInformation().isMaven3OrLater()) {
93
84
String fieldName = "testFailureIgnore";
94
85
if (mojo.mojoExecution.getConfiguration().getChild( fieldName ) != null) {
95
86
mojo.mojoExecution.getConfiguration().getChild( fieldName ).setValue( Boolean.TRUE.toString() );
131
122
if(reportsDir.exists()) {
132
123
// surefire:test just skips itself when the current project is not a java project
134
FileSet fileSet = getFileSet(reportsDir);
135
DirectoryScanner ds = fileSet.getDirectoryScanner();
137
if(ds.getIncludedFiles().length==0)
138
// no test in this module
141
rememberCheckedFiles(reportsDir, ds);
143
if(result==null) result = new TestResult();
144
result.parse(System.currentTimeMillis() - build.getMilliSecsSinceBuildStart(), ds);
146
int failCount = build.execute(new BuildCallable<Integer, IOException>() {
147
public Integer call(MavenBuild build) throws IOException, InterruptedException {
148
SurefireReport sr = build.getAction(SurefireReport.class);
150
build.getActions().add(new SurefireReport(build, result, listener));
152
sr.setResult(result,listener);
153
if(result.getFailCount()>0)
154
build.setResult(Result.UNSTABLE);
155
build.registerAsProjectAction(new FactoryImpl());
156
return result.getFailCount();
160
// if surefire plugin is going to kill maven because of a test failure,
161
// intercept that (or otherwise build will be marked as failure)
162
if(failCount>0 && error instanceof MojoFailureException) {
163
MavenBuilder.markAsSuccess = true;
125
synchronized (build) {
126
FileSet fileSet = getFileSet(reportsDir);
128
DirectoryScanner ds = fileSet.getDirectoryScanner();
130
if(ds.getIncludedFilesCount()==0)
131
// no test in this module
134
String[] reportFiles = ds.getIncludedFiles();
135
rememberCheckedFiles(reportsDir, reportFiles);
137
if(result==null) result = new TestResult();
138
result.parse(System.currentTimeMillis() - build.getMilliSecsSinceBuildStart(), reportsDir, reportFiles);
140
// final reference in order to serialize it:
141
final TestResult r = result;
143
int failCount = build.execute(new BuildCallable<Integer, IOException>() {
144
private static final long serialVersionUID = -1023888330720922136L;
146
public Integer call(MavenBuild build) throws IOException, InterruptedException {
147
SurefireReport sr = build.getAction(SurefireReport.class);
149
build.getActions().add(new SurefireReport(build, r, listener));
151
sr.setResult(r,listener);
152
if(r.getFailCount()>0)
153
build.setResult(Result.UNSTABLE);
154
build.registerAsProjectAction(new FactoryImpl());
155
return r.getFailCount();
159
// if surefire plugin is going to kill maven because of a test failure,
160
// intercept that (or otherwise build will be marked as failure)
161
if(failCount>0 && error instanceof MojoFailureException) {
162
MavenBuilder.markAsSuccess = true;
164
// TODO currently error is empty : will be here with maven 3.0.2+
166
Maven3Builder.markAsSuccess = true;
165
// TODO currenlty error is empty : will be here with maven 3.0.2+
167
Maven3Builder.markAsSuccess = true;
179
private FileSet getFileSet(File baseDir) {
181
if (fileSets == null) {
182
fileSetsWriteLock.lock();
184
fileSets = new ConcurrentHashMap<File,FileSet>();
186
fileSetsWriteLock.unlock();
190
fileSetsReadLock.lock();
192
fs = fileSets.get(baseDir);
194
fileSetsReadLock.unlock();
179
FileSet getFileSet(File baseDir) {
180
FileSet fs = fileSets.get(baseDir);
196
181
if (fs == null) {
197
182
fs = Util.createFileSet(baseDir, "*.xml","testng-results.xml,testng-failed.xml");
198
fileSetsWriteLock.lock();
200
fileSets.put(baseDir, fs);
202
fileSetsWriteLock.unlock();
183
FileSet previous = fileSets.putIfAbsent(baseDir, fs);
184
if (previous != null) {
210
193
* Add checked files to the exclude list of the fileSet
212
private void rememberCheckedFiles(File baseDir, DirectoryScanner ds) {
195
private void rememberCheckedFiles(File baseDir, String[] reportFiles) {
213
196
FileSet fileSet = getFileSet(baseDir);
215
for (String file : ds.getIncludedFiles()) {
216
fileSet.setExcludes(file);
198
for (String file : reportFiles) {
199
fileSet.createExclude().setName(file);
245
228
private boolean isSurefireTest(MojoInfo mojo) {
246
229
if ((!mojo.is("com.sun.maven", "maven-junit-plugin", "test"))
247
230
&& (!mojo.is("org.sonatype.flexmojos", "flexmojos-maven-plugin", "test-run"))
231
&& (!mojo.is("org.eclipse.tycho", "tycho-surefire-plugin", "test"))
232
&& (!mojo.is("org.sonatype.tycho", "maven-osgi-test-plugin", "test"))
233
&& (!mojo.is("org.codehaus.mojo", "gwt-maven-plugin", "test"))
248
234
&& (!mojo.is("org.apache.maven.plugins", "maven-surefire-plugin", "test"))
249
235
&& (!mojo.is("org.apache.maven.plugins", "maven-failsafe-plugin", "integration-test")))
284
270
if (((skipTests != null) && (skipTests))) {
273
} else if (mojo.is("org.sonatype.tycho", "maven-osgi-test-plugin", "test")) {
274
Boolean skipTests = mojo.getConfigurationValue("skipTest", Boolean.class);
275
if (((skipTests != null) && (skipTests))) {
278
} else if (mojo.is("org.eclipse.tycho", "tycho-surefire-plugin", "test")) {
279
Boolean skipTests = mojo.getConfigurationValue("skipTest", Boolean.class);
280
if (((skipTests != null) && (skipTests))) {
289
285
} catch (ComponentConfigurationException e) {
296
public boolean maven3orLater(String mavenVersion) {
297
// null or empty so false !
298
if (StringUtils.isBlank( mavenVersion )) {
301
return new ComparableVersion (mavenVersion).compareTo( new ComparableVersion ("3.0") ) >= 0;
292
// I'm not sure if SurefireArchiver is actually ever (de-)serialized,
293
// but just to be sure, set fileSets here
294
protected Object readResolve() {
295
fileSets = new ConcurrentHashMap<File,FileSet>();
305
300
public static final class DescriptorImpl extends MavenReporterDescriptor {