activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r533761 - in /activemq/camel/trunk/camel-mail: ./ src/main/java/org/apache/camel/component/mail/ src/main/resources/META-INF/services/org/apache/camel/component/ src/test/java/org/apache/camel/component/mail/
Date Mon, 30 Apr 2007 14:35:22 GMT
Author: jstrachan
Date: Mon Apr 30 07:35:21 2007
New Revision: 533761

URL: http://svn.apache.org/viewvc?view=rev&rev=533761
Log:
added a test case using the excellent mock_javamail library that email sending works

Added:
    activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop3
      - copied, changed from r533622, activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop
    activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
  (with props)
Removed:
    activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop
Modified:
    activemq/camel/trunk/camel-mail/pom.xml
    activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
    activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
    activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
    activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java

Modified: activemq/camel/trunk/camel-mail/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/pom.xml?view=diff&rev=533761&r1=533760&r2=533761
==============================================================================
--- activemq/camel/trunk/camel-mail/pom.xml (original)
+++ activemq/camel/trunk/camel-mail/pom.xml Mon Apr 30 07:35:21 2007
@@ -32,13 +32,22 @@
   <name>Camel :: Mail</name>
   <description>Camel Mail support</description>
 
+  <repositories>
+    <repository>
+      <id>maven2-repository.dev.java.net</id>
+      <name>Java.net Repository for Maven</name>
+      <url>http://download.java.net/maven/2/</url>
+      <layout>default</layout>
+    </repository>
+  </repositories>
+
   <dependencies>
 
     <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
     </dependency>
-    
+
     <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
@@ -49,7 +58,6 @@
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-spring</artifactId>
       <optional>true</optional>
-      <scope>test</scope>
     </dependency>
 
     <dependency>
@@ -60,6 +68,15 @@
     <dependency>
       <groupId>javax.mail</groupId>
       <artifactId>mail</artifactId>
+    </dependency>
+
+
+    <!-- testing -->
+    <dependency>
+      <groupId>org.jvnet.mock-javamail</groupId>
+      <artifactId>mock-javamail</artifactId>
+      <version>1.1</version>
+      <scope>test</scope>
     </dependency>
 
     <dependency>

Modified: activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?view=diff&rev=533761&r1=533760&r2=533761
==============================================================================
--- activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
(original)
+++ activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
Mon Apr 30 07:35:21 2007
@@ -19,7 +19,9 @@
 
 import javax.mail.Message;
 import javax.mail.MessagingException;
+import javax.mail.Address;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.InternetAddress;
 import java.util.Map;
 import java.util.Set;
 
