camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1351575 - in /camel/trunk/components/camel-twitter/src: main/java/org/apache/camel/component/twitter/ main/java/org/apache/camel/component/twitter/consumer/search/ main/java/org/apache/camel/component/twitter/producer/ test/java/org/apache...
Date Tue, 19 Jun 2012 05:52:36 GMT
Author: davsclaus
Date: Tue Jun 19 05:52:35 2012
New Revision: 1351575

URL: http://svn.apache.org/viewvc?rev=1351575&view=rev
Log:
CAMEL-5374: Added twitter search producer. Thanks to Bruno Borges for the patch.

Added:
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConstants.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/SearchProducer.java
    camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchByExchangeDirectTest.java
Modified:
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/Twitter4JFactory.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConfiguration.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpoint.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/consumer/search/SearchConsumer.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/DirectMessageProducer.java
    camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/Twitter4JFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/Twitter4JFactory.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/Twitter4JFactory.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/Twitter4JFactory.java
Tue Jun 19 05:52:35 2012
@@ -33,6 +33,7 @@ import org.apache.camel.component.twitte
 import org.apache.camel.component.twitter.data.TimelineType;
 import org.apache.camel.component.twitter.data.TrendsType;
 import org.apache.camel.component.twitter.producer.DirectMessageProducer;
+import org.apache.camel.component.twitter.producer.SearchProducer;
 import org.apache.camel.component.twitter.producer.UserProducer;
 import org.apache.camel.impl.DefaultProducer;
 import org.slf4j.Logger;
