Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-22187] Added tests for multiple actions.
  • Loading branch information
ikedam committed Oct 30, 2014
1 parent a8f232f commit b598099
Show file tree
Hide file tree
Showing 8 changed files with 562 additions and 1 deletion.
Expand Up @@ -40,6 +40,7 @@
import hudson.model.AbstractProject;
import hudson.model.FreeStyleProject;
import hudson.model.Saveable;
import hudson.tasks.BuildStep;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.BuildTrigger;
import hudson.tasks.Mailer;
Expand All @@ -60,6 +61,8 @@
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.google.common.base.Function;
import com.google.common.collect.Lists;

/**
*
Expand Down Expand Up @@ -166,6 +169,98 @@ public void testMiltipleConditions() throws Exception {
assertTrue(archiver.isLatestOnly());
}

public void testMiltipleConditionsMultipleActions() throws Exception {
FreeStyleProject p = createFreeStyleProject();
ConditionalPublisher conditionalPublisher1 = new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new BuildTrigger("anotherProject1", Result.SUCCESS),
new BuildTrigger("anotherProject2", Result.UNSTABLE)
),
new BuildStepRunner.Run(),
false,
null,
null
);
ConditionalPublisher conditionalPublisher2 = new ConditionalPublisher(
new NeverRun(),
Arrays.<BuildStep>asList(
new ArtifactArchiver("**/*.jar", "some/bad.jar", true),
new ArtifactArchiver("**/*.class", "some/bad.class", false)
),
new BuildStepRunner.DontRun(),
false,
null,
null
);
FlexiblePublisher flexiblePublisher = new FlexiblePublisher(Arrays.asList(
conditionalPublisher1,
conditionalPublisher2
));
p.getPublishersList().add(flexiblePublisher);
p.save();

reconfigure(p);

conditionalPublisher1 = p.getPublishersList().get(FlexiblePublisher.class).getPublishers().get(0);
assertEquals(AlwaysRun.class, conditionalPublisher1.getCondition().getClass());
assertEquals(BuildStepRunner.Run.class, conditionalPublisher1.getRunner().getClass());
assertFalse(conditionalPublisher1.isConfiguredAggregation());
assertNull(conditionalPublisher1.getAggregationCondition());
assertNull(conditionalPublisher1.getAggregationRunner());
assertEquals(Arrays.<Class<?>>asList(
BuildTrigger.class,
BuildTrigger.class
),
Lists.transform(conditionalPublisher1.getPublisherList(), new Function<BuildStep, Class<?>>() {
@Override
public Class<?> apply(BuildStep input) {
return input.getClass();
}
})
);
{
BuildTrigger trigger = (BuildTrigger)conditionalPublisher1.getPublisherList().get(0);
assertEquals("anotherProject1", trigger.getChildProjectsValue());
assertEquals(Result.SUCCESS, trigger.getThreshold());
}
{
BuildTrigger trigger = (BuildTrigger)conditionalPublisher1.getPublisherList().get(1);
assertEquals("anotherProject2", trigger.getChildProjectsValue());
assertEquals(Result.UNSTABLE, trigger.getThreshold());
}

conditionalPublisher2 = p.getPublishersList().get(FlexiblePublisher.class).getPublishers().get(1);
assertEquals(NeverRun.class, conditionalPublisher2.getCondition().getClass());
assertEquals(BuildStepRunner.DontRun.class, conditionalPublisher2.getRunner().getClass());
assertFalse(conditionalPublisher2.isConfiguredAggregation());
assertNull(conditionalPublisher2.getAggregationCondition());
assertNull(conditionalPublisher2.getAggregationRunner());
assertEquals(Arrays.<Class<?>>asList(
ArtifactArchiver.class,
ArtifactArchiver.class
),
Lists.transform(conditionalPublisher2.getPublisherList(), new Function<BuildStep, Class<?>>() {
@Override
public Class<?> apply(BuildStep input) {
return input.getClass();
}
})
);
{
ArtifactArchiver archiver = (ArtifactArchiver)conditionalPublisher2.getPublisherList().get(0);
assertEquals("**/*.jar", archiver.getArtifacts());
assertEquals("some/bad.jar", archiver.getExcludes());
assertTrue(archiver.isLatestOnly());
}
{
ArtifactArchiver archiver = (ArtifactArchiver)conditionalPublisher2.getPublisherList().get(1);
assertEquals("**/*.class", archiver.getArtifacts());
assertEquals("some/bad.class", archiver.getExcludes());
assertFalse(archiver.isLatestOnly());
}
}

