Author: eric
Date: Wed Apr 30 03:43:39 2014
New Revision: 1591185

URL: http://svn.apache.org/r1591185
Log:
Empty mails do not get a received header attached when using smtpserver, patch 
contributed by Mark (JAMES-1547)

Added:
    james/server/trunk/protocols/protocols-smtp/src/test/resources/log4j.xml
Modified:
    james/server/trunk/protocols/protocols-smtp/pom.xml
    
james/server/trunk/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
    james/server/trunk/queue/queue-api/pom.xml
    
james/server/trunk/queue/queue-api/src/test/java/org/apache/james/queue/api/mock/MockMailQueue.java

Modified: james/server/trunk/protocols/protocols-smtp/pom.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/protocols/protocols-smtp/pom.xml?rev=1591185&r1=1591184&r2=1591185&view=diff
==============================================================================
--- james/server/trunk/protocols/protocols-smtp/pom.xml (original)
+++ james/server/trunk/protocols/protocols-smtp/pom.xml Wed Apr 30 03:43:39 2014
@@ -202,6 +202,17 @@
             <artifactId>commons-codec</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-log4j12</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

Modified: 
james/server/trunk/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java?rev=1591185&r1=1591184&r2=1591185&view=diff
==============================================================================
--- 
james/server/trunk/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
 (original)
+++ 
james/server/trunk/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/SMTPServerTest.java
 Wed Apr 30 03:43:39 2014
@@ -18,13 +18,6 @@
  ****************************************************************/
 package org.apache.james.smtpserver;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -39,10 +32,10 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
-
+import org.apache.commons.net.ProtocolCommandEvent;
+import org.apache.commons.net.ProtocolCommandListener;
 import org.apache.commons.net.smtp.SMTPClient;
 import org.apache.commons.net.smtp.SMTPReply;
 import org.apache.james.dnsservice.api.DNSService;
@@ -62,7 +55,14 @@ import org.apache.mailet.HostAddress;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.junit.After;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -154,6 +154,8 @@ public class SMTPServerTest {
         }
     }
 
+    private static final Logger log = 
LoggerFactory.getLogger(SMTPServerTest.class.getName());
+
     protected final int smtpListenerPort;
     
     protected SMTPTestConfiguration smtpConfiguration;
@@ -460,42 +462,71 @@ public class SMTPServerTest {
 
     }
 
