james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject james-project git commit: JAMES-1727 Redirected email should have no attribute
Date Thu, 12 May 2016 04:28:26 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 0cb80385a -> def919909


JAMES-1727 Redirected email should have no attribute


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

Branch: refs/heads/master
Commit: def9199099c90e1d28fe0deed0b73f40f3f6c3f0
Parents: 0cb8038
Author: Benoit Tellier <btellier@linagora.com>
Authored: Wed May 4 16:45:55 2016 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Thu May 12 11:27:40 2016 +0700

----------------------------------------------------------------------
 mailet/base/pom.xml                             |  5 ++
 .../mailet/base/test/FakeMailContext.java       | 73 ++++++++++------
 mailet/pom.xml                                  |  5 ++
 .../transport/mailets/AbstractRedirect.java     |  1 +
 .../james/transport/mailets/BounceTest.java     | 87 ++++++++++++++++++++
 5 files changed, 145 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/def91990/mailet/base/pom.xml
----------------------------------------------------------------------
diff --git a/mailet/base/pom.xml b/mailet/base/pom.xml
index 5df80e7..ca3dd2e 100644
--- a/mailet/base/pom.xml
+++ b/mailet/base/pom.xml
@@ -57,6 +57,11 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/james-project/blob/def91990/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
index 40a7ded..19cc53f 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java
@@ -19,22 +19,28 @@
 
 package org.apache.mailet.base.test;
 
