MAILET-122 Upgrade UserIsTest - It should match our coding conventions - 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/3136f5b2 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3136f5b2 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3136f5b2 Branch: refs/heads/master Commit: 3136f5b2ee038379b8226669eccc2f03feba8c6e Parents: 05d5860 Author: Benoit Tellier <[email protected]> Authored: Wed Aug 31 14:22:27 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Mon Oct 10 11:35:59 2016 +0200 ---------------------------------------------------------------------- .../james/transport/matchers/UserIsTest.java | 102 +++++++++++-------- 1 file changed, 60 insertions(+), 42 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/3136f5b2/mailet/standard/src/test/java/org/apache/james/transport/matchers/UserIsTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/UserIsTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/UserIsTest.java index 7dd1e85..faa98b4 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/UserIsTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/UserIsTest.java @@ -20,69 +20,87 @@ 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.ANY_AT_JAMES2; +import static org.apache.mailet.base.MailAddressFixture.OTHER_AT_JAMES; +import static org.assertj.core.api.Assertions.assertThat; import javax.mail.MessagingException; -import org.apache.james.transport.matchers.UserIs; -import org.apache.mailet.MailAddress; -import org.apache.mailet.Matcher; +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 UserIsTest extends AbstractRecipientIsTest { +public class UserIsTest { - public UserIsTest(String arg0) throws UnsupportedEncodingException { - super(arg0); - } + @Rule + public ExpectedException expectedException = ExpectedException.none(); - protected String getRecipientName() { - return "test"; - } + private UserIs matcher; - // test if the recipients get returned as matched - public void testHostIsMatchedAllRecipients() throws MessagingException { - setRecipients(new MailAddress[] { new MailAddress( - "[email protected]") }); + @Before + public void setUp() throws Exception { + matcher = new UserIs(); + } - setupMockedMail(); - setupMatcher(); + @Test + public void shouldMatchCorrespondingUser() throws MessagingException { + matcher.init(new FakeMatcherConfig("UserIs=any", FakeMailContext.defaultContext())); - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + FakeMail fakeMail = FakeMail.builder() + .recipient(ANY_AT_JAMES) + .build(); - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), mockedMail.getRecipients() - .size()); + assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); } - // test if one recipients get returned as matched - public void testHostIsMatchedOneRecipient() throws MessagingException { - setRecipients(new MailAddress[] { - new MailAddress("[email protected]"), - new MailAddress("[email protected]") }); + @Test + public void shouldMatchCorrespondingUserAccrossDomains() throws MessagingException { + matcher.init(new FakeMatcherConfig("UserIs=any", FakeMailContext.defaultContext())); - setupAll(); + FakeMail fakeMail = FakeMail.builder() + .recipients(ANY_AT_JAMES, ANY_AT_JAMES2) + .build(); - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES, ANY_AT_JAMES2); + } + + @Test + public void shouldNotMatchNonSpecifiedUsersButPreserveSpecifiedUsers() throws MessagingException { + matcher.init(new FakeMatcherConfig("UserIs=any", FakeMailContext.defaultContext())); - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), 1); + FakeMail fakeMail = FakeMail.builder() + .recipients(ANY_AT_JAMES, OTHER_AT_JAMES) + .build(); + + assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); } - // test if no recipient get returned cause it not match - public void testHostIsNotMatch() throws MessagingException { - setRecipients(new MailAddress[] { - new MailAddress("[email protected]"), - new MailAddress("[email protected]") }); + @Test + public void shouldNotMatchNonSpecifiedUsers() throws MessagingException { + matcher.init(new FakeMatcherConfig("UserIs=any", FakeMailContext.defaultContext())); - setupMockedMail(); - setupMatcher(); + FakeMail fakeMail = FakeMail.builder() + .recipients(OTHER_AT_JAMES) + .build(); - Collection<MailAddress> matchedRecipients = matcher.match(mockedMail); + assertThat(matcher.match(fakeMail)).isEmpty(); + } - assertEquals(matchedRecipients.size(), 0); + @Test + public void initShouldThrowOnMissingCondition() throws Exception { + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("UserIs", FakeMailContext.defaultContext())); } - protected Matcher createMatcher() { - return new UserIs(); + @Test + public void initShouldThrowOnEmptyCondition() throws Exception { + expectedException.expect(MessagingException.class); + matcher.init(new FakeMatcherConfig("UserIs=", FakeMailContext.defaultContext())); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
