james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [13/50] [abbrv] james-project git commit: MAILET-115 Extract replaceMailAddresses from AbstractRedirect to MailAddressesUtils
Date Wed, 11 Jan 2017 09:26:01 GMT
MAILET-115 Extract replaceMailAddresses from AbstractRedirect to MailAddressesUtils


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

Branch: refs/heads/master
Commit: d6b0899d089340a0a32ecec55bdd20a616601d7a
Parents: 82625a9
Author: Antoine Duprat <aduprat@apache.org>
Authored: Thu Nov 3 14:25:01 2016 +0100
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Jan 11 10:03:28 2017 +0700

----------------------------------------------------------------------
 .../mailets/redirect/AbstractRedirect.java      |  94 +-------
 .../transport/util/SpecialAddressesUtils.java   | 144 +++++++++++
 .../mailets/redirect/AbstractRedirectTest.java  | 186 --------------
 .../util/SpecialAddressesUtilsTest.java         | 241 +++++++++++++++++++
 4 files changed, 387 insertions(+), 278 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d6b0899d/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java
index 32ae435..59201bd 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/AbstractRedirect.java
@@ -35,12 +35,12 @@ import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
-import javax.mail.internet.ParseException;
 
 import org.apache.james.core.MailImpl;
 import org.apache.james.core.MimeMessageUtil;
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.transport.mailets.Redirect;
+import org.apache.james.transport.util.SpecialAddressesUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.DateFormats;
@@ -210,7 +210,7 @@ public abstract class AbstractRedirect extends GenericMailet {
             if (containsOnlyUnalteredOrRecipients(recipients)) {
                 return null;
             }
-            return replaceMailAddresses(originalMail, recipients);
+            return SpecialAddressesUtils.from(this).replaceSpecialAddresses(originalMail,
recipients);
         }
         return null;
     }
