SurefireArchiver.java 9.5 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jason Chaffee, Maciek Starzyk
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
package hudson.maven.reporters;

26
import hudson.Util;
27
import hudson.Extension;
S
Seiji Sogabe 已提交
28
import hudson.maven.Maven3Builder;
29 30 31
import hudson.maven.MavenBuild;
import hudson.maven.MavenBuildProxy;
import hudson.maven.MavenBuildProxy.BuildCallable;
K
kohsuke 已提交
32
import hudson.maven.MavenBuilder;
33
import hudson.maven.MavenModule;
34
import hudson.maven.MavenProjectActionBuilder;
35 36 37
import hudson.maven.MavenReporter;
import hudson.maven.MavenReporterDescriptor;
import hudson.maven.MojoInfo;
38
import hudson.model.Action;
39 40 41
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.junit.TestResult;
42
import hudson.tasks.test.TestResultProjectAction;
43
import org.apache.maven.plugin.MojoFailureException;
44 45 46 47
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
K
kohsuke 已提交
48
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
49 50 51

import java.io.File;
import java.io.IOException;
52 53
import java.util.Collection;
import java.util.Collections;
54 55
import java.util.List;
import java.util.ListIterator;
56 57 58 59 60 61

/**
 * Records the surefire test result.
 * @author Kohsuke Kawaguchi
 */
public class SurefireArchiver extends MavenReporter {
62 63
    private TestResult result;

64
    public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
K
kohsuke 已提交
65
        if (isSurefireTest(mojo)) {
66 67 68 69 70 71 72 73
            if (!mojo.is("org.apache.maven.plugins", "maven-failsafe-plugin", "integration-test")) {
                // tell surefire:test to keep going even if there was a failure,
                // so that we can record this as yellow.
                // note that because of the way Maven works, just updating system property at this point is too late
                XmlPlexusConfiguration c = (XmlPlexusConfiguration) mojo.configuration.getChild("testFailureIgnore");
                if(c!=null && c.getValue().equals("${maven.test.failure.ignore}") && System.getProperty("maven.test.failure.ignore")==null)
                    c.setValue("true");
            }
K
kohsuke 已提交
74
        }
75 76 77
        return true;
    }

78
    public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, final BuildListener listener, Throwable error) throws InterruptedException, IOException {
79 80
        if (!isSurefireTest(mojo)) return true;

K
i18n  
kohsuke 已提交
81
        listener.getLogger().println(Messages.SurefireArchiver_Recording());
82

83
        File reportsDir;
84 85
        if (mojo.is("org.apache.maven.plugins", "maven-surefire-plugin", "test") ||
            mojo.is("org.apache.maven.plugins", "maven-failsafe-plugin", "integration-test")) {
86 87 88 89 90 91 92 93 94 95
            try {
                reportsDir = mojo.getConfigurationValue("reportsDirectory", File.class);
            } catch (ComponentConfigurationException e) {
                e.printStackTrace(listener.fatalError(Messages.SurefireArchiver_NoReportsDir()));
                build.setResult(Result.FAILURE);
                return true;
            }
        }
        else {
            reportsDir = new File(pom.getBasedir(), "target/surefire-reports");
96 97 98 99 100
        }

        if(reportsDir.exists()) {
            // surefire:test just skips itself when the current project is not a java project

101
            FileSet fs = Util.createFileSet(reportsDir,"*.xml","testng-results.xml,testng-failed.xml");
102
            DirectoryScanner ds = fs.getDirectoryScanner();
103 104 105 106 107

            if(ds.getIncludedFiles().length==0)
                // no test in this module
                return true;

108 109
            if(result==null)    result = new TestResult();
            result.parse(System.currentTimeMillis() - build.getMilliSecsSinceBuildStart(), ds);
110

111 112
            int failCount = build.execute(new BuildCallable<Integer, IOException>() {
                public Integer call(MavenBuild build) throws IOException, InterruptedException {
113 114 115 116 117 118
                    SurefireReport sr = build.getAction(SurefireReport.class);
                    if(sr==null)
                        build.getActions().add(new SurefireReport(build, result, listener));
                    else
                        sr.setResult(result,listener);
                    if(result.getFailCount()>0)
119
                        build.setResult(Result.UNSTABLE);
120
                    build.registerAsProjectAction(new FactoryImpl());
121
                    return result.getFailCount();
122 123
                }
            });
124 125 126 127

            // if surefire plugin is going to kill maven because of a test failure,
            // intercept that (or otherwise build will be marked as failure)
            if(failCount>0 && error instanceof MojoFailureException) {
128
                MavenBuilder.markAsSuccess = true;
129
            }
S
Seiji Sogabe 已提交
130 131 132 133
            // TODO currenlty error is empty : will be here with maven 3.0.2+
            if(failCount>0) {
                Maven3Builder.markAsSuccess = true;
            }            
134 135 136 137 138
        }

        return true;
    }

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    /**
     * Up to 1.372, there was a bug that causes Hudson to persist {@link SurefireArchiver} with the entire test result
     * in it. If we are loading those, fix it up in memory to reduce the memory footprint.
     *
     * It'd be nice we can save the record to remove problematic portion, but that might have
     * additional side effect.
     */
    public static void fixUp(List<MavenProjectActionBuilder> builders) {
        if (builders==null) return;
        for (ListIterator<MavenProjectActionBuilder> itr = builders.listIterator(); itr.hasNext();) {
            MavenProjectActionBuilder b =  itr.next();
            if (b instanceof SurefireArchiver)
                itr.set(new FactoryImpl());
        }
    }

