INT-1553 initial refactoring to introduce Twitter4jTemplate in place of the twitter-connection

This commit is contained in:
Oleg Zhurakousky
2010-11-08 09:03:44 -05:00
parent 16458de1f5
commit 56143b63aa
21 changed files with 345 additions and 202 deletions

View File

@@ -17,11 +17,7 @@
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd">
<twitter:twitter-connection id="twitter"
consumer-key="consumerKey"
consumer-secret="consumerSecret"
access-token="accessToken"
access-token-secret="accessTokenSecret"/>
<beans:bean id="twitter" class="org.springframework.integration.twitter.config.TestReceivingMessageSourceParserTests.TwitterTemplateFactoryBean"/>
<channel id="inbound_mentions"/>

View File

@@ -16,12 +16,17 @@
package org.springframework.integration.twitter.config;
import static junit.framework.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.twitter.core.Twitter4jTemplate;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.integration.twitter.inbound.AbstractTwitterMessageSource;
/**
@@ -45,4 +50,25 @@ public class TestReceivingMessageSourceParserTests {
ms = (AbstractTwitterMessageSource<?>) TestUtils.getPropertyValue(spca, "source");
assertFalse(ms.isAutoStartup());
}
public static class TwitterTemplateFactoryBean implements FactoryBean<TwitterOperations>{
@Override
public TwitterOperations getObject() throws Exception {
TwitterOperations oper = mock(TwitterOperations.class);
when(oper.getProfileId()).thenReturn("kermit");
return oper;
}
@Override
public Class<?> getObjectType() {
return TwitterOperations.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
}

View File

@@ -18,11 +18,7 @@
http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter-2.0.xsd">
<twitter:twitter-connection id="twitter"
consumer-key="consumerKey"
consumer-secret="consumerSecret"
access-token="accessToken"
access-token-secret="accessTokenSecret"/>
<beans:bean id="twitter" class="org.springframework.integration.twitter.core.Twitter4jTemplate"/>
<channel id="inbound_mentions"/>

View File

@@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:twitter="http://www.springframework.org/schema/integration/twitter"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter-2.0.xsd">
<twitter:twitter-connection id="twitter"
consumer-key="consumerKey"
consumer-secret="consumerSecret"
access-token="accessToken"
access-token-secret="accessTokenSecret"/>
</beans:beans>

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed 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.springframework.integration.twitter.config;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.twitter.oauth.OAuthTwitterFactoryBean;
import twitter4j.Twitter;
import twitter4j.http.AccessToken;
/**
* @author Oleg Zhurakousky
* @since 2.0
*
*/
public class TwitterConnectionParserTests {
@Test
public void testOAuthTwitterFactoryBean(){
ApplicationContext ac = new ClassPathXmlApplicationContext("TwitterConnectionParserTests-context.xml", this.getClass());
OAuthTwitterFactoryBean twitterFb = ac.getBean("&twitter", OAuthTwitterFactoryBean.class);
assertEquals("consumerKey", TestUtils.getPropertyValue(twitterFb, "consumerKey"));
assertEquals("consumerSecret", TestUtils.getPropertyValue(twitterFb, "consumerSecret"));
AccessToken accessToken = (AccessToken) TestUtils.getPropertyValue(twitterFb, "accessToken");
assertEquals("accessToken", accessToken.getToken());
assertEquals("accessTokenSecret", accessToken.getTokenSecret());
Twitter twitter = ac.getBean("twitter", Twitter.class);
assertTrue(twitter.isOAuthEnabled());
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed 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.springframework.integration.twitter.core;
import org.junit.Test;
/**
* @author Oleg Zhurakousky
*
*/
public class Twitter4jTemplateTests {
@Test
public void testProfileId(){
}
}

View File

@@ -23,13 +23,13 @@ import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Queue;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.CollectionUtils;
@@ -37,8 +37,6 @@ import twitter4j.DirectMessage;
import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.http.AccessToken;
/**
* @author Oleg Zhurakousky
@@ -49,12 +47,12 @@ public class DirectMessageReceivingMessageSourceTests {
private DirectMessage secondMessage;
private Twitter twitter = mock(Twitter.class);
private TwitterOperations twitter;
@Before
public void prepare() throws Exception{
twitter = mock(Twitter.class);
twitter = mock(TwitterOperations.class);
firstMessage = mock(DirectMessage.class);
when(firstMessage.getCreatedAt()).thenReturn(new Date(5555555555L));
when(firstMessage.getId()).thenReturn(200);
@@ -63,7 +61,7 @@ public class DirectMessageReceivingMessageSourceTests {
when(secondMessage.getId()).thenReturn(2000);
when(twitter.getOAuthAccessToken()).thenReturn(new AccessToken("token123", "tokenSecret123"));
when(twitter.getProfileId()).thenReturn("kermit");
}
@@ -76,7 +74,7 @@ public class DirectMessageReceivingMessageSourceTests {
source.setBeanName("twitterEndpoint");
source.afterPropertiesSet();
source.start();
assertEquals("twitter:inbound-dm-channel-adapter.twitterEndpoint.token123", TestUtils.getPropertyValue(source, "metadataKey"));
assertEquals("twitter:inbound-dm-channel-adapter.twitterEndpoint.kermit", TestUtils.getPropertyValue(source, "metadataKey"));
assertTrue(source.isRunning());
}

View File

@@ -22,10 +22,10 @@ import static org.mockito.Mockito.when;
import java.util.Date;
import org.junit.Test;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.scheduling.TriggerContext;
import twitter4j.RateLimitStatus;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
@@ -35,7 +35,7 @@ public class RateLimitStatusTriggerTests {
@Test
public void testTriggerImediateAndSubsequentExecutionTime() throws Exception{
Twitter twitter = mock(Twitter.class);
TwitterOperations twitter = mock(TwitterOperations.class);
RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(twitter);
TriggerContext context = mock(TriggerContext.class);
Date currentDate = new Date(System.currentTimeMillis());

View File

@@ -23,16 +23,16 @@ import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.integration.twitter.core.TwitterOperations;
import twitter4j.GeoLocation;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
*/
public class OutboundDirectMessageMessageHandlerTests {
private Twitter twitter = mock(Twitter.class);
private TwitterOperations twitter = mock(TwitterOperations.class);
@Test
public void validateSendDirectMessage() throws Exception{