Skip to content

Commit

Permalink
[FIXED JENKINS-24694] Added manager.buildIsA to test a type of build.…
Browse files Browse the repository at this point in the history
… mainly used for test whether the build is a matrix parent.
  • Loading branch information
ikedam committed Sep 13, 2014
1 parent 00a39a3 commit 23412b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Expand Up @@ -283,6 +283,16 @@ private Pattern compilePattern(String regexp) throws AbortException {
return pattern;
}

/**
* Test whether the current build is specified type.
*
* @param buildClass
* @return true if the current build is an instance of buildClass
*/
@Whitelisted
public boolean buildIsA(Class<? extends AbstractBuild<?, ?>> buildClass) {
return buildClass.isInstance(getBuild());
}
}

@DataBoundConstructor
Expand Down
Expand Up @@ -8,11 +8,11 @@
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;

if (manager.build instanceof MatrixBuild)
if (manager.buildIsA(MatrixBuild.class))
{
// codes for matrix parents.
}
else if (manager.build instanceof MatrixRun)
else if (manager.buildIsA(MatrixRun.class))
{
// codes for matrix children.
}
Expand Down
Expand Up @@ -47,29 +47,24 @@ public class GroovyPostbuildRecorderTest {
private static final String SCRIPT_FOR_MATRIX = StringUtils.join(new String[]{
"import hudson.matrix.MatrixBuild;",
"import hudson.matrix.MatrixRun;",
"if (manager.build instanceof MatrixBuild) {",
"if (manager.buildIsA(MatrixBuild.class)) {",
" // codes for matrix parents.",
" manager.addShortText(\"parent\");",
"} else if(manager.build instanceof MatrixRun) {",
"} else if(manager.buildIsA(MatrixRun)) {",
" // codes for matrix children.",
" manager.addShortText(manager.build.buildVariables[\"axis1\"]);",
" manager.addShortText(manager.getEnvVariable(\"axis1\"));",
"} else {",
" // unexpected case.",
" manager.buildFailure();",
"}"
}, '\n');

@Before
public void setUp() throws Exception {
ScriptApproval.get().preapprove(SCRIPT_FOR_MATRIX, GroovyLanguage.get());
}

@Test
public void testMatrixProjectWithParent() throws Exception {
MatrixProject p = j.createMatrixProject();
AxisList axisList = new AxisList(new TextAxis("axis1", "value1", "value2"));
p.setAxes(axisList);
p.getPublishersList().add(new GroovyPostbuildRecorder(new SecureGroovyScript(SCRIPT_FOR_MATRIX, false), 2, true));
p.getPublishersList().add(new GroovyPostbuildRecorder(new SecureGroovyScript(SCRIPT_FOR_MATRIX, true), 2, true));

MatrixBuild b = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(b);
Expand All @@ -84,7 +79,7 @@ public void testMatrixProjectWithoutParent() throws Exception {
MatrixProject p = j.createMatrixProject();
AxisList axisList = new AxisList(new TextAxis("axis1", "value1", "value2"));
p.setAxes(axisList);
p.getPublishersList().add(new GroovyPostbuildRecorder(new SecureGroovyScript(SCRIPT_FOR_MATRIX, false), 2, false));
p.getPublishersList().add(new GroovyPostbuildRecorder(new SecureGroovyScript(SCRIPT_FOR_MATRIX, true), 2, false));

MatrixBuild b = p.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(b);
Expand Down

0 comments on commit 23412b4

Please sign in to comment.