Skip to content

Commit

Permalink
MAILET-113 IsSingleRecipientTest should match our coding conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Aug 30, 2016
1 parent 094d627 commit 284424d
Showing 1 changed file with 21 additions and 40 deletions.
Expand Up @@ -20,8 +20,7 @@

package org.apache.james.transport.matchers;

import java.util.Arrays;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;

import javax.mail.MessagingException;

Expand All @@ -30,62 +29,44 @@
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMatcherConfig;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class IsSingleRecipientTest {

private FakeMail mockedMail;

private Matcher matcher;
private MailAddress mailAddress1;
private MailAddress mailAddress2;

private MailAddress[] recipients;
@Before
public void setUp() throws MessagingException {
matcher = new IsSingleRecipient();
FakeMatcherConfig matcherConfig = new FakeMatcherConfig("IsSingleRecipient", FakeMailContext.defaultContext());
matcher.init(matcherConfig);

private void setRecipients(MailAddress[] recipients) {
this.recipients = recipients;
mailAddress1 = new MailAddress("test@james.apache.org");
mailAddress2 = new MailAddress("test2@james.apache.org");
}

private void setupMockedMail() {
mockedMail = new FakeMail();
mockedMail.setRecipients(Arrays.asList(recipients));
@Test
public void matchShouldMatchOneRecipientsEmails() throws MessagingException {
FakeMail fakeMail = FakeMail.builder().recipient(mailAddress1).build();

assertThat(matcher.match(fakeMail)).containsExactly(mailAddress1);
}

private void setupMatcher() throws MessagingException {

matcher = new IsSingleRecipient();
FakeMatcherConfig mci = new FakeMatcherConfig("IsSingleRecipient",
FakeMailContext.defaultContext());
matcher.init(mci);
}

// test if matched
@Test
public void testHostIsMatchedAllRecipients() throws MessagingException {
setRecipients(new MailAddress[]{new MailAddress(
"test@james.apache.org")});

setupMockedMail();
setupMatcher();

Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
public void matchShouldNotMatchMultiRecipientsEMail() throws MessagingException {
FakeMail fakeMail = FakeMail.builder().recipients(mailAddress1, mailAddress2).build();

Assert.assertNotNull(matchedRecipients);
assertThat(matcher.match(fakeMail)).isNull();
}

// test if not matched
@Test
public void testHostIsMatchedOneRecipient() throws MessagingException {
setRecipients(new MailAddress[]{
new MailAddress("test@james2.apache.org"),
new MailAddress("test2@james.apache.org")});

setupMockedMail();
setupMatcher();

Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
public void matchShouldNotMatchMailWithNotRecipients() throws MessagingException {
FakeMail fakeMail = FakeMail.builder().build();

Assert.assertNull(matchedRecipients);
assertThat(matcher.match(fakeMail)).isNull();
}

}

0 comments on commit 284424d

Please sign in to comment.