Skip to content

Commit

Permalink
A test case to check exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Sep 17, 2016
1 parent 40055a4 commit 783a3ad
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 4 deletions.
@@ -0,0 +1,34 @@
package org.jenkinsci.plugins.workflow.cps.steps.ingroovy;

import hudson.Extension;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author Kohsuke Kawaguchi
* @see StepInGroovyTest#exception()
*/
public class ExceptionStep extends GroovyStep {
private String mode;

@DataBoundConstructor
public ExceptionStep(String mode) {
this.mode = mode;
}

public String getMode() {
return mode;
}

@Extension
public static class DescriptorImpl extends GroovyStepDescriptor {
@Override
public String getFunctionName() {
return "exception";
}

@Override
public boolean takesImplicitBlockArgument() {
return true;
}
}
}
@@ -0,0 +1,14 @@
package org.jenkinsci.plugins.workflow.cps.steps.ingroovy;

/**
* Checked exception that we'll throw around to make sure
* no strange wrapping happens.
*
* @author Kohsuke Kawaguchi
* @see StepInGroovyTest#exception()
*/
public class LifeIsToughException extends Exception {
public LifeIsToughException(String message) {
super(message);
}
}
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.workflow.cps.steps.ingroovy;

import hudson.model.Result;
import hudson.model.queue.QueueTaskFuture;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
Expand All @@ -14,6 +13,7 @@

import java.io.IOException;

import static hudson.model.Result.*;
import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ public void evaluate() throws Throwable {
true
));
WorkflowRun b = p.scheduleBuild2(0).get();
story.j.assertBuildStatus(Result.FAILURE, b);
story.j.assertBuildStatus(FAILURE, b);
story.j.assertLogContains("$$$instantiated",b);
story.j.assertLogNotContains("$$$stole money",b);
story.j.assertLogContains(
Expand Down Expand Up @@ -165,9 +165,9 @@ public void evaluate() throws Throwable {

p.setDefinition(new CpsFlowDefinition(
"def p = [$class:'BooleanParameterDefinition',name:'production',description:'check']\n"+
"assert 'foo'==complex(numbers:[1,2,3,4], param:p) {" +
"assert 'foo'==complex(numbers:[1,2,3,4], param:p) {\n" +
" echo '42'\n" +
" return 'foo'" +
" return 'foo'\n" +
"}"
));
b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
Expand All @@ -177,4 +177,65 @@ public void evaluate() throws Throwable {
}
});
}

/**
* Tests the behaviour of exception coming in and out of a groovy step
*/
@Test
public void exception() throws Exception {
story.addStep(new Statement() {
WorkflowJob p;
WorkflowRun b;

@Override
public void evaluate() throws Throwable {
p = story.j.createProject(WorkflowJob.class, "demo");

fromGroovyStepToCaller();
passThrough();
fromBodyToGroovyStep();
}

private void fromGroovyStepToCaller() throws Exception {
p.setDefinition(new CpsFlowDefinition(
"import "+LifeIsToughException.class.getName()+"\n" +
"try {\n" +
" exception('fromGroovyStepToCaller'){}\n" +
" fail\n"+
"} catch (LifeIsToughException e) {\n" +
" echo 'Caught='+e.message\n"+
"}"
));
b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("Caught=Jesse wants so many test cases",b);
}

private void passThrough() throws Exception {
p.setDefinition(new CpsFlowDefinition(
"import "+LifeIsToughException.class.getName()+"\n" +
"try {\n" +
" exception('passThrough'){\n" +
" throw new LifeIsToughException('There is not enough bacon')\n" +
" }\n" +
" fail\n"+
"} catch (LifeIsToughException e) {\n" +
" echo 'Caught='+e.message\n"+
"}"
));
b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("Caught=There is not enough bacon",b);
}

private void fromBodyToGroovyStep() throws Exception {
p.setDefinition(new CpsFlowDefinition(
"import "+LifeIsToughException.class.getName()+"\n" +
"echo 'Reported='+exception('fromBodyToGroovyStep'){\n" +
" throw new LifeIsToughException('Room is too cold')\n" +
"}"
));
b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("Reported=Room is too cold",b);
}
});
}
}
@@ -0,0 +1,22 @@
package org.jenkinsci.plugins.workflow.cps.steps.ingroovy

/**
* @see StepInGroovyTest#exception()
*/
class ExceptionStepExecution extends GroovyStepExecution {
def call(Closure body) {
switch(step.mode) {
case 'fromGroovyStepToCaller':
throw new LifeIsToughException("Jesse wants so many test cases");
case 'passThrough':
return body()
case 'fromBodyToGroovyStep':
try {
body()
fail;
} catch (LifeIsToughException e) {
return e.message;
}
}
}
}

0 comments on commit 783a3ad

Please sign in to comment.