Skip to content
Permalink
Browse files

[Fix JENKINS-37995] X failure trigger

Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
  • Loading branch information...
KostyaSha committed Sep 6, 2016
1 parent 186435c commit 3770605b45b1cf56175938abefc14f93c18a7528
@@ -0,0 +1,49 @@
package hudson.plugins.emailext.plugins.trigger;

import hudson.Extension;
import hudson.plugins.emailext.plugins.EmailTrigger;
import hudson.plugins.emailext.plugins.RecipientProvider;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.util.List;

/**
* @author Kanstantsin Shautsou
*/
public class XNthFailureTrigger extends NthFailureTrigger {
public static final String TRIGGER_NAME = "Failure - X";

private int requiredFailureCount = 3;

@DataBoundConstructor
public XNthFailureTrigger(List<RecipientProvider> recipientProviders, String recipientList, String replyTo,
String subject, String body, String attachmentsPattern, int attachBuildLog,
String contentType) {
super(recipientProviders, recipientList, replyTo, subject, body, attachmentsPattern, attachBuildLog, contentType);
}

@Override
protected int getRequiredFailureCount() {
return requiredFailureCount;
}

@DataBoundSetter
public void setRequiredFailureCount(int requiredFailureCount) {
this.requiredFailureCount = requiredFailureCount;
}

@Extension
public static final class DescriptorImpl extends NthFailureTrigger.DescriptorImpl {

@Override
public String getDisplayName() {
return TRIGGER_NAME;
}

@Override
public EmailTrigger createDefault() {
return _createDefault();
}
}
}
@@ -0,0 +1,3 @@
<div>
An email will be sent when the build fails X times in a row after a successful build.
</div>
@@ -0,0 +1,7 @@
package hudson.plugins.emailext.plugins.trigger.XNthFailureTrigger

f = namespace("/lib/form")

f.entry(title: _("Number Of Failures")) {
f.number(name: "requiredFailureCount")
}
@@ -0,0 +1,54 @@
package hudson.plugins.emailext.plugins.trigger;

import hudson.model.Result;
import hudson.plugins.emailext.plugins.EmailTrigger;
import org.junit.Test;

import java.io.IOException;

import static hudson.model.Result.FAILURE;
import static hudson.model.Result.SUCCESS;
import static org.junit.Assert.*;

/**
* @author Kanstantsin Shautsou
*/
public class XNthFailureTriggerTest extends TriggerTestBase {
@Override
EmailTrigger newInstance() {
XNthFailureTrigger trigger = new XNthFailureTrigger(recProviders, "", "", "", "", "", 0, "project");
trigger.setRequiredFailureCount(3);
return trigger;
}

@Test
public void testTrigger_success()
throws IOException, InterruptedException {
assertNotTriggered(SUCCESS);
assertNotTriggered(SUCCESS, SUCCESS);
assertNotTriggered(SUCCESS, SUCCESS, SUCCESS);
assertNotTriggered(SUCCESS, SUCCESS, SUCCESS, SUCCESS);
}

@Test
public void testTrigger_thirdFailureAfterSuccess()
throws IOException, InterruptedException {
assertTriggered(FAILURE, SUCCESS, FAILURE, FAILURE, FAILURE);
}

@Test
public void testTrigger_thirdBuildFails()
throws IOException, InterruptedException {
assertTriggered(FAILURE, FAILURE, FAILURE);
assertTriggered(SUCCESS, FAILURE, FAILURE, FAILURE);
}

@Test
public void testTrigger_failure()
throws IOException, InterruptedException {
assertNotTriggered(FAILURE);
assertNotTriggered(FAILURE, FAILURE);
assertNotTriggered(SUCCESS, FAILURE, FAILURE);
assertNotTriggered(SUCCESS, FAILURE, FAILURE, SUCCESS);
}
}

0 comments on commit 3770605

Please sign in to comment.
You can’t perform that action at this time.