Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-30730] Prevent ClassCastException in Abstractbuild::re…
…portError() if the build step is not Publisher.

This pull request integrates https://github.com/jenkinsci/jenkins/pulls/1851, which is a reworked version of #1846 from @fbelzunc (no original codelines left)

(cherry picked from commit 725b6af)
  • Loading branch information
oleg-nenashev authored and olivergondza committed Oct 19, 2015
1 parent 012071b commit 3af6c6b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/src/main/java/hudson/model/AbstractBuild.java
Expand Up @@ -742,13 +742,19 @@ protected final boolean performAllBuildSteps(BuildListener listener, Iterable<?
}

private void reportError(BuildStep bs, Throwable e, BuildListener listener, boolean phase) {
final String publisher = ((Publisher) bs).getDescriptor().getDisplayName();
final String buildStep;

if (bs instanceof Describable) {
buildStep = ((Describable) bs).getDescriptor().getDisplayName();
} else {
buildStep = bs.getClass().getName();
}

if (e instanceof AbortException) {
LOGGER.log(Level.FINE, "{0} : {1} failed", new Object[] {AbstractBuild.this, publisher});
listener.error("Publisher '" + publisher + "' failed: " + e.getMessage());
LOGGER.log(Level.FINE, "{0} : {1} failed", new Object[] {AbstractBuild.this, buildStep});
listener.error("Step ‘" + buildStep + " failed: " + e.getMessage());
} else {
String msg = "Publisher '" + publisher + "' aborted due to exception: ";
String msg = "Step ‘" + buildStep + " aborted due to exception: ";
e.printStackTrace(listener.error(msg));
LOGGER.log(WARNING, msg, e);
}
Expand Down
82 changes: 82 additions & 0 deletions test/src/test/java/hudson/model/AbstractBuildTest2.java
@@ -0,0 +1,82 @@
/*
* The MIT License
*
* Copyright (c) 2015 CloudBees, Inc.
*
* 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 hudson.model;

import hudson.Launcher;
import hudson.model.queue.QueueTaskFuture;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

/**
* Unit tests of {@link AbstractBuild}.
* @author Oleg Nenashev
*/
public class AbstractBuildTest2 {

@Rule
public JenkinsRule rule = new JenkinsRule();

@Test
@Issue("JENKINS-30730")
@SuppressWarnings("deprecation")
public void reportErrorShouldNotFailForNonPublisherClass() throws Exception {
FreeStyleProject prj = rule.createFreeStyleProject();
ErrorneousJobProperty errorneousJobProperty = new ErrorneousJobProperty();
prj.addProperty(errorneousJobProperty);
QueueTaskFuture<FreeStyleBuild> future = prj.scheduleBuild2(0);
assertThat("Build should be actually scheduled by Jenkins", future, notNullValue());
FreeStyleBuild build = future.get();
rule.assertLogContains(ErrorneousJobProperty.ERROR_MESSAGE, build);
rule.assertLogNotContains(ClassCastException.class.getName(), build);
}

/**
* Job property, which always fails with an exception.
*/
public static class ErrorneousJobProperty extends JobProperty<FreeStyleProject> {

public static final String ERROR_MESSAGE = "This publisher fails by design";

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
throw new IOException(ERROR_MESSAGE);
}

@TestExtension("reportErrorShouldNotFailForNonPublisherClass")
public static class DescriptorImpl extends JobPropertyDescriptor {

@Override
public String getDisplayName() {
return "Always throws exception in perform()";
}
}
}
}

0 comments on commit 3af6c6b

Please sign in to comment.