From ab8e193b01636ae2112e36edac6af3f10ca14f87 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 7 Apr 2014 09:11:18 -0400 Subject: [PATCH] INT-3355 Improve Redis Tests JIRA: https://jira.spring.io/browse/INT-3355 Previously, the RedisAvailableTests unconditionally flushed Redis; this is not appropriate and could affect other builds on the CI server (as well as wiping out the developer's Redis DB). Change the tests to clean up their own redis data. Conflicts: spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java Resolved. --- .gitignore | 1 + ...InboundChannelAdapterIntegrationTests.java | 58 ++++++++++++++++++- .../redis/inbound/list-inbound-adapter.xml | 2 +- .../metadata/RedisMetadataStoreTests.java | 29 ++++++---- ...utboundChannelAdapterIntegrationTests.java | 46 ++++++++++++++- .../RedisStoreWritingMessageHandlerTests.java | 28 +++++++++ .../redis/rules/RedisAvailableTests.java | 52 +++++++++-------- ...ayerHandlerRescheduleIntegrationTests.java | 7 ++- .../store/RedisMessageGroupStoreTests.java | 18 +++++- .../redis/store/RedisMessageStoreTests.java | 12 +++- 10 files changed, 211 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 4b939dc033..11fee3f1a0 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ target vf.gf.dmn-* /atlassian-ide-plugin.xml hostkey.ser +.springBeans diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java index dda1589666..66ceb84f28 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -21,14 +21,21 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.support.collections.RedisList; import org.springframework.data.redis.support.collections.RedisZSet; import org.springframework.integration.Message; +import org.springframework.integration.MessagingException; import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.core.MessageHandler; +import org.springframework.integration.core.SubscribableChannel; import org.springframework.integration.endpoint.SourcePollingChannelAdapter; import org.springframework.integration.redis.rules.RedisAvailable; import org.springframework.integration.redis.rules.RedisAvailableTests; @@ -36,6 +43,7 @@ import org.springframework.integration.redis.rules.RedisAvailableTests; /** * @author Oleg Zhurakousky * @author Artem Bilan + * @author Gary Russell * @since 2.2 */ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvailableTests { @@ -59,14 +67,18 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila message = (Message>) redisChannel.receive(1000); assertNotNull(message); assertEquals(13, message.getPayload().size()); + this.deletePresidents(jcf); context.close(); } @Test @RedisAvailable @SuppressWarnings("unchecked") + // synchronization commit renames the list public void testListInboundConfigurationWithSynchronization() throws Exception{ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + StringRedisTemplate template = this.createStringRedisTemplate(jcf); + template.delete("bar"); this.prepareList(jcf); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("list-inbound-adapter.xml", this.getClass()); SourcePollingChannelAdapter spca = context.getBean("listAdapterWithSynchronization", SourcePollingChannelAdapter.class); @@ -80,6 +92,44 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila //poll again, should get nothing since the collection was removed during synchronization message = (Message>) redisChannel.receive(1000); assertNull(message); + assertEquals(Long.valueOf(13), template.boundListOps("bar").size()); + template.delete("bar"); + + spca.stop(); + context.close(); + } + + @Test + @RedisAvailable + // synchronization rollback renames the list + public void testListInboundConfigurationWithSynchronizationAndRollback() throws Exception{ + RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + StringRedisTemplate template = this.createStringRedisTemplate(jcf); + template.delete("baz"); + this.prepareList(jcf); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("list-inbound-adapter.xml", + this.getClass()); + SubscribableChannel fail = context.getBean("redisFailChannel", SubscribableChannel.class); + final CountDownLatch latch = new CountDownLatch(1); + fail.subscribe(new MessageHandler() { + + @Override + public void handleMessage(Message message) throws MessagingException { + latch.countDown(); + throw new RuntimeException("Test Rollback"); + } + }); + SourcePollingChannelAdapter spca = context.getBean("listAdapterWithSynchronizationAndRollback", + SourcePollingChannelAdapter.class); + spca.start(); + assertTrue(latch.await(10, TimeUnit.SECONDS)); + int n = 0; + while (n++ < 100 && template.keys("baz").size() == 0) { + Thread.sleep(100); + } + assertTrue("Rename didn't occcur", n < 100); + assertEquals(Long.valueOf(13), template.boundListOps("baz").size()); + template.delete("baz"); spca.stop(); context.close(); @@ -88,8 +138,11 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila @Test @RedisAvailable @SuppressWarnings("unchecked") + // synchronization commit renames the list public void testListInboundConfigurationWithSynchronizationAndTemplate() throws Exception{ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + StringRedisTemplate template = this.createStringRedisTemplate(jcf); + template.delete("bar"); this.prepareList(jcf); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("list-inbound-adapter.xml", this.getClass()); SourcePollingChannelAdapter spca = context.getBean("listAdapterWithSynchronizationAndRedisTemplate", SourcePollingChannelAdapter.class); @@ -103,6 +156,8 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila //poll again, should get nothing since the collection was removed during synchronization message = (Message>) redisChannel.receive(1000); assertNull(message); + assertEquals(Long.valueOf(13), template.boundListOps("bar").size()); + template.delete("bar"); spca.stop(); context.close(); @@ -201,6 +256,7 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila zsetAdapterNoScore.stop(); zsetAdapterWithSingleScoreAndSynchronization.stop(); + this.deletePresidents(jcf); context.close(); } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml index b40c91700f..9027b98590 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml @@ -49,7 +49,7 @@ - + diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java index f08856f77e..9e53d63c89 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors + * Copyright 2013-2014 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. @@ -19,6 +19,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -30,11 +32,18 @@ import org.springframework.integration.redis.rules.RedisAvailableTests; /** * @author Gunnar Hillert * @author Artem Bilan + * @author Gary Russell * @since 3.0 * */ public class RedisMetadataStoreTests extends RedisAvailableTests { + @Before + @After + public void setUpTearDown() { + this.createStringRedisTemplate(this.getConnectionFactoryForTest()).delete("testMetadata"); + } + @Test @RedisAvailable public void testGetNonExistingKeyValue() { @@ -48,11 +57,11 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @RedisAvailable public void testPersistKeyValue() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "foo"); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); metadataStore.put("RedisMetadataStoreTests-Spring", "Integration"); StringRedisTemplate redisTemplate = new StringRedisTemplate(jcf); - BoundHashOperations ops = redisTemplate.boundHashOps("foo"); + BoundHashOperations ops = redisTemplate.boundHashOps("testMetadata"); assertEquals("Integration", ops.get("RedisMetadataStoreTests-Spring")); } @@ -62,7 +71,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { public void testGetValueFromMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); metadataStore.put("RedisMetadataStoreTests-GetValue", "Hello Redis"); String retrievedValue = metadataStore.get("RedisMetadataStoreTests-GetValue"); @@ -74,7 +83,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { public void testPersistEmptyStringToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); metadataStore.put("RedisMetadataStoreTests-PersistEmpty", ""); String retrievedValue = metadataStore.get("RedisMetadataStoreTests-PersistEmpty"); @@ -86,7 +95,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { public void testPersistNullStringToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); try { metadataStore.put("RedisMetadataStoreTests-PersistEmpty", null); @@ -104,7 +113,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @RedisAvailable public void testPersistWithEmptyKeyToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); metadataStore.put("", "PersistWithEmptyKey"); String retrievedValue = metadataStore.get(""); @@ -115,7 +124,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @RedisAvailable public void testPersistWithNullKeyToMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); try { metadataStore.put(null, "something"); @@ -132,7 +141,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @RedisAvailable public void testGetValueWithNullKeyFromMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); try { metadataStore.get(null); @@ -149,7 +158,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests { @RedisAvailable public void testRemoveFromMetadataStore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); - RedisMetadataStore metadataStore = new RedisMetadataStore(jcf); + RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata"); String testKey = "RedisMetadataStoreTests-Remove"; String testValue = "Integration"; diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java index 33646a2153..708c0e31ec 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2013 the original author or authors + * Copyright 2007-2014 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. @@ -56,6 +56,7 @@ import org.springframework.integration.test.util.TestUtils; /** * @author Oleg Zhurakousky * @author Mark Fisher + * @author Gary Russell * @since 2.2 */ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvailableTests { @@ -65,6 +66,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail public void testListWithKeyAsHeader(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisList redisList = new DefaultRedisList("pepboys", this.initTemplate(jcf, new StringRedisTemplate())); assertEquals(0, redisList.size()); @@ -79,6 +81,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail redisChannel.send(message); assertEquals(3, redisList.size()); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @@ -98,12 +102,14 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals(1, redisList.size()); redisTemplate.delete("foo"); + context.close(); } @Test @RedisAvailable public void testListWithProvidedKey(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisList redisList = new DefaultRedisList("pepboys", this.initTemplate(jcf, new StringRedisTemplate())); assertEquals(0, redisList.size()); @@ -118,6 +124,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail redisChannel.send(message); assertEquals(3, redisList.size()); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @@ -143,6 +151,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals(1, redisZSet.size()); assertEquals(Double.valueOf(2), redisZSet.score("bar")); redisTemplate.delete("foo"); + context.close(); } @Test @@ -171,6 +180,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals(1, redisZSet.size()); assertEquals(Double.valueOf(1), redisZSet.score("bar")); redisTemplate.delete("foo"); + context.close(); } @Test @@ -199,6 +209,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals(1, redisZSet.size()); assertEquals(Double.valueOf(4), redisZSet.score("bar")); redisTemplate.delete("foo"); + context.close(); } @Test @@ -228,12 +239,14 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals(1, redisZSet.size()); assertEquals(Double.valueOf(15), redisZSet.score("bar")); redisTemplate.delete("foo"); + context.close(); } @Test @RedisAvailable public void testMapToZsetWithProvidedKey(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deletePresidents(jcf); RedisZSet redisZset = new DefaultRedisZSet("presidents", this.initTemplate(jcf, new StringRedisTemplate())); assertEquals(0, redisZset.size()); @@ -274,12 +287,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals(1, redisZset.rangeByScore(18, 18).size()); assertEquals(4, redisZset.rangeByScore(18, 19).size()); assertEquals(1, redisZset.rangeByScore(31, 31).size()); + this.deletePresidents(jcf); + context.close(); } @Test @RedisAvailable public void testMapToMapWithProvidedKey(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisMap redisMap = new DefaultRedisMap("pepboys", this.initTemplate(jcf, new StringRedisTemplate())); @@ -303,12 +319,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail RedisStoreWritingMessageHandler.class); assertEquals("pepboys", TestUtils.getPropertyValue(handler, "keyExpression", LiteralExpression.class).getExpressionString()); assertEquals("'foo'", TestUtils.getPropertyValue(handler, "mapKeyExpression", SpelExpression.class).getExpressionString()); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test(expected=MessageHandlingException.class) // map key is not provided @RedisAvailable public void testMapToMapAsSingleEntryWithKeyAsHeaderFail(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisMap> redisMap = new DefaultRedisMap>("pepboys", this.initTemplate(jcf, new RedisTemplate>>())); @@ -325,12 +344,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail Message> message = MessageBuilder.withPayload(pepboys). setHeader(RedisHeaders.KEY, "pepboys").build(); redisChannel.send(message); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test(expected=MessageHandlingException.class) // key is not provided @RedisAvailable public void testMapToMapNoKey(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisTemplate>> redisTemplate = new RedisTemplate>>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); @@ -349,12 +371,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail Message> message = MessageBuilder.withPayload(pepboys).build(); redisChannel.send(message); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @RedisAvailable public void testMapToMapAsSingleEntryWithKeyAsHeader(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisTemplate>> redisTemplate = new RedisTemplate>>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); @@ -379,12 +404,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals("Manny", pepboyz.get("1")); assertEquals("Moe", pepboyz.get("2")); assertEquals("Jack", pepboyz.get("3")); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @RedisAvailable public void testStoreSimpleStringInMap(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "bar"); StringRedisTemplate redisTemplate = new StringRedisTemplate(); RedisMap redisMap = new DefaultRedisMap("bar", @@ -401,12 +429,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail String hello = redisMap.get("foo"); assertEquals("hello, world!", hello); + this.deleteKey(jcf, "bar"); + context.close(); } @Test @RedisAvailable public void testSetWithKeyAsHeader(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisSet redisSet = new DefaultRedisSet("pepboys", this.initTemplate(jcf, new StringRedisTemplate())); assertEquals(0, redisSet.size()); @@ -421,6 +452,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail redisChannel.send(message); assertEquals(3, redisSet.size()); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @@ -440,12 +473,14 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals(1, redisSet.size()); redisTemplate.delete("foo"); + context.close(); } @Test @RedisAvailable public void testSetWithKeyAsHeaderNotParsed(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); @@ -463,12 +498,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail redisChannel.send(message); assertEquals(1, redisSet.size()); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @RedisAvailable public void testPojoIntoSet(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisSet redisSet = new DefaultRedisSet("pepboys", this.initTemplate(jcf, new StringRedisTemplate())); assertEquals(0, redisSet.size()); @@ -480,12 +518,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail redisChannel.send(message); assertEquals(1, redisSet.size()); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @RedisAvailable public void testProperties(){ RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "pepboys"); RedisProperties redisProperties = new RedisProperties("pepboys", this.initTemplate(jcf, new StringRedisTemplate())); @@ -503,6 +544,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals("Manny", redisProperties.get("1")); assertEquals("Moe", redisProperties.get("2")); assertEquals("Jack", redisProperties.get("3")); + this.deleteKey(jcf, "pepboys"); + context.close(); } @Test @@ -525,6 +568,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail assertEquals("bar", redisProperties.get("qux")); redisTemplate.delete("foo"); + context.close(); } private RedisTemplate initTemplate(RedisConnectionFactory rcf, RedisTemplate redisTemplate){ diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java index c805f02ecd..732deda158 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java @@ -33,6 +33,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ZSetOperations.TypedTuple; +import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.data.redis.support.collections.DefaultRedisList; import org.springframework.data.redis.support.collections.DefaultRedisZSet; import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType; @@ -58,6 +59,7 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ @RedisAvailable public void testListWithListPayloadParsedAndProvidedKey() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisList redisList = new DefaultRedisList(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -80,12 +82,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ assertEquals("Manny", redisList.get(0)); assertEquals("Moe", redisList.get(1)); assertEquals("Jack", redisList.get(2)); + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testListWithListPayloadParsedAndProvidedKeyAsHeader() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisList redisList = new DefaultRedisList(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -108,12 +112,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ assertEquals("Manny", redisList.get(0)); assertEquals("Moe", redisList.get(1)); assertEquals("Jack", redisList.get(2)); + this.deleteKey(jcf, "foo"); } @RedisAvailable @Test(expected=MessageHandlingException.class) public void testListWithListPayloadParsedAndNoKey() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisList redisList = new DefaultRedisList(key, this.initTemplate(jcf, new RedisTemplate())); @@ -130,12 +136,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ list.add("Jack"); Message> message = MessageBuilder.withPayload(list).build(); handler.handleMessage(message); + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testListWithListPayloadAsSingleEntry() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisList> redisList = new DefaultRedisList>(key, this.initTemplate(jcf, new RedisTemplate>())); @@ -161,12 +169,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ assertEquals("Manny", resultList.get(0)); assertEquals("Moe", resultList.get(1)); assertEquals("Jack", resultList.get(2)); + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testZsetWithListPayloadParsedAndProvidedKeyDefault() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisZSet redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -199,12 +209,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple pepboy : pepboys) { assertEquals(Double.valueOf(2), pepboy.getScore()); } + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrement() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisZSet redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -240,12 +252,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple pepboy : pepboys) { assertTrue(pepboy.getScore() == 2); } + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrementAsStringHeader() {// see INT-2775 RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisZSet redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -281,12 +295,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple pepboy : pepboys) { assertTrue(pepboy.getScore() == 2); } + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testZsetWithListPayloadAsSingleEntryAndHeaderKeyHeaderScore() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisZSet> redisZset = new DefaultRedisZSet>(key, this.initTemplate(jcf, new RedisTemplate>())); @@ -314,12 +330,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple> pepboys : entries) { assertTrue(pepboys.getScore() == 4); } + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testZsetWithMapPayloadParsedHeaderKey() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deletePresidents(jcf); String key = "presidents"; RedisZSet redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -356,12 +374,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ Set> entries = redisZset.rangeByScoreWithScores(18, 19); assertEquals(6, entries.size()); + this.deletePresidents(jcf); } @Test @RedisAvailable public void testZsetWithMapPayloadPojoParsedHeaderKey() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deletePresidents(jcf); String key = "presidents"; RedisZSet redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new RedisTemplate())); @@ -399,12 +419,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ Set> entries = redisZset.rangeByScoreWithScores(18, 19); assertEquals(6, entries.size()); + this.deletePresidents(jcf); } @Test @RedisAvailable public void testZsetWithMapPayloadPojoAsSingleEntryHeaderKey() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deletePresidents(jcf); String key = "presidents"; RedisZSet> redisZset = new DefaultRedisZSet>(key, this.initTemplate(jcf, new RedisTemplate>())); @@ -429,6 +451,7 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ handler.handleMessage(message); assertEquals(1, redisZset.size()); + this.deletePresidents(jcf); } @Test(expected=IllegalStateException.class) @@ -473,6 +496,7 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ @RedisAvailable public void testMapWithMapKeyExpression() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf); @@ -485,12 +509,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ catch (Exception e) { fail("No exception expected:" + e.getMessage()); } + this.deleteKey(jcf, "foo"); } @Test @RedisAvailable public void testPropertiesWithMapKeyExpression() { RedisConnectionFactory jcf = this.getConnectionFactoryForTest(); + this.deleteKey(jcf, "foo"); String key = "foo"; RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf); @@ -503,10 +529,12 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ catch (Exception e) { fail("No exception expected:" + e.getMessage()); } + this.deleteKey(jcf, "foo"); } private RedisTemplate initTemplate(RedisConnectionFactory rcf, RedisTemplate redisTemplate) { redisTemplate.setConnectionFactory(rcf); + redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java index b1d089a889..baa0d82de1 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java @@ -17,18 +17,13 @@ package org.springframework.integration.redis.rules; import static org.junit.Assert.assertTrue; -import java.util.UUID; - import org.junit.Rule; -import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.BoundListOperations; import org.springframework.data.redis.core.BoundZSetOperations; -import org.springframework.data.redis.core.RedisCallback; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.integration.test.util.TestUtils; @@ -44,19 +39,14 @@ public class RedisAvailableTests { @Rule public RedisAvailableRule redisAvailableRule = new RedisAvailableRule(); - protected RedisConnectionFactory getConnectionFactoryForTest(){ - LettuceConnectionFactory connectionFactory = RedisAvailableRule.connectionFactoryResource.get(); - RedisTemplate rt = new RedisTemplate(); - rt.setConnectionFactory(connectionFactory); - rt.afterPropertiesSet(); - rt.execute(new RedisCallback() { + private RedisConnectionFactory connectionFactory; - public Object doInRedis(RedisConnection connection) - throws DataAccessException { - connection.flushDb(); - return null; - } - }); + protected RedisConnectionFactory getConnectionFactoryForTest() { + if (this.connectionFactory != null) { + return this.connectionFactory; + } + LettuceConnectionFactory connectionFactory = RedisAvailableRule.connectionFactoryResource.get(); + this.connectionFactory = connectionFactory; return connectionFactory; } @@ -89,9 +79,8 @@ public class RedisAvailableTests { protected void prepareList(RedisConnectionFactory connectionFactory){ - StringRedisTemplate redisTemplate = new StringRedisTemplate(); - redisTemplate.setConnectionFactory(connectionFactory); - redisTemplate.afterPropertiesSet(); + StringRedisTemplate redisTemplate = createStringRedisTemplate(connectionFactory); + redisTemplate.delete("presidents"); BoundListOperations ops = redisTemplate.boundListOps("presidents"); ops.rightPush("John Adams"); @@ -113,10 +102,9 @@ public class RedisAvailableTests { protected void prepareZset(RedisConnectionFactory connectionFactory){ - StringRedisTemplate redisTemplate = new StringRedisTemplate(); - redisTemplate.setConnectionFactory(connectionFactory); - redisTemplate.afterPropertiesSet(); + StringRedisTemplate redisTemplate = createStringRedisTemplate(connectionFactory); + redisTemplate.delete("presidents"); BoundZSetOperations ops = redisTemplate.boundZSetOps("presidents"); ops.add("John Adams", 18); @@ -133,7 +121,23 @@ public class RedisAvailableTests { ops.add("Ronald Reagan", 20); ops.add("William J. Clinton", 20); ops.add("Abraham Lincoln", 19); - ops.add("George Washington", 18); + ops.add("George Wahington", 18); + } + + protected void deletePresidents(RedisConnectionFactory connectionFactory){ + this.deleteKey(connectionFactory, "presidents"); + } + + protected void deleteKey(RedisConnectionFactory connectionFactory, String key) { + StringRedisTemplate redisTemplate = createStringRedisTemplate(connectionFactory); + redisTemplate.delete(key); + } + + protected StringRedisTemplate createStringRedisTemplate(RedisConnectionFactory connectionFactory) { + StringRedisTemplate redisTemplate = new StringRedisTemplate(); + redisTemplate.setConnectionFactory(connectionFactory); + redisTemplate.afterPropertiesSet(); + return redisTemplate; } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java index 7dcd7483e2..ff5fba3c06 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 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 @@ -21,6 +21,7 @@ import static org.junit.Assert.fail; import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; @@ -44,6 +45,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; /** * @author Artem Bilan + * @author Gary Russell * @since 3.0 */ public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTests { @@ -62,6 +64,7 @@ public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTest connectionFactory.afterPropertiesSet(); } + @AfterClass public static void tearDown() { connectionFactory.destroy(); } @@ -127,6 +130,8 @@ public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTest assertEquals(1, messageStore.getMessageGroupCount()); assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId)); + messageStore.removeMessageGroup(delayerMessageGroupId); + } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java index d7be5f82a7..246da6fc36 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java @@ -31,11 +31,14 @@ import java.util.concurrent.TimeUnit; import junit.framework.AssertionFailedError; +import org.junit.After; +import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.channel.DirectChannel; @@ -51,10 +54,20 @@ import org.springframework.integration.support.MessageBuilder; /** * @author Oleg Zhurakousky * @author Artem Bilan + * @author Gary Russell * */ public class RedisMessageGroupStoreTests extends RedisAvailableTests { + @Before + @After + public void setUpTearDown() { + StringRedisTemplate template = this.createStringRedisTemplate(this.getConnectionFactoryForTest()); + template.delete("MESSAGE_GROUP_1"); + template.delete("MESSAGE_GROUP_2"); + template.delete("MESSAGE_GROUP_3"); + } + @Test @RedisAvailable public void testNonExistingEmptyMessageGroup() throws Exception{ @@ -347,9 +360,7 @@ public class RedisMessageGroupStoreTests extends RedisAvailableTests { @Test @RedisAvailable - public void testWithAggregatorWithShutdown(){ - this.getConnectionFactoryForTest(); // for this test it only ensures that DB was flushed before test - + public void testWithAggregatorWithShutdown() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("redis-aggregator-config.xml", this.getClass()); MessageChannel input = context.getBean("inputChannel", MessageChannel.class); QueueChannel output = context.getBean("outputChannel", QueueChannel.class); @@ -369,6 +380,7 @@ public class RedisMessageGroupStoreTests extends RedisAvailableTests { Message m3 = MessageBuilder.withPayload("3").setSequenceNumber(3).setSequenceSize(3).setCorrelationId(1).build(); input.send(m3); assertNotNull(output.receive(1000)); + context.close(); } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java index 22c3ee8959..cee845e8e1 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2013 the original author or authors + * Copyright 2007-2014 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. @@ -24,9 +24,12 @@ import java.io.Serializable; import java.util.Properties; import java.util.UUID; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.integration.Message; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.history.MessageHistory; @@ -40,6 +43,13 @@ import org.springframework.integration.redis.rules.RedisAvailableTests; */ public class RedisMessageStoreTests extends RedisAvailableTests { + @Before + @After + public void setUpTearDown() { + StringRedisTemplate template = this.createStringRedisTemplate(this.getConnectionFactoryForTest()); + template.delete(template.keys("MESSAGE_*")); + } + @Test @RedisAvailable public void testGetNonExistingMessage(){