james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [07/10] james-project git commit: MAILET-112 FetchFromTest should match our coding conventions
Date Tue, 30 Aug 2016 18:01:42 GMT
MAILET-112 FetchFromTest should match our coding conventions


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/2c4a612c
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2c4a612c
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2c4a612c

Branch: refs/heads/master
Commit: 2c4a612c77635e95cd8197757720f3e2964bce30
Parents: a5195b3
Author: Benoit Tellier <btellier@linagora.com>
Authored: Wed Aug 17 13:28:02 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Aug 31 00:59:27 2016 +0700

----------------------------------------------------------------------
 .../transport/matchers/FetchedFromTest.java     | 103 ++++++-------------
 1 file changed, 34 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2c4a612c/mailet/standard/src/test/java/org/apache/james/transport/matchers/FetchedFromTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/FetchedFromTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/FetchedFromTest.java
index 91a4827..a0e8a7b 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/matchers/FetchedFromTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/FetchedFromTest.java
@@ -20,11 +20,9 @@
 
 package org.apache.james.transport.matchers;
 
-import java.util.Collection;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
-import javax.mail.internet.ParseException;
 
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.Matcher;
@@ -32,93 +30,60 @@ import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMatcherConfig;
 import org.apache.mailet.base.test.MailUtil;
-import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
 public class FetchedFromTest {
-
-    private MimeMessage mockedMimeMessage;
-
-    private FakeMail mockedMail;
+    private static final String FETCH_FROM_HEADER = "X-fetched-from";
+    private static final String EXPECTED_HEADER_VALUE = "james-user";
+    private static final String WRONG_HEADER_VALUE = "defaultHeaderValue";
 
     private Matcher matcher;
+    private MailAddress mailAddress1;
+    private MailAddress mailAddress2;
 
-    private final String HEADER_NAME = "X-fetched-from";
-
-    private final String HEADER_VALUE = "james-user";
-
-    private String headerName = "defaultHeaderName";
-
-    private String headerValue = "defaultHeaderValue";
-
-    private void setHeaderName(String headerName) {
-        this.headerName = headerName;
-    }
-
-    private void setHeaderValue(String headerValue) {
-        this.headerValue = headerValue;
-    }
-
-    private void setupMockedMimeMessage() throws MessagingException {
-        mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
-    }
-
-    private void setupMockedMail(MimeMessage m) throws ParseException {
-        mockedMail = MailUtil.createMockMail2Recipients(m);
-    }
-
-    private void setupMatcher() throws MessagingException {
-        setupMockedMimeMessage();
+    @Before
+    public void setUp() throws MessagingException {
         matcher = new FetchedFrom();
-        FakeMatcherConfig mci = new FakeMatcherConfig("FetchedFrom="
-                + HEADER_VALUE, FakeMailContext.defaultContext());
-        matcher.init(mci);
+        FakeMatcherConfig matcherConfig = new FakeMatcherConfig("FetchedFrom=" + EXPECTED_HEADER_VALUE,
FakeMailContext.defaultContext());
+        matcher.init(matcherConfig);
+
+        mailAddress1 = new MailAddress("me@apache.org");
+        mailAddress2 = new MailAddress("you@apache.org");
     }
 
     // test if the Header was matched
     @Test
-    public void testHeaderIsMatched() throws MessagingException {
-        setHeaderName(HEADER_NAME);
-        setHeaderValue(HEADER_VALUE);
-
-        setupMockedMimeMessage();
-        setupMockedMail(mockedMimeMessage);
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-        Assert.assertNotNull(matchedRecipients);
-        Assert.assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
-                .size());
+    public void matchShouldReturnMatchWhenFetchFromHeaderHoldsRightValue() throws MessagingException
{
+        FakeMail fakeMail = FakeMail.builder()
+            .recipients(mailAddress1, mailAddress2)
+            .mimeMessage(MailUtil.createMimeMessage(FETCH_FROM_HEADER, EXPECTED_HEADER_VALUE))
+            .build();
+
+        assertThat(matcher.match(fakeMail)).containsExactly(mailAddress1, mailAddress2);
     }
 
     // test if the Header was not matched
     @Test
-    public void testHeaderIsNotMatched() throws MessagingException {
-        setHeaderName(HEADER_NAME);
-        setHeaderValue(headerValue);
+    public void matchShouldReturnNotMatchWhenFetchFromHeaderHoldsWrongValue() throws MessagingException
{
+        FakeMail fakeMail = FakeMail.builder()
+            .recipients(mailAddress1, mailAddress2)
+            .mimeMessage(MailUtil.createMimeMessage(FETCH_FROM_HEADER, WRONG_HEADER_VALUE))
+            .build();
 
-        setupMockedMimeMessage();
-        setupMockedMail(mockedMimeMessage);
-        setupMatcher();
-
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-        Assert.assertNull(matchedRecipients);
+        assertThat(matcher.match(fakeMail)).isNull();
     }
 
     // test if the Header was removed after matched
     @Test
-    public void testHeaderWasRemovedAfterMatched() throws MessagingException {
-        setHeaderName(HEADER_NAME);
-        setHeaderValue(HEADER_VALUE);
-
-        setupMockedMimeMessage();
-        setupMockedMail(mockedMimeMessage);
-        setupMatcher();
+    public void matchShouldRemoveMatchingHeaders() throws MessagingException {
+        FakeMail fakeMail = FakeMail.builder()
+            .recipients(mailAddress1, mailAddress2)
+            .mimeMessage(MailUtil.createMimeMessage(FETCH_FROM_HEADER, EXPECTED_HEADER_VALUE))
+            .build();
 
-        Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
-        Collection<MailAddress> matchedRecipients2 = matcher.match(mockedMail);
+        matcher.match(fakeMail);
 
-        Assert.assertNotNull(matchedRecipients);
-        Assert.assertNull(matchedRecipients2);
+        assertThat(fakeMail.getMessage().getHeader(FETCH_FROM_HEADER)).isNull();
     }
 }


---------------------------------------------------------------------
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