~ubuntu-branches/ubuntu/precise/jenkins/precise-updates

« back to all changes in this revision

Viewing changes to maven-plugin/src/main/java/hudson/maven/reporters/SurefireArchiver.java

  • Committer: Package Import Robot
  • Author(s): James Page, Miguel Landaeta, James Page
  • Date: 2012-01-31 10:33:56 UTC
  • mfrom: (1.1.4) (5.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20120131103356-kg3w0k3s5dfdnsw8
Tags: 1.424.2+dfsg-1
[ Miguel Landaeta ]
* Replace dependencies on Spring Framework 2.5 libraries with 3.0 ones.
  (Closes: #655906).

[ James Page ]
* New upstream release.
  - d/control: Add new dependencies on libjenkins-remoting-java, 
    libstapler-adjunct-codemirror-java and libmaven-hpi-plugin-java.
  - d/control: Dropped libjcaptcha-java; no longer needed.
* d/control: Switch to using packaged animal-sniffer.
* Refreshed patches:
  - d/patches/build/{debianize-antrun-war,animal-sniffer-annotation}.patch:
    dropped as no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
import java.util.Collections;
48
48
import java.util.List;
49
49
import java.util.ListIterator;
50
 
import java.util.Map;
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;
55
52
 
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
69
64
 */
70
65
public class SurefireArchiver extends MavenReporter {
71
 
    private TestResult result;
 
66
    private transient TestResult result;
72
67
    
73
68
    /**
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>
77
72
     */
78
 
    private transient Map<File, FileSet> fileSets = new ConcurrentHashMap<File,FileSet>();// Collections.synchronizedMap(new HashMap<File, FileSet>());
79
 
 
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>();
83
74
 
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
133
124
 
134
 
                FileSet fileSet = getFileSet(reportsDir);
135
 
            DirectoryScanner ds = fileSet.getDirectoryScanner();
136
 
 
137
 
            if(ds.getIncludedFiles().length==0)
138
 
                // no test in this module
139
 
                return true;
140
 
            
141
 
            rememberCheckedFiles(reportsDir, ds);
142
 
 
143
 
            if(result==null)    result = new TestResult();
144
 
            result.parse(System.currentTimeMillis() - build.getMilliSecsSinceBuildStart(), ds);
145
 
 
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);
149
 
                    if(sr==null)
150
 
                        build.getActions().add(new SurefireReport(build, result, listener));
151
 
                    else
152
 
                        sr.setResult(result,listener);
153
 
                    if(result.getFailCount()>0)
154
 
                        build.setResult(Result.UNSTABLE);
155
 
                    build.registerAsProjectAction(new FactoryImpl());
156
 
                    return result.getFailCount();
157
 
                }
158
 
            });
159
 
 
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);
 
127
                
 
128
                DirectoryScanner ds = fileSet.getDirectoryScanner();
 
129
                
 
130
                if(ds.getIncludedFilesCount()==0)
 
131
                    // no test in this module
 
132
                    return true;
 
133
                
 
134
                String[] reportFiles = ds.getIncludedFiles();
 
135
                rememberCheckedFiles(reportsDir, reportFiles);
 
136
    
 
137
                if(result==null)    result = new TestResult();
 
138
                result.parse(System.currentTimeMillis() - build.getMilliSecsSinceBuildStart(), reportsDir, reportFiles);
 
139
                
 
140
                // final reference in order to serialize it:
 
141
                final TestResult r = result;
 
142
    
 
143
                int failCount = build.execute(new BuildCallable<Integer, IOException>() {
 
144
                        private static final long serialVersionUID = -1023888330720922136L;
 
145
 
 
146
                        public Integer call(MavenBuild build) throws IOException, InterruptedException {
 
147
                            SurefireReport sr = build.getAction(SurefireReport.class);
 
148
                            if(sr==null)
 
149
                                build.getActions().add(new SurefireReport(build, r, listener));
 
150
                            else
 
151
                                sr.setResult(r,listener);
 
152
                            if(r.getFailCount()>0)
 
153
                                build.setResult(Result.UNSTABLE);
 
154
                            build.registerAsProjectAction(new FactoryImpl());
 
155
                            return r.getFailCount();
 
156
                        }
 
157
                    });
 
158
                
 
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;
 
163
                }
 
164
                // TODO currently error is empty : will be here with maven 3.0.2+
 
165
                if(failCount>0) {
 
166
                    Maven3Builder.markAsSuccess = true;
 
167
                }
164
168
            }
165
 
            // TODO currenlty error is empty : will be here with maven 3.0.2+
166
 
            if(failCount>0) {
167
 
                Maven3Builder.markAsSuccess = true;
168
 
            }            
169
169
        }
170
170
 
171
171
        return true;
176
176
     * @param baseDir
177
177
     * @return
178
178
     */
179
 
    private FileSet getFileSet(File baseDir) {
180
 
        FileSet fs = null;
181
 
        if (fileSets == null) {
182
 
            fileSetsWriteLock.lock();
183
 
            try {
184
 
                fileSets = new ConcurrentHashMap<File,FileSet>();
185
 
            } finally {
186
 
                fileSetsWriteLock.unlock();
187
 
            }
188
 
        }
189
 
 
190
 
        fileSetsReadLock.lock();
191
 
        try {
192
 
            fs = fileSets.get(baseDir);
193
 
        } finally {
194
 
            fileSetsReadLock.unlock();
195
 
        }
 
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();
199
 
            try {
200
 
                    fileSets.put(baseDir, fs);
201
 
            } finally {
202
 
                fileSetsWriteLock.unlock();
203
 
            }
 
183
                FileSet previous = fileSets.putIfAbsent(baseDir, fs);
 
184
                if (previous != null) {
 
185
                    return previous;
 
186
                }
204
187
        }
205
188
        
206
189
        return fs;
209
192
    /**
210
193
     * Add checked files to the exclude list of the fileSet
211
194
     */
212
 
    private void rememberCheckedFiles(File baseDir, DirectoryScanner ds) {
 
195
    private void rememberCheckedFiles(File baseDir, String[] reportFiles) {
213
196
        FileSet fileSet = getFileSet(baseDir);
214
197
        
215
 
        for (String file : ds.getIncludedFiles()) {
216
 
                fileSet.setExcludes(file);
 
198
        for (String file : reportFiles) {
 
199
                fileSet.createExclude().setName(file);
217
200
        }
218
201
    }
219
202
 
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")))
250
236
            return false;
284
270
                if (((skipTests != null) && (skipTests))) {
285
271
                    return false;
286
272
                }
287
 
                }
 
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))) {
 
276
                    return false;
 
277
                }
 
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))) {
 
281
                    return false;
 
282
                }
 
283
            }
288
284
 
289
285
        } catch (ComponentConfigurationException e) {
290
286
            return false;
293
289
        return true;
294
290
    }
295
291
    
296
 
    public boolean maven3orLater(String mavenVersion) {
297
 
        // null or empty so false !
298
 
        if (StringUtils.isBlank( mavenVersion )) {
299
 
            return false;
300
 
        }
301
 
        return new ComparableVersion (mavenVersion).compareTo( new ComparableVersion ("3.0") ) >= 0;
302
 
    }       
 
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>();
 
296
        return this;
 
297
    }
303
298
 
304
299
    @Extension
305
300
    public static final class DescriptorImpl extends MavenReporterDescriptor {