Permalink
Browse files

[JENKINS-51056] xUnit throws NPE when no threshold was specified

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 875dc671cc49b7981a7b9dfb2a7aa99e5d3504df
@@ -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;
@@ -60,14 +62,14 @@
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;
@@ -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;
@@ -68,14 +70,14 @@
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;
@@ -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
@@ -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");

0 comments on commit 875dc67

Please sign in to comment.