-import org.apache.mailet.HostAddress;
-import org.apache.mailet.LookupException;
-import org.apache.mailet.MailetContext;
-import org.apache.mailet.Mail;
-import org.apache.mailet.MailAddress;
-import org.apache.mailet.TemporaryLookupException;
-
-import javax.mail.MessagingException;
-import javax.mail.internet.MimeMessage;
-
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.mailet.HostAddress;
+import org.apache.mailet.LookupException;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.MailetContext;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
 
 @SuppressWarnings("deprecation")
 public class FakeMailContext implements MailetContext {
@@ -44,11 +50,17 @@ public class FakeMailContext implements MailetContext {
         private final MailAddress sender;
         private final Collection<MailAddress> recipients;
         private final MimeMessage msg;
+        private final Map<String, Serializable> attributes;
 
-        public SentMail(MailAddress sender, Collection<MailAddress> recipients, MimeMessage
msg) {
+        public SentMail(MailAddress sender, Collection<MailAddress> recipients, MimeMessage
msg, Map<String, Serializable> attributes) {
             this.sender = sender;
-            this.recipients = recipients;
+            this.recipients = ImmutableList.copyOf(recipients);
             this.msg = msg;
+            this.attributes = ImmutableMap.copyOf(attributes);
+        }
+
+        public SentMail(MailAddress sender, Collection<MailAddress> recipients, MimeMessage
msg) {
+            this(sender, recipients, msg, ImmutableMap.<String, Serializable>of());
         }
 
         public MailAddress getSender() {
@@ -65,29 +77,29 @@ public class FakeMailContext implements MailetContext {
 
         @Override
         public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
+            if (!(o instanceof SentMail)) {
+                return false;
+            }
 
             SentMail sentMail = (SentMail) o;
 
-            if (sender != null ? !sender.equals(sentMail.sender) : sentMail.sender != null)
return false;
-            return !(recipients != null ? !recipients.equals(sentMail.recipients) : sentMail.recipients
!= null);
-
+            return Objects.equal(this.sender, sentMail.sender)
+                && Objects.equal(this.recipients, sentMail.recipients)
+                && Objects.equal(this.attributes, sentMail.attributes);
         }
 
         @Override
         public int hashCode() {
-            int result = sender != null ? sender.hashCode() : 0;
-            result = 31 * result + (recipients != null ? recipients.hashCode() : 0);
-            return result;
+            return Objects.hashCode(sender, recipients, attributes);
         }
 
         @Override
         public String toString() {
-            return "SentMail{" +
-                "recipients=" + recipients +
-                ", sender=" + sender +
-                '}';
+            return MoreObjects.toStringHelper(this)
+                .add("recipients", recipients)
+                .add("sender", sender)
+                .add("attributeNames", attributes)
+                .toString();
         }
     }
     
@@ -181,7 +193,16 @@ public class FakeMailContext implements MailetContext {
     }
 
     public void sendMail(Mail mail) throws MessagingException {
-        sentMails.add(new SentMail(mail.getSender(), mail.getRecipients(), mail.getMessage()));
+        sentMails.add(new SentMail(mail.getSender(), mail.getRecipients(), mail.getMessage(),
buildAttributesMap(mail)));
+    }
+
+    private ImmutableMap<String, Serializable> buildAttributesMap(Mail mail) {
+        Map<String, Serializable> result = new HashMap<String, Serializable>();
+        List<String> attributesNames = Lists.newArrayList(mail.getAttributeNames());
+        for (String attributeName: attributesNames) {
+            result.put(attributeName, mail.getAttribute(attributeName));
+        }
+        return ImmutableMap.copyOf(result);
     }
 
     public void setAttribute(String name, Serializable object) {
@@ -212,7 +233,7 @@ public class FakeMailContext implements MailetContext {
         t.printStackTrace(System.out);
     }
 
-    public List<String> dnsLookup(String name, RecordType type) throws TemporaryLookupException,
LookupException {
+    public List<String> dnsLookup(String name, RecordType type) throws LookupException
{
         return null;   // trivial implementation
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/def91990/mailet/pom.xml
----------------------------------------------------------------------
diff --git a/mailet/pom.xml b/mailet/pom.xml
index f2bb2fd..8cec493 100644
--- a/mailet/pom.xml
+++ b/mailet/pom.xml
@@ -139,6 +139,11 @@
                 <scope>test</scope>
                 <version>${junit.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.google.guava</groupId>
+                <artifactId>guava</artifactId>
+                <version>18.0</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/def91990/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRedirect.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRedirect.java
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRedirect.java
index 7289edb..8c97c57 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRedirect.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRedirect.java
@@ -1025,6 +1025,7 @@ public abstract class AbstractRedirect extends GenericMailet {
             setIsReply(newMail, isReply(originalMail), originalMail);
 
             newMail.getMessage().saveChanges();
+            newMail.removeAllAttributes();
 
             if (keepMessageId) {
                 setMessageId(newMail, originalMail);

http://git-wip-us.apache.org/repos/asf/james-project/blob/def91990/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
----------------------------------------------------------------------
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
new file mode 100644
index 0000000..5ffa43b
--- /dev/null
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/BounceTest.java
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.Serializable;
+import java.net.UnknownHostException;
+import java.util.Collections;
+import java.util.Properties;
+
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+
+public class BounceTest {
+
+    private static final String MAILET_NAME = "mailetName";
+
+    private Bounce bounce;
+    private MailAddress recipientMailAddress;
+    private MailAddress senderMailAddress;
+    private FakeMailContext fakeMailContext;
+
+    @Before
+    public void setUp() throws Exception {
+        bounce = new Bounce();
+        DNSService dnsService = mock(DNSService.class);
+        bounce.setDNSService(dnsService);
+        fakeMailContext = new FakeMailContext();
+
+        when(dnsService.getLocalHost()).thenThrow(new UnknownHostException());
+        bounce.init(new FakeMailetConfig(MAILET_NAME, fakeMailContext));
+
+        senderMailAddress = new MailAddress("sender@domain.com");
+        recipientMailAddress = new MailAddress("recipient@domain.com");
+    }
+
+    @Test
+    public void bounceShouldReturnAMailToTheSenderWithoutAttributes() throws Exception {
+        FakeMail mail = new FakeMail();
+        mail.setSender(senderMailAddress);
+        mail.setName(MAILET_NAME);
+        mail.setRecipients(Lists.newArrayList(recipientMailAddress));
+        MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.setText("My content");
+        mail.setMessage(mimeMessage);
+
+        bounce.service(mail);
+
+        FakeMailContext.SentMail expected = new FakeMailContext.SentMail(null,
+            Lists.newArrayList(senderMailAddress),
+            null,
+            ImmutableMap.<String, Serializable>of());
+        assertThat(fakeMailContext.getSentMails()).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