SurefireArchiver.java 7.0 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;
28 29 30
import hudson.maven.MavenBuild;
import hudson.maven.MavenBuildProxy;
import hudson.maven.MavenBuildProxy.BuildCallable;
K
kohsuke 已提交
31
import hudson.maven.MavenBuilder;
32 33 34 35
import hudson.maven.MavenModule;
import hudson.maven.MavenReporter;
import hudson.maven.MavenReporterDescriptor;
import hudson.maven.MojoInfo;
36
import hudson.model.Action;
37 38 39
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.junit.TestResult;
40
import hudson.tasks.test.TestResultProjectAction;
41
import org.apache.maven.plugin.MojoFailureException;
42 43 44 45
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 已提交
46
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
47 48 49

import java.io.File;
import java.io.IOException;
50 51
import java.util.Collection;
import java.util.Collections;
52 53 54 55 56 57

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

60
    public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
K
kohsuke 已提交
61 62 63
        if (isSurefireTest(mojo)) {
            // tell surefire:test to keep going even if there was a failure,
            // so that we can record this as yellow.
64 65 66 67
            // 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 已提交
68
        }
69 70 71
        return true;
    }

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

K
i18n  
kohsuke 已提交
75
        listener.getLogger().println(Messages.SurefireArchiver_Recording());
76

77 78 79 80
        File reportsDir;
        try {
            reportsDir = mojo.getConfigurationValue("reportsDirectory", File.class);
        } catch (ComponentConfigurationException e) {
K
i18n  
kohsuke 已提交
81
            e.printStackTrace(listener.fatalError(Messages.SurefireArchiver_NoReportsDir()));
82 83 84 85 86 87 88
            build.setResult(Result.FAILURE);
            return true;
        }

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

89
            FileSet fs = Util.createFileSet(reportsDir,"*.xml","testng-results.xml,testng-failed.xml");
90
            DirectoryScanner ds = fs.getDirectoryScanner();
91 92 93 94 95

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

96 97 98 99
            if(result==null) {
                long t = System.currentTimeMillis() - build.getMilliSecsSinceBuildStart();
                result = new TestResult(t - 1000/*error margin*/, ds);
            } else
100
                result.parse(build.getTimestamp().getTimeInMillis() - 1000/*error margin*/, ds);
101

102 103
            int failCount = build.execute(new BuildCallable<Integer, IOException>() {
                public Integer call(MavenBuild build) throws IOException, InterruptedException {
104 105 106 107 108 109
                    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)
110
                        build.setResult(Result.UNSTABLE);
111
                    build.registerAsProjectAction(SurefireArchiver.this);
112
                    return result.getFailCount();
113 114
                }
            });
115 116 117 118

            // 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) {
119
                MavenBuilder.markAsSuccess = true;
120
            }
121 122 123 124 125
        }

        return true;
    }

126

127 128
    public Collection<? extends Action> getProjectActions(MavenModule module) {
        return Collections.singleton(new TestResultProjectAction(module));
129 130
    }

131
    private boolean isSurefireTest(MojoInfo mojo) {
K
TAB->WS  
kohsuke 已提交
132
        if (!mojo.is("org.apache.maven.plugins", "maven-surefire-plugin", "test"))
K
kohsuke 已提交
133 134 135
            return false;

        try {
K
TAB->WS  
kohsuke 已提交
136
            Boolean skip = mojo.getConfigurationValue("skip", Boolean.class);
J
jasonchaffee 已提交
137
            if (((skip != null) && (skip))) {
K
TAB->WS  
kohsuke 已提交
138
                return false;
J
jasonchaffee 已提交
139
            }
K
TAB->WS  
kohsuke 已提交
140 141 142 143

            if (mojo.pluginName.version.compareTo("2.3") >= 0) {
                Boolean skipExec = mojo.getConfigurationValue("skipExec", Boolean.class);

J
jasonchaffee 已提交
144
                if (((skipExec != null) && (skipExec))) {
K
TAB->WS  
kohsuke 已提交
145 146 147 148 149 150 151 152 153 154 155
                    return false;
                }
            }

            if (mojo.pluginName.version.compareTo("2.4") >= 0) {
                Boolean skipTests = mojo.getConfigurationValue("skipTests", Boolean.class);

                if (((skipTests != null) && (skipTests))) {
                    return false;
                }
            }
J
jasonchaffee 已提交
156

K
kohsuke 已提交
157 158 159
        } catch (ComponentConfigurationException e) {
            return false;
        }
K
TAB->WS  
kohsuke 已提交
160

J
jasonchaffee 已提交
161
        return true;
162 163
    }

164
    @Extension
165 166
    public static final class DescriptorImpl extends MavenReporterDescriptor {
        public String getDisplayName() {
K
i18n  
kohsuke 已提交
167
            return Messages.SurefireArchiver_DisplayName();
168 169 170 171 172 173
        }

        public SurefireArchiver newAutoInstance(MavenModule module) {
            return new SurefireArchiver();
        }
    }
174 175

    private static final long serialVersionUID = 1L;
176
}