Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A test case to check exception handling
- Loading branch information
Showing
4 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/test/java/org/jenkinsci/plugins/workflow/cps/steps/ingroovy/ExceptionStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/org/jenkinsci/plugins/workflow/cps/steps/ingroovy/LifeIsToughException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...resources/org/jenkinsci/plugins/workflow/cps/steps/ingroovy/ExceptionStepExecution.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |