james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [05/11] james-project git commit: MAILET-121 Improve SenderIsRegexTest
Date Fri, 02 Sep 2016 06:23:30 GMT
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 <btellier@linagora.com>
Authored: Wed Aug 31 11:34:16 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
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 = "test@james.apache.org";
+    private SenderIsRegex matcher;
+    private MailAddress recipient;
+
+    @Before
+    public void setUp() throws Exception {
+        matcher = new SenderIsRegex();
+        recipient = new MailAddress("recipient@apache.org");
     }
 
-    // test if matched
-    public void testSenderIsRegexMatchedAllRecipients()
-            throws MessagingException {
-        String SENDER_NAME = "test@james.apache.org";
-        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("t@james.apache.org"));
-        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("t@james.apache.org"));
-        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("t@james.apache.org"));
-        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: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message