MAILET-122 Sanitize RecipientIsRegexTest - Match our standards - Add missing tests
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/2838d4f8 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2838d4f8 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2838d4f8 Branch: refs/heads/master Commit: 2838d4f8412d121297b9d8a1d5275048f8c53e7f Parents: 0430a90 Author: Benoit Tellier <[email protected]> Authored: Wed Aug 31 14:34:37 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Mon Oct 10 11:36:00 2016 +0200 ---------------------------------------------------------------------- .../matchers/RecipientIsRegexTest.java | 150 +++++++------------ 1 file changed, 57 insertions(+), 93 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/2838d4f8/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsRegexTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsRegexTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsRegexTest.java index ed9c0ad..f261a3c 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsRegexTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientIsRegexTest.java @@ -20,127 +20,91 @@ package org.apache.james.transport.matchers; -import java.io.UnsupportedEncodingException; -import java.util.Collection; +import static org.apache.mailet.base.MailAddressFixture.ANY_AT_JAMES; +import static org.apache.mailet.base.MailAddressFixture.OTHER_AT_JAMES; +import static org.assertj.core.api.Assertions.assertThat; import javax.mail.MessagingException; -import junit.framework.AssertionFailedError; +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; -import org.apache.james.transport.matchers.RecipientIsRegex; -import org.apache.mailet.MailAddress; -import org.apache.mailet.Matcher; +public class RecipientIsRegexTest { -public class RecipientIsRegexTest extends AbstractRecipientIsTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); - private String regex = ".*"; + private RecipientIsRegex matcher; - public RecipientIsRegexTest(String arg0) - throws UnsupportedEncodingException { - super(arg0); + @Before + public void setUp() throws Exception { + matcher = new RecipientIsRegex(); } - private void setRegex(String regex) { - this.regex = regex; - } + @Test + public void shouldMatchOneAddress() throws MessagingException { + matcher.init(new FakeMatcherConfig("RecipientIsRegex=.*@.*", FakeMailContext.defaultContext())); - // test if the recipients get returned as matched - public void testRegexIsMatchedAllRecipients() throws MessagingException { - setRecipients(new MailAddress[] { new MailAddress( - "[email protected]") }); - setRegex(".*@.*"); + FakeMail fakeMail = FakeMail.builder() + .recipient(ANY_AT_JAMES) + .build(); - setupAll(); + assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); + } - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + @Test + public void shouldNotMatchPartially() throws MessagingException { + matcher.init(new FakeMatcherConfig("RecipientIsRegex=any", FakeMailContext.defaultContext())); - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), mockedMail.getRecipients() - .size()); - } + FakeMail fakeMail = FakeMail.builder() + .recipient(ANY_AT_JAMES) + .build(); - // test if one recipients get returned as matched - public void testRegexIsMatchedOneRecipient() throws MessagingException { - setRecipients(new MailAddress[] { - new MailAddress("[email protected]"), - new MailAddress("[email protected]") }); - setRegex("^test@.*"); + assertThat(matcher.match(fakeMail)).isEmpty(); + } - setupAll(); + @Test + public void shouldMatchOnlyMatchingPatterns() throws MessagingException { + matcher.init(new FakeMatcherConfig("RecipientIsRegex=^any@.*", FakeMailContext.defaultContext())); - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + FakeMail fakeMail = FakeMail.builder() + .recipients(ANY_AT_JAMES, OTHER_AT_JAMES) + .build(); - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), 1); + assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); } - // test if no recipient get returned cause it not match - public void testRegexIsNotMatch() throws MessagingException { - setRecipients(new MailAddress[] { - new MailAddress("[email protected]"), - new MailAddress("[email protected]") }); - setRegex(".*\\+"); - - setupAll(); + @Test + public void shouldNotMatchNonMatchingPatterns() throws MessagingException { + matcher.init(new FakeMatcherConfig("RecipientIsRegex=.*\\+", FakeMailContext.defaultContext())); - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + FakeMail fakeMail = FakeMail.builder() + .recipients(ANY_AT_JAMES, OTHER_AT_JAMES) + .build(); - assertEquals(matchedRecipients.size(), 0); + assertThat(matcher.match(fakeMail)).isEmpty(); } - // test if an exception was thrown cause the regex was invalid + @Test public void testRegexIsNotMatchedCauseError() throws MessagingException { - Collection<MailAddress> matchedRecipients = null; - String invalidRegex = "(!("; - String regexException = null; - String exception = "Malformed pattern: " + invalidRegex; - - setRecipients(new MailAddress[] { - new MailAddress("[email protected]"), - new MailAddress("[email protected]") }); - setRegex(invalidRegex); - - try { - setupAll(); - matchedRecipients = matcher.match(mockedMail); - } catch (MessagingException m) { - regexException = m.getMessage(); - } - - assertNull(matchedRecipients); - try { - assertEquals(exception, regexException); - } catch (AssertionFailedError e) { - // NOTE the expected exception changes when the project is built/run - // against non java 1.4 jvm. - assertEquals(exception+" (org.apache.oro.text.regex.MalformedPatternException: Unmatched parentheses.)", regexException); - } - + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("RecipientIsRegex=(.", FakeMailContext.defaultContext())); } - // test if an exception was thrown cause the regex was invalid + @Test public void testThrowExceptionWithEmptyPattern() throws MessagingException { - boolean catchException = false; - - setRecipients(new MailAddress[] { - new MailAddress("[email protected]"), - new MailAddress("[email protected]") }); - setRegex(""); - - try { - setupAll(); - } catch (MessagingException m) { - catchException = true; - } - assertTrue(catchException); - - } - - protected String getRecipientName() { - return regex; + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("RecipientIsRegex=", FakeMailContext.defaultContext())); } - protected Matcher createMatcher() { - return new RecipientIsRegex(); + @Test + public void testThrowExceptionWithNoCondition() throws MessagingException { + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("RecipientIsRegex", FakeMailContext.defaultContext())); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