155 156 157 158 159 160 161
    /**
     * Part of the serialization data attached to {@link MavenBuild}.
     */
    static final class FactoryImpl implements MavenProjectActionBuilder {
        public Collection<? extends Action> getProjectActions(MavenModule module) {
            return Collections.singleton(new TestResultProjectAction(module));
        }
162 163
    }

164
    private boolean isSurefireTest(MojoInfo mojo) {
165
        if ((!mojo.is("com.sun.maven", "maven-junit-plugin", "test"))
166
            && (!mojo.is("org.sonatype.flexmojos", "flexmojos-maven-plugin", "test-run"))
167 168
            && (!mojo.is("org.apache.maven.plugins", "maven-surefire-plugin", "test"))
            && (!mojo.is("org.apache.maven.plugins", "maven-failsafe-plugin", "integration-test")))
K
kohsuke 已提交
169 170 171
            return false;

        try {
172 173 174
            if (mojo.is("org.apache.maven.plugins", "maven-surefire-plugin", "test")) {
                Boolean skip = mojo.getConfigurationValue("skip", Boolean.class);
                if (((skip != null) && (skip))) {
K
TAB->WS  
kohsuke 已提交
175 176
                    return false;
                }
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
                
                if (mojo.pluginName.version.compareTo("2.3") >= 0) {
                    Boolean skipExec = mojo.getConfigurationValue("skipExec", Boolean.class);
                    
                    if (((skipExec != null) && (skipExec))) {
                        return false;
                    }
                }
                
                if (mojo.pluginName.version.compareTo("2.4") >= 0) {
                    Boolean skipTests = mojo.getConfigurationValue("skipTests", Boolean.class);
                    
                    if (((skipTests != null) && (skipTests))) {
                        return false;
                    }
                }
K
TAB->WS  
kohsuke 已提交
193
            }
194
            else if (mojo.is("com.sun.maven", "maven-junit-plugin", "test")) {
K
TAB->WS  
kohsuke 已提交
195
                Boolean skipTests = mojo.getConfigurationValue("skipTests", Boolean.class);
196
                
K
TAB->WS  
kohsuke 已提交
197 198 199 200
                if (((skipTests != null) && (skipTests))) {
                    return false;
                }
            }
201 202 203 204 205 206 207
            else if (mojo.is("org.sonatype.flexmojos", "flexmojos-maven-plugin", "test-run")) {
		Boolean skipTests = mojo.getConfigurationValue("skipTest", Boolean.class);
		
		if (((skipTests != null) && (skipTests))) {
		    return false;
		}
	    }
J
jasonchaffee 已提交
208

K
kohsuke 已提交
209 210 211
        } catch (ComponentConfigurationException e) {
            return false;
        }
K
TAB->WS  
kohsuke 已提交
212

J
jasonchaffee 已提交
213
        return true;
214 215
    }

216
    @Extension
217 218
    public static final class DescriptorImpl extends MavenReporterDescriptor {
        public String getDisplayName() {
K
i18n  
kohsuke 已提交
219
            return Messages.SurefireArchiver_DisplayName();
220 221 222 223 224 225
        }

        public SurefireArchiver newAutoInstance(MavenModule module) {
            return new SurefireArchiver();
        }
    }
226 227

    private static final long serialVersionUID = 1L;
228
}