@@ -66,13 +67,12 @@ import org.slf4j.LoggerFactory;
 public final class Twitter4JFactory {
 
     private static final transient Logger LOG = LoggerFactory.getLogger(Twitter4JFactory.class);
-    
+
     private Twitter4JFactory() {
         // helper class
     }
 
-    public static Twitter4JConsumer getConsumer(TwitterEndpoint te, String uri)
-        throws IllegalArgumentException {
+    public static Twitter4JConsumer getConsumer(TwitterEndpoint te, String uri) throws IllegalArgumentException
{
         String[] uriSplit = splitUri(uri);
 
         if (uriSplit.length > 0) {
@@ -80,8 +80,9 @@ public final class Twitter4JFactory {
             case DIRECTMESSAGE:
                 return new DirectMessageConsumer(te);
             case SEARCH:
-                if (te.getProperties().getKeywords() == null
-                    || te.getProperties().getKeywords().trim().isEmpty()) {
+                boolean hasKeywords = te.getProperties().getKeywords() == null
+                        || te.getProperties().getKeywords().trim().isEmpty();
+                if (hasKeywords) {
                     throw new IllegalArgumentException("Type set to SEARCH but no keywords
were provided.");
                 } else {
                     return new SearchConsumer(te);
@@ -108,8 +109,7 @@ public final class Twitter4JFactory {
                     case RETWEETSOFME:
                         return new RetweetsConsumer(te);
                     case USER:
-                        if (te.getProperties().getUser() == null
-                            || te.getProperties().getUser().trim().isEmpty()) {
+                        if (te.getProperties().getUser() == null || te.getProperties().getUser().trim().isEmpty())
{
                             throw new IllegalArgumentException("Fetch type set to USER TIMELINE
but no user was set.");
                         } else {
                             return new UserConsumer(te);
@@ -155,7 +155,8 @@ public final class Twitter4JFactory {
             switch (ConsumerType.fromUri(uriSplit[0])) {
             case DIRECTMESSAGE:
                 if (te.getProperties().getUser() == null || te.getProperties().getUser().trim().isEmpty())
{
-                    throw new IllegalArgumentException("Producer type set to DIRECT MESSAGE
but no recipient user was set.");
+                    throw new IllegalArgumentException(
+                            "Producer type set to DIRECT MESSAGE but no recipient user was
set.");
                 } else {
                     return new DirectMessageProducer(te);
                 }
@@ -169,14 +170,16 @@ public final class Twitter4JFactory {
                     }
                 }
                 break;
+            case SEARCH:
+                return new SearchProducer(te);
             default:
                 break;
             }
-            
+
         }
 
         throw new IllegalArgumentException("Cannot create any producer with uri " + uri
-                                           + ". A producer type was not provided (or an incorrect
pairing was used).");
+                + ". A producer type was not provided (or an incorrect pairing was used).");
     }
 
     private static String[] splitUri(String uri) {

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConfiguration.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConfiguration.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConfiguration.java
Tue Jun 19 05:52:35 2012
@@ -35,6 +35,7 @@ public class TwitterConfiguration {
     private String type;
     private String locations;
     private String userIds;
+    private boolean filterOld = true;
     private Twitter twitter;
     private TwitterStream twitterStream;
 
@@ -134,6 +135,14 @@ public class TwitterConfiguration {
         this.userIds = userIds;
     }
 
+    public boolean isFilterOld() {
+        return filterOld;
+    }
+    
+    public void setFilterOld(boolean filterOld) {
+        this.filterOld = filterOld;
+    }
+
     public Twitter getTwitter() {
         return twitter;
     }

Added: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConstants.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConstants.java?rev=1351575&view=auto
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConstants.java
(added)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterConstants.java
Tue Jun 19 05:52:35 2012
@@ -0,0 +1,27 @@
+/**
+ * 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.twitter;
+
+public final class TwitterConstants {
+
+    public static final String TWITTER_SEARCH_QUERY = "CamelTwitterSearchQuery";
+
+    private TwitterConstants() {
+        // utility
+    }
+
+}

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpoint.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpoint.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpoint.java
Tue Jun 19 05:52:35 2012
@@ -17,7 +17,7 @@
 package org.apache.camel.component.twitter;
 
 import org.apache.camel.Endpoint;
-
+import org.apache.camel.component.twitter.data.EndpointType;
 import twitter4j.Twitter;
 
 public interface TwitterEndpoint extends Endpoint {
@@ -25,4 +25,6 @@ public interface TwitterEndpoint extends
     Twitter getTwitter();
 
     TwitterConfiguration getProperties();
+
+    EndpointType getEndpointType();
 }

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java
Tue Jun 19 05:52:35 2012
@@ -17,11 +17,14 @@
 package org.apache.camel.component.twitter;
 
 import org.apache.camel.Consumer;
+import org.apache.camel.ExchangePattern;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.component.direct.DirectEndpoint;
 import org.apache.camel.component.twitter.consumer.Twitter4JConsumer;
 import org.apache.camel.component.twitter.consumer.TwitterConsumerDirect;
+import org.apache.camel.component.twitter.data.EndpointType;
+
 import twitter4j.Twitter;
 
 /**
@@ -60,4 +63,14 @@ public class TwitterEndpointDirect exten
     public TwitterConfiguration getProperties() {
         return properties;
     }
+
+    @Override
+    public EndpointType getEndpointType() {
+        return EndpointType.DIRECT;
+    }
+
+    @Override
+    public ExchangePattern getExchangePattern() {
+        return ExchangePattern.InOptionalOut;
+    }
 }

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java
Tue Jun 19 05:52:35 2012
@@ -22,6 +22,7 @@ import org.apache.camel.Producer;
 import org.apache.camel.component.direct.DirectEndpoint;
 import org.apache.camel.component.twitter.consumer.Twitter4JConsumer;
 import org.apache.camel.component.twitter.consumer.TwitterConsumerEvent;
+import org.apache.camel.component.twitter.data.EndpointType;
 
 import twitter4j.Twitter;
 
@@ -58,4 +59,9 @@ public class TwitterEndpointEvent extend
     public TwitterConfiguration getProperties() {
         return properties;
     }
+
+    @Override
+    public EndpointType getEndpointType() {
+        return EndpointType.EVENT;
+    }
 }

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java
Tue Jun 19 05:52:35 2012
@@ -22,6 +22,7 @@ import org.apache.camel.Producer;
 import org.apache.camel.component.twitter.consumer.Twitter4JConsumer;
 import org.apache.camel.component.twitter.consumer.TwitterConsumer;
 import org.apache.camel.component.twitter.consumer.TwitterConsumerPolling;
+import org.apache.camel.component.twitter.data.EndpointType;
 import org.apache.camel.impl.DefaultPollingEndpoint;
 import twitter4j.Twitter;
 
@@ -67,4 +68,9 @@ public class TwitterEndpointPolling exte
     public TwitterConfiguration getProperties() {
         return properties;
     }
+
+    @Override
+    public EndpointType getEndpointType() {
+        return EndpointType.POLLING;
+    }
 }

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/consumer/search/SearchConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/consumer/search/SearchConsumer.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/consumer/search/SearchConsumer.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/consumer/search/SearchConsumer.java
Tue Jun 19 05:52:35 2012
@@ -16,11 +16,13 @@
  */
 package org.apache.camel.component.twitter.consumer.search;
 
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.camel.component.twitter.TwitterEndpoint;
 import org.apache.camel.component.twitter.consumer.Twitter4JConsumer;
-
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import twitter4j.Query;
 import twitter4j.QueryResult;
 import twitter4j.Tweet;
@@ -32,6 +34,7 @@ import twitter4j.TwitterException;
  */
 public class SearchConsumer extends Twitter4JConsumer {
 
+    private static final transient Logger LOG = LoggerFactory.getLogger(SearchConsumer.class);
     TwitterEndpoint te;
 
     public SearchConsumer(TwitterEndpoint te) {
@@ -41,12 +44,19 @@ public class SearchConsumer extends Twit
     public List<Tweet> pollConsume() throws TwitterException {
         String keywords = te.getProperties().getKeywords();
         Query query = new Query(keywords);
-        query.setSinceId(lastId);
+        if (te.getProperties().isFilterOld()) {
+            query.setSinceId(lastId);
+        }
+        LOG.debug("Searching twitter with keywords: {}", keywords);
         return search(query);
     }
 
     public List<Tweet> directConsume() throws TwitterException {
         String keywords = te.getProperties().getKeywords();
+        if (keywords == null || keywords.trim().length() == 0) {
+            return Collections.emptyList();
+        }
+        LOG.debug("Searching twitter with keywords: {}", keywords);
         return search(new Query(keywords));
     }
 
@@ -54,9 +64,13 @@ public class SearchConsumer extends Twit
         QueryResult qr = te.getTwitter().search(query);
         List<Tweet> tweets = qr.getTweets();
 
-        for (Tweet t : tweets) {
-            checkLastId(t.getId());
+        if (te.getProperties().isFilterOld()) {
+            for (Tweet t : tweets) {
+                checkLastId(t.getId());
+            }
         }
+
         return tweets;
     }
+
 }

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/DirectMessageProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/DirectMessageProducer.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/DirectMessageProducer.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/DirectMessageProducer.java
Tue Jun 19 05:52:35 2012
@@ -21,8 +21,6 @@ import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.component.twitter.TwitterEndpoint;
 import org.apache.camel.impl.DefaultProducer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Produces text as a direct message.
@@ -30,7 +28,6 @@ import org.slf4j.LoggerFactory;
  */
 public class DirectMessageProducer extends DefaultProducer implements Processor {
 
-    private static final transient Logger LOG = LoggerFactory.getLogger(DirectMessageProducer.class);
     private TwitterEndpoint te;
 
     public DirectMessageProducer(TwitterEndpoint te) {
@@ -46,7 +43,7 @@ public class DirectMessageProducer exten
         if (toUsername.isEmpty()) {
             throw new CamelExchangeException("Username not configured on TwitterEndpoint",
exchange);
         } else {
-            LOG.debug("Sending to: {} message: {}", toUsername, text);
+            log.debug("Sending to: {} message: {}", toUsername, text);
             te.getTwitter().sendDirectMessage(toUsername, text);
         }
     }

Added: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/SearchProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/SearchProducer.java?rev=1351575&view=auto
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/SearchProducer.java
(added)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/SearchProducer.java
Tue Jun 19 05:52:35 2012
@@ -0,0 +1,73 @@
+/**
+ * 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.twitter.producer;
+
+import java.util.List;
+
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.twitter.TwitterConstants;
+import org.apache.camel.component.twitter.TwitterEndpoint;
+import org.apache.camel.impl.DefaultProducer;
+import twitter4j.Query;
+import twitter4j.QueryResult;
+import twitter4j.Tweet;
+import twitter4j.Twitter;
+
+public class SearchProducer extends DefaultProducer {
+
+    private long lastId;
+    private TwitterEndpoint twitterEndpoint;
+
+    public SearchProducer(TwitterEndpoint te) {
+        super(te);
+        this.twitterEndpoint = te;
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        // keywords from header take precedence
+        String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_QUERY,
String.class);
+        if (keywords == null) {
+            keywords = twitterEndpoint.getProperties().getKeywords();
+        }
+
+        if (keywords == null) {
+            throw new CamelExchangeException("No keywords to use for query", exchange);
+        }
+
+        Query query = new Query(keywords);
+        if (lastId != 0) {
+            query.setSinceId(lastId);
+        }
+
+        Twitter twitter = twitterEndpoint.getTwitter();
+        log.debug("Searching twitter with keywords: {}", keywords);
+        QueryResult results = twitter.search(query);
+        List<Tweet> list = results.getTweets();
+
+        for (Tweet t : list) {
+            long newId = t.getId();
+            if (newId > lastId) {
+                lastId = newId;
+            }
+        }
+
+        exchange.getIn().setBody(list);
+    }
+
+}

Modified: camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java?rev=1351575&r1=1351574&r2=1351575&view=diff
==============================================================================
--- camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java
(original)
+++ camel/trunk/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/producer/UserProducer.java
Tue Jun 19 05:52:35 2012
@@ -20,8 +20,6 @@ import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.component.twitter.TwitterEndpoint;
 import org.apache.camel.impl.DefaultProducer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import twitter4j.StatusUpdate;
 
 /**
@@ -29,7 +27,6 @@ import twitter4j.StatusUpdate;
  */
 public class UserProducer extends DefaultProducer implements Processor {
 
-    private static final transient Logger LOG = LoggerFactory.getLogger(UserProducer.class);
     private TwitterEndpoint te;
 
     public UserProducer(TwitterEndpoint te) {
@@ -50,16 +47,16 @@ public class UserProducer extends Defaul
 
     private void updateStatus(StatusUpdate status) throws Exception {
         te.getTwitter().updateStatus(status);
-        LOG.debug("Updated status: {}", status);
+        log.debug("Updated status: {}", status);
     }
 
     private void updateStatus(String status) throws Exception {
         if (status.length() > 160) {
-            LOG.warn("Message is longer than 160 characters. Message will be truncated!");
+            log.warn("Message is longer than 160 characters. Message will be truncated!");
             status = status.substring(0, 160);
         }
 
         te.getTwitter().updateStatus(status);
-        LOG.debug("Updated status: {}", status);
+        log.debug("Updated status: {}", status);
     }
 }

Added: camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchByExchangeDirectTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchByExchangeDirectTest.java?rev=1351575&view=auto
==============================================================================
--- camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchByExchangeDirectTest.java
(added)
+++ camel/trunk/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchByExchangeDirectTest.java
Tue Jun 19 05:52:35 2012
@@ -0,0 +1,109 @@
+/**
+ * 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.twitter;
+
+import java.util.List;
+
+import junit.framework.Assert;
+import org.apache.camel.Exchange;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class SearchByExchangeDirectTest extends CamelTwitterTestSupport {
+
+    @Produce(uri = "direct:start")
+    protected ProducerTemplate template;
+
+    @Produce(uri = "direct:header")
+    protected ProducerTemplate templateHeader;
+
+    @Produce(uri = "direct:double")
+    protected ProducerTemplate templateDouble;
+
+    @Test
+    public void testSearchTimelineWithStaticQuery() throws Exception {
+        template.sendBody(null);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(1);
+        mock.assertIsSatisfied();
+        List<Exchange> tweets = mock.getExchanges();
+        for (Exchange e : tweets) {
+            log.info("Tweet: " + e.getIn().getBody(String.class));
+        }
+    }
+
+    @Test
+    public void testSearchTimelineWithDynamicQuery() throws Exception {
+        templateHeader.sendBodyAndHeader(null, TwitterConstants.TWITTER_SEARCH_QUERY, "java");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(1);
+        mock.assertIsSatisfied();
+        List<Exchange> tweets = mock.getExchanges();
+        for (Exchange e : tweets) {
+            log.info("Tweet: " + e.getIn().getBody(String.class));
+        }
+    }
+
+    @Test
+    public void testDoubleSearchKeepingOld() throws Exception {
+        templateDouble.sendBodyAndHeader(null, TwitterConstants.TWITTER_SEARCH_QUERY, "java");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(1);
+        mock.assertIsSatisfied();
+        List<Exchange> tweets = mock.getExchanges();
+        for (Exchange e : tweets) {
+            log.info("Tweet: " + e.getIn().getBody(String.class));
+        }
+
+        // calls the same query again, expecting to receive the same amount of
+        // tweets, because we told this route to not filter old(consumed) tweets
+        int total = mock.getReceivedCounter();
+        templateDouble.sendBodyAndHeader(null, TwitterConstants.TWITTER_SEARCH_QUERY, "java");
+
+        Assert.assertEquals(total, mock.getReceivedCounter());
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start")
+                    .toF("twitter://search?%s&keywords=java", getUriTokens())
+                    .split().body()
+                        .to("mock:result");
+
+                from("direct:header")
+                    .toF("twitter://search?%s", getUriTokens())
+                    .split().body()
+                        .to("mock:result");
+
+                from("direct:double")
+                    .toF("twitter://search?filterOld=false&%s", getUriTokens())
+                    .split().body()
+                        .to("mock:result");
+            }
+        };
+    }
+}



Mime
View raw message