james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [24/50] [abbrv] james-project git commit: MAILET-115 Remove abstract getRecipients with original mail implementation
Date Wed, 11 Jan 2017 09:26:12 GMT
MAILET-115 Remove abstract getRecipients with original mail implementation


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

Branch: refs/heads/master
Commit: fffe05750020242aa3df7dbbe8d44828e6fa28c8
Parents: 6f52c81
Author: Antoine Duprat <aduprat@apache.org>
Authored: Tue Nov 15 12:06:37 2016 +0100
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Jan 11 10:03:30 2017 +0700

----------------------------------------------------------------------
 .../apache/james/transport/mailets/Bounce.java  |   8 +-
 .../james/transport/mailets/DSNBounce.java      |   8 +-
 .../apache/james/transport/mailets/Forward.java |   8 +-
 .../transport/mailets/NotifyPostmaster.java     |   8 +-
 .../james/transport/mailets/NotifySender.java   |   8 +-
 .../james/transport/mailets/Redirect.java       |  10 +-
 .../apache/james/transport/mailets/Resend.java  |   8 +-
 .../mailets/redirect/AbstractRedirect.java      |  23 +---
 .../james/transport/util/RecipientsUtils.java   |  59 +++++++++
 .../james/transport/mailets/RedirectTest.java   |   5 +-
 .../transport/util/RecipientsUtilsTest.java     | 122 +++++++++++++++++++
 11 files changed, 237 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
index b5e7ca7..dec9c6f 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Bounce.java
@@ -30,6 +30,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
+import org.apache.james.transport.util.RecipientsUtils;
 import org.apache.james.transport.util.SpecialAddressesUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -138,11 +139,16 @@ public class Bounce extends AbstractRedirect {
     }
 
     @Override
