Skip to content

Commit

Permalink
[JENKINS-37611] Add "@symbol" annotation to xunit and parameters
Browse files Browse the repository at this point in the history
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 f8af22b commit 46caa54
Show file tree
Hide file tree
Showing 18 changed files with 431 additions and 94 deletions.
Expand Up @@ -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);
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitBuilder.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

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

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

public XUnitThreshold[] getThresholds() {
return thresholds;
}
Expand All @@ -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());
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitPublisher.java
Expand Up @@ -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;
}

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

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

public XUnitThreshold[] getThresholds() {
return thresholds;
}
Expand Down Expand Up @@ -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());
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitUtil.java
@@ -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;
}
}
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 46caa54

Please sign in to comment.