-    /**
-     * TODO: Understand why this fails!
-     *
-     * public void testEmptyMessage() throws Exception {
-     * finishSetUp(m_testConfiguration);
-     *
-     * SMTPClient smtp = new SMTPClient(); smtp.connect("127.0.0.1",
-     * m_smtpListenerPort);
-     *
-     * // no message there, yet assertNull("no mail received by mail server",
-     * m_mailServer.getLastMail());
-     *
-     * smtp.helo(InetAddress.getLocalHost().toString());
-     *
-     * smtp.setSender("mail@localhost");
-     *
-     * smtp.addRecipient("mail@localhost");
-     *
-     * smtp.sendShortMessageData("");
-     *
-     * smtp.quit();
-     *
-     * smtp.disconnect();
-     *
-     * // mail was propagated by SMTPServer
-     * assertNotNull("mail received by mail server",
-     * m_mailServer.getLastMail());
-     *
-     * // added to check a NPE in the test (JAMES-474) due to MockMailServer //
-     * not cloning the message (added a MimeMessageCopyOnWriteProxy there)
-     * System.gc();
-     *
-     * int size = queue.getLastMail().getMessage().getSize();
-     *
-     * assertEquals(size, 2); }
-     */
+    protected SMTPClient newSMTPClient() throws IOException {
+        SMTPClient smtp = new SMTPClient();
+        smtp.connect("127.0.0.1", smtpListenerPort);
+        if (log.isDebugEnabled()) {
+            smtp.addProtocolCommandListener(new ProtocolCommandListener() {
+
+                @Override
+                public void protocolCommandSent(ProtocolCommandEvent event) {
+                    log.debug("> " + event.getMessage().trim());
+                }
+
+                @Override
+                public void protocolReplyReceived(ProtocolCommandEvent event) {
+                    log.debug("< " + event.getMessage().trim());
+                }
+            });
+        }
+        return smtp;
+    }
+
+    @Test
+    public void testReceivedHeader() throws Exception {
+        init(smtpConfiguration);
+
+        SMTPClient smtp = newSMTPClient();
+
+        // no message there, yet
+        assertNull("no mail received by mail server", queue.getLastMail());
+
+        smtp.helo(InetAddress.getLocalHost().toString());
+        smtp.setSender("mail@localhost");
+        smtp.addRecipient("mail@localhost");
+        smtp.sendShortMessageData("Subject: test\r\n\r\n");
+
+        smtp.quit();
+        smtp.disconnect();
+
+        assertNotNull("spooled mail has Received header",
+                queue.getLastMail().getMessage().getHeader("Received"));
+    }
+
+    // FIXME
+    @Ignore
+    @Test
+    public void testEmptyMessageReceivedHeader() throws Exception {
+        init(smtpConfiguration);
+
+        SMTPClient smtp = newSMTPClient();
+
+        // no message there, yet
+        assertNull("no mail received by mail server", queue.getLastMail());
+
+        smtp.helo(InetAddress.getLocalHost().toString());
+        smtp.setSender("mail@localhost");
+        smtp.addRecipient("mail@localhost");
+        smtp.sendShortMessageData("");
+
+        smtp.quit();
+        smtp.disconnect();
+
+        assertNotNull("spooled mail has Received header",
+                queue.getLastMail().getMessage().getHeader("Received"));
+        // TODO: test body size
+    }
+
     @Test
     public void testSimpleMailSendWithHELO() throws Exception {
         init(smtpConfiguration);

Added: james/server/trunk/protocols/protocols-smtp/src/test/resources/log4j.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/protocols/protocols-smtp/src/test/resources/log4j.xml?rev=1591185&view=auto
==============================================================================
--- james/server/trunk/protocols/protocols-smtp/src/test/resources/log4j.xml 
(added)
+++ james/server/trunk/protocols/protocols-smtp/src/test/resources/log4j.xml 
Wed Apr 30 03:43:39 2014
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
+<log4j:configuration>
+    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
+        <layout class="org.apache.log4j.PatternLayout">
+            <param name="ConversionPattern" value="%d{ISO8601} %-5p [%-11t] 
[%-50c{4}] %L %m%n" />
+        </layout>
+    </appender>
+
+    <logger name="james" additivity="false">
+        <level value="info"></level>
+        <appender-ref ref="stdout" />
+    </logger>
+    <logger name="org.apache.james" additivity="false">
+        <level value="info"></level>
+        <appender-ref ref="stdout" />
+    </logger>
+
+    
+    <root>
+        <priority value="info" />
+        <appender-ref ref="stdout" />
+    </root>
+</log4j:configuration>

Modified: james/server/trunk/queue/queue-api/pom.xml
URL: 
http://svn.apache.org/viewvc/james/server/trunk/queue/queue-api/pom.xml?rev=1591185&r1=1591184&r2=1591185&view=diff
==============================================================================
--- james/server/trunk/queue/queue-api/pom.xml (original)
+++ james/server/trunk/queue/queue-api/pom.xml Wed Apr 30 03:43:39 2014
@@ -43,6 +43,11 @@
             <groupId>${javax.mail.groupId}</groupId>
             <artifactId>${javax.mail.artifactId}</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.james</groupId>
+            <artifactId>james-server-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

Modified: 
james/server/trunk/queue/queue-api/src/test/java/org/apache/james/queue/api/mock/MockMailQueue.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/queue/queue-api/src/test/java/org/apache/james/queue/api/mock/MockMailQueue.java?rev=1591185&r1=1591184&r2=1591185&view=diff
==============================================================================
--- 
james/server/trunk/queue/queue-api/src/test/java/org/apache/james/queue/api/mock/MockMailQueue.java
 (original)
+++ 
james/server/trunk/queue/queue-api/src/test/java/org/apache/james/queue/api/mock/MockMailQueue.java
 Wed Apr 30 03:43:39 2014
@@ -18,20 +18,29 @@
  ****************************************************************/
 package org.apache.james.queue.api.mock;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Random;
 import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
-
+import javax.mail.MessagingException;
+import org.apache.james.core.MailImpl;
 import org.apache.james.queue.api.MailQueue;
 import org.apache.mailet.Mail;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class MockMailQueue implements MailQueue {
 
+    private static final Logger log = 
LoggerFactory.getLogger(MockMailQueue.class.getName());
+
     private final LinkedBlockingQueue<Mail> queue = new 
LinkedBlockingQueue<Mail>();
     private boolean throwException;
     private final ScheduledExecutorService scheduler = 
Executors.newScheduledThreadPool(1);
-    private Mail lastMail;
 
     /**
      * Throw an {@link MailQueueException} on next operation
@@ -46,11 +55,9 @@ public class MockMailQueue implements Ma
             throwException = false;
             throw new MailQueueException("Mock");
         }
+
         try {
             final Mail mail = queue.take();
-            if (queue.isEmpty()) {
-                lastMail = null;
-            }
             return new MailQueueItem() {
 
                 @Override
@@ -65,10 +72,44 @@ public class MockMailQueue implements Ma
             };
 
         } catch (InterruptedException e) {
+            log.error("", e);
             throw new MailQueueException("Mock", e);
         }
     }
 
+    private Mail cloneMail(Mail mail) {
+        ByteArrayOutputStream baos = null;
+        ByteArrayInputStream bais = null;
+        try {
+            baos = new ByteArrayOutputStream();
+            ((MailImpl) mail).writeMessageTo(baos);
+            log.trace("mimemessage stream: >>>" + new 
String(baos.toByteArray()) + "<<<");
+            bais = new ByteArrayInputStream(baos.toByteArray());
+            Mail newMail = (Mail) new MailImpl("MockMailCopy" + new 
Random().nextLong(),
+                    mail.getSender(), mail.getRecipients(), bais);
+            return newMail;
+        } catch (MessagingException ex) {
+            log.error("", ex);
+            throw new RuntimeException(ex);
+        } catch (IOException ex) {
+            log.error("", ex);
+            throw new RuntimeException(ex);
+        } finally {
+            try {
+                if (bais != null) {
+                    bais.close();
+                }
+            } catch (IOException ex) {
+            }
+            try {
+                if (baos != null) {
+                    baos.close();
+                }
+            } catch (IOException ex) {
+            }
+        }
+    }
+
     @Override
     public void enQueue(final Mail mail, long delay, TimeUnit unit) throws 
MailQueueException {
         if (throwException) {
@@ -81,10 +122,10 @@ public class MockMailQueue implements Ma
             @Override
             public void run() {
                 try {
-                    queue.put(mail);
-                    lastMail = mail;
+                    queue.put(MockMailQueue.this.cloneMail(mail));
                 } catch (InterruptedException e) {
-                    e.printStackTrace();
+                    log.error("", e);
+                    throw new RuntimeException("Mock", e);
                 }
             }
         }, delay, unit);
@@ -96,16 +137,24 @@ public class MockMailQueue implements Ma
             throwException = false;
             throw new MailQueueException("Mock");
         }
+
         try {
-            queue.put(mail);
-            lastMail = mail;
+            queue.put(cloneMail(mail));
         } catch (InterruptedException e) {
+            log.error("", e);
             throw new MailQueueException("Mock", e);
         }
     }
 
     public Mail getLastMail() {
-        return lastMail;
+        Iterator<Mail> it = queue.iterator();
+
+        Mail mail = null;
+        while(it.hasNext()) {
+            mail = it.next();
+        }
+
+        return mail;
     }
 
     public void clear() {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to