Skip to content

Commit

Permalink
[JENKINS-51056] xUnit throws NPE when no threshold was specified
Browse files Browse the repository at this point in the history
Add check for null in builder and publisher constructors when tools and thresholds are not defined.
  • Loading branch information
nfalco79 committed May 2, 2018
1 parent 22ee249 commit 875dc67
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitBuilder.java
Expand Up @@ -27,6 +27,8 @@
import java.io.IOException;
import java.util.Arrays;

import javax.annotation.CheckForNull;

import org.jenkinsci.lib.dtkit.descriptor.TestTypeDescriptor;
import org.jenkinsci.lib.dtkit.type.TestType;
import org.jenkinsci.plugins.xunit.threshold.FailedThreshold;
Expand Down Expand Up @@ -60,14 +62,14 @@ public class XUnitBuilder extends Builder implements SimpleBuildStep {
private int thresholdMode;
private ExtraConfiguration extraConfiguration;

public XUnitBuilder(TestType[] tools, XUnitThreshold[] thresholds) {
this.types = Arrays.copyOf(tools, tools.length);
this.thresholds = Arrays.copyOf(thresholds, thresholds.length);
public XUnitBuilder(@CheckForNull TestType[] tools, @CheckForNull XUnitThreshold[] thresholds) {
this.types = (tools != null ? Arrays.copyOf(tools, tools.length) : new TestType[0]);
this.thresholds = (thresholds != null ? Arrays.copyOf(thresholds, thresholds.length) : new XUnitThreshold[0]);
this.thresholdMode = 1;
}

@DataBoundConstructor
public XUnitBuilder(TestType[] tools, XUnitThreshold[] thresholds, int thresholdMode, String testTimeMargin) {
public XUnitBuilder(@CheckForNull TestType[] tools, @CheckForNull XUnitThreshold[] thresholds, int thresholdMode, @CheckForNull String testTimeMargin) {
this(tools, thresholds);
this.thresholdMode = thresholdMode;
long longTestTimeMargin = XUnitDefaultValues.TEST_REPORT_TIME_MARGING;
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitPublisher.java
Expand Up @@ -28,6 +28,8 @@
import java.io.Serializable;
import java.util.Arrays;

import javax.annotation.CheckForNull;

import org.jenkinsci.lib.dryrun.DryRun;
import org.jenkinsci.lib.dtkit.descriptor.TestTypeDescriptor;
import org.jenkinsci.lib.dtkit.type.TestType;
Expand Down Expand Up @@ -68,14 +70,14 @@ public class XUnitPublisher extends Recorder implements DryRun, Serializable, Si
private int thresholdMode;
private ExtraConfiguration extraConfiguration;

public XUnitPublisher(TestType[] types, XUnitThreshold[] thresholds) {
this.types = Arrays.copyOf(types, types.length);
this.thresholds = Arrays.copyOf(thresholds, thresholds.length);;
public XUnitPublisher(@CheckForNull TestType[] tools, @CheckForNull XUnitThreshold[] thresholds) {
this.types = (tools != null ? Arrays.copyOf(tools, tools.length) : new TestType[0]);
this.thresholds = (thresholds != null ? Arrays.copyOf(thresholds, thresholds.length) : new XUnitThreshold[0]);
this.thresholdMode = 1;
}

@DataBoundConstructor
public XUnitPublisher(TestType[] tools, XUnitThreshold[] thresholds, int thresholdMode, String testTimeMargin) {
public XUnitPublisher(@CheckForNull TestType[] tools, @CheckForNull XUnitThreshold[] thresholds, int thresholdMode, @CheckForNull String testTimeMargin) {
this(tools, thresholds);
this.thresholdMode = thresholdMode;
long longTestTimeMargin = XUnitDefaultValues.TEST_REPORT_TIME_MARGING;
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/org/jenkinsci/plugins/xunit/XUnitWorkflowTest.java
Expand Up @@ -23,14 +23,16 @@ of this software and associated documentation files (the "Software"), to deal
*/
package org.jenkinsci.plugins.xunit;

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

import hudson.FilePath;
import hudson.model.Result;

public class XUnitWorkflowTest {

@ClassRule
Expand All @@ -56,6 +58,18 @@ public void xunitBuilderWorkflowStepTest() throws Exception {
jenkinsRule.assertBuildStatus(Result.UNSTABLE, job.scheduleBuild2(0).get());
}

@Issue("JENKINS-51056")
@Test
public void xunitBuilderThresholdsAreOptional() throws Exception {
WorkflowJob job = getBaseJob("JENKINS-51056");
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'XUnitBuilder', tools: [[$class: 'GoogleTestType', pattern: 'input.xml']], thresholdMode: 1])\n"
+ "}"));

jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
}

@Test
public void xunitPublisherWorkflowStepTest() throws Exception {
WorkflowJob job = getBaseJob("publisher");
Expand Down

0 comments on commit 875dc67

Please sign in to comment.