@@ -844,96 +844,6 @@ public abstract class AbstractRedirect extends GenericMailet {
 
     /**
      * Returns a new Collection built over <i>list</i> replacing special
-     * addresses with real <code>MailAddress</code>-es.<br>
-     * Manages <code>SpecialAddress.SENDER</code>,
-     * <code>SpecialAddress.REVERSE_PATH</code>,
-     * <code>SpecialAddress.FROM</code>, <code>SpecialAddress.REPLY_TO</code>,
-     * <code>SpecialAddress.RECIPIENTS</code>, <code>SpecialAddress.TO</code>,
-     * <code>SpecialAddress.NULL</code> and
-     * <code>SpecialAddress.UNALTERED</code>.<br>
-     * <code>SpecialAddress.FROM</code> is made equivalent to
-     * <code>SpecialAddress.SENDER</code>; <code>SpecialAddress.TO</code>
is
-     * made equivalent to <code>SpecialAddress.RECIPIENTS</code>.<br>
-     * <code>SpecialAddress.REPLY_TO</code> uses the ReplyTo header if
-     * available, otherwise the From header if available, otherwise the Sender
-     * header if available, otherwise the return-path.<br>
-     * <code>SpecialAddress.NULL</code> and
-     * <code>SpecialAddress.UNALTERED</code> are ignored.<br>
-     * Any other address is not replaced.
-     */
-    protected Collection<MailAddress> replaceMailAddresses(Mail mail, Collection<MailAddress>
list) {
-        ImmutableSet.Builder<MailAddress> builder = ImmutableSet.builder();
-        for (MailAddress mailAddress : list) {
-            if (!SpecialAddress.isSpecialAddress(mailAddress)) {
-                builder.add(mailAddress);
-                continue;
-            }
-
-            SpecialAddressKind specialAddressKind = SpecialAddressKind.forValue(mailAddress.getLocalPart());
-            if (specialAddressKind == null) {
-                builder.add(mailAddress);
-                continue;
-            }
-            switch (specialAddressKind) {
-            case SENDER:
-            case FROM:
-                MailAddress sender = mail.getSender();
-                if (sender != null) {
-                    builder.add(sender);
-                }
-                break;
-            case REPLY_TO:
-                addReplyToFromMail(builder, mail);
-                break;
-            case REVERSE_PATH:
-                MailAddress reversePath = mail.getSender();
-                if (reversePath != null) {
-                    builder.add(reversePath);
-                }
-                break;
-            case RECIPIENTS:
-            case TO:
-                builder.addAll(mail.getRecipients());
-                break;
-            case UNALTERED:
-            case NULL:
-                break;
-            case DELETE:
-                builder.add(mailAddress);
-                break;
-            }
-        }
-        return builder.build();
-    }
-
-    private void addReplyToFromMail(ImmutableSet.Builder<MailAddress> set, Mail mail)
{
-        try {
-            InternetAddress[] replyToArray = (InternetAddress[]) mail.getMessage().getReplyTo();
-            if (replyToArray == null || replyToArray.length == 0) {
-                MailAddress sender = mail.getSender();
-                if (sender != null) {
-                    set.add(sender);
-                }
-            } else {
-                addReplyTo(set, replyToArray);
-            }
-        } catch (MessagingException ae) {
-            log("Unable to parse the \"REPLY_TO\" header in the original message; ignoring.");
-        }
-    }
-
-    private void addReplyTo(ImmutableSet.Builder<MailAddress> set, InternetAddress[]
replyToArray) {
-        for (InternetAddress replyTo : replyToArray) {
-            try {
-                set.add(new MailAddress(replyTo));
-            } catch (ParseException pe) {
-                log("Unable to parse a \"REPLY_TO\" header address in the original message:
" + replyTo + "; ignoring.");
-            }
-        }
-    }
-
-    /**
-     * Returns a new Collection built over <i>list</i> replacing special
      * addresses with real <code>InternetAddress</code>-es.<br>
      * Manages <code>SpecialAddress.SENDER</code>,
      * <code>SpecialAddress.REVERSE_PATH</code>,

http://git-wip-us.apache.org/repos/asf/james-project/blob/d6b0899d/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
new file mode 100644
index 0000000..02318b3
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/SpecialAddressesUtils.java
@@ -0,0 +1,144 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.transport.util;
+
+import java.util.Collection;
+import java.util.Set;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.ParseException;
+
+import org.apache.james.transport.mailets.redirect.SpecialAddress;
+import org.apache.james.transport.mailets.redirect.SpecialAddressKind;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMailet;
+
+import com.google.common.collect.ImmutableSet;
+
+public class SpecialAddressesUtils {
+
+    public static SpecialAddressesUtils from(GenericMailet genericMailet) {
+        return new SpecialAddressesUtils(genericMailet);
+    }
+
+    private final GenericMailet genericMailet;
+
+    public SpecialAddressesUtils(GenericMailet genericMailet) {
+        this.genericMailet = genericMailet;
+    }
+
+    /**
+     * Returns a new Collection built over <i>mailAddresses</i> replacing special
+     * addresses with real <code>MailAddress</code>-es.<br>
+     * Manages <code>SpecialAddress.SENDER</code>,
+     * <code>SpecialAddress.REVERSE_PATH</code>,
+     * <code>SpecialAddress.FROM</code>, <code>SpecialAddress.REPLY_TO</code>,
+     * <code>SpecialAddress.RECIPIENTS</code>, <code>SpecialAddress.TO</code>,
+     * <code>SpecialAddress.NULL</code> and
+     * <code>SpecialAddress.UNALTERED</code>.<br>
+     * <code>SpecialAddress.FROM</code> is made equivalent to
+     * <code>SpecialAddress.SENDER</code>; <code>SpecialAddress.TO</code>
is
+     * made equivalent to <code>SpecialAddress.RECIPIENTS</code>.<br>
+     * <code>SpecialAddress.REPLY_TO</code> uses the ReplyTo header if
+     * available, otherwise the From header if available, otherwise the Sender
+     * header if available, otherwise the return-path.<br>
+     * <code>SpecialAddress.NULL</code> and
+     * <code>SpecialAddress.UNALTERED</code> are ignored.<br>
+     * Any other address is not replaced.
+     */
+    public Collection<MailAddress> replaceSpecialAddresses(Mail mailWithReplacementAddresses,
Collection<MailAddress> mailAddresses) {
+        ImmutableSet.Builder<MailAddress> builder = ImmutableSet.builder();
+        for (MailAddress mailAddress : mailAddresses) {
+            if (!SpecialAddress.isSpecialAddress(mailAddress)) {
+                builder.add(mailAddress);
+                continue;
+            }
+
+            SpecialAddressKind specialAddressKind = SpecialAddressKind.forValue(mailAddress.getLocalPart());
+            if (specialAddressKind == null) {
+                builder.add(mailAddress);
+                continue;
+            }
+            switch (specialAddressKind) {
+            case SENDER:
+            case FROM:
+                MailAddress sender = mailWithReplacementAddresses.getSender();
+                if (sender != null) {
+                    builder.add(sender);
+                }
+                break;
+            case REPLY_TO:
+                builder.addAll(getReplyTosFromMail(mailWithReplacementAddresses));
+                break;
+            case REVERSE_PATH:
+                MailAddress reversePath = mailWithReplacementAddresses.getSender();
+                if (reversePath != null) {
+                    builder.add(reversePath);
+                }
+                break;
+            case RECIPIENTS:
+            case TO:
+                builder.addAll(mailWithReplacementAddresses.getRecipients());
+                break;
+            case UNALTERED:
+            case NULL:
+                break;
+            case DELETE:
+                builder.add(mailAddress);
+                break;
+            }
+        }
+        return builder.build();
+    }
+
+    private Set<MailAddress> getReplyTosFromMail(Mail mail) {
+        try {
+            InternetAddress[] replyToArray = (InternetAddress[]) mail.getMessage().getReplyTo();
+            if (replyToArray == null || replyToArray.length == 0) {
+                return getSender(mail);
+            }
+            return getReplyTos(replyToArray);
+        } catch (MessagingException ae) {
+            genericMailet.log("Unable to parse the \"REPLY_TO\" header in the original message;
ignoring.");
+            return ImmutableSet.of();
+        }
+    }
+
+    private Set<MailAddress> getSender(Mail mail) {
+        MailAddress sender = mail.getSender();
+        if (sender != null) {
+            return ImmutableSet.of(sender);
+        }
+        return ImmutableSet.of();
+    }
+
+    private Set<MailAddress> getReplyTos(InternetAddress[] replyToArray) {
+        ImmutableSet.Builder<MailAddress> builder = ImmutableSet.builder();
+        for (InternetAddress replyTo : replyToArray) {
+            try {
+                builder.add(new MailAddress(replyTo));
+            } catch (ParseException pe) {
+                genericMailet.log("Unable to parse a \"REPLY_TO\" header address in the original
message: " + replyTo + "; ignoring.");
+            }
+        }
+        return builder.build();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/d6b0899d/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/AbstractRedirectTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/AbstractRedirectTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/AbstractRedirectTest.java
index d7885fd..c7069e4 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/AbstractRedirectTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/AbstractRedirectTest.java
@@ -86,192 +86,6 @@ public class AbstractRedirectTest {
     }
 
     @Test
-    public void replaceMailAddressesShouldReturnEmptyWhenEmptyList() throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.<MailAddress>
of());
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnSameContentWhenAddressesDoesntMatchAddressMarkerDomain()
throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        MailAddress mailAddress = new MailAddress("user", "addres.marker");
-        MailAddress mailAddress2 = new MailAddress("user2", "address.mar");
-        ImmutableList<MailAddress> list = ImmutableList.of(mailAddress, mailAddress2);
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, list);
-
-        assertThat(addresses).containsOnly(mailAddress, mailAddress2);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchSender() throws Exception
{
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .sender(sender)
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.SENDER));
-
-        assertThat(addresses).containsOnly(sender);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchFrom() throws Exception
{
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .sender(sender)
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.FROM));
-
-        assertThat(addresses).containsOnly(sender);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchSenderAndSenderIsNull()
throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.SENDER));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchReplyToAndReplyToIsNull()
throws Exception {
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        FakeMail mail = FakeMail.from(message);
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnReplyToWhenAddressesMatchReplyTo() throws
Exception {
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
-        message.setReplyTo(InternetAddress.parse(MailAddressFixture.ANY_AT_JAMES.toString()
+ ", " + MailAddressFixture.OTHER_AT_JAMES.toString()));
-        FakeMail mail = FakeMail.from(message);
-
-        MailAddress expectedReplyTo = MailAddressFixture.ANY_AT_JAMES;
-        MailAddress expectedReplyTo2 = MailAddressFixture.OTHER_AT_JAMES;
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO));
-
-        assertThat(addresses).containsOnly(expectedReplyTo, expectedReplyTo2);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchReplyToAndNoReplyTo()
throws Exception {
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .sender(sender)
-                .mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties())))
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO));
-
-        assertThat(addresses).containsOnly(sender);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchReversePath() throws
Exception {
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .sender(sender)
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH));
-
-        assertThat(addresses).containsOnly(sender);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchReversePathAndNoSender()
throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnRecipientsWhenAddressesMatchRecipients()
throws Exception {
-        MailAddress recipient = MailAddressFixture.ANY_AT_JAMES;
-        MailAddress recipient2 = MailAddressFixture.OTHER_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .recipients(recipient, recipient2)
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.RECIPIENTS));
-
-        assertThat(addresses).containsOnly(recipient, recipient2);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnRecipientsWhenAddressesMatchTo() throws Exception
{
-        MailAddress recipient = MailAddressFixture.ANY_AT_JAMES;
-        MailAddress recipient2 = MailAddressFixture.OTHER_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .recipients(recipient, recipient2)
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.TO));
-
-        assertThat(addresses).containsOnly(recipient, recipient2);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchUnaltered() throws
Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.UNALTERED));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchNull() throws Exception
{
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.NULL));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnSameAddressWhenAddressesDoesntMatch() throws
Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        MailAddress address = new MailAddress("user", "address.marker");
-        MailAddress address2 = new MailAddress("user2", "address.marker");
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(address,
address2));
-
-        assertThat(addresses).containsOnly(address, address2);
-    }
-
-    @Test
-    public void replaceMailAddressesShouldReturnSameListWhenAddressesMatchDelete() throws
Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<MailAddress> addresses = testee.replaceMailAddresses(mail, ImmutableList.of(SpecialAddress.DELETE));
-
-        MailAddress expected = new MailAddress("delete", "address.marker");
-        assertThat(addresses).containsOnly(expected);
-    }
-
-    @Test
     public void replaceInternetAddressesShouldReturnEmptyWhenEmptyList() throws Exception
{
         FakeMail mail = FakeMail.builder()
                 .build();

http://git-wip-us.apache.org/repos/asf/james-project/blob/d6b0899d/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java
new file mode 100644
index 0000000..b55dd46
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/SpecialAddressesUtilsTest.java
@@ -0,0 +1,241 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.transport.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Collection;
+import java.util.Properties;
+
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.transport.mailets.redirect.SpecialAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMailet;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class SpecialAddressesUtilsTest {
+
+    private SpecialAddressesUtils testee;
+
+    @Before
+    public void setup() {
+        testee = SpecialAddressesUtils.from(new GenericMailet() {
+            
+            @Override
+            public void service(Mail mail) throws MessagingException {
+            }
+        });
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnEmptyWhenEmptyList() throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.<MailAddress>
of());
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnSameContentWhenAddressesDoesntMatchAddressMarkerDomain()
throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        MailAddress mailAddress = new MailAddress("user", "addres.marker");
+        MailAddress mailAddress2 = new MailAddress("user2", "address.mar");
+        ImmutableList<MailAddress> list = ImmutableList.of(mailAddress, mailAddress2);
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, list);
+
+        assertThat(addresses).containsOnly(mailAddress, mailAddress2);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchSender() throws Exception
{
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .sender(sender)
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.SENDER));
+
+        assertThat(addresses).containsOnly(sender);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchFrom() throws Exception
{
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .sender(sender)
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.FROM));
+
+        assertThat(addresses).containsOnly(sender);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchSenderAndSenderIsNull()
throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.SENDER));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchReplyToAndReplyToIsNull()
throws Exception {
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        FakeMail mail = FakeMail.from(message);
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnReplyToWhenAddressesMatchReplyTo() throws
Exception {
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        message.setReplyTo(InternetAddress.parse(MailAddressFixture.ANY_AT_JAMES.toString()
+ ", " + MailAddressFixture.OTHER_AT_JAMES.toString()));
+        FakeMail mail = FakeMail.from(message);
+
+        MailAddress expectedReplyTo = MailAddressFixture.ANY_AT_JAMES;
+        MailAddress expectedReplyTo2 = MailAddressFixture.OTHER_AT_JAMES;
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO));
+
+        assertThat(addresses).containsOnly(expectedReplyTo, expectedReplyTo2);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchReplyToAndNoReplyTo()
throws Exception {
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .sender(sender)
+                .mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties())))
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO));
+
+        assertThat(addresses).containsOnly(sender);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnSenderWhenAddressesMatchReversePath() throws
Exception {
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .sender(sender)
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH));
+
+        assertThat(addresses).containsOnly(sender);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchReversePathAndNoSender()
throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnRecipientsWhenAddressesMatchRecipients()
throws Exception {
+        MailAddress recipient = MailAddressFixture.ANY_AT_JAMES;
+        MailAddress recipient2 = MailAddressFixture.OTHER_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .recipients(recipient, recipient2)
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.RECIPIENTS));
+
+        assertThat(addresses).containsOnly(recipient, recipient2);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnRecipientsWhenAddressesMatchTo() throws Exception
{
+        MailAddress recipient = MailAddressFixture.ANY_AT_JAMES;
+        MailAddress recipient2 = MailAddressFixture.OTHER_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .recipients(recipient, recipient2)
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.TO));
+
+        assertThat(addresses).containsOnly(recipient, recipient2);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchUnaltered() throws
Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.UNALTERED));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchNull() throws Exception
{
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.NULL));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnSameAddressWhenAddressesDoesntMatch() throws
Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        MailAddress address = new MailAddress("user", "address.marker");
+        MailAddress address2 = new MailAddress("user2", "address.marker");
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(address,
address2));
+
+        assertThat(addresses).containsOnly(address, address2);
+    }
+
+    @Test
+    public void replaceMailAddressesShouldReturnSameListWhenAddressesMatchDelete() throws
Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.DELETE));
+
+        MailAddress expected = new MailAddress("delete", "address.marker");
+        assertThat(addresses).containsOnly(expected);
+    }
+}


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