From 452a02ece317f0fc3a407ae6cf6f4a2bede881a7 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sun, 4 Feb 2007 00:14:00 +0000 Subject: [PATCH] added MavenReporter that record Surefire test reports. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@2061 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/maven/MavenBuild.java | 6 ++ .../hudson/maven/MavenModuleSetBuild.java | 7 ++ .../java/hudson/maven/MavenReporters.java | 4 +- .../maven/reporters/SurefireArchiver.java | 102 ++++++++++++++++++ .../main/java/hudson/model/AbstractBuild.java | 11 ++ core/src/main/java/hudson/model/Build.java | 11 +- 6 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 core/src/main/java/hudson/maven/reporters/SurefireArchiver.java diff --git a/core/src/main/java/hudson/maven/MavenBuild.java b/core/src/main/java/hudson/maven/MavenBuild.java index ffceb953be..4fd387539c 100644 --- a/core/src/main/java/hudson/maven/MavenBuild.java +++ b/core/src/main/java/hudson/maven/MavenBuild.java @@ -1,6 +1,7 @@ package hudson.maven; import hudson.FilePath; +import hudson.tasks.test.AbstractTestResultAction; import hudson.FilePath.FileCallable; import hudson.maven.PluginManagerInterceptor.AbortException; import hudson.model.AbstractBuild; @@ -84,6 +85,11 @@ public class MavenBuild extends AbstractBuild { return true; } + @Override + public AbstractTestResultAction getTestResultAction() { + return getAction(AbstractTestResultAction.class); + } + @Override public void run() { run(new RunnerImpl()); diff --git a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java index 694677c8c3..a5707d856d 100644 --- a/core/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/core/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -8,6 +8,7 @@ import hudson.model.Hudson; import hudson.model.Result; import hudson.remoting.VirtualChannel; import hudson.util.IOException2; +import hudson.tasks.test.AbstractTestResultAction; import org.apache.maven.embedder.MavenEmbedderException; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingException; @@ -46,6 +47,12 @@ public final class MavenModuleSetBuild extends AbstractBuild LIST = Descriptor.toList( MavenArtifactArchiver.DescriptorImpl.DESCRIPTOR, MavenFingerprinter.DescriptorImpl.DESCRIPTOR, - MavenJavadocArchiver.DescriptorImpl.DESCRIPTOR + MavenJavadocArchiver.DescriptorImpl.DESCRIPTOR, + SurefireArchiver.DescriptorImpl.DESCRIPTOR ); /** diff --git a/core/src/main/java/hudson/maven/reporters/SurefireArchiver.java b/core/src/main/java/hudson/maven/reporters/SurefireArchiver.java new file mode 100644 index 0000000000..cf2dc689bd --- /dev/null +++ b/core/src/main/java/hudson/maven/reporters/SurefireArchiver.java @@ -0,0 +1,102 @@ +package hudson.maven.reporters; + +import hudson.maven.MavenBuild; +import hudson.maven.MavenBuildProxy; +import hudson.maven.MavenBuildProxy.BuildCallable; +import hudson.maven.MavenModule; +import hudson.maven.MavenReporter; +import hudson.maven.MavenReporterDescriptor; +import hudson.maven.MojoInfo; +import hudson.model.BuildListener; +import hudson.model.Result; +import hudson.tasks.junit.TestResult; +import hudson.tasks.junit.TestResultAction; +import org.apache.maven.project.MavenProject; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.FileSet; +import org.codehaus.plexus.component.configurator.ComponentConfigurationException; + +import java.io.File; +import java.io.IOException; +import java.util.Date; + +/** + * Records the surefire test result. + * @author Kohsuke Kawaguchi + */ +public class SurefireArchiver extends MavenReporter { + private transient Date started; + + public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException { + if (isSurefireTest(mojo)) + started = new Date(); + return true; + } + + public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, final BuildListener listener) throws InterruptedException, IOException { + if (!isSurefireTest(mojo)) return true; + + File reportsDir; + try { + reportsDir = mojo.getConfigurationValue("reportsDirectory", File.class); + } catch (ComponentConfigurationException e) { + e.printStackTrace(listener.fatalError("Unable to obtain the reportsDirectory from surefire:test mojo")); + build.setResult(Result.FAILURE); + return true; + } + + if(reportsDir.exists()) { + // surefire:test just skips itself when the current project is not a java project + + FileSet fs = new FileSet(); + Project p = new Project(); + fs.setProject(p); + fs.setDir(reportsDir); + fs.setIncludes("*.xml"); + DirectoryScanner ds = fs.getDirectoryScanner(p); + + if(ds.getIncludedFiles().length==0) + // no test in this module + return true; + + final TestResult tr = new TestResult(started.getTime() - 1000/*error margin*/, ds); + + build.execute(new BuildCallable() { + public Void call(MavenBuild build) throws IOException, InterruptedException { + TestResultAction action = new TestResultAction(build, tr, listener); + build.getActions().add(action); + if(tr.getFailCount()>0) + build.setResult(Result.UNSTABLE); + return null; + } + }); + } + + return true; + } + + private boolean isSurefireTest(MojoInfo mojo) { + return mojo.pluginName.matches("org.apache.maven.plugins", "maven-surefire-plugin") && mojo.getGoal().equals("test"); + } + + public DescriptorImpl getDescriptor() { + return DescriptorImpl.DESCRIPTOR; + } + + public static final class DescriptorImpl extends MavenReporterDescriptor { + public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); + + private DescriptorImpl() { + super(SurefireArchiver.class); + } + + public String getDisplayName() { + return "Publish surefire reports"; + } + + public SurefireArchiver newAutoInstance(MavenModule module) { + return new SurefireArchiver(); + } + } +} diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index 0058ec8c5a..5d2b8af24b 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -4,6 +4,7 @@ import hudson.Launcher; import hudson.Proc.LocalProc; import hudson.Util; import hudson.tasks.Fingerprinter.FingerprintAction; +import hudson.tasks.test.AbstractTestResultAction; import hudson.maven.MavenBuild; import static hudson.model.Hudson.isWindows; import hudson.model.listeners.SCMListener; @@ -211,6 +212,16 @@ public abstract class AbstractBuild

,R extends Abs return env; } + public Calendar due() { + return timestamp; + } + + /** + * Gets {@link AbstractTestResultAction} associated with this build if any. + *

+ */ + public abstract AbstractTestResultAction getTestResultAction(); + /** * Invoked by {@link Executor} to performs a build. */ diff --git a/core/src/main/java/hudson/model/Build.java b/core/src/main/java/hudson/model/Build.java index 371c21d61d..c0bda692c7 100644 --- a/core/src/main/java/hudson/model/Build.java +++ b/core/src/main/java/hudson/model/Build.java @@ -11,7 +11,6 @@ import hudson.triggers.SCMTrigger; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Calendar; import java.util.List; import java.util.Map; @@ -36,17 +35,11 @@ public final class Build extends AbstractBuild { super(project,buildDir); } - public Calendar due() { - return timestamp; - } - - /** - * Gets {@link AbstractTestResultAction} associated with this build if any. - */ + @Override public AbstractTestResultAction getTestResultAction() { return getAction(AbstractTestResultAction.class); } - + /** * During the build this field remembers {@link Environment}s created by * {@link BuildWrapper}. This design is bit ugly but forced due to compatibility. -- GitLab