MAILET-115 Extract replaceInternetAddresses from AbstractRedirect to 
SpecialAddressesUtils


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

Branch: refs/heads/master
Commit: b94bc281287202784bb63b6d763cc10b517cbd09
Parents: d010021
Author: Antoine Duprat <adup...@apache.org>
Authored: Thu Nov 3 16:07:59 2016 +0100
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Wed Jan 11 10:03:28 2017 +0700

----------------------------------------------------------------------
 .../mailets/redirect/AbstractRedirect.java      | 113 +-------
 .../transport/util/SpecialAddressesUtils.java   | 111 +++++++
 .../mailets/redirect/AbstractRedirectTest.java  | 287 -------------------
 .../util/SpecialAddressesUtilsTest.java         | 201 +++++++++++++
 4 files changed, 313 insertions(+), 399 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b94bc281/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 a3cdced..4f646e1 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
@@ -49,7 +49,6 @@ import org.apache.mailet.base.RFC2822Headers;
 
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
 
 /**
  * <p>
@@ -261,7 +260,7 @@ public abstract class AbstractRedirect extends 
GenericMailet {
             if (containsOnlyUnalteredOrTo(apparentlyTo)) {
                 return null;
             }
-            Collection<InternetAddress> addresses = 
replaceInternetAddresses(originalMail, ImmutableList.copyOf(apparentlyTo));
+            Collection<InternetAddress> addresses = 
SpecialAddressesUtils.from(this).replaceInternetAddresses(originalMail, 
ImmutableList.copyOf(apparentlyTo));
             return addresses.toArray(new InternetAddress[addresses.size()]);
         }
 
@@ -841,114 +840,4 @@ public abstract class AbstractRedirect extends 
GenericMailet {
                 || mail.getSender() == null
                 || 
!getMailetContext().getMailServers(mail.getSender().getDomain()).isEmpty();
     }
-
-    /**
-     * 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>,
-     * <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.RECIPIENTS</code> is made equivalent to
-     * <code>SpecialAddress.TO</code>.<br>
-     * <code>SpecialAddress.FROM</code> uses the From header if available,
-     * otherwise the Sender header if available, otherwise the return-path.<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.UNALTERED</code> is ignored.<br>
-     * Any other address is not replaced.<br>
-     */
-    protected Collection<InternetAddress> replaceInternetAddresses(Mail mail, 
Collection<InternetAddress> list) throws MessagingException {
-        ImmutableSet.Builder<InternetAddress> builder = ImmutableSet.builder();
-        for (InternetAddress internetAddress : list) {
-            MailAddress mailAddress = new MailAddress(internetAddress);
-            if (!SpecialAddress.isSpecialAddress(mailAddress)) {
-                builder.add(internetAddress);
-                continue;
-            }
-
-            SpecialAddressKind specialAddressKind = 
SpecialAddressKind.forValue(mailAddress.getLocalPart());
-            if (specialAddressKind == null) {
-                builder.add(mailAddress.toInternetAddress());
-                continue;
-            }
-
-            switch (specialAddressKind) {
-            case SENDER:
-                MailAddress sender = mail.getSender();
-                if (sender != null) {
-                    builder.add(sender.toInternetAddress());
-                }
-                break;
-            case REVERSE_PATH:
-                MailAddress reversePath = mail.getSender();
-                if (reversePath != null) {
-                    builder.add(reversePath.toInternetAddress());
-                }
-                break;
-            case FROM:
-                try {
-                    InternetAddress[] fromArray = (InternetAddress[]) 
mail.getMessage().getFrom();
-                    builder.addAll(allOrSender(mail, fromArray));
-                } catch (MessagingException me) {
-                    log("Unable to parse the \"FROM\" header in the original 
message; ignoring.");
-                }
-                break;
-            case REPLY_TO:
-                try {
-                    InternetAddress[] replyToArray = (InternetAddress[]) 
mail.getMessage().getReplyTo();
-                    builder.addAll(allOrSender(mail, replyToArray));
-                } catch (MessagingException me) {
-                    log("Unable to parse the \"REPLY_TO\" header in the 
original message; ignoring.");
-                }
-                break;
-            case TO:
-            case RECIPIENTS:
-                builder.addAll(toHeaders(mail));
-                break;
-            case NULL:
-            case UNALTERED:
-                break;
-            case DELETE:
-                builder.add(internetAddress);
-                break;
-            }
-        }
-        return builder.build();
-    }
-
-    private ImmutableSet<InternetAddress> allOrSender(Mail mail, 
InternetAddress[] addresses) {
-        if (addresses != null) {
-            return ImmutableSet.copyOf(addresses);
-        } else {
-            MailAddress reversePath = mail.getSender();
-            if (reversePath != null) {
-                return ImmutableSet.of(reversePath.toInternetAddress());
-            }
-        }
-        return ImmutableSet.of();
-    }
-
-    private ImmutableSet<InternetAddress> toHeaders(Mail mail) {
-        try {
-            String[] toHeaders = 
mail.getMessage().getHeader(RFC2822Headers.TO);
-            if (toHeaders != null) {
-                for (String toHeader : toHeaders) {
-                    try {
-                        InternetAddress[] originalToInternetAddresses = 
InternetAddress.parse(toHeader, false);
-                        return 
ImmutableSet.copyOf(originalToInternetAddresses);
-                    } catch (MessagingException ae) {
-                        log("Unable to parse a \"TO\" header address in the 
original message: " + toHeader + "; ignoring.");
-                    }
-                }
-            }
-            return ImmutableSet.of();
-        } catch (MessagingException ae) {
-            log("Unable to parse the \"TO\" header  in the original message; 
ignoring.");
-            return ImmutableSet.of();
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/b94bc281/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
index 6d8131a..0fd9c7d 100644
--- 
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
@@ -30,6 +30,7 @@ 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 org.apache.mailet.base.RFC2822Headers;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -142,4 +143,114 @@ public class SpecialAddressesUtils {
         }
         return builder.build();
     }
+
+    /**
+     * 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>,
+     * <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.RECIPIENTS</code> is made equivalent to
+     * <code>SpecialAddress.TO</code>.<br>
+     * <code>SpecialAddress.FROM</code> uses the From header if available,
+     * otherwise the Sender header if available, otherwise the return-path.<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.UNALTERED</code> is ignored.<br>
+     * Any other address is not replaced.<br>
+     */
+    public List<InternetAddress> replaceInternetAddresses(Mail 
mailWithReplacementAddresses, List<InternetAddress> internetAddresses) throws 
MessagingException {
+        ImmutableList.Builder<InternetAddress> builder = 
ImmutableList.builder();
+        for (InternetAddress internetAddress : internetAddresses) {
+            MailAddress mailAddress = new MailAddress(internetAddress);
+            if (!SpecialAddress.isSpecialAddress(mailAddress)) {
+                builder.add(internetAddress);
+                continue;
+            }
+
+            SpecialAddressKind specialAddressKind = 
SpecialAddressKind.forValue(mailAddress.getLocalPart());
+            if (specialAddressKind == null) {
+                builder.add(mailAddress.toInternetAddress());
+                continue;
+            }
+
+            switch (specialAddressKind) {
+            case SENDER:
+                MailAddress sender = mailWithReplacementAddresses.getSender();
+                if (sender != null) {
+                    builder.add(sender.toInternetAddress());
+                }
+                break;
+            case REVERSE_PATH:
+                MailAddress reversePath = 
mailWithReplacementAddresses.getSender();
+                if (reversePath != null) {
+                    builder.add(reversePath.toInternetAddress());
+                }
+                break;
+            case FROM:
+                try {
+                    InternetAddress[] fromArray = (InternetAddress[]) 
mailWithReplacementAddresses.getMessage().getFrom();
+                    builder.addAll(allOrSender(mailWithReplacementAddresses, 
fromArray));
+                } catch (MessagingException me) {
+                    genericMailet.log("Unable to parse the \"FROM\" header in 
the original message; ignoring.");
+                }
+                break;
+            case REPLY_TO:
+                try {
+                    InternetAddress[] replyToArray = (InternetAddress[]) 
mailWithReplacementAddresses.getMessage().getReplyTo();
+                    builder.addAll(allOrSender(mailWithReplacementAddresses, 
replyToArray));
+                } catch (MessagingException me) {
+                    genericMailet.log("Unable to parse the \"REPLY_TO\" header 
in the original message; ignoring.");
+                }
+                break;
+            case TO:
+            case RECIPIENTS:
+                builder.addAll(toHeaders(mailWithReplacementAddresses));
+                break;
+            case NULL:
+            case UNALTERED:
+                break;
+            case DELETE:
+                builder.add(internetAddress);
+                break;
+            }
+        }
+        return builder.build();
+    }
+
+    private ImmutableSet<InternetAddress> allOrSender(Mail mail, 
InternetAddress[] addresses) {
+        if (addresses != null) {
+            return ImmutableSet.copyOf(addresses);
+        } else {
+            MailAddress reversePath = mail.getSender();
+            if (reversePath != null) {
+                return ImmutableSet.of(reversePath.toInternetAddress());
+            }
+        }
+        return ImmutableSet.of();
+    }
+
+    private ImmutableSet<InternetAddress> toHeaders(Mail mail) {
+        try {
+            String[] toHeaders = 
mail.getMessage().getHeader(RFC2822Headers.TO);
+            if (toHeaders != null) {
+                for (String toHeader : toHeaders) {
+                    try {
+                        InternetAddress[] originalToInternetAddresses = 
InternetAddress.parse(toHeader, false);
+                        return 
ImmutableSet.copyOf(originalToInternetAddresses);
+                    } catch (MessagingException ae) {
+                        genericMailet.log("Unable to parse a \"TO\" header 
address in the original message: " + toHeader + "; ignoring.");
+                    }
+                }
+            }
+            return ImmutableSet.of();
+        } catch (MessagingException ae) {
+            genericMailet.log("Unable to parse the \"TO\" header  in the 
original message; ignoring.");
+            return ImmutableSet.of();
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/b94bc281/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
deleted file mode 100644
index c7069e4..0000000
--- 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/AbstractRedirectTest.java
+++ /dev/null
@@ -1,287 +0,0 @@
-/****************************************************************
- * 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.mailets.redirect;
-
-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.mailet.Mail;
-import org.apache.mailet.MailAddress;
-import org.apache.mailet.base.MailAddressFixture;
-import org.apache.mailet.base.RFC2822Headers;
-import org.apache.mailet.base.test.FakeMail;
-import org.junit.Before;
-import org.junit.Test;
-
-import com.google.common.collect.ImmutableList;
-
-public class AbstractRedirectTest {
-
-    private TesteeRedirect testee;
-
-    @Before
-    public void setup() {
-        testee = new TesteeRedirect();
-    }
-
-    private class TesteeRedirect extends AbstractRedirect {
-
-        @Override
-        protected String[] getAllowedInitParameters() {
-            return null;
-        }
-
-        @Override
-        protected InitParameters getInitParameters() {
-            return RedirectMailetInitParameters.from(this);
-        }
-
-        @Override
-        protected String getMessage(Mail originalMail) throws 
MessagingException {
-            return getInitParameters().getMessage();
-        }
-
-        @Override
-        protected InternetAddress[] getTo() throws MessagingException {
-            return null;
-        }
-
-        @Override
-        protected MailAddress getReplyTo() throws MessagingException {
-            return null;
-        }
-
-        @Override
-        protected MailAddress getReversePath(Mail originalMail) throws 
MessagingException {
-            return null;
-        }
-
-        @Override
-        protected void setSubjectPrefix(Mail newMail, String subjectPrefix, 
Mail originalMail) throws MessagingException {
-        }
-    }
-
-    @Test
-    public void replaceInternetAddressesShouldReturnEmptyWhenEmptyList() 
throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, ImmutableList.<InternetAddress> of());
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnSameContentWhenAddressesDoesntMatchAddressMarkerDomain()
 throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        InternetAddress internetAddress = new 
InternetAddress("user@addres.marker");
-        InternetAddress internetAddress2 = new 
InternetAddress("us...@address.mar");
-        ImmutableList<InternetAddress> list = 
ImmutableList.of(internetAddress, internetAddress2);
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, list);
-
-        assertThat(addresses).containsOnly(internetAddress, internetAddress2);
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchSender() throws 
Exception {
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .sender(sender)
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.SENDER.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(sender.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnFromWhenAddressesMatchFrom() throws 
Exception {
-        MailAddress from = MailAddressFixture.ANY_AT_JAMES;
-        MailAddress from2 = MailAddressFixture.OTHER_AT_JAMES;
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
-        message.addFrom(new InternetAddress[] { from.toInternetAddress(), 
from2.toInternetAddress() });
-        FakeMail mail = FakeMail.from(message);
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.FROM.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(from.toInternetAddress(), 
from2.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchFromAndNoFrom() 
throws Exception {
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .mimeMessage(new MimeMessage(Session.getDefaultInstance(new 
Properties())))
-                .sender(sender)
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.FROM.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(sender.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchSenderAndSenderIsNull()
 throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.SENDER.toInternetAddress()));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchReplyToAndReplyToIsNull()
 throws Exception {
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
-        FakeMail mail = FakeMail.from(message);
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REPLY_TO.toInternetAddress()));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnReplyToWhenAddressesMatchReplyTo() 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<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REPLY_TO.toInternetAddress()));
-
-        
assertThat(addresses).containsOnly(expectedReplyTo.toInternetAddress(), 
expectedReplyTo2.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchReplyToAndNoReplyTo()
 throws Exception {
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .sender(sender)
-                .mimeMessage(new MimeMessage(Session.getDefaultInstance(new 
Properties())))
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REPLY_TO.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(sender.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchReversePath() 
throws Exception {
-        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
-        FakeMail mail = FakeMail.builder()
-                .sender(sender)
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REVERSE_PATH.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(sender.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchReversePathAndNoSender()
 throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REVERSE_PATH.toInternetAddress()));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnToWhenAddressesMatchRecipients() throws 
Exception {
-        MailAddress to = MailAddressFixture.ANY_AT_JAMES;
-        MailAddress to2 = MailAddressFixture.OTHER_AT_JAMES;
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
-        message.addHeader(RFC2822Headers.TO, 
MailAddressFixture.ANY_AT_JAMES.toString() + ", " + 
MailAddressFixture.OTHER_AT_JAMES.toString());
-        FakeMail mail = FakeMail.from(message);
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.RECIPIENTS.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(to.toInternetAddress(), 
to2.toInternetAddress());
-    }
-
-    @Test
-    public void replaceInternetAddressesShouldReturnToWhenAddressesMatchTo() 
throws Exception {
-        MailAddress to = MailAddressFixture.ANY_AT_JAMES;
-        MailAddress to2 = MailAddressFixture.OTHER_AT_JAMES;
-        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
-        message.addHeader(RFC2822Headers.TO, 
MailAddressFixture.ANY_AT_JAMES.toString() + ", " + 
MailAddressFixture.OTHER_AT_JAMES);
-        FakeMail mail = FakeMail.from(message);
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.TO.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(to.toInternetAddress(), 
to2.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchUnaltered() throws 
Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.UNALTERED.toInternetAddress()));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchNull() throws 
Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.NULL.toInternetAddress()));
-
-        assertThat(addresses).isEmpty();
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnSameAddressWhenAddressesDoesntMatch() 
throws Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        MailAddress address = new MailAddress("user", "address.marker");
-        MailAddress address2 = new MailAddress("user2", "address.marker");
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(address.toInternetAddress(), address2.toInternetAddress()));
-
-        assertThat(addresses).containsOnly(address.toInternetAddress(), 
address2.toInternetAddress());
-    }
-
-    @Test
-    public void 
replaceInternetAddressesShouldReturnSameListWhenAddressesMatchDelete() throws 
Exception {
-        FakeMail mail = FakeMail.builder()
-                .build();
-
-        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.DELETE.toInternetAddress()));
-
-        InternetAddress expected = new 
InternetAddress("delete@address.marker");
-        assertThat(addresses).containsOnly(expected);
-    }
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/b94bc281/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
index b55dd46..a68dd8f 100644
--- 
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
@@ -33,6 +33,7 @@ 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.RFC2822Headers;
 import org.apache.mailet.base.test.FakeMail;
 import org.junit.Before;
 import org.junit.Test;
@@ -238,4 +239,204 @@ public class SpecialAddressesUtilsTest {
         MailAddress expected = new MailAddress("delete", "address.marker");
         assertThat(addresses).containsOnly(expected);
     }
+
+    @Test
+    public void replaceInternetAddressesShouldReturnEmptyWhenEmptyList() 
throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, ImmutableList.<InternetAddress> of());
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnSameContentWhenAddressesDoesntMatchAddressMarkerDomain()
 throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        InternetAddress internetAddress = new 
InternetAddress("user@addres.marker");
+        InternetAddress internetAddress2 = new 
InternetAddress("us...@address.mar");
+        ImmutableList<InternetAddress> list = 
ImmutableList.of(internetAddress, internetAddress2);
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, list);
+
+        assertThat(addresses).containsOnly(internetAddress, internetAddress2);
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchSender() throws 
Exception {
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .sender(sender)
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.SENDER.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(sender.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnFromWhenAddressesMatchFrom() throws 
Exception {
+        MailAddress from = MailAddressFixture.ANY_AT_JAMES;
+        MailAddress from2 = MailAddressFixture.OTHER_AT_JAMES;
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
+        message.addFrom(new InternetAddress[] { from.toInternetAddress(), 
from2.toInternetAddress() });
+        FakeMail mail = FakeMail.from(message);
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.FROM.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(from.toInternetAddress(), 
from2.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchFromAndNoFrom() 
throws Exception {
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .mimeMessage(new MimeMessage(Session.getDefaultInstance(new 
Properties())))
+                .sender(sender)
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.FROM.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(sender.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchSenderAndSenderIsNull()
 throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.SENDER.toInternetAddress()));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchReplyToAndReplyToIsNull()
 throws Exception {
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
+        FakeMail mail = FakeMail.from(message);
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REPLY_TO.toInternetAddress()));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnReplyToWhenAddressesMatchReplyTo() 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<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REPLY_TO.toInternetAddress()));
+
+        
assertThat(addresses).containsOnly(expectedReplyTo.toInternetAddress(), 
expectedReplyTo2.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchReplyToAndNoReplyTo()
 throws Exception {
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .sender(sender)
+                .mimeMessage(new MimeMessage(Session.getDefaultInstance(new 
Properties())))
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REPLY_TO.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(sender.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnSenderWhenAddressesMatchReversePath() 
throws Exception {
+        MailAddress sender = MailAddressFixture.ANY_AT_JAMES;
+        FakeMail mail = FakeMail.builder()
+                .sender(sender)
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REVERSE_PATH.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(sender.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchReversePathAndNoSender()
 throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.REVERSE_PATH.toInternetAddress()));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnToWhenAddressesMatchRecipients() throws 
Exception {
+        MailAddress to = MailAddressFixture.ANY_AT_JAMES;
+        MailAddress to2 = MailAddressFixture.OTHER_AT_JAMES;
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
+        message.addHeader(RFC2822Headers.TO, 
MailAddressFixture.ANY_AT_JAMES.toString() + ", " + 
MailAddressFixture.OTHER_AT_JAMES.toString());
+        FakeMail mail = FakeMail.from(message);
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.RECIPIENTS.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(to.toInternetAddress(), 
to2.toInternetAddress());
+    }
+
+    @Test
+    public void replaceInternetAddressesShouldReturnToWhenAddressesMatchTo() 
throws Exception {
+        MailAddress to = MailAddressFixture.ANY_AT_JAMES;
+        MailAddress to2 = MailAddressFixture.OTHER_AT_JAMES;
+        MimeMessage message = new MimeMessage(Session.getDefaultInstance(new 
Properties()));
+        message.addHeader(RFC2822Headers.TO, 
MailAddressFixture.ANY_AT_JAMES.toString() + ", " + 
MailAddressFixture.OTHER_AT_JAMES);
+        FakeMail mail = FakeMail.from(message);
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.TO.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(to.toInternetAddress(), 
to2.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchUnaltered() throws 
Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.UNALTERED.toInternetAddress()));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchNull() throws 
Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.NULL.toInternetAddress()));
+
+        assertThat(addresses).isEmpty();
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnSameAddressWhenAddressesDoesntMatch() 
throws Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        MailAddress address = new MailAddress("user", "address.marker");
+        MailAddress address2 = new MailAddress("user2", "address.marker");
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(address.toInternetAddress(), address2.toInternetAddress()));
+
+        assertThat(addresses).containsOnly(address.toInternetAddress(), 
address2.toInternetAddress());
+    }
+
+    @Test
+    public void 
replaceInternetAddressesShouldReturnSameListWhenAddressesMatchDelete() throws 
Exception {
+        FakeMail mail = FakeMail.builder()
+                .build();
+
+        Collection<InternetAddress> addresses = 
testee.replaceInternetAddresses(mail, 
ImmutableList.of(SpecialAddress.DELETE.toInternetAddress()));
+
+        InternetAddress expected = new 
InternetAddress("delete@address.marker");
+        assertThat(addresses).containsOnly(expected);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to