camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1195144 - in /camel/trunk/components/camel-mail/src: main/java/org/apache/camel/component/mail/ test/java/org/apache/camel/component/mail/
Date Sun, 30 Oct 2011 10:54:52 GMT
Author: davsclaus
Date: Sun Oct 30 10:54:51 2011
New Revision: 1195144

URL: http://svn.apache.org/viewvc?rev=1195144&view=rev
Log:
CAMEL-4225: Added disconnect option to camel-mail.

Added:
    camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java
      - copied, changed from r1195125, camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailBatchConsumerTest.java
Modified:
    camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
    camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java

Modified: camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java?rev=1195144&r1=1195143&r2=1195144&view=diff
==============================================================================
--- camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
(original)
+++ camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
Sun Oct 30 10:54:51 2011
@@ -62,6 +62,7 @@ public class MailConfiguration implement
     private String alternativeBodyHeader = MailConstants.MAIL_ALTERNATIVE_BODY;
     private boolean useInlineAttachments;
     private boolean ignoreUnsupportedCharset;
+    private boolean disconnect;
 
     public MailConfiguration() {
     }
@@ -462,4 +463,12 @@ public class MailConfiguration implement
     public void setIgnoreUnsupportedCharset(boolean ignoreUnsupportedCharset) {
         this.ignoreUnsupportedCharset = ignoreUnsupportedCharset;
     }
+
+    public boolean isDisconnect() {
+        return disconnect;
+    }
+
+    public void setDisconnect(boolean disconnect) {
+        this.disconnect = disconnect;
+    }
 }

Modified: camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java?rev=1195144&r1=1195143&r2=1195144&view=diff
==============================================================================
--- camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
(original)
+++ camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
Sun Oct 30 10:54:51 2011
@@ -137,6 +137,19 @@ public class MailConsumer extends Schedu
             }
         }
 
+        // should we disconnect, the header can override the configuration
+        boolean disconnect = getEndpoint().getConfiguration().isDisconnect();
+        if (disconnect) {
+            LOG.debug("Disconnecting from {}", getEndpoint().getConfiguration().getMailStoreLogInformation());
+            try {
+                store.close();
+            } catch (Exception e) {
+                LOG.debug("Could not disconnect from {}: " + getEndpoint().getConfiguration().getMailStoreLogInformation(),
e);
+            }
+            store = null;
+            folder = null;
+        }
+
         return polledMessages;
     }
 
@@ -356,7 +369,7 @@ public class MailConsumer extends Schedu
                 buffer.append(header.getName()).append("=").append(header.getValue()).append("\n");
             }
             if (buffer.length() > 0) {
-                LOG.debug("Generating UID from the following:\n" + buffer);
+                LOG.trace("Generating UID from the following:\n {}", buffer);
                 uid = UUID.nameUUIDFromBytes(buffer.toString().getBytes()).toString();
             }
         } catch (MessagingException e) {

Copied: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java
(from r1195125, camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailBatchConsumerTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java?p2=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java&p1=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailBatchConsumerTest.java&r1=1195125&r2=1195144&rev=1195144&view=diff
==============================================================================
--- camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailBatchConsumerTest.java
(original)
+++ camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailDisconnectTest.java
Sun Oct 30 10:54:51 2011
@@ -16,79 +16,41 @@
  */
 package org.apache.camel.component.mail;
 
-import javax.mail.Folder;
-import javax.mail.Message;
-import javax.mail.Store;
-import javax.mail.internet.MimeMessage;
-
-import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
-import org.jvnet.mock_javamail.Mailbox;
-import org.springframework.mail.javamail.JavaMailSenderImpl;
 
 /**
  * Unit test for batch consumer.
  */
-public class MailBatchConsumerTest extends CamelTestSupport {
-
-    @Override
-    public void setUp() throws Exception {
-        prepareMailbox();
-        super.setUp();
-    }
+public class MailDisconnectTest extends CamelTestSupport {
 
     @Test
-    public void testBatchConsumer() throws Exception {
-        Mailbox mailbox = Mailbox.get("jones@localhost");
-        assertEquals(5, mailbox.size());
-
+    public void testDisconnect() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(5);
-        mock.expectsAscending(body());
-        mock.message(0).property(Exchange.BATCH_INDEX).isEqualTo(0);
-        mock.message(1).property(Exchange.BATCH_INDEX).isEqualTo(1);
-        mock.message(2).property(Exchange.BATCH_INDEX).isEqualTo(2);
-        mock.message(3).property(Exchange.BATCH_INDEX).isEqualTo(3);
-        mock.message(4).property(Exchange.BATCH_INDEX).isEqualTo(4);
-        mock.message(0).property(Exchange.BATCH_COMPLETE).isEqualTo(false);
-        mock.message(1).property(Exchange.BATCH_COMPLETE).isEqualTo(false);
-        mock.message(2).property(Exchange.BATCH_COMPLETE).isEqualTo(false);
-        mock.message(3).property(Exchange.BATCH_COMPLETE).isEqualTo(false);
-        mock.message(3).property(Exchange.BATCH_COMPLETE).isEqualTo(false);
-        mock.message(4).property(Exchange.BATCH_COMPLETE).isEqualTo(true);
-        mock.expectedPropertyReceived(Exchange.BATCH_SIZE, 5);
 
-        assertMockEndpointsSatisfied();
-    }
+        // send 5 mails with some delay so we do multiple polls with disconnect between
+        template.sendBodyAndHeader("smtp://jones@localhost", "A Bla bla", "Subject", "Hello
A");
+        template.sendBodyAndHeader("smtp://jones@localhost", "B Bla bla", "Subject", "Hello
B");
+
+        Thread.sleep(500);
+        template.sendBodyAndHeader("smtp://jones@localhost", "C Bla bla", "Subject", "Hello
C");
+
+        Thread.sleep(1500);
+        template.sendBodyAndHeader("smtp://jones@localhost", "D Bla bla", "Subject", "Hello
D");
 
-    private void prepareMailbox() throws Exception {
-        // connect to mailbox
-        Mailbox.clearAll();
-        JavaMailSenderImpl sender = new JavaMailSenderImpl();
-        Store store = sender.getSession().getStore("pop3");
-        store.connect("localhost", 25, "jones", "secret");
-        Folder folder = store.getFolder("INBOX");
-        folder.open(Folder.READ_WRITE);
-        folder.expunge();
-
-        // inserts 5 new messages
-        Message[] messages = new Message[5];
-        for (int i = 0; i < 5; i++) {
-            messages[i] = new MimeMessage(sender.getSession());
-            messages[i].setText("Message " + i);
-        }
-        folder.appendMessages(messages);
-        folder.close(true);
+        Thread.sleep(1500);
+        template.sendBodyAndHeader("smtp://jones@localhost", "E Bla bla", "Subject", "Hello
E");
+
+        assertMockEndpointsSatisfied();
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("pop3://jones@localhost?password=secret&consumer.delay=5000"
-                    + "&delete=true").to("mock:result");
+                from("pop3://jones@localhost?password=secret&disconnect=true&delay=1000").to("mock:result");
             }
         };
     }



Mime
View raw message