-    protected List<MailAddress> getRecipients() {
+    public List<MailAddress> getRecipients() {
         return RECIPIENTS;
     }
 
     @Override
+    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        return RecipientsUtils.from(this).getRecipients(originalMail);
+    }
+
+    @Override
     protected List<InternetAddress> getTo() {
         return TO;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
index 6e7197e..6d64570 100755
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/DSNBounce.java
@@ -40,6 +40,7 @@ import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.redirect.TypeCode;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
 import org.apache.james.transport.util.Patterns;
+import org.apache.james.transport.util.RecipientsUtils;
 import org.apache.james.transport.util.SpecialAddressesUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -130,11 +131,16 @@ public class DSNBounce extends AbstractRedirect {
     }
 
     @Override
-    protected List<MailAddress> getRecipients() {
+    public List<MailAddress> getRecipients() {
         return RECIPIENT_MAIL_ADDRESSES;
     }
 
     @Override
+    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        return RecipientsUtils.from(this).getRecipients(originalMail);
+    }
+
+    @Override
     protected List<InternetAddress> getTo() {
         return TO_INTERNET_ADDRESSES;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
index a3fd04d..c873710 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Forward.java
@@ -31,6 +31,7 @@ import org.apache.james.transport.mailets.redirect.InitParameters;
 import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.TypeCode;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
+import org.apache.james.transport.util.RecipientsUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 
@@ -110,7 +111,7 @@ public class Forward extends AbstractRedirect {
     }
 
     @Override
-    protected List<MailAddress> getRecipients() throws MessagingException {
+    public List<MailAddress> getRecipients() throws MessagingException {
         ImmutableList.Builder<MailAddress> builder = ImmutableList.builder();
         for (InternetAddress address : extractAddresses(getForwardTo())) {
             builder.add(toMailAddress(address));
@@ -118,6 +119,11 @@ public class Forward extends AbstractRedirect {
         return builder.build();
     }
 
+    @Override
+    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        return RecipientsUtils.from(this).getRecipients(originalMail);
+    }
+
     private InternetAddress[] extractAddresses(String addressList) throws MessagingException
{
         try {
             return InternetAddress.parse(addressList, false);

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
index 14144fc..d38212a 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifyPostmaster.java
@@ -31,6 +31,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
+import org.apache.james.transport.util.RecipientsUtils;
 import org.apache.james.transport.util.SpecialAddressesUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -146,11 +147,16 @@ public class NotifyPostmaster extends AbstractRedirect {
     }
 
     @Override
-    protected List<MailAddress> getRecipients() {
+    public List<MailAddress> getRecipients() {
         return ImmutableList.of(getMailetContext().getPostmaster());
     }
 
     @Override
+    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        return RecipientsUtils.from(this).getRecipients(originalMail);
+    }
+
+    @Override
     protected List<InternetAddress> getTo() throws MessagingException {
         if (to.isPresent()) {
             Optional<MailAddress> specialAddress = AddressExtractor.withContext(getMailetContext())

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
index 2d35948..e72417c 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/NotifySender.java
@@ -31,6 +31,7 @@ import org.apache.james.transport.mailets.redirect.NotifyMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.NotifyMailetsMessage;
 import org.apache.james.transport.mailets.redirect.SpecialAddress;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
+import org.apache.james.transport.util.RecipientsUtils;
 import org.apache.james.transport.util.SpecialAddressesUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -146,11 +147,16 @@ public class NotifySender extends AbstractRedirect {
     }
 
     @Override
-    protected List<MailAddress> getRecipients() {
+    public List<MailAddress> getRecipients() {
         return RECIPIENT_MAIL_ADDRESSES;
     }
 
     @Override
+    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        return RecipientsUtils.from(this).getRecipients(originalMail);
+    }
+
+    @Override
     protected List<InternetAddress> getTo() throws MessagingException {
         if (to.isPresent()) {
             Optional<MailAddress> specialAddress = AddressExtractor.withContext(getMailetContext())

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
index 22fddc6..ce5e85d 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Redirect.java
@@ -31,6 +31,7 @@ import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters;
 import org.apache.james.transport.mailets.redirect.TypeCode;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
 import org.apache.james.transport.util.MailAddressUtils;
+import org.apache.james.transport.util.RecipientsUtils;
 import org.apache.james.transport.util.SpecialAddressesUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -318,10 +319,10 @@ public class Redirect extends AbstractRedirect {
     }
 
     @Override
-    protected List<MailAddress> getRecipients() throws MessagingException {
+    public List<MailAddress> getRecipients() throws MessagingException {
         String recipientsOrTo = getRecipientsOrTo();
         if (recipientsOrTo == null) {
-            return null;
+            return ImmutableList.of();
         }
         if (recipientsOrTo.isEmpty()) {
             throw new MessagingException("Failed to initialize \"recipients\" list; empty
<recipients> init parameter found.");
@@ -341,6 +342,11 @@ public class Redirect extends AbstractRedirect {
     }
 
     @Override
+    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        return RecipientsUtils.from(this).getRecipients(originalMail);
+    }
+
+    @Override
     protected List<InternetAddress> getTo() throws MessagingException {
         String toOrRecipients = getToOrRecipients();
         if (toOrRecipients == null) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
index 9fd24ff..a0893a6 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Resend.java
@@ -30,6 +30,7 @@ import org.apache.james.transport.mailets.redirect.InitParameters;
 import org.apache.james.transport.mailets.redirect.RedirectMailetInitParameters;
 import org.apache.james.transport.mailets.utils.MimeMessageModifier;
 import org.apache.james.transport.util.MailAddressUtils;
+import org.apache.james.transport.util.RecipientsUtils;
 import org.apache.james.transport.util.SpecialAddressesUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -334,7 +335,7 @@ public class Resend extends AbstractRedirect {
     }
 
     @Override
-    protected List<MailAddress> getRecipients() throws MessagingException {
+    public List<MailAddress> getRecipients() throws MessagingException {
           ImmutableList.Builder<MailAddress> builder = ImmutableList.builder();
           List<MailAddress> mailAddresses = AddressExtractor.withContext(getMailetContext())
                   .allowedSpecials(ImmutableList.of("postmaster", "sender", "from", "replyTo",
"reversePath", "unaltered", "recipients", "to", "null"))
@@ -346,6 +347,11 @@ public class Resend extends AbstractRedirect {
       }
 
     @Override
+    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        return RecipientsUtils.from(this).getRecipients(originalMail);
+    }
+
+    @Override
     protected MailAddress getReversePath() throws MessagingException {
         return SpecialAddressesUtils.from(this)
                 .getFirstSpecialAddressIfMatchingOrGivenAddress(getInitParameters().getReversePath(),
AbstractRedirect.REVERSE_PATH_ALLOWED_SPECIALS);

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/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 8a12293..5a2cf43 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
@@ -21,7 +21,6 @@ package org.apache.james.transport.mailets.redirect;
 
 import java.io.ByteArrayOutputStream;
 import java.net.UnknownHostException;
-import java.util.Collection;
 import java.util.Date;
 import java.util.Enumeration;
 import java.util.List;
@@ -188,7 +187,7 @@ public abstract class AbstractRedirect extends GenericMailet {
      *         <code>SpecialAddress.RECIPIENTS</code> or <code>null</code>
if
      *         missing
      */
-    protected abstract List<MailAddress> getRecipients() throws MessagingException;

+    public abstract List<MailAddress> getRecipients() throws MessagingException; 
 
     /**
      * Gets the <code>recipients</code> property, built dynamically using the
@@ -196,28 +195,14 @@ public abstract class AbstractRedirect extends GenericMailet {
      *
      * @return {@link #replaceMailAddresses} on {@link #getRecipients()},
      */
-    protected List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
-        List<MailAddress> recipients = getRecipients();
-        if (recipients != null) {
-            if (containsOnlyUnalteredOrRecipients(recipients)) {
-                return null;
-            }
-            return SpecialAddressesUtils.from(this).replaceSpecialAddresses(originalMail,
recipients);
-        }
-        return null;
-    }
-
-    private boolean containsOnlyUnalteredOrRecipients(Collection<MailAddress> recipients)
{
-        return recipients.size() == 1 && 
-                (recipients.contains(SpecialAddress.UNALTERED) || recipients.contains(SpecialAddress.RECIPIENTS));
-    }
+    protected abstract List<MailAddress> getRecipients(Mail originalMail) throws MessagingException;

 
     /**
      * Sets the recipients of <i>newMail</i> to <i>recipients</i>.
If the
      * requested value is null does nothing. Is a "setX(Mail, Tx, Mail)" method.
      */
-    protected void setRecipients(Mail newMail, Collection<MailAddress> recipients,
Mail originalMail) {
-        if (recipients != null) {
+    protected void setRecipients(Mail newMail, List<MailAddress> recipients, Mail originalMail)
{
+        if (!recipients.isEmpty()) {
             newMail.setRecipients(recipients);
             if (getInitParameters().isDebug()) {
                 log("recipients set to: " + arrayToString(recipients.toArray()));

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/main/java/org/apache/james/transport/util/RecipientsUtils.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/util/RecipientsUtils.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/RecipientsUtils.java
new file mode 100644
index 0000000..f99ae25
--- /dev/null
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/util/RecipientsUtils.java
@@ -0,0 +1,59 @@
+/****************************************************************
+ * 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.List;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.SpecialAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+
+import com.google.common.collect.ImmutableList;
+
+public class RecipientsUtils {
+
+    public static RecipientsUtils from(AbstractRedirect mailet) {
+        return new RecipientsUtils(mailet);
+    }
+
+    private final AbstractRedirect mailet;
+
+    private RecipientsUtils(AbstractRedirect mailet) {
+        this.mailet = mailet;
+    }
+
+    public List<MailAddress> getRecipients(Mail originalMail) throws MessagingException
{
+        List<MailAddress> recipients = mailet.getRecipients();
+        if (!recipients.isEmpty()) {
+            if (containsOnlyUnalteredOrRecipients(recipients)) {
+                return ImmutableList.of();
+            }
+            return SpecialAddressesUtils.from(mailet).replaceSpecialAddresses(originalMail,
recipients);
+        }
+        return ImmutableList.of();
+    }
+
+    private boolean containsOnlyUnalteredOrRecipients(List<MailAddress> recipients)
{
+        return recipients.size() == 1 && 
+                (recipients.contains(SpecialAddress.UNALTERED) || recipients.contains(SpecialAddress.RECIPIENTS));
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
index cce7db6..152e59e 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/RedirectTest.java
@@ -31,7 +31,6 @@ import javax.mail.Session;
 import javax.mail.internet.MimeMessage;
 
 import org.apache.james.dnsservice.api.DNSService;
-import org.apache.james.transport.mailets.redirect.TypeCode;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
@@ -104,11 +103,11 @@ public class RedirectTest {
     }
 
     @Test
-    public void initShouldReturnNullWhenNoRecipientsOrToParameters() throws Exception {
+    public void initShouldReturnEmptyWhenNoRecipientsOrToParameters() throws Exception {
         FakeMailetConfig mailetConfig = new FakeMailetConfig(MAILET_NAME, fakeMailContext);
         redirect.init(mailetConfig);
 
-        assertThat(redirect.getRecipients()).isNull();
+        assertThat(redirect.getRecipients()).isEmpty();
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/fffe0575/server/mailet/mailets/src/test/java/org/apache/james/transport/util/RecipientsUtilsTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/util/RecipientsUtilsTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/RecipientsUtilsTest.java
new file mode 100644
index 0000000..68080d1
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/util/RecipientsUtilsTest.java
@@ -0,0 +1,122 @@
+/****************************************************************
+ * 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 static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Properties;
+
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMessage.RecipientType;
+
+import org.apache.james.transport.mailets.redirect.AbstractRedirect;
+import org.apache.james.transport.mailets.redirect.SpecialAddress;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.test.FakeMail;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+public class RecipientsUtilsTest {
+
+    private AbstractRedirect mailet;
+    private RecipientsUtils testee;
+
+    @Before
+    public void setup() {
+        mailet = mock(AbstractRedirect.class);
+        testee = RecipientsUtils.from(mailet);
+    }
+
+    @Test
+    public void getRecipientsShouldReturnEmptyWhenMailetRecipientsIsEmpty() throws Exception
{
+        when(mailet.getRecipients())
+            .thenReturn(ImmutableList.<MailAddress> of());
+
+        FakeMail fakeMail = FakeMail.defaultFakeMail();
+
+        List<MailAddress> recipients = testee.getRecipients(fakeMail);
+
+        assertThat(recipients).isEmpty();
+    }
+
+    @Test
+    public void getRecipientsShouldReturnEmptyWhenMailetRecipientsContainsOnlyUnaltered()
throws Exception {
+        when(mailet.getRecipients())
+            .thenReturn(ImmutableList.of(SpecialAddress.UNALTERED));
+
+        FakeMail fakeMail = FakeMail.defaultFakeMail();
+
+        List<MailAddress> recipients = testee.getRecipients(fakeMail);
+
+        assertThat(recipients).isEmpty();
+    }
+
+    @Test
+    public void getRecipientsShouldReturnEmptyWhenMailetRecipientsContainsOnlyRecipients()
throws Exception {
+        when(mailet.getRecipients())
+            .thenReturn(ImmutableList.of(SpecialAddress.RECIPIENTS));
+
+        FakeMail fakeMail = FakeMail.defaultFakeMail();
+
+        List<MailAddress> recipients = testee.getRecipients(fakeMail);
+
+        assertThat(recipients).isEmpty();
+    }
+
+    @Test
+    public void getRecipientsShouldReturnRecipientsWhenMailetRecipientsAreCommon() throws
Exception {
+        ImmutableList<MailAddress> expectedRecipients = ImmutableList.of(new MailAddress("test",
"james.org"), new MailAddress("test2", "james.org"));
+        when(mailet.getRecipients())
+            .thenReturn(expectedRecipients);
+
+        FakeMail fakeMail = FakeMail.defaultFakeMail();
+
+        List<MailAddress> recipients = testee.getRecipients(fakeMail);
+
+        assertThat(recipients).containsOnlyElementsOf(expectedRecipients);
+    }
+
+    @Test
+    public void getRecipientsShouldReturnAddressesFromOriginalMailWhenMailetRecipientsAreSpecialAddresses()
throws Exception {
+        when(mailet.getRecipients())
+            .thenReturn(ImmutableList.of(SpecialAddress.FROM, SpecialAddress.TO));
+
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setRecipients(RecipientType.TO, "to@james.org, to2@james.org");
+        MailAddress from = new MailAddress("from", "james.org");
+        MailAddress to = new MailAddress("to", "james.org");
+        MailAddress to2 = new MailAddress("to2", "james.org");
+        FakeMail fakeMail = FakeMail.builder()
+                .sender(from)
+                .recipients(to, to2)
+                .mimeMessage(mimeMessage)
+                .build();
+
+        List<MailAddress> recipients = testee.getRecipients(fakeMail);
+
+        ImmutableList<MailAddress> expectedRecipients = ImmutableList.of(from, to,
to2);
+        assertThat(recipients).containsOnlyElementsOf(expectedRecipients);
+    }
+}


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