INT-2024 added @Rule to run Redis tests only when Redis is available

This commit is contained in:
Oleg Zhurakousky
2011-08-01 08:48:20 -04:00
parent ffc4b07abb
commit 413e8a4689
9 changed files with 157 additions and 31 deletions

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.integration.redis.channel;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.BeanFactory;
@@ -23,6 +22,8 @@ import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.integration.Message;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -31,9 +32,11 @@ import static org.mockito.Mockito.verify;
* @author Oleg Zhurakousky
* @since 2.0
*/
public class SubscribableRedisChannelTests {
public class SubscribableRedisChannelTests extends RedisAvailableTests{
@Test @Ignore
@Test
@RedisAvailable
public void pubSubChanneTest() throws Exception{
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setPort(7379);
@@ -49,7 +52,7 @@ public class SubscribableRedisChannelTests {
channel.send(new GenericMessage<String>("1"));
channel.send(new GenericMessage<String>("2"));
channel.send(new GenericMessage<String>("3"));
Thread.sleep(100);
Thread.sleep(1000);
verify(handler, times(3)).handleMessage(Mockito.any(Message.class));
}
}

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.integration.redis.config;
import static junit.framework.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -27,15 +24,20 @@ import org.springframework.integration.MessagingException;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.test.util.TestUtils;
import static junit.framework.Assert.assertEquals;
/**
* @author Oleg Zhurakousky
*
*/
public class RedisChannelParserTests {
public class RedisChannelParserTests extends RedisAvailableTests{
@Test @Ignore
@Test
@RedisAvailable
public void testPubSubChannelConfig(){
ApplicationContext context = new ClassPathXmlApplicationContext("RedisChannelParserTests-context.xml", this.getClass());
SubscribableChannel redisChannel = context.getBean("redisChannel", SubscribableChannel.class);
@@ -46,7 +48,7 @@ public class RedisChannelParserTests {
}
@Test
@Ignore
@RedisAvailable
public void testPubSubChannelUsage(){
ApplicationContext context = new ClassPathXmlApplicationContext("RedisChannelParserTests-context.xml", this.getClass());
SubscribableChannel redisChannel = context.getBean("redisChannel", SubscribableChannel.class);

View File

@@ -27,6 +27,8 @@ import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.redis.inbound.RedisInboundChannelAdapter;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.support.converter.SimpleMessageConverter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -37,12 +39,13 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class RedisInboundChannelAdapterParserTests {
public class RedisInboundChannelAdapterParserTests extends RedisAvailableTests{
@Autowired
private ApplicationContext context;
@Test @Ignore
@Test
@RedisAvailable
public void validateConfiguration() {
RedisInboundChannelAdapter adapter = context.getBean("adapter", RedisInboundChannelAdapter.class);
assertEquals("adapter", adapter.getComponentName());
@@ -54,7 +57,8 @@ public class RedisInboundChannelAdapterParserTests {
assertEquals(converterBean, accessor.getPropertyValue("messageConverter"));
}
@Test @Ignore
@Test
@RedisAvailable
public void testInboundChannelAdapterMessaging() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setPort(7379);

View File

@@ -16,9 +16,6 @@
package org.springframework.integration.redis.config;
import static junit.framework.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
@@ -29,22 +26,27 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.redis.outbound.RedisPublishingMessageHandler;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.support.converter.SimpleMessageConverter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static junit.framework.Assert.assertEquals;
/**
* @author Oleg Zhurakousky
* @author Mark Fisher
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class RedisOutboundChannelAdapterParserTests {
public class RedisOutboundChannelAdapterParserTests extends RedisAvailableTests{
@Autowired
private ApplicationContext context;
@Test @Ignore
@Test
@RedisAvailable
public void validateConfiguration() {
EventDrivenConsumer adapter = context.getBean("outboundAdapter", EventDrivenConsumer.class);
RedisPublishingMessageHandler handler = (RedisPublishingMessageHandler)
@@ -56,7 +58,8 @@ public class RedisOutboundChannelAdapterParserTests {
assertEquals(converterBean, accessor.getPropertyValue("messageConverter"));
}
@Test @Ignore
@Test
@RedisAvailable
public void testOutboundChannelAdapterMessaging(){
MessageChannel sendChannel = context.getBean("sendChannel", MessageChannel.class);
sendChannel.send(new GenericMessage<String>("Hello Redis"));

View File

@@ -16,24 +16,26 @@
package org.springframework.integration.redis.inbound;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.integration.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Mark Fisher
* @since 2.1
*/
public class RedisInboundChannelAdapterTests {
public class RedisInboundChannelAdapterTests extends RedisAvailableTests{
@Test @Ignore
@Test
@RedisAvailable
public void testRedisInboundChannelAdapter() throws Exception {
int numToTest = 100;
String redisChannelName = "si.test.channel";

View File

@@ -16,13 +16,10 @@
package org.springframework.integration.redis.outbound;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
@@ -30,15 +27,20 @@ import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.support.MessageBuilder;
import static org.junit.Assert.assertEquals;
/**
* @author Mark Fisher
* @since 2.1
*/
public class RedisPublishingMessageHandlerTests {
public class RedisPublishingMessageHandlerTests extends RedisAvailableTests{
@Test @Ignore
@Test
@RedisAvailable
public void testRedisPublishingMessageHandler() throws Exception {
int numToTest = 100;
String topic = "si.test.channel";

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2011 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.redis.rules;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Oleg Zhurakousky
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface RedisAvailable {
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2002-2011 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.redis.rules;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
/**
* @author Oleg Zhurakousky
*
*/
public final class RedisAvailableRule implements MethodRule{
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement(){
@Override
public void evaluate() throws Throwable {
RedisAvailable redisAvailable = method.getAnnotation(RedisAvailable.class);
if (redisAvailable != null){
try {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setPort(7379);
connectionFactory.afterPropertiesSet();
connectionFactory.getConnection();
} catch (Exception e) {
System.out.println("Redis is not available. Skipping the test.");
return;
}
}
base.evaluate();
}
};
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2002-2011 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.redis.rules;
import org.junit.Rule;
/**
* @author Oleg Zhurakousky
*
*/
public class RedisAvailableTests {
@Rule
public RedisAvailableRule redisAvailableRule = new RedisAvailableRule();
}