MAILET-121 Improve SenderIsRegexTest
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1fdde845 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1fdde845 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1fdde845 Branch: refs/heads/master Commit: 1fdde845e1f2daad7e500461dd6c8941f79ced70 Parents: a6b30bd Author: Benoit Tellier <[email protected]> Authored: Wed Aug 31 11:34:16 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Fri Sep 2 13:22:36 2016 +0700 ---------------------------------------------------------------------- .../transport/matchers/SenderIsRegexTest.java | 129 ++++++++----------- 1 file changed, 57 insertions(+), 72 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/1fdde845/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java index 75cebb9..6345f33 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/SenderIsRegexTest.java @@ -20,99 +20,84 @@ package org.apache.james.transport.matchers; -import java.io.UnsupportedEncodingException; -import java.util.Collection; +import static org.assertj.core.api.Assertions.assertThat; import javax.mail.MessagingException; -import org.apache.james.transport.matchers.SenderIsRegex; import org.apache.mailet.MailAddress; -import org.apache.mailet.Matcher; - -public class SenderIsRegexTest extends AbstractSenderIsTest { - - private String regex = ".*"; - - public SenderIsRegexTest(String arg0) throws UnsupportedEncodingException { - super(arg0); - } - - private void setRegex(String regex) { - this.regex = regex; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailContext; +import org.apache.mailet.base.test.FakeMatcherConfig; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class SenderIsRegexTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + public static final String SENDER_NAME = "[email protected]"; + private SenderIsRegex matcher; + private MailAddress recipient; + + @Before + public void setUp() throws Exception { + matcher = new SenderIsRegex(); + recipient = new MailAddress("[email protected]"); } - // test if matched - public void testSenderIsRegexMatchedAllRecipients() - throws MessagingException { - String SENDER_NAME = "[email protected]"; - setSender(new MailAddress(SENDER_NAME)); - setRegex(".*@.*"); - setupMockedMail(); - setupMatcher(); + @Test + public void shouldMatchOnMatchingPattern() throws Exception { + matcher.init(new FakeMatcherConfig("SenderIsRegex=.*@.*", FakeMailContext.defaultContext())); - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + FakeMail fakeMail = FakeMail.builder() + .sender(new MailAddress(SENDER_NAME)) + .recipient(recipient) + .build(); - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), mockedMail.getRecipients() - .size()); + assertThat(matcher.match(fakeMail)).containsExactly(recipient); } - // test if not matched - public void testSenderIsRegexNotMatchedAllRecipients() - throws MessagingException { - setSender(new MailAddress("[email protected]")); - setRegex("^\\."); - - setupMockedMail(); - setupMatcher(); + @Test + public void shouldNotMatchWhenNullSender() throws Exception { + matcher.init(new FakeMatcherConfig("SenderIsRegex=.*@.*", FakeMailContext.defaultContext())); - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + FakeMail fakeMail = FakeMail.builder() + .recipient(recipient) + .build(); - assertNull(matchedRecipients); + assertThat(matcher.match(fakeMail)).isNull(); } - // test if throw exception if no pattern is used - public void testThrowExceptionWithoutPattern() throws MessagingException { - boolean exceptionCatched = false; - setSender(new MailAddress("[email protected]")); - setRegex(""); + @Test + public void shouldNotMatchOnNonMatchingPattern() throws Exception { + matcher.init(new FakeMatcherConfig("SenderIsRegex=^\\.", FakeMailContext.defaultContext())); - setupMockedMail(); - - try { - setupMatcher(); - } catch (MessagingException m) { - exceptionCatched = true; - } - assertTrue(exceptionCatched); - } + FakeMail fakeMail = FakeMail.builder() + .sender(new MailAddress(SENDER_NAME)) + .recipient(recipient) + .build(); - // test if throw exception if invalid pattern is used - public void testThrowExceptionWithInvalidPattern() - throws MessagingException { - boolean exceptionCatched = false; - setSender(new MailAddress("[email protected]")); - setRegex("(."); - - setupMockedMail(); - - try { - setupMatcher(); - } catch (MessagingException m) { - exceptionCatched = true; - } - assertTrue(exceptionCatched); + assertThat(matcher.match(fakeMail)).isNull(); } - protected Matcher createMatcher() { - return new SenderIsRegex(); + @Test + public void initShouldThrowWhenEmptyCondition() throws MessagingException { + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("SenderIsRegex=", FakeMailContext.defaultContext())); } - protected String getConfigOption() { - return "SenderIsRegex="; + @Test + public void initShouldThrowWhenNoConditions() throws MessagingException { + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("SenderIsRegex", FakeMailContext.defaultContext())); } - protected String getConfigValue() { - return regex; + @Test + public void initShouldThrowWhenInvalidPattern() throws MessagingException { + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("SenderIsRegex=(.", FakeMailContext.defaultContext())); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