public void testMatrixWithAggregationCondition() throws Exception {
MatrixProject p = createMatrixProject();
p.setAxes(new AxisList(new TextAxis("axis1", "value1", "values2")));
Expand Down
@@ -0,0 +1,161 @@
/*
* The MIT License
*
* Copyright (c) 2014 IKEDA Yasuyuki
*
* 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.jenkins_ci.plugins.flexible_publish;

import java.io.File;
import java.util.Arrays;

import hudson.model.FreeStyleBuild;
import hudson.model.Cause;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.tasks.BuildStep;
import hudson.tasks.ArtifactArchiver;

import org.jenkins_ci.plugins.flexible_publish.testutils.FileWriteBuilder;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner;
import org.jenkins_ci.plugins.run_condition.core.StringsMatchCondition;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.recipes.LocalData;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

/**
*
*/
public class FlexiblePublisherTest extends HudsonTestCase {
@LocalData
public void testMigrationFrom0_12() throws Exception {
FreeStyleProject p = jenkins.getItemByFullName("migration_from_0.12", FreeStyleProject.class);
FlexiblePublisher fp = p.getPublishersList().get(FlexiblePublisher.class);
ConditionalPublisher cp = fp.getPublishers().get(0);
assertEquals(
Arrays.<Class<?>>asList(ArtifactArchiver.class),
Lists.transform(cp.getPublisherList(), new Function<BuildStep, Class<?>>() {
public Class<?> apply(BuildStep input) {
return input.getClass();
}
})
);
ArtifactArchiver aa = (ArtifactArchiver)cp.getPublisherList().get(0);
assertEquals("artifact.txt", aa.getArtifacts());

{
@SuppressWarnings("deprecation")
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(
new StringParameterValue("SWITCH", "off")
)).get();
assertBuildStatusSuccess(b);
assertFalse(new File(b.getArtifactsDir(), "artifact.txt").exists());
}

{
@SuppressWarnings("deprecation")
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(
new StringParameterValue("SWITCH", "on")
)).get();
assertBuildStatusSuccess(b);
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}
}

public void testMultipleConditionsMultipleActions() throws Exception {
FreeStyleProject p = createFreeStyleProject();

p.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("SWITCH", "off")
));

p.getBuildersList().add(new FileWriteBuilder("artifact1.txt", "blahblahblah"));
p.getBuildersList().add(new FileWriteBuilder("artifact2.txt", "blahblahblah"));
p.getBuildersList().add(new FileWriteBuilder("artifact3.txt", "blahblahblah"));
p.getBuildersList().add(new FileWriteBuilder("artifact4.txt", "blahblahblah"));

p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new StringsMatchCondition("${SWITCH}", "off", false),
Arrays.<BuildStep>asList(
new ArtifactArchiver("artifact1.txt", "", false),
new ArtifactArchiver("artifact2.txt", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
),
new ConditionalPublisher(
new StringsMatchCondition("${SWITCH}", "on", false),
Arrays.<BuildStep>asList(
new ArtifactArchiver("artifact3.txt", "", false),
new ArtifactArchiver("artifact4.txt", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));

{
@SuppressWarnings("deprecation")
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(
new StringParameterValue("SWITCH", "off")
)).get();
assertBuildStatusSuccess(b);
assertTrue(new File(b.getArtifactsDir(), "artifact1.txt").exists());
assertTrue(new File(b.getArtifactsDir(), "artifact2.txt").exists());
assertFalse(new File(b.getArtifactsDir(), "artifact3.txt").exists());
assertFalse(new File(b.getArtifactsDir(), "artifact4.txt").exists());
}

{
@SuppressWarnings("deprecation")
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(
new StringParameterValue("SWITCH", "on")
)).get();
assertBuildStatusSuccess(b);
assertFalse(new File(b.getArtifactsDir(), "artifact1.txt").exists());
assertFalse(new File(b.getArtifactsDir(), "artifact2.txt").exists());
assertTrue(new File(b.getArtifactsDir(), "artifact3.txt").exists());
assertTrue(new File(b.getArtifactsDir(), "artifact4.txt").exists());
}

{
@SuppressWarnings("deprecation")
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(
new StringParameterValue("SWITCH", "badValue")
)).get();
assertBuildStatusSuccess(b);
assertFalse(new File(b.getArtifactsDir(), "artifact1.txt").exists());
assertFalse(new File(b.getArtifactsDir(), "artifact2.txt").exists());
assertFalse(new File(b.getArtifactsDir(), "artifact3.txt").exists());
assertFalse(new File(b.getArtifactsDir(), "artifact4.txt").exists());
}
}
}
Expand Up @@ -40,7 +40,11 @@
import hudson.model.ParametersDefinitionProperty;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.tasks.BuildStep;
import hudson.tasks.ArtifactArchiver;

import org.jenkins_ci.plugins.flexible_publish.testutils.AggregationRecorder;
import org.jenkins_ci.plugins.flexible_publish.testutils.FileWriteBuilder;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner;
import org.jenkins_ci.plugins.run_condition.core.AlwaysRun;
import org.jenkins_ci.plugins.run_condition.core.StringsMatchCondition;
Expand Down Expand Up @@ -462,4 +466,52 @@ public void testRunnerForParent() throws Exception {
assertNull(aggregator);
}
}

public void testMiltipleActions() throws Exception {
MatrixProject p = createMatrixProject();
p.setAxes(new AxisList(new TextAxis("axis1", "value1", "value2")));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new AggregationRecorder(),
new AggregationRecorder()
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));
p.save();

MatrixBuild build = p.scheduleBuild2(0).get();
assertBuildStatusSuccess(build);
assertEquals(2, build.getActions(AggregationRecorder.RecorderAction.class).size());
}


public void testMixedWithNonaggregateble() throws Exception {
MatrixProject p = createMatrixProject();
p.setAxes(new AxisList(new TextAxis("axis1", "value1", "value2")));
p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ArtifactArchiver("**/*", "", false),
new AggregationRecorder()
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));
p.save();

MatrixBuild build = p.scheduleBuild2(0).get();
assertBuildStatusSuccess(build);
assertNotNull(build.getAction(AggregationRecorder.RecorderAction.class));
}
}

0 comments on commit b598099

Please sign in to comment.