Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #25 from andresrc/JENKINS-33291
[JENKINS-33291] Additional Tests
  • Loading branch information
abayer committed Mar 28, 2016
2 parents 8551722 + 600520a commit 9a487a9
Showing 1 changed file with 162 additions and 11 deletions.
173 changes: 162 additions & 11 deletions src/test/java/hudson/tasks/MailerTest.java
Expand Up @@ -31,15 +31,19 @@
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.Email;
import org.jvnet.hudson.test.FakeChangeLogSCM;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.UnstableBuilder;
import org.jvnet.mock_javamail.Mailbox;

import javax.mail.Address;
import javax.mail.internet.InternetAddress;

import jenkins.model.JenkinsLocationConfiguration;

import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
Expand All @@ -49,29 +53,48 @@
* @author Kohsuke Kawaguchi
*/
public class MailerTest {
/** Default recipient used in tests. */
private static final String RECIPIENT = "you <you@sun.com>";
/** Fixed FAILURE builder. */
private static final Builder FAILURE = new FailureBuilder();
/** Fixed UNSTABLE builder. */
private static final Builder UNSTABLE = new UnstableBuilder();
/** Commit author 1. */
private static final String AUTHOR1 = "author1@example.com";
/** Commit author 2. */
private static final String AUTHOR2 = "author2@example.com";
/** Change counter. */
private static final AtomicLong COUNTER = new AtomicLong(0L);

@Rule
public JenkinsRule rule = new JenkinsRule();

private static Mailbox getMailbox(String recipient) throws Exception {
return Mailbox.get(new InternetAddress(recipient));
}

private static Mailbox getEmptyMailbox(String recipient) throws Exception {
Mailbox inbox = getMailbox(recipient);
inbox.clear();
return inbox;
}

private TestProject create(boolean notifyEveryUnstableBuild, boolean sendToIndividuals) throws Exception {
Mailer m = new Mailer(RECIPIENT, notifyEveryUnstableBuild, sendToIndividuals);
return new TestProject(m);
}

@Bug(1566)
@Test
public void testSenderAddress() throws Exception {
// intentionally give the whole thin in a double quote
JenkinsLocationConfiguration.get().setAdminAddress("\"me <me@sun.com>\"");

String recipient = "you <you@sun.com>";
Mailbox yourInbox = Mailbox.get(new InternetAddress(recipient));
yourInbox.clear();

// create a project to simulate a build failure
FreeStyleProject project = rule.createFreeStyleProject();
project.getBuildersList().add(new FailureBuilder());
Mailer m = new Mailer(recipient, false, false);
project.getPublishersList().add(m);

FreeStyleBuild b = project.scheduleBuild2(0).get();
TestProject project = create(false, false).failure().buildAndCheck(1);

assertEquals(rule.getLog(b), 1, yourInbox.size());
// build it and check sender address
Mailbox yourInbox = getMailbox(RECIPIENT);
Address[] senders = yourInbox.get(0).getFrom();
assertEquals(1,senders.length);
assertEquals("me <me@sun.com>",senders[0].toString());
Expand Down Expand Up @@ -154,4 +177,132 @@ public void testHudsonUrlCompatibility() throws Exception {
// configured via the marshaled XML file of Mailer
assertEquals(wc.getContextPath(), new CleanJenkinsLocationConfiguration().getUrl());
}

@Test
public void testNotifyEveryUnstableBuild() throws Exception {
create(true, false).failure()
.buildAndCheck(1)
.buildAndCheck(1)
.unstable()
.buildAndCheck(1)
.buildAndCheck(1)
.success()
.buildAndCheck(1) /* back to normal mail. */
.buildAndCheck(0)
.unstable()
.buildAndCheck(1)
.failure()
.buildAndCheck(1);
}

@Test
public void testNotNotifyEveryUnstableBuild() throws Exception {
create(false, false)
.buildAndCheck(0)
.buildAndCheck(0)
.unstable()
.buildAndCheck(1)
.buildAndCheck(0)
.success()
.buildAndCheck(1) /* back to normal mail. */
.buildAndCheck(0)
.unstable()
.buildAndCheck(1)
.buildAndCheck(0)
.failure()
.buildAndCheck(1)
.buildAndCheck(1);
}

@Test
public void testNotifyCulprits() throws Exception {
TestProject project = create(true, true).buildAndCheck(0);
// Changes with no problems
project.commit(AUTHOR1).clearBuild().check(0, 0, 0);
// Commit causes build to be unstable
project.commit(AUTHOR1).unstable().clearBuild().check(1, 1, 0);
// Another one
project.commit(AUTHOR2).clearBuild().check(1, 1, 1);
// Back to normal
project.commit(AUTHOR1).success().clearBuild().check(1, 1, 1);
// Now a failure
project.commit(AUTHOR2).failure().clearBuild().check(1, 0, 1);
}

private final class TestProject {
private final FakeChangeLogSCM scm = new FakeChangeLogSCM();
private final FreeStyleProject project;

TestProject(Mailer m) throws Exception {
project = rule.createFreeStyleProject();
project.setScm(scm);
project.getPublishersList().add(m);
}

TestProject changeStatus(Builder status) {
project.getBuildersList().clear();
if (status != null) {
project.getBuildersList().add(status);
}
return this;
}

TestProject success() {
return changeStatus(null);
}

TestProject failure() {
return changeStatus(FAILURE);
}

TestProject unstable() {
return changeStatus(UNSTABLE);
}

TestProject commit(String author) {
scm.addChange().withAuthor(author).withMsg("commit #" + COUNTER.incrementAndGet());
return this;
}

TestBuild build(String... mailboxesToClear) throws Exception {
for (String recipient : mailboxesToClear) {
getEmptyMailbox(recipient);
}
return new TestBuild(project.scheduleBuild2(0).get());
}

TestBuild clearBuild() throws Exception {
return build(RECIPIENT, AUTHOR1, AUTHOR2);
}

TestProject buildAndCheck(int expectedSize, String recipient) throws Exception {
build(recipient).check(expectedSize, recipient);
return this;
}

TestProject buildAndCheck(int expectedSize) throws Exception {
return buildAndCheck(expectedSize, RECIPIENT);
}
}

private final class TestBuild {
private final FreeStyleBuild build;
private final String log;

TestBuild(FreeStyleBuild build) throws Exception {
this.build = build;
this.log = rule.getLog(build);
}

TestBuild check(int expectedSize, String recipient) throws Exception {
final Mailbox inbox = getMailbox(recipient);
assertEquals(log, expectedSize, inbox.size());
return this;
}

TestBuild check(int expectedRecipient, int expectedAuthor1, int expectedAuthor2) throws Exception {
return check(expectedRecipient, RECIPIENT).check(expectedAuthor1, AUTHOR1).check(expectedAuthor2, AUTHOR2);
}
}

}

0 comments on commit 9a487a9

Please sign in to comment.