@@ -30,14 +32,29 @@
  * @version $Revision: 521240 $
  */
 public class MailBinding {
-    public void populateMailMessage(MimeMessage mimeMessage, MailExchange exchange) {
+    public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, MailExchange
exchange) {
         try {
             appendMailHeaders(mimeMessage, exchange.getIn());
-            mimeMessage.setContent(exchange.getIn().getBody(), "body");
+
+            if (empty(mimeMessage.getAllRecipients())) {
+                // lets default the address to the endpoint destination
+                String destination = endpoint.getConfiguration().getDestination();
+                mimeMessage.setRecipients(Message.RecipientType.TO, destination);
+            }
+            if (empty(mimeMessage.getFrom())) {
+                // lets default the address to the endpoint destination
+                String from = endpoint.getConfiguration().getFrom();
+                mimeMessage.setFrom(new InternetAddress(from));
+            }
+            mimeMessage.setText(exchange.getIn().getBody(String.class));
         }
         catch (Exception e) {
             throw new RuntimeMailException("Failed to populate body due to: " + e + ". Exchange:
" + exchange, e);
         }
+    }
+
+    protected boolean empty(Address[] addresses) {
+        return addresses == null || addresses.length == 0;
     }
 
     /**

Modified: activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java?view=diff&rev=533761&r1=533760&r2=533761
==============================================================================
--- activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
(original)
+++ activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
Mon Apr 30 07:35:21 2007
@@ -66,7 +66,6 @@
 
         MailConfiguration config = getConfiguration().copy();
         config.configure(new URI(uri));
-        //config.configure(new URL(uri));
 
         // lets make sure we copy the configuration as each endpoint can customize its own
version
         MailEndpoint endpoint = new MailEndpoint(uri, this, config);

Modified: activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java?view=diff&rev=533761&r1=533760&r2=533761
==============================================================================
--- activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
(original)
+++ activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
Mon Apr 30 07:35:21 2007
@@ -37,6 +37,8 @@
     private Session session;
     private String username;
     private int port = -1;
+    private String destination;
+    private String from = "camel@localhost";
 
     public MailConfiguration() {
     }
@@ -71,6 +73,17 @@
         if (port >= 0) {
             setPort(port);
         }
+
+        // we can either be invoked with
+        // mailto:address
+        // or
+        // smtp:user@host:port/name@address
+
+        String fragment = uri.getFragment();
+        if (fragment == null || fragment.length() == 0) {
+            fragment = userInfo + "@" + host;
+        }
+        setDestination(fragment);
     }
 
     public JavaMailConnection createJavaMailConnection(MailEndpoint mailEndpoint) {
@@ -168,5 +181,21 @@
 
     public void setUsername(String username) {
         this.username = username;
+    }
+
+    public String getDestination() {
+        return destination;
+    }
+
+    public void setDestination(String destination) {
+        this.destination = destination;
+    }
+
+    public String getFrom() {
+        return from;
+    }
+
+    public void setFrom(String from) {
+        this.from = from;
     }
 }

Modified: activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java?view=diff&rev=533761&r1=533760&r2=533761
==============================================================================
--- activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
(original)
+++ activemq/camel/trunk/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
Mon Apr 30 07:35:21 2007
@@ -43,7 +43,7 @@
     public void process(final MailExchange exchange) {
         sender.send(new MimeMessagePreparator() {
             public void prepare(MimeMessage mimeMessage) throws Exception {
-                endpoint.getBinding().populateMailMessage(mimeMessage, exchange);
+                endpoint.getBinding().populateMailMessage(endpoint, mimeMessage, exchange);
             }
         });
     }

Copied: activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop3
(from r533622, activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop3?view=diff&rev=533761&p1=activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop&r1=533622&p2=activemq/camel/trunk/camel-mail/src/main/resources/META-INF/services/org/apache/camel/component/pop3&r2=533761
==============================================================================
    (empty)

Added: activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java?view=auto&rev=533761
==============================================================================
--- activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
(added)
+++ activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
Mon Apr 30 07:35:21 2007
@@ -0,0 +1,76 @@
+/**
+ *
+ * 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.camel.component.mail;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import static org.apache.camel.builder.Builder.constant;
+import org.apache.camel.builder.RouteBuilder;
+import static org.apache.camel.util.ObjectHelper.asString;
+import org.jvnet.mock_javamail.Mailbox;
+
+import static javax.mail.Message.RecipientType;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import java.io.IOException;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class MailRouteTest extends ContextTestSupport {
+    public void testSendAndReceiveMails() throws Exception {
+        client.send("smtp://james@localhost", new Processor<Exchange>() {
+            public void process(Exchange exchange) {
+                exchange.getIn().setBody("hello world!");
+            }
+        });
+
+        // lets test the first sent worked
+        assertMailboxReceivedMessages("james@localhost");
+
+        // lets test the receive worked
+        // TODO 
+        // assertMailboxReceivedMessages("result@localhost");
+    }
+
+    protected void assertMailboxReceivedMessages(String name) throws IOException, MessagingException
{
+        Mailbox mailbox = Mailbox.get(name);
+        assertEquals(name + " should have received 1 mail", 1, mailbox.size());
+
+        Message message = mailbox.get(0);
+        assertNotNull(name + " should have received at least one mail!", message);
+        logMessage(message);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("smtp://james@localhost").to("direct:a");
+                from("direct:a").setHeader("name", constant("James")).to("pop3:result@localhost");
+            }
+        };
+    }
+
+    protected void logMessage(Message message) throws IOException, MessagingException {
+        log.info("Received: " + message.getContent()
+                + " from: " + asString(message.getFrom())
+                + " to: " + asString(message.getRecipients(RecipientType.TO)));
+    }
+}

Propchange: activemq/camel/trunk/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message