Skip to content
Permalink
Browse files

[JENKINS-37611] Add "@symbol" annotation to xunit and parameters

Move threshold parameters from required to optional. Remove also unreachable checks of % character in case of percent threshold mode. Add form validation for all threshold values.
Add unit test to guarantee serialisation works properly with plugin version before 1.103.
Manage negative value of time margin and fix NPE when a build step or recorder has no tools defined.
  • Loading branch information
nfalco79 committed May 3, 2018
1 parent e52107f commit 845615f222e19c949bcf7ab7f09126e4f33c7c3e
@@ -40,10 +40,10 @@
*/
public class AliasInitializer {

@Initializer(before = InitMilestone.PLUGINS_STARTED)
public static void addAliases() {
@Initializer(before = InitMilestone.JOB_LOADED)
public static void init(Jenkins jenkins) {
Items.XSTREAM.alias("xunit", XUnitPublisher.class);
DescriptorExtensionList<TestType, TestTypeDescriptor<TestType>> extensionList = Jenkins.getActiveInstance().getDescriptorList(TestType.class);
DescriptorExtensionList<TestType, TestTypeDescriptor<TestType>> extensionList = jenkins.getDescriptorList(TestType.class);
for (Iterator<TestTypeDescriptor<TestType>> it = extensionList.iterator(); it.hasNext(); ) {
Class<? extends TestType> classType = it.next().clazz;
String className = getClassName(classType);
@@ -29,6 +29,7 @@

import javax.annotation.CheckForNull;

import org.jenkinsci.Symbol;
import org.jenkinsci.lib.dtkit.descriptor.TestTypeDescriptor;
import org.jenkinsci.lib.dtkit.type.TestType;
import org.jenkinsci.plugins.xunit.threshold.FailedThreshold;
@@ -37,6 +38,8 @@
import org.jenkinsci.plugins.xunit.threshold.XUnitThresholdDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import hudson.DescriptorExtensionList;
import hudson.Extension;
import hudson.FilePath;
@@ -57,28 +60,26 @@
*/
public class XUnitBuilder extends Builder implements SimpleBuildStep {

private TestType[] types;
@XStreamAlias("types")
private TestType[] tools;
private XUnitThreshold[] thresholds;
private int thresholdMode;
private ExtraConfiguration extraConfiguration;

@DataBoundConstructor
public XUnitBuilder(@CheckForNull TestType[] tools, @CheckForNull XUnitThreshold[] thresholds, int thresholdMode, @CheckForNull String testTimeMargin) {
this.types = (tools != null ? Arrays.copyOf(tools, tools.length) : new TestType[0]);
this.tools = (tools != null ? Arrays.copyOf(tools, tools.length) : new TestType[0]);
this.thresholds = (thresholds != null ? Arrays.copyOf(thresholds, thresholds.length) : new XUnitThreshold[0]);
this.thresholdMode = thresholdMode;
long longTestTimeMargin = XUnitDefaultValues.TEST_REPORT_TIME_MARGING;
if (testTimeMargin != null && testTimeMargin.trim().length() != 0) {
longTestTimeMargin = Long.parseLong(testTimeMargin);
}
long longTestTimeMargin = XUnitUtil.parsePositiveLong(testTimeMargin, XUnitDefaultValues.TEST_REPORT_TIME_MARGING);
this.extraConfiguration = new ExtraConfiguration(longTestTimeMargin);
}

/*
* Needed to support Snippet Generator and Workflow properly.
*/
public TestType[] getTools() {
return types;
return tools;
}

/*
@@ -88,10 +89,6 @@ public String getTestTimeMargin() {
return String.valueOf(getExtraConfiguration().getTestTimeMargin());
}

public TestType[] getTypes() {
return types;
}

public XUnitThreshold[] getThresholds() {
return thresholds;
}
@@ -110,14 +107,14 @@ public ExtraConfiguration getExtraConfiguration() {
@Override
public void perform(final Run<?, ?> build, FilePath workspace, Launcher launcher, final TaskListener listener)
throws InterruptedException, IOException {
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTypes(), getThresholds(), getThresholdMode(), getExtraConfiguration());
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTools(), getThresholds(), getThresholdMode(), getExtraConfiguration());
xUnitProcessor.performXUnit(false, build, workspace, listener);
}

public boolean performDryRun(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
try {
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTypes(), getThresholds(), getThresholdMode(), getExtraConfiguration());
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTools(), getThresholds(), getThresholdMode(), getExtraConfiguration());
xUnitProcessor.performXUnit(true, build, build.getWorkspace(), listener);
} catch (Throwable t) {
listener.getLogger().println("[ERROR] - There is an error: " + t.getCause().getMessage());
@@ -68,28 +68,26 @@
*/
public class XUnitPublisher extends Recorder implements DryRun, Serializable, SimpleBuildStep {

private TestType[] types;
@XStreamAlias("types")
private TestType[] tools;
private XUnitThreshold[] thresholds;
private int thresholdMode;
private ExtraConfiguration extraConfiguration;

@DataBoundConstructor
public XUnitPublisher(@CheckForNull TestType[] tools, @CheckForNull XUnitThreshold[] thresholds, int thresholdMode, @CheckForNull String testTimeMargin) {
this.types = (tools != null ? Arrays.copyOf(tools, tools.length) : new TestType[0]);
this.tools = (tools != null ? Arrays.copyOf(tools, tools.length) : new TestType[0]);
this.thresholds = (thresholds != null ? Arrays.copyOf(thresholds, thresholds.length) : new XUnitThreshold[0]);
this.thresholdMode = thresholdMode;
long longTestTimeMargin = XUnitDefaultValues.TEST_REPORT_TIME_MARGING;
if (testTimeMargin != null && testTimeMargin.trim().length() != 0) {
longTestTimeMargin = Long.parseLong(testTimeMargin);
}
long longTestTimeMargin = XUnitUtil.parsePositiveLong(testTimeMargin, XUnitDefaultValues.TEST_REPORT_TIME_MARGING);
this.extraConfiguration = new ExtraConfiguration(longTestTimeMargin);
}

/*
* Needed to support Snippet Generator and Workflow properly.
*/
public TestType[] getTools() {
return types;
return tools;
}

/*
@@ -99,10 +97,6 @@ public String getTestTimeMargin() {
return String.valueOf(getExtraConfiguration().getTestTimeMargin());
}

public TestType[] getTypes() {
return types;
}

public XUnitThreshold[] getThresholds() {
return thresholds;
}
@@ -131,15 +125,15 @@ public Action getProjectAction(AbstractProject<?, ?> project) {
@Override
public void perform(final Run<?, ?> build, FilePath workspace, Launcher launcher, final TaskListener listener)
throws InterruptedException, IOException {
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTypes(), getThresholds(), getThresholdMode(), getExtraConfiguration());
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTools(), getThresholds(), getThresholdMode(), getExtraConfiguration());
xUnitProcessor.performXUnit(false, build, workspace, listener);
}

@Override
public boolean performDryRun(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
try {
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTypes(), getThresholds(), getThresholdMode(), getExtraConfiguration());
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTools(), getThresholds(), getThresholdMode(), getExtraConfiguration());
xUnitProcessor.performXUnit(true, build, build.getWorkspace(), listener);
} catch (Throwable t) {
listener.getLogger().println("[ERROR] - There is an error: " + t.getCause().getMessage());
@@ -0,0 +1,55 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018, Falco Nikolas
*
* 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.
*/
package org.jenkinsci.plugins.xunit;

import hudson.Util;

/* package */ final class XUnitUtil {

private XUnitUtil() {
}

/**
* Parses the string argument as a signed decimal {@code long}. In case the
* given value is empty or null, the default value is returned.
*
* @param value
* to parse
* @param defaultValue
* in case argument is not valid or empty or null
* @return the {@code long} represented by the argument in decimal,
* otherwise default value if argument is null, empty or invalid.
*/
public static long parsePositiveLong(String value, long defaultValue) {
long result = defaultValue;
if (Util.fixEmptyAndTrim(value) != null) {
try {
result = Math.abs(Long.parseLong(value));
} catch (NumberFormatException e) {
// leave result valued with default value
}
}
return result;
}
}
@@ -24,23 +24,20 @@

package org.jenkinsci.plugins.xunit.threshold;

import org.jenkinsci.plugins.xunit.service.XUnitLog;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.junit.TestResultAction;
import org.jenkinsci.plugins.xunit.service.XUnitLog;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author Gregory Boissinot
*/
public class FailedThreshold extends XUnitThreshold {

public FailedThreshold() {
}

@DataBoundConstructor
public FailedThreshold(String unstableThreshold, String unstableNewThreshold, String failureThreshold, String failureNewThreshold) {
super(unstableThreshold, unstableNewThreshold, failureThreshold, failureNewThreshold);
public FailedThreshold() {
}

@Override
@@ -24,23 +24,20 @@

package org.jenkinsci.plugins.xunit.threshold;

import org.jenkinsci.plugins.xunit.service.XUnitLog;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.junit.TestResultAction;
import org.jenkinsci.plugins.xunit.service.XUnitLog;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author Gregory Boissinot
*/
public class SkippedThreshold extends XUnitThreshold {

public SkippedThreshold() {
}

@DataBoundConstructor
public SkippedThreshold(String unstableThreshold, String unstableNewThreshold, String failureThreshold, String failureNewThreshold) {
super(unstableThreshold, unstableNewThreshold, failureThreshold, failureNewThreshold);
public SkippedThreshold() {
}

@Override

0 comments on commit 845615f

Please sign in to comment.
You can’t perform that action at this time.