Skip to content
Permalink
Browse files

correction JENKINS-15474 : does not add the same Cause type multiple …

…times
  • Loading branch information
mpailloncy committed Oct 13, 2012
1 parent a758115 commit 80282bcdcc2ffb16725326b599970850becec6cd
@@ -1,12 +1,17 @@
package org.jenkinsci.plugins.buildtriggerbadge;

import hudson.Extension;
import hudson.Plugin;
import hudson.PluginWrapper;
import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jenkins.model.Jenkins;

@@ -17,19 +22,26 @@
*/
@Extension
public class RunListenerImpl extends RunListener<AbstractBuild> {

public RunListenerImpl() {
super(AbstractBuild.class);
}

@Override
public void onStarted(AbstractBuild build, TaskListener listener) {
BuildTriggerBadgePlugin plugin = Jenkins.getInstance().getPlugin(BuildTriggerBadgePlugin.class);
if(plugin.isActivated()) {
Set<Class<? extends Cause>> causeClasses = new HashSet<Class<? extends Cause>>();
List<Cause> causes = build.getCauses();
for (Cause cause : causes) {
build.addAction(new BuildTriggerBadgeAction(cause));
// filter causes by its Class type
if(!causeClasses.contains(cause.getClass())) {
causeClasses.add(cause.getClass());
build.addAction(new BuildTriggerBadgeAction(cause));
}
}
}
super.onStarted(build, listener);
}

}
@@ -0,0 +1,32 @@
package org.jenkinsci.plugins.buildtriggerbadge;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.triggers.SCMTrigger.SCMTriggerCause;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.HudsonTestCase;

/**
* Tests for {@link RunListenerImpl}
* @author Michael Pailloncy
*/
public class RunListenerImplTest extends HudsonTestCase {

@Bug(15474)
public void testSameIconMultipleTimesCorrection() throws IOException, InterruptedException, ExecutionException{
FreeStyleProject p = createFreeStyleProject();
Future<FreeStyleBuild> futureBuild = p.scheduleBuild2(20, new SCMTriggerCause("scm change 1"));
assertFalse(p.scheduleBuild(new SCMTriggerCause("scm change 2")));
assertFalse(p.scheduleBuild(new SCMTriggerCause("scm change 3")));
assertFalse(p.scheduleBuild(new SCMTriggerCause("scm change 4")));
assertFalse(p.scheduleBuild(new SCMTriggerCause("scm change 5")));
FreeStyleBuild build = futureBuild.get();
// there should be only one badge action here
assertEquals(1, build.getBadgeActions().size());
}
}

0 comments on commit 80282bc

Please sign in to comment.