From c57a0067cb9ce28aac72e56ebe51f4a1d2410b72 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Fri, 19 Nov 2010 03:48:59 -0500 Subject: [PATCH] Twitter test polishing --- .../TestReceivingUsingNamespace-context.xml | 16 +++--- .../TestSendingUpdatesUsingNamespace.java | 2 +- ...ectMessageReceivingMessageSourceTests.java | 57 +++++-------------- .../SearchReceivingMessageSourceTests.java | 5 ++ .../TimelineReceivingMessageSourceTests.java | 38 +++---------- .../src/test/java/twitter.receiver.properties | 8 +-- 6 files changed, 39 insertions(+), 87 deletions(-) diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestReceivingUsingNamespace-context.xml b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestReceivingUsingNamespace-context.xml index 585acb0ff6..45e8a05ef7 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestReceivingUsingNamespace-context.xml +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestReceivingUsingNamespace-context.xml @@ -34,10 +34,10 @@ - - - - + + + + @@ -49,10 +49,10 @@ - - - - + + + + diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestSendingUpdatesUsingNamespace.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestSendingUpdatesUsingNamespace.java index c809767f6a..d26146f8a7 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestSendingUpdatesUsingNamespace.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/ignored/TestSendingUpdatesUsingNamespace.java @@ -42,7 +42,7 @@ public class TestSendingUpdatesUsingNamespace extends AbstractJUnit4SpringContex @Test @Ignore public void testSendingATweet() throws Throwable { - MessageBuilder mb = MessageBuilder.withPayload("Aligning #springintegration Twitter adapter with #SpringSocial API - " + MessageBuilder mb = MessageBuilder.withPayload("Early start today" + new Date(System.currentTimeMillis())); Message m = mb.build(); this.messagingTemplate.send(this.channel, m); diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java index 785bdfcaaf..03425b9d4a 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/DirectMessageReceivingMessageSourceTests.java @@ -17,7 +17,7 @@ package org.springframework.integration.twitter.inbound; import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; @@ -28,10 +28,8 @@ import java.io.File; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; -import java.util.Queue; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; @@ -44,7 +42,6 @@ import org.springframework.integration.twitter.core.Tweet; import org.springframework.integration.twitter.core.Twitter4jTemplate; import org.springframework.integration.twitter.core.TwitterOperations; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; -import org.springframework.util.CollectionUtils; import twitter4j.DirectMessage; import twitter4j.Paging; @@ -106,39 +103,28 @@ public class DirectMessageReceivingMessageSourceTests { DirectMessageReceivingMessageSource source = new DirectMessageReceivingMessageSource(twitter); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); assertEquals("twitter:dm-inbound-channel-adapter.twitterEndpoint.kermit", TestUtils.getPropertyValue(source, "metadataKey")); - //assertTrue(source.isRunning()); - //source.stop(); } - @SuppressWarnings("rawtypes") + @SuppressWarnings({ "unchecked" }) @Test - @Ignore public void testSuccessfullInitializationWithMessages() throws Exception{ this.setUpMockScenarioForMessagePolling(); DirectMessageReceivingMessageSource source = new DirectMessageReceivingMessageSource(twitter); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); - Thread.sleep(1000); - Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets"); - assertTrue(!CollectionUtils.isEmpty(msg)); - assertEquals(1, msg.size()); - Tweet message = (Tweet) msg.poll(); + Message msg = (Message) source.receive(); + assertNotNull(msg); + + Tweet message = (Tweet) msg.getPayload(); assertEquals(2000, message.getId()); Thread.sleep(1000); verify(twitter, times(1)).getDirectMessages(); - // based on the Mock, the Queue should now have 2 more messages third and fourth - assertTrue(((Queue)TestUtils.getPropertyValue(source, "tweets")).size() == 2); - //source.stop(); } /** * This test will validate that last status is initialized from the metadatastore @@ -146,7 +132,6 @@ public class DirectMessageReceivingMessageSourceTests { */ @SuppressWarnings("rawtypes") @Test - @Ignore public void testSuccessfullInitializationWithMessagesWithPersistentMetadata() throws Exception{ String fileName = System.getProperty("java.io.tmpdir") + "/spring-integration/metadata-store.properties"; File file = new File(fileName); @@ -162,21 +147,12 @@ public class DirectMessageReceivingMessageSourceTests { source.setBeanFactory(bf); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); - Thread.sleep(1000); - Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets"); - assertTrue(!CollectionUtils.isEmpty(msg)); - assertEquals(1, msg.size()); - //Thread.sleep(15000); - Message message = source.receive(); - Tweet tweet = (Tweet) message.getPayload(); - assertEquals(2000, tweet.getId()); - //source.stop(); - Thread.sleep(3000); - + Message msg = source.receive(); + assertNotNull(msg); + Tweet tweet = (Tweet) msg.getPayload(); + // Resuming this.prepare(); @@ -190,18 +166,13 @@ public class DirectMessageReceivingMessageSourceTests { source.setBeanFactory(bf); scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); - Thread.sleep(1000); - msg = (Queue) TestUtils.getPropertyValue(source, "tweets"); - assertTrue(!CollectionUtils.isEmpty(msg)); - message = source.receive(); - tweet = (Tweet) message.getPayload(); + msg = source.receive(); + tweet = (Tweet) msg.getPayload(); assertEquals(3000, tweet.getId()); - message = source.receive(); - tweet = (Tweet) message.getPayload(); + msg = source.receive(); + tweet = (Tweet) msg.getPayload(); assertEquals(4000, tweet.getId()); file.delete(); } diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceTests.java index ab9d17d211..0b50e438f2 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceTests.java @@ -34,6 +34,11 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; */ public class SearchReceivingMessageSourceTests { + /** + * THis test is a sample test and wil require connecting to a real Twitter + * however no OAuth is required sincxe uts a search, so simply uncomment and run + * @throws Exception + */ @Test @Ignore public void testSearchReceiving() throws Exception{ diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/TimelineReceivingMessageSourceTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/TimelineReceivingMessageSourceTests.java index 63993ae8f2..8bcbd0d85f 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/TimelineReceivingMessageSourceTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/TimelineReceivingMessageSourceTests.java @@ -17,6 +17,7 @@ package org.springframework.integration.twitter.inbound; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -105,39 +106,26 @@ public class TimelineReceivingMessageSourceTests { TimelineReceivingMessageSource source = new TimelineReceivingMessageSource(twitter); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); assertEquals("twitter:inbound-channel-adapter.twitterEndpoint.kermit", TestUtils.getPropertyValue(source, "metadataKey")); - //assertTrue(source.isRunning()); - //source.stop(); } @SuppressWarnings("rawtypes") @Test - @Ignore public void testSuccessfulInitializationWithMessages() throws Exception{ this.setUpMockScenarioForMessagePolling(); TimelineReceivingMessageSource source = new TimelineReceivingMessageSource(twitter); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); - Thread.sleep(1000); - Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets"); - assertTrue(!CollectionUtils.isEmpty(msg)); - assertEquals(1, msg.size()); // because the other message has a older timestamp and is assumed to be read by - Tweet message = (Tweet) msg.poll(); + Message msg = source.receive(); + assertNotNull(msg); + Tweet message = (Tweet) msg.getPayload(); assertEquals(2000, message.getId()); - Thread.sleep(1000); - verify(twitter, times(1)).getTimeline(2000); - // based on the Mock, the Queue shoud now have 2 mopre messages third and fourth - assertTrue(((Queue)TestUtils.getPropertyValue(source, "tweets")).size() == 2); - //source.stop(); + verify(twitter, times(1)).getTimeline(); } /** * This test will validate that last status is initilaized from the metadatastore @@ -145,7 +133,6 @@ public class TimelineReceivingMessageSourceTests { */ @SuppressWarnings("rawtypes") @Test - @Ignore public void testSuccessfulInitializationWithMessagesWithPersistentMetadata() throws Exception{ String fileName = System.getProperty("java.io.tmpdir") + "/spring-integration/metadata-store.properties"; File file = new File(fileName); @@ -161,19 +148,12 @@ public class TimelineReceivingMessageSourceTests { source.setBeanFactory(bf); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); - Thread.sleep(1000); - Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets"); - assertTrue(!CollectionUtils.isEmpty(msg)); - assertEquals(1, msg.size()); // because the other message has a older timestamp and is assumed to be read by + Message message = source.receive(); Tweet tweet = (Tweet) message.getPayload(); assertEquals(2000, tweet.getId()); - //source.stop(); - Thread.sleep(2000); // Resuming @@ -188,13 +168,9 @@ public class TimelineReceivingMessageSourceTests { source.setBeanFactory(bf); scheduler = new ThreadPoolTaskScheduler(); scheduler.afterPropertiesSet(); - //source.setTaskScheduler(scheduler); source.setBeanName("twitterEndpoint"); source.afterPropertiesSet(); - //source.start(); - Thread.sleep(1000); - msg = (Queue) TestUtils.getPropertyValue(source, "tweets"); - assertTrue(!CollectionUtils.isEmpty(msg)); + message = source.receive(); tweet = (Tweet) message.getPayload(); assertEquals(3000, tweet.getId()); diff --git a/spring-integration-twitter/src/test/java/twitter.receiver.properties b/spring-integration-twitter/src/test/java/twitter.receiver.properties index 869caa3956..4bd8eb5301 100644 --- a/spring-integration-twitter/src/test/java/twitter.receiver.properties +++ b/spring-integration-twitter/src/test/java/twitter.receiver.properties @@ -1,5 +1,5 @@ # oauth setup for prosibook twitter account -twitter.oauth.consumerKey=OU4CbkHKIWl1SI0VwiOgAQ -twitter.oauth.consumerSecret=p6pPukUG6d0ebSXuLSI9iaq2MIpzxJoLZyj6ilRmO3o -twitter.oauth.accessToken=61091649-vWVUNginOL069jjpm2lHEuxGXjW163kK3CPNZCdcc -twitter.oauth.accessTokenSecret=dufKyVlcngRitGDFfqiTlJC5leh6lsxPaq8FbeBc \ No newline at end of file +twitter.oauth.consumerKey= +twitter.oauth.consumerSecret= +twitter.oauth.accessToken= +twitter.oauth.accessTokenSecret=