Skip to content

Commit

Permalink
[FIXED JENKINS-8923] make number of notifications for matrix jobs con…
Browse files Browse the repository at this point in the history
…figurable
  • Loading branch information
kutzi committed Sep 17, 2011
1 parent 75037ce commit d85d10a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/main/java/hudson/plugins/im/IMPublisher.java
Expand Up @@ -3,9 +3,9 @@
import hudson.Launcher;
import hudson.matrix.MatrixAggregatable;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.BuildListener;
import hudson.model.UserProperty;
import hudson.model.AbstractBuild;
Expand Down Expand Up @@ -232,7 +232,7 @@ public final String getStrategy() {
* Specifies if the starting of builds should be notified to
* the registered chat rooms.
*/
public final boolean getNotifyOnStart() {
public boolean getNotifyOnStart() {
return notifyOnBuildStart;
}

Expand Down Expand Up @@ -281,7 +281,7 @@ public boolean perform(final AbstractBuild<?,?> build, final Launcher launcher,
Assert.notNull(build, "Parameter 'build' must not be null.");
Assert.notNull(buildListener, "Parameter 'buildListener' must not be null.");

if (build.getProject() instanceof MatrixProject) {
if (build.getProject() instanceof MatrixConfiguration) {
if (getMatrixNotifier() == MatrixJobMultiplier.ONLY_CONFIGURATIONS
|| getMatrixNotifier() == MatrixJobMultiplier.ALL) {
notifyOnBuildEnd(build, buildListener);
Expand All @@ -296,7 +296,7 @@ public boolean perform(final AbstractBuild<?,?> build, final Launcher launcher,
/**
* Sends notification at build end including maybe notifications of culprits, fixers or so.
*/
private void notifyOnBuildEnd(final AbstractBuild<?, ?> build,
/* package for testing */ void notifyOnBuildEnd(final AbstractBuild<?, ?> build,
final BuildListener buildListener) throws IOException,
InterruptedException {
if (getNotificationStrategy().notificationWanted(build)) {
Expand Down Expand Up @@ -545,8 +545,8 @@ private void notifyChats(final AbstractBuild<?, ?> build, final BuildListener bu
*/
@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener buildListener) {
if (notifyOnBuildStart) {
if (build.getProject() instanceof MatrixProject) {
if (getNotifyOnStart()) {
if (build.getProject() instanceof MatrixConfiguration) {
if (getMatrixNotifier() == MatrixJobMultiplier.ONLY_CONFIGURATIONS
|| getMatrixNotifier() == MatrixJobMultiplier.ALL) {
notifyOnBuildStart(build, buildListener);
Expand All @@ -561,7 +561,7 @@ public boolean prebuild(AbstractBuild<?, ?> build, BuildListener buildListener)
/**
* Sends notification about the build start.
*/
private void notifyOnBuildStart(AbstractBuild<?, ?> build, BuildListener buildListener) {
/* package for testing */ void notifyOnBuildStart(AbstractBuild<?, ?> build, BuildListener buildListener) {
try {
final String msg = buildToChatNotifier.buildStartMessage(this,build,buildListener);
for (final IMMessageTarget target : calculateTargets()) {
Expand Down Expand Up @@ -695,7 +695,7 @@ public MatrixAggregator createAggregator(MatrixBuild build, Launcher launcher, B
@Override
public boolean startBuild() throws InterruptedException,
IOException {
if (IMPublisher.this.notifyOnBuildStart) {
if (getNotifyOnStart()) {
if (getMatrixNotifier() == MatrixJobMultiplier.ALL || getMatrixNotifier() == MatrixJobMultiplier.ONLY_PARENT) {
notifyOnBuildStart(build, listener);
}
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/hudson/plugins/im/IMPublisherTest.java
Expand Up @@ -5,9 +5,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
Expand Down
102 changes: 102 additions & 0 deletions src/test/java/hudson/plugins/im/MatrixNotificationTest.java
@@ -0,0 +1,102 @@
package hudson.plugins.im;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixBuild;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Tests the various notification options (only parent, only configurations, both) for Matrix jobs.
*
* @author kutzi
*/
@SuppressWarnings("rawtypes")
public class MatrixNotificationTest {

private IMPublisher publisher;
private BuildListener listener;
private AbstractBuild configurationBuild;
private MatrixBuild parentBuild;

@Before
public void before() throws InterruptedException, IOException {
this.publisher = mock(IMPublisher.class);
when(publisher.prebuild(any(AbstractBuild.class), any(BuildListener.class))).thenCallRealMethod();
when(publisher.perform(any(AbstractBuild.class), any(Launcher.class), any(BuildListener.class))).thenCallRealMethod();
when(publisher.createAggregator(any(MatrixBuild.class), any(Launcher.class), any(BuildListener.class))).thenCallRealMethod();
when(publisher.getNotifyOnStart()).thenReturn(Boolean.TRUE);

Mockito.doNothing().when(publisher).notifyOnBuildStart(any(AbstractBuild.class), any(BuildListener.class));
Mockito.doNothing().when(publisher).notifyOnBuildEnd(any(AbstractBuild.class), any(BuildListener.class));

this.listener = mock(BuildListener.class);

this.configurationBuild = mock(AbstractBuild.class);
AbstractProject project = mock(MatrixConfiguration.class);
when(configurationBuild.getParent()).thenReturn(project);

this.parentBuild = mock(MatrixBuild.class);
}

@Test
public void testOnlyParent() throws InterruptedException, IOException {
when(publisher.getMatrixNotifier()).thenReturn(MatrixJobMultiplier.ONLY_PARENT);

publisher.prebuild(configurationBuild, listener);
publisher.perform(configurationBuild, null, listener);
verify(publisher, times(0)).notifyOnBuildStart(any(AbstractBuild.class), any(BuildListener.class));
verify(publisher, times(0)).notifyOnBuildEnd(any(AbstractBuild.class), any(BuildListener.class));

MatrixAggregator aggregator = publisher.createAggregator(parentBuild, null, listener);
aggregator.startBuild();
aggregator.endBuild();
verify(publisher).notifyOnBuildStart(parentBuild, listener);
verify(publisher).notifyOnBuildEnd(parentBuild, listener);
}

@Test
public void testOnlyConfigurations() throws InterruptedException, IOException {
when(publisher.getMatrixNotifier()).thenReturn(MatrixJobMultiplier.ONLY_CONFIGURATIONS);

MatrixAggregator aggregator = publisher.createAggregator(parentBuild, null, listener);
aggregator.startBuild();
aggregator.endBuild();
verify(publisher, times(0)).notifyOnBuildStart(parentBuild, listener);
verify(publisher, times(0)).notifyOnBuildEnd(parentBuild, listener);

publisher.prebuild(configurationBuild, listener);
publisher.perform(configurationBuild, null, listener);
verify(publisher).notifyOnBuildStart(configurationBuild, listener);
verify(publisher).notifyOnBuildEnd(configurationBuild, listener);
}

@Test
public void testOnlyBoth() throws InterruptedException, IOException {
when(publisher.getMatrixNotifier()).thenReturn(MatrixJobMultiplier.ALL);

MatrixAggregator aggregator = publisher.createAggregator(parentBuild, null, listener);
aggregator.startBuild();
aggregator.endBuild();
verify(publisher).notifyOnBuildStart(parentBuild, listener);
verify(publisher).notifyOnBuildEnd(parentBuild, listener);

publisher.prebuild(configurationBuild, listener);
publisher.perform(configurationBuild, null, listener);
verify(publisher).notifyOnBuildStart(configurationBuild, listener);
verify(publisher).notifyOnBuildEnd(configurationBuild, listener);
}
}

0 comments on commit d85d10a

Please sign in to comment.