Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Commit

Permalink
[JENKINS-29844] Workflow compatible. Added new tests (workflow specific)
Browse files Browse the repository at this point in the history
  • Loading branch information
recena committed Aug 24, 2015
1 parent db4e9a4 commit 6a25c52
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
target
.idea/
26 changes: 24 additions & 2 deletions pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>analysis-pom</artifactId>
<version>1.59</version>
<version>1.63</version>
</parent>

<artifactId>pmd</artifactId>
Expand All @@ -16,6 +16,10 @@
open source static code analysis program.
</description>

<properties>
<workflow-jenkins-plugin.version>1.4</workflow-jenkins-plugin.version>
</properties>

<licenses>
<license>
<name>MIT license</name>
Expand All @@ -37,7 +41,7 @@
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>analysis-core</artifactId>
<version>1.73-SNAPSHOT</version>
<version>1.73-beta-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
Expand Down Expand Up @@ -71,6 +75,24 @@
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/hudson/plugins/pmd/PmdPublisher.java
Expand Up @@ -2,13 +2,14 @@

import java.io.IOException;

import hudson.FilePath;
import hudson.model.Run;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
Expand All @@ -18,6 +19,7 @@
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.util.PluginLogger;
import hudson.plugins.pmd.parser.PmdParser;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Publishes the results of the PMD analysis (freestyle project type).
Expand All @@ -32,7 +34,7 @@ public class PmdPublisher extends HealthAwarePublisher {
/** Default PMD pattern. */
private static final String DEFAULT_PATTERN = "**/pmd.xml";
/** Ant file-set pattern of files to work with. */
private final String pattern;
private String pattern;

/**
* Creates a new instance of <code>PmdPublisher</code>.
Expand Down Expand Up @@ -100,7 +102,7 @@ public class PmdPublisher extends HealthAwarePublisher {
*/
// CHECKSTYLE:OFF
@SuppressWarnings("PMD.ExcessiveParameterList")
@DataBoundConstructor
@Deprecated
public PmdPublisher(final String healthy, final String unHealthy, final String thresholdLimit,
final String defaultEncoding, final boolean useDeltaValues,
final String unstableTotalAll, final String unstableTotalHigh, final String unstableTotalNormal, final String unstableTotalLow,
Expand All @@ -120,6 +122,15 @@ public PmdPublisher(final String healthy, final String unHealthy, final String t
}
// CHECKSTYLE:ON

/**
* Constructor used from methods like {@link StaplerRequest#bindJSON(Class, JSONObject)} and
* {@link StaplerRequest#bindParameters(Class, String)}.
*/
@DataBoundConstructor
public PmdPublisher() {
super(PLUGIN_NAME);
}

/**
* Returns the Ant file-set pattern of files to work with.
*
Expand All @@ -129,17 +140,26 @@ public String getPattern() {
return pattern;
}

/**
* Sets the Ant file-set pattern of files to work with.
*/
@DataBoundSetter
public void setPattern(final String pattern) {
this.pattern = pattern;
}

@Override
public Action getProjectAction(final AbstractProject<?, ?> project) {
return new PmdProjectAction(project);
}

@Override
public BuildResult perform(final AbstractBuild<?, ?> build, final PluginLogger logger) throws InterruptedException, IOException {
public BuildResult perform(final Run<?, ?> build, final FilePath workspace, final PluginLogger logger) throws
InterruptedException, IOException {
logger.log("Collecting PMD analysis files...");
FilesParser pmdCollector = new FilesParser(PLUGIN_NAME, StringUtils.defaultIfEmpty(getPattern(), DEFAULT_PATTERN),
FilesParser parser = new FilesParser(PLUGIN_NAME, StringUtils.defaultIfEmpty(getPattern(), DEFAULT_PATTERN),
new PmdParser(getDefaultEncoding()), shouldDetectModules(), isMavenBuild(build));
ParserResult project = build.getWorkspace().act(pmdCollector);
ParserResult project = workspace.act(parser);
logger.logLines(project.getLogMessages());

PmdResult result = new PmdResult(build, getDefaultEncoding(), project,
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/hudson/plugins/pmd/PmdReporterResult.java
@@ -1,6 +1,7 @@
package hudson.plugins.pmd;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.core.ResultAction;
import hudson.plugins.analysis.core.BuildResult;
Expand Down Expand Up @@ -28,9 +29,33 @@ public class PmdReporterResult extends PmdResult {
* @param useStableBuildAsReference
* determines whether only stable builds should be used as
* reference builds or not
*
* @deprecated use {@link #PmdReporterResult(Run, String, ParserResult, boolean, boolean)}
*/
@Deprecated
public PmdReporterResult(final AbstractBuild<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference) {
this((Run<?, ?>) build, defaultEncoding, result, usePreviousBuildAsReference, useStableBuildAsReference);
}

/**
* Creates a new instance of {@link PmdReporterResult}.
*
* @param build
* the current build as owner of this action
* @param defaultEncoding
* the default encoding to be used when reading and parsing files
* @param result
* the parsed result with all annotations
* @param usePreviousBuildAsReference
* determines whether to use the previous build as the reference
* build
* @param useStableBuildAsReference
* determines whether only stable builds should be used as
* reference builds or not
*/
public PmdReporterResult(final Run<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference) {
super(build, defaultEncoding, result, usePreviousBuildAsReference, useStableBuildAsReference,
PmdMavenResultAction.class);
}
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/hudson/plugins/pmd/PmdResult.java
Expand Up @@ -3,6 +3,7 @@
import com.thoughtworks.xstream.XStream;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.analysis.core.BuildHistory;
import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.core.ParserResult;
Expand Down Expand Up @@ -33,9 +34,33 @@ public class PmdResult extends BuildResult {
* @param useStableBuildAsReference
* determines whether only stable builds should be used as
* reference builds or not
*
* @deprecated use {@link #PmdResult(Run, String, ParserResult, boolean, boolean, Class)}
*/
@Deprecated
public PmdResult(final AbstractBuild<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference) {
this((Run<?, ?>) build, defaultEncoding, result, usePreviousBuildAsReference, useStableBuildAsReference);
}

/**
* Creates a new instance of {@link PmdResult}.
*
* @param build
* the current build as owner of this action
* @param defaultEncoding
* the default encoding to be used when reading and parsing files
* @param result
* the parsed result with all annotations
* @param usePreviousBuildAsReference
* determines whether to use the previous build as the reference
* build
* @param useStableBuildAsReference
* determines whether only stable builds should be used as
* reference builds or not
*/
public PmdResult(final Run<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference) {
this(build, defaultEncoding, result, usePreviousBuildAsReference, useStableBuildAsReference, PmdResultAction.class);
}

Expand All @@ -48,17 +73,47 @@ public PmdResult(final AbstractBuild<?, ?> build, final String defaultEncoding,
* @param useStableBuildAsReference determines whether only stable builds should be used as
reference builds or not
* @param actionType the type of the result action
*
* @deprecated use {@link #PmdResult(Run, String, ParserResult, boolean, boolean, Class)}
*/
@Deprecated
protected PmdResult(final AbstractBuild<?, ?> build,
final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference,
final boolean useStableBuildAsReference,
final Class<? extends ResultAction<PmdResult>> actionType) {
this((Run<?, ?>) build, defaultEncoding, result, usePreviousBuildAsReference, useStableBuildAsReference, actionType);
}

/**
* Creates a new instance of {@link PmdResult}.
*
* @param build the current build as owner of this action
* @param defaultEncoding the default encoding to be used when reading and parsing files
* @param result the parsed result with all annotations
* @param usePreviousBuildAsReference the value of usePreviousBuildAsReference
* @param useStableBuildAsReference determines whether only stable builds should be used as reference builds or not
* @param actionType the type of the result action
*/
protected PmdResult(final Run<?, ?> build,
final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference,
final boolean useStableBuildAsReference,
final Class<? extends ResultAction<PmdResult>> actionType) {
this(build, new BuildHistory(build, actionType, usePreviousBuildAsReference, useStableBuildAsReference), result, defaultEncoding, true);
}

/**
*@deprecated use {@link #PmdResult(Run, BuildHistory, ParserResult, String, boolean)}
*/
@Deprecated
PmdResult(final AbstractBuild<?, ?> build, final BuildHistory history,
final ParserResult result, final String defaultEncoding, final boolean canSerialize) {
this((Run<?, ?>) build, history, result, defaultEncoding, canSerialize);
}

PmdResult(final Run<?, ?> build, final BuildHistory history,
final ParserResult result, final String defaultEncoding, final boolean canSerialize) {
super(build, history, result, defaultEncoding);

if (canSerialize) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/hudson/plugins/pmd/PmdResultAction.java
@@ -1,6 +1,7 @@
package hudson.plugins.pmd;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.analysis.core.HealthDescriptor;
import hudson.plugins.analysis.core.PluginDescriptor;
import hudson.plugins.analysis.core.AbstractResultAction;
Expand All @@ -26,8 +27,25 @@ public class PmdResultAction extends AbstractResultAction<PmdResult> {
* health descriptor to use
* @param result
* the result in this build
*
* @deprecated use {@link #PmdResultAction(Run, HealthDescriptor, PmdResult)}
*/
@Deprecated
public PmdResultAction(final AbstractBuild<?, ?> owner, final HealthDescriptor healthDescriptor, final PmdResult result) {
this((Run<?, ?>) owner, healthDescriptor, result);
}

/**
* Creates a new instance of <code>PmdResultAction</code>.
*
* @param owner
* the associated build of this action
* @param healthDescriptor
* health descriptor to use
* @param result
* the result in this build
*/
public PmdResultAction(final Run<?, ?> owner, final HealthDescriptor healthDescriptor, final PmdResult result) {
super(owner, new PmdHealthDescriptor(healthDescriptor), result);
}

Expand Down
78 changes: 78 additions & 0 deletions src/test/java/hudson/plugins/pmd/PmdWorkflowTest.java
@@ -0,0 +1,78 @@
package hudson.plugins.pmd;

import hudson.FilePath;
import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class PmdWorkflowTest {

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

/**
* Run a workflow job using {@link PmdPublisher} and check for success.
*/
@Test
public void pmdPublisherWorkflowStep() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pmdPublisherWorkflowStep");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
FilePath report = workspace.child("target").child("pmd.xml");
report.copyFrom(PmdWorkflowTest.class.getResourceAsStream("/hudson/plugins/pmd/parser/4-pmd-warnings.xml"));
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'PmdPublisher'])\n"
+ "}\n", true)
);
jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
PmdResultAction result = job.getLastBuild().getAction(PmdResultAction.class);
assertEquals(4, result.getResult().getAnnotations().size());
}

/**
* Run a workflow job using {@link PmdPublisher} with a failing threshold of 0, so the given example file
* "/hudson/plugins/pmd/parser/4-pmd-warnings.xml" will make the build to fail.
*/
@Test
public void pmdPublisherWorkflowStepSetLimits() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pmdPublisherWorkflowStepSetLimits");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
FilePath report = workspace.child("target").child("pmd.xml");
report.copyFrom(PmdWorkflowTest.class.getResourceAsStream("/hudson/plugins/pmd/parser/4-pmd-warnings.xml"));
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'PmdPublisher', pattern: '**/pmd.xml', failedTotalAll: '0', usePreviousBuildAsReference: false])\n"
+ "}\n", true)
);
jenkinsRule.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
PmdResultAction result = job.getLastBuild().getAction(PmdResultAction.class);
assertEquals(4, result.getResult().getAnnotations().size());
}

/**
* Run a workflow job using {@link PmdPublisher} with a unstable threshold of 0, so the given example file
* "/hudson/plugins/pmd/parser/4-pmd-warnings.xml" will make the build to fail.
*/
@Test
public void pmdPublisherWorkflowStepFailure() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pmdPublisherWorkflowStepFailure");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
FilePath report = workspace.child("target").child("pmd.xml");
report.copyFrom(PmdWorkflowTest.class.getResourceAsStream("/hudson/plugins/pmd/parser/4-pmd-warnings.xml"));
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'PmdPublisher', pattern: '**/pmd.xml', unstableTotalAll: '0', usePreviousBuildAsReference: false])\n"
+ "}\n")
);
jenkinsRule.assertBuildStatus(Result.UNSTABLE, job.scheduleBuild2(0).get());
PmdResultAction result = job.getLastBuild().getAction(PmdResultAction.class);
assertEquals(4, result.getResult().getAnnotations().size());
}
}

0 comments on commit 6a25c52

Please sign in to comment.