Author: davsclaus
Date: Tue Nov 13 17:25:58 2012
New Revision: 1408845

URL: http://svn.apache.org/viewvc?rev=1408845&view=rev
Log:
CAMEL-1069: Added support for searchTerm on camel-mail. Ticket was only 4 years 
old ;)

Added:
    
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SearchTermBuilder.java
   (with props)
    
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SimpleSearchTerm.java
   (with props)
    
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermNotSpamTest.java
   (with props)
    
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermTest.java
      - copied, changed from r1408743, 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java
    
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermUriConfigTest.java
   (with props)
    
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/SearchTermBuilderTest.java
   (with props)
Modified:
    
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
    
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
    
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
    
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java

Modified: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java?rev=1408845&r1=1408844&r2=1408845&view=diff
==============================================================================
--- 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
 (original)
+++ 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
 Tue Nov 13 17:25:58 2012
@@ -17,13 +17,17 @@
 package org.apache.camel.component.mail;
 
 import java.net.URI;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import javax.mail.search.SearchTerm;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
+import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ObjectHelper;
 
 /**
@@ -67,6 +71,15 @@ public class MailComponent extends Defau
         endpoint.setContentTypeResolver(contentTypeResolver);
         setProperties(endpoint.getConfiguration(), parameters);
 
+        Map<String, Object> sstParams = 
IntrospectionSupport.extractProperties(parameters, "searchTerm.");
+        if (!sstParams.isEmpty()) {
+            // use SimpleSearchTerm as POJO to store the configuration and 
then convert that to the actual SearchTerm
+            SimpleSearchTerm sst = new SimpleSearchTerm();
+            setProperties(sst, sstParams);
+            SearchTerm st = 
getCamelContext().getTypeConverter().mandatoryConvertTo(SearchTerm.class, sst);
+            endpoint.setSearchTerm(st);
+        }
+
         // sanity check that we know the mail server
         ObjectHelper.notEmpty(config.getHost(), "host");
         ObjectHelper.notEmpty(config.getProtocol(), "protocol");

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=1408845&r1=1408844&r2=1408845&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
 Tue Nov 13 17:25:58 2012
@@ -38,6 +38,9 @@ import org.apache.camel.util.ObjectHelpe
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.camel.component.mail.SearchTermBuilder.*;
+import static org.apache.camel.component.mail.SearchTermBuilder.Op.and;
+
 /**
  * A {@link org.apache.camel.Consumer Consumer} which consumes messages from 
JavaMail using a
  * {@link javax.mail.Transport Transport} and dispatches them to the {@link 
Processor}
@@ -105,10 +108,14 @@ public class MailConsumer extends Schedu
             if (count > 0) {
                 Message[] messages;
 
-                // should we process all messages or only unseen messages
-                if (getEndpoint().getConfiguration().isUnseen()) {
-                    messages = folder.search(new FlagTerm(new 
Flags(Flags.Flag.SEEN), false));
+                if (getEndpoint().getSearchTerm() != null) {
+                    // use custom search term
+                    messages = folder.search(getEndpoint().getSearchTerm());
+                } else if (getEndpoint().getConfiguration().isUnseen()) {
+                    // only unseen messages
+                    messages = folder.search(new 
SearchTermBuilder().unseen().build());
                 } else {
+                    // get all messages
                     messages = folder.getMessages();
                 }
 

Modified: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java?rev=1408845&r1=1408844&r2=1408845&view=diff
==============================================================================
--- 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
 (original)
+++ 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConverters.java
 Tue Nov 13 17:25:58 2012
@@ -18,12 +18,15 @@ package org.apache.camel.component.mail;
 
 import java.io.IOException;
 import java.io.InputStream;
-
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import javax.mail.BodyPart;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Multipart;
 import javax.mail.internet.MimeMultipart;
+import javax.mail.search.SearchTerm;
 
 import org.apache.camel.Converter;
 import org.apache.camel.converter.IOConverter;
@@ -97,4 +100,51 @@ public final class MailConverters {
         return IOConverter.toInputStream(s, null);
     }
 
+    @Converter
+    public static SearchTerm toSearchTerm(SimpleSearchTerm simple) throws 
ParseException {
+        SearchTermBuilder builder = new SearchTermBuilder();
+        if (simple.isUnseen()) {
+            builder = builder.unseen();
+        }
+
+        if (simple.getSubjectOrBody() != null) {
+            String text = simple.getSubjectOrBody();
+            builder = builder.subject(text).body(SearchTermBuilder.Op.or, 
text);
+        }
+        if (simple.getSubject() != null) {
+            builder = builder.subject(simple.getSubject());
+        }
+        if (simple.getBody() != null) {
+            builder = builder.body(simple.getBody());
+        }
+        if (simple.getFrom() != null) {
+            builder = builder.from(simple.getFrom());
+        }
+        if (simple.getTo() != null) {
+            builder = builder.recipient(Message.RecipientType.TO, 
simple.getTo());
+        }
+        if (simple.getFromSentDate() != null) {
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
+            Date date;
+            if ("now".equals(simple.getFromSentDate())) {
+                date = new Date();
+            } else {
+                date = sdf.parse(simple.getFromSentDate());
+            }
+            builder = builder.sent(SearchTermBuilder.Comparison.GE, date);
+        }
+        if (simple.getToSentDate() != null) {
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
+            Date date;
+            if ("now".equals(simple.getToSentDate())) {
+                date = new Date();
+            } else {
+                date = sdf.parse(simple.getToSentDate());
+            }
+            builder = builder.sent(SearchTermBuilder.Comparison.LE, date);
+        }
+
+        return builder.build();
+    }
+
 }

Modified: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java?rev=1408845&r1=1408844&r2=1408845&view=diff
==============================================================================
--- 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
 (original)
+++ 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
 Tue Nov 13 17:25:58 2012
@@ -17,6 +17,7 @@
 package org.apache.camel.component.mail;
 
 import javax.mail.Message;
+import javax.mail.search.SearchTerm;
 
 import org.apache.camel.Consumer;
 import org.apache.camel.Exchange;
@@ -39,6 +40,7 @@ public class MailEndpoint extends Schedu
     private HeaderFilterStrategy headerFilterStrategy = new 
DefaultHeaderFilterStrategy();
     private ContentTypeResolver contentTypeResolver;
     private int maxMessagesPerPoll;
+    private SearchTerm searchTerm;
 
     public MailEndpoint() {
     }
@@ -169,4 +171,13 @@ public class MailEndpoint extends Schedu
     public void setMaxMessagesPerPoll(int maxMessagesPerPoll) {
         this.maxMessagesPerPoll = maxMessagesPerPoll;
     }
+
+    public SearchTerm getSearchTerm() {
+        return searchTerm;
+    }
+
+    public void setSearchTerm(SearchTerm searchTerm) {
+        this.searchTerm = searchTerm;
+    }
+
 }

Added: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SearchTermBuilder.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SearchTermBuilder.java?rev=1408845&view=auto
==============================================================================
--- 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SearchTermBuilder.java
 (added)
+++ 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SearchTermBuilder.java
 Tue Nov 13 17:25:58 2012
@@ -0,0 +1,176 @@
+/**
+ * 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 java.util.Date;
+import javax.mail.Flags;
+import javax.mail.Message;
+import javax.mail.search.AndTerm;
+import javax.mail.search.BodyTerm;
+import javax.mail.search.FlagTerm;
+import javax.mail.search.FromStringTerm;
+import javax.mail.search.HeaderTerm;
+import javax.mail.search.NotTerm;
+import javax.mail.search.OrTerm;
+import javax.mail.search.ReceivedDateTerm;
+import javax.mail.search.RecipientStringTerm;
+import javax.mail.search.SearchTerm;
+import javax.mail.search.SentDateTerm;
+import javax.mail.search.SubjectTerm;
+
+/**
+ * A builder to build compound {@link SearchTerm}s.
+ */
+public class SearchTermBuilder {
+
+    private SearchTerm term;
+
+    public enum Op {
+        and, or, not;
+    }
+
+    public enum Comparison {
+        LE, LT, EQ, NE, GT, GE;
+
+        int asNum() {
+            // should start from 1
+            return this.ordinal() + 1;
+        }
+    }
+
+    public SearchTerm build() {
+        return term;
+    }
+
+    public SearchTermBuilder unseen() {
+        return unseen(Op.and);
+    }
+
+    public SearchTermBuilder unseen(Op op) {
+        SearchTerm st = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder header(String headerName, String pattern) {
+        return header(Op.and, headerName, pattern);
+    }
+
+    public SearchTermBuilder header(Op op, String headerName, String pattern) {
+        SearchTerm st = new HeaderTerm(headerName, pattern);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder subject(String pattern) {
+        return subject(Op.and, pattern);
+    }
+
+    public SearchTermBuilder subject(Op op, String pattern) {
+        SearchTerm st = new SubjectTerm(pattern);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder body(String pattern) {
+        return body(Op.and, pattern);
+    }
+
+    public SearchTermBuilder body(Op op, String pattern) {
+        SearchTerm st = new BodyTerm(pattern);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder from(String pattern) {
+        return from(Op.and, pattern);
+    }
+
+    public SearchTermBuilder from(Op op, String pattern) {
+        SearchTerm st = new FromStringTerm(pattern);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder recipient(Message.RecipientType type, String 
pattern) {
+        return recipient(Op.and, type, pattern);
+    }
+
+    public SearchTermBuilder recipient(Op op, Message.RecipientType type, 
String pattern) {
+        SearchTerm st = new RecipientStringTerm(type, pattern);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder flag(Flags flags, boolean set) {
+        return flag(Op.and, flags, set);
+    }
+
+    public SearchTermBuilder flag(Op op, Flags flags, boolean set) {
+        SearchTerm st = new FlagTerm(flags, set);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder sent(Comparison comparison, Date date) {
+        return sent(Op.and, comparison, date);
+    }
+
+    public SearchTermBuilder sent(Op op, Comparison comparison, Date date) {
+        SentDateTerm st = new SentDateTerm(comparison.asNum(), date);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder received(Comparison comparison, Date date) {
+        return received(Op.and, comparison, date);
+    }
+
+    public SearchTermBuilder received(Op op, Comparison comparison, Date date) 
{
+        ReceivedDateTerm st = new ReceivedDateTerm(comparison.asNum(), date);
+        addTerm(op, st);
+        return this;
+    }
+
+    public SearchTermBuilder and(SearchTerm term) {
+        addTerm(Op.and, term);
+        return this;
+    }
+
+    public SearchTermBuilder or(SearchTerm term) {
+        addTerm(Op.or, term);
+        return this;
+    }
+
+    public SearchTermBuilder not(SearchTerm term) {
+        addTerm(Op.not, term);
+        return this;
+    }
+
+    private void addTerm(Op op, SearchTerm newTerm) {
+        if (term == null) {
+            term = newTerm;
+        } else if (op == Op.and) {
+            term = new AndTerm(term, newTerm);
+        } else if (op == Op.or) {
+            term = new OrTerm(term, newTerm);
+        } else {
+            // need to and the existing with the not
+            term = new AndTerm(term, new NotTerm(newTerm));
+        }
+    }
+}

Propchange: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SearchTermBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SearchTermBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SimpleSearchTerm.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SimpleSearchTerm.java?rev=1408845&view=auto
==============================================================================
--- 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SimpleSearchTerm.java
 (added)
+++ 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SimpleSearchTerm.java
 Tue Nov 13 17:25:58 2012
@@ -0,0 +1,102 @@
+/**
+ * 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;
+
+/**
+ * Allows to configure common {@link javax.mail.search.SearchTerm}'s using a 
POJO style,
+ * which can be done from XML DSLs.
+ * <p/>
+ * This POJO has default <tt>true</tt> for the {@link #isUnseen()} option.
+ * <p/>
+ * The {@link #setFromSentDate(String)} and {@link #setToSentDate(String)} is 
using the following date pattern
+ * <tt>yyyy-MM-dd HH:mm:SS</tt>.
+ */
+public class SimpleSearchTerm {
+
+    private boolean unseen = true;
+    private String subjectOrBody;
+    private String subject;
+    private String body;
+    private String from;
+    private String to;
+    private String fromSentDate;
+    private String toSentDate;
+
+    public boolean isUnseen() {
+        return unseen;
+    }
+
+    public void setUnseen(boolean unseen) {
+        this.unseen = unseen;
+    }
+
+    public String getSubjectOrBody() {
+        return subjectOrBody;
+    }
+
+    public void setSubjectOrBody(String subjectOrBody) {
+        this.subjectOrBody = subjectOrBody;
+    }
+
+    public String getSubject() {
+        return subject;
+    }
+
+    public void setSubject(String subject) {
+        this.subject = subject;
+    }
+
+    public String getBody() {
+        return body;
+    }
+
+    public void setBody(String body) {
+        this.body = body;
+    }
+
+    public String getFrom() {
+        return from;
+    }
+
+    public void setFrom(String from) {
+        this.from = from;
+    }
+
+    public String getTo() {
+        return to;
+    }
+
+    public void setTo(String to) {
+        this.to = to;
+    }
+
+    public String getFromSentDate() {
+        return fromSentDate;
+    }
+
+    public void setFromSentDate(String fromSentDate) {
+        this.fromSentDate = fromSentDate;
+    }
+
+    public String getToSentDate() {
+        return toSentDate;
+    }
+
+    public void setToSentDate(String toSentDate) {
+        this.toSentDate = toSentDate;
+    }
+}

Propchange: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SimpleSearchTerm.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SimpleSearchTerm.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermNotSpamTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermNotSpamTest.java?rev=1408845&view=auto
==============================================================================
--- 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermNotSpamTest.java
 (added)
+++ 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermNotSpamTest.java
 Tue Nov 13 17:25:58 2012
@@ -0,0 +1,50 @@
+/**
+ * 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 javax.mail.search.SearchTerm;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.jvnet.mock_javamail.Mailbox;
+
+import static org.apache.camel.component.mail.SearchTermBuilder.Op;
+
+public class MailSearchTermNotSpamTest extends MailSearchTermTest {
+
+    @Override
+    protected SearchTerm createSearchTerm() {
+        // we just want the unseen mails which is not spam
+        SearchTermBuilder build = new SearchTermBuilder();
+        build.unseen().body(Op.not, "Spam").subject(Op.not, "Spam");
+
+        return build.build();
+    }
+
+    @Test
+    public void testSearchTerm() throws Exception {
+        Mailbox mailbox = Mailbox.get("bill@localhost");
+        assertEquals(6, mailbox.size());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("I like riding the Camel", 
"Ordering Camel in Action",
+                "Ordering ActiveMQ in Action", "We meet at 7pm the usual 
place");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

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

Propchange: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermNotSpamTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermTest.java
 (from r1408743, 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermTest.java?p2=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermTest.java&p1=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java&r1=1408743&r2=1408845&rev=1408845&view=diff
==============================================================================
--- 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java
 (original)
+++ 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermTest.java
 Tue Nov 13 17:25:58 2012
@@ -19,18 +19,20 @@ package org.apache.camel.component.mail;
 import javax.mail.Folder;
 import javax.mail.Message;
 import javax.mail.Store;
+import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
+import javax.mail.search.SearchTerm;
 
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 import org.jvnet.mock_javamail.Mailbox;
 
-/**
- * Unit test for a special corner case with fetchSize=0
- */
-public class MailFetchSizeZeroTest extends CamelTestSupport {
+import static org.apache.camel.component.mail.SearchTermBuilder.Op;
+
+public class MailSearchTermTest extends CamelTestSupport {
 
     @Override
     public void setUp() throws Exception {
@@ -38,19 +40,30 @@ public class MailFetchSizeZeroTest exten
         super.setUp();
     }
 
+    protected SearchTerm createSearchTerm() {
+        // we just want the unseen Camel related mails
+        SearchTermBuilder build = new SearchTermBuilder();
+        build.unseen().subject("Camel").body(Op.or, "Camel");
+
+        return build.build();
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myTerm", createSearchTerm());
+        return jndi;
+    }
+
     @Test
-    public void testFetchSize() throws Exception {
+    public void testSearchTerm() throws Exception {
         Mailbox mailbox = Mailbox.get("bill@localhost");
-        assertEquals(5, mailbox.size());
+        assertEquals(6, mailbox.size());
 
         MockEndpoint mock = getMockEndpoint("mock:result");
-        // no messages expected as we have a fetch size of zero
-        mock.expectedMessageCount(0);
-        // should be done within 2 seconds as no delay when started
-        mock.setResultWaitTime(2000L);
-        mock.assertIsSatisfied();
+        mock.expectedBodiesReceivedInAnyOrder("I like riding the Camel", 
"Ordering Camel in Action");
 
-        assertEquals(5, mailbox.size());
+        assertMockEndpointsSatisfied();
     }
 
     private void prepareMailbox() throws Exception {
@@ -64,11 +77,37 @@ public class MailFetchSizeZeroTest exten
         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);
-        }
+        Message[] messages = new Message[6];
+        messages[0] = new MimeMessage(sender.getSession());
+        messages[0].setSubject("Apache Camel rocks");
+        messages[0].setText("I like riding the Camel");
+        messages[0].setFrom(new InternetAddress("[email protected]"));
+
+        messages[1] = new MimeMessage(sender.getSession());
+        messages[1].setSubject("Order");
+        messages[1].setText("Ordering Camel in Action");
+        messages[1].setFrom(new InternetAddress("[email protected]"));
+
+        messages[2] = new MimeMessage(sender.getSession());
+        messages[2].setSubject("Order");
+        messages[2].setText("Ordering ActiveMQ in Action");
+        messages[2].setFrom(new InternetAddress("[email protected]"));
+
+        messages[3] = new MimeMessage(sender.getSession());
+        messages[3].setSubject("Buy pharmacy");
+        messages[3].setText("This is spam");
+        messages[3].setFrom(new InternetAddress("[email protected]"));
+
+        messages[4] = new MimeMessage(sender.getSession());
+        messages[4].setSubject("Beers tonight?");
+        messages[4].setText("We meet at 7pm the usual place");
+        messages[4].setFrom(new InternetAddress("[email protected]"));
+
+        messages[5] = new MimeMessage(sender.getSession());
+        messages[5].setSubject("Spambot attack");
+        messages[5].setText("I am attaching you");
+        messages[5].setFrom(new InternetAddress("[email protected]"));
+
         folder.appendMessages(messages);
         folder.close(true);
     }
@@ -76,7 +115,7 @@ public class MailFetchSizeZeroTest exten
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                
from("pop3://bill@localhost?password=secret&fetchSize=0&consumer.delay=1000").to("mock:result");
+                
from("pop3://bill@localhost?password=secret&searchTerm=#myTerm").to("mock:result");
             }
         };
     }

Added: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermUriConfigTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermUriConfigTest.java?rev=1408845&view=auto
==============================================================================
--- 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermUriConfigTest.java
 (added)
+++ 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermUriConfigTest.java
 Tue Nov 13 17:25:58 2012
@@ -0,0 +1,104 @@
+/**
+ * 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 javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.Store;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+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;
+
+public class MailSearchTermUriConfigTest extends CamelTestSupport {
+
+    @Override
+    public void setUp() throws Exception {
+        prepareMailbox();
+        super.setUp();
+    }
+
+    @Test
+    public void testSearchTerm() throws Exception {
+        Mailbox mailbox = Mailbox.get("bill@localhost");
+        assertEquals(6, mailbox.size());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("I like riding the Camel", 
"Ordering Camel in Action");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    private void prepareMailbox() throws Exception {
+        // connect to mailbox
+        Mailbox.clearAll();
+        JavaMailSender sender = new DefaultJavaMailSender();
+        Store store = sender.getSession().getStore("pop3");
+        store.connect("localhost", 25, "bill", "secret");
+        Folder folder = store.getFolder("INBOX");
+        folder.open(Folder.READ_WRITE);
+        folder.expunge();
+
+        // inserts 5 new messages
+        Message[] messages = new Message[6];
+        messages[0] = new MimeMessage(sender.getSession());
+        messages[0].setSubject("Apache Camel rocks");
+        messages[0].setText("I like riding the Camel");
+        messages[0].setFrom(new InternetAddress("[email protected]"));
+
+        messages[1] = new MimeMessage(sender.getSession());
+        messages[1].setSubject("Order");
+        messages[1].setText("Ordering Camel in Action");
+        messages[1].setFrom(new InternetAddress("[email protected]"));
+
+        messages[2] = new MimeMessage(sender.getSession());
+        messages[2].setSubject("Order");
+        messages[2].setText("Ordering ActiveMQ in Action");
+        messages[2].setFrom(new InternetAddress("[email protected]"));
+
+        messages[3] = new MimeMessage(sender.getSession());
+        messages[3].setSubject("Buy pharmacy");
+        messages[3].setText("This is spam");
+        messages[3].setFrom(new InternetAddress("[email protected]"));
+
+        messages[4] = new MimeMessage(sender.getSession());
+        messages[4].setSubject("Beers tonight?");
+        messages[4].setText("We meet at 7pm the usual place");
+        messages[4].setFrom(new InternetAddress("[email protected]"));
+
+        messages[5] = new MimeMessage(sender.getSession());
+        messages[5].setSubject("Spambot attack");
+        messages[5].setText("I am attaching you");
+        messages[5].setFrom(new InternetAddress("[email protected]"));
+
+        folder.appendMessages(messages);
+        folder.close(true);
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                
from("pop3://bill@localhost?password=secret&searchTerm.subjectOrBody=Camel").to("mock:result");
+            }
+        };
+    }
+
+}

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

Propchange: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSearchTermUriConfigTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/SearchTermBuilderTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/SearchTermBuilderTest.java?rev=1408845&view=auto
==============================================================================
--- 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/SearchTermBuilderTest.java
 (added)
+++ 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/SearchTermBuilderTest.java
 Tue Nov 13 17:25:58 2012
@@ -0,0 +1,88 @@
+/**
+ * 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 javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.mail.search.SearchTerm;
+
+import junit.framework.TestCase;
+import org.jvnet.mock_javamail.Mailbox;
+
+import static org.apache.camel.component.mail.SearchTermBuilder.Op.or;
+
+/**
+ *
+ */
+public class SearchTermBuilderTest extends TestCase {
+
+    public void testSearchTermBuilderFromAndSubject() throws Exception {
+        SearchTermBuilder build = new SearchTermBuilder();
+        SearchTerm st = 
build.from("[email protected]").subject("Camel").build();
+
+        assertNotNull(st);
+
+        // create dummy message
+        Mailbox.clearAll();
+        JavaMailSender sender = new DefaultJavaMailSender();
+
+        MimeMessage msg = new MimeMessage(sender.getSession());
+        msg.setSubject("Yeah Camel rocks");
+        msg.setText("Apache Camel is a cool project. Have a fun ride.");
+        msg.setFrom(new InternetAddress("[email protected]"));
+        assertTrue("Should match message", st.match(msg));
+
+        MimeMessage msg2 = new MimeMessage(sender.getSession());
+        msg2.setSubject("Apache Camel is fantastic");
+        msg2.setText("I like Camel.");
+        msg2.setFrom(new InternetAddress("[email protected]"));
+        assertFalse("Should not match message, as from doesn't match", 
st.match(msg2));
+    }
+
+    public void testSearchTermBuilderFromOrSubject() throws Exception {
+        SearchTermBuilder build = new SearchTermBuilder();
+        SearchTerm st = build.subject("Camel").from(or, 
"[email protected]").build();
+
+        assertNotNull(st);
+
+        // create dummy message
+        Mailbox.clearAll();
+        JavaMailSender sender = new DefaultJavaMailSender();
+
+        MimeMessage msg = new MimeMessage(sender.getSession());
+        msg.setSubject("Yeah Camel rocks");
+        msg.setText("Apache Camel is a cool project. Have a fun ride.");
+        msg.setFrom(new InternetAddress("[email protected]"));
+        assertTrue("Should match message", st.match(msg));
+
+        MimeMessage msg2 = new MimeMessage(sender.getSession());
+        msg2.setSubject("Beware");
+        msg2.setText("This is from the administrator.");
+        msg2.setFrom(new InternetAddress("[email protected]"));
+        assertTrue("Should match message, as its from admin", st.match(msg2));
+    }
+
+    public void testComparison() throws Exception {
+        assertEquals(1, SearchTermBuilder.Comparison.LE.asNum());
+        assertEquals(2, SearchTermBuilder.Comparison.LT.asNum());
+        assertEquals(3, SearchTermBuilder.Comparison.EQ.asNum());
+        assertEquals(4, SearchTermBuilder.Comparison.NE.asNum());
+        assertEquals(5, SearchTermBuilder.Comparison.GT.asNum());
+        assertEquals(6, SearchTermBuilder.Comparison.GE.asNum());
+    }
+
+}

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

Propchange: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/SearchTermBuilderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to