From f738efff6d672cff031f4f61530bbb58d389aba2 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 25 Nov 2010 19:16:24 +0200 Subject: [PATCH] + refactor integration tests through easy parametrization - removed some of the shallow tests --- .../util/AbstractRedisCollectionTests.java | 66 ++++++++++++----- .../redis/util/AbstractRedisListTests.java | 12 +++- .../keyvalue/redis/util/ObjectFactory.java | 26 +++++++ .../redis/util/PersonObjectFactory.java | 35 ++++++++++ .../redis/util/PersonRedisListTests.java | 67 ------------------ .../keyvalue/redis/util/RedisListTests.java | 70 +++++++++++++++++++ .../redis/util/StringObjectFactory.java | 31 ++++++++ .../redis/util/StringRedisListTests.java | 63 ----------------- 8 files changed, 220 insertions(+), 150 deletions(-) create mode 100644 spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/ObjectFactory.java create mode 100644 spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonObjectFactory.java delete mode 100644 spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonRedisListTests.java create mode 100644 spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/RedisListTests.java create mode 100644 spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringObjectFactory.java delete mode 100644 spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringRedisListTests.java diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisCollectionTests.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisCollectionTests.java index 52e30cbe9..bee4a1bf7 100644 --- a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisCollectionTests.java +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisCollectionTests.java @@ -16,26 +16,25 @@ package org.springframework.data.keyvalue.redis.util; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.matchers.JUnitMatchers.hasItem; -import static org.junit.matchers.JUnitMatchers.hasItems; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.junit.matchers.JUnitMatchers.*; import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; -import org.springframework.data.keyvalue.redis.util.AbstractRedisCollection; -import org.springframework.data.keyvalue.redis.util.RedisStore; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory; +import org.springframework.data.keyvalue.redis.core.RedisTemplate; /** @@ -43,9 +42,14 @@ import org.springframework.data.keyvalue.redis.util.RedisStore; * * @author Costin Leau */ +@RunWith(Parameterized.class) public abstract class AbstractRedisCollectionTests { protected AbstractRedisCollection collection; + protected ObjectFactory factory; + protected RedisTemplate template; + + private static Set connFactories = new LinkedHashSet(); @Before public void setUp() throws Exception { @@ -54,22 +58,41 @@ public abstract class AbstractRedisCollectionTests { abstract AbstractRedisCollection createCollection(); - abstract void destroyCollection(); - abstract RedisStore copyStore(RedisStore store); + public AbstractRedisCollectionTests(ObjectFactory factory, RedisTemplate template) { + this.factory = factory; + this.template = template; + connFactories.add(template.getConnectionFactory()); + } + + @AfterClass + public static void cleanUp() { + if (connFactories != null) { + for (RedisConnectionFactory connectionFactory : connFactories) { + try { + ((DisposableBean) connectionFactory).destroy(); + System.out.println("Succesfully cleaned up factory " + connectionFactory); + } catch (Exception ex) { + System.err.println("Cannot clean factory " + connectionFactory + ex); + } + } + } + } + /** * Return a new instance of T * @return */ - abstract T getT(); + protected T getT() { + return factory.instance(); + } @After public void tearDown() throws Exception { // remove the collection entirely since clear() doesn't always work collection.getOperations().delete(collection.getKey()); - destroyCollection(); } @Test @@ -107,7 +130,7 @@ public abstract class AbstractRedisCollectionTests { } @Test - public void containsObject() { + public void testContainsObject() { T t1 = getT(); assertThat(collection, not(hasItem(t1))); assertThat(collection.add(t1), is(true)); @@ -116,7 +139,7 @@ public abstract class AbstractRedisCollectionTests { @SuppressWarnings("unchecked") @Test - public void containsAll() { + public void testContainsAll() { T t1 = getT(); T t2 = getT(); T t3 = getT(); @@ -241,7 +264,7 @@ public abstract class AbstractRedisCollectionTests { List list = (List) Arrays.asList(expectedArray); assertThat(collection.addAll(list), is(true)); - + Object[] array = collection.toArray(); assertArrayEquals(expectedArray, array); } @@ -264,4 +287,9 @@ public abstract class AbstractRedisCollectionTests { collection.add(getT()); assertEquals(name, collection.toString()); } + + @Test + public void testGetKey() throws Exception { + assertNotNull(collection.getKey()); + } } \ No newline at end of file diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisListTests.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisListTests.java index 0f26cdbb8..6d82d1912 100644 --- a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisListTests.java +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/AbstractRedisListTests.java @@ -23,7 +23,7 @@ import java.util.NoSuchElementException; import org.junit.Before; import org.junit.Test; -import org.springframework.data.keyvalue.redis.util.RedisList; +import org.springframework.data.keyvalue.redis.core.RedisTemplate; /** * Integration test for RedisList @@ -34,6 +34,16 @@ public abstract class AbstractRedisListTests extends AbstractRedisCollectionT protected RedisList list; + /** + * Constructs a new AbstractRedisListTests instance. + * + * @param factory + * @param template + */ + public AbstractRedisListTests(ObjectFactory factory, RedisTemplate template) { + super(factory, template); + } + @SuppressWarnings("unchecked") @Before public void setUp() throws Exception { diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/ObjectFactory.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/ObjectFactory.java new file mode 100644 index 000000000..882111a55 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/ObjectFactory.java @@ -0,0 +1,26 @@ +/* + * Copyright 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.data.keyvalue.redis.util; + +/** + * Simple object factory. + * + * @author Costin Leau + */ +public interface ObjectFactory { + + T instance(); +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonObjectFactory.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonObjectFactory.java new file mode 100644 index 000000000..9ca4f63b1 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonObjectFactory.java @@ -0,0 +1,35 @@ +/* + * Copyright 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.data.keyvalue.redis.util; + +import java.util.UUID; + +import org.springframework.data.keyvalue.redis.Address; +import org.springframework.data.keyvalue.redis.Person; + +/** + * @author Costin Leau + */ +public class PersonObjectFactory implements ObjectFactory { + + private int counter = 0; + + @Override + public Person instance() { + String uuid = UUID.randomUUID().toString(); + return new Person(uuid, uuid, ++counter, new Address(uuid, counter)); + } +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonRedisListTests.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonRedisListTests.java deleted file mode 100644 index c00b591d2..000000000 --- a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/PersonRedisListTests.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 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.data.keyvalue.redis.util; - -import java.util.UUID; - -import org.springframework.data.keyvalue.redis.Address; -import org.springframework.data.keyvalue.redis.Person; -import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory; -import org.springframework.data.keyvalue.redis.core.RedisTemplate; -import org.springframework.data.keyvalue.redis.util.AbstractRedisCollection; -import org.springframework.data.keyvalue.redis.util.DefaultRedisList; -import org.springframework.data.keyvalue.redis.util.RedisStore; - - -/** - * Person-based Redis List test. - * - * @author Costin Leau - */ -public class PersonRedisListTests extends AbstractRedisListTests { - - private JedisConnectionFactory factory; - private int counter = 0; - - @Override - AbstractRedisCollection createCollection() { - String redisName = getClass().getName(); - factory = new JedisConnectionFactory(); - factory.setPooling(false); - factory.afterPropertiesSet(); - - RedisTemplate template = new RedisTemplate(factory); - return new DefaultRedisList(redisName, template); - } - - @Override - void destroyCollection() { - factory.destroy(); - } - - @Override - RedisStore copyStore(RedisStore store) { - //return new DefaultRedisList(store.getKey(), (RedisOperations) store.getOperations()); - return null; - } - - @Override - Person getT() { - String uuid = UUID.randomUUID().toString(); - return new Person(uuid, uuid, ++counter, new Address(uuid, counter)); - } -} - diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/RedisListTests.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/RedisListTests.java new file mode 100644 index 000000000..a684b26c6 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/RedisListTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 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.data.keyvalue.redis.util; + +import java.util.Arrays; +import java.util.Collection; + +import org.junit.runners.Parameterized.Parameters; +import org.springframework.data.keyvalue.redis.Person; +import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.keyvalue.redis.core.RedisTemplate; + +/** + * Parameterized instance of Redis tests. + * + * @author Costin Leau + */ +public class RedisListTests extends AbstractRedisListTests { + + /** + * Constructs a new RedisListTests instance. + * + * @param factory + * @param connFactory + */ + public RedisListTests(ObjectFactory factory, RedisTemplate template) { + super(factory, template); + } + + @Parameters + public static Collection testParams() { + // create Jedis Factory + ObjectFactory stringFactory = new StringObjectFactory(); + ObjectFactory personFactory = new PersonObjectFactory(); + + JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory(); + jedisConnFactory.setPooling(false); + jedisConnFactory.afterPropertiesSet(); + + RedisTemplate stringTemplate = new RedisTemplate(jedisConnFactory); + RedisTemplate personTemplate = new RedisTemplate(jedisConnFactory); + + return Arrays.asList(new Object[][] { { stringFactory, stringTemplate }, { personFactory, personTemplate } }); + + } + + @Override + RedisStore copyStore(RedisStore store) { + return new DefaultRedisList(store.getKey().toString(), store.getOperations()); + } + + @Override + AbstractRedisCollection createCollection() { + String redisName = getClass().getName(); + return new DefaultRedisList(redisName, template); + } +} \ No newline at end of file diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringObjectFactory.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringObjectFactory.java new file mode 100644 index 000000000..4e3d21e79 --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringObjectFactory.java @@ -0,0 +1,31 @@ +/* + * Copyright 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.data.keyvalue.redis.util; + +import java.util.UUID; + +/** + * String object factory based on UUID. + * + * @author Costin Leau + */ +public class StringObjectFactory implements ObjectFactory { + + @Override + public String instance() { + return UUID.randomUUID().toString(); + } +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringRedisListTests.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringRedisListTests.java deleted file mode 100644 index 9b157fb9c..000000000 --- a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/util/StringRedisListTests.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 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.data.keyvalue.redis.util; - -import java.util.UUID; - -import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory; -import org.springframework.data.keyvalue.redis.core.RedisOperations; -import org.springframework.data.keyvalue.redis.core.RedisTemplate; -import org.springframework.data.keyvalue.redis.util.AbstractRedisCollection; -import org.springframework.data.keyvalue.redis.util.DefaultRedisList; -import org.springframework.data.keyvalue.redis.util.RedisStore; - - -/** - * String-based Redis List test. - * - * @author Costin Leau - */ -public class StringRedisListTests extends AbstractRedisListTests { - - private JedisConnectionFactory factory; - - @Override - AbstractRedisCollection createCollection() { - String redisName = getClass().getName(); - factory = new JedisConnectionFactory(); - factory.setPooling(false); - factory.afterPropertiesSet(); - - RedisTemplate template = new RedisTemplate(factory); - return new DefaultRedisList(redisName, template); - } - - @Override - void destroyCollection() { - factory.destroy(); - } - - @Override - RedisStore copyStore(RedisStore store) { - return new DefaultRedisList(store.getKey(), (RedisOperations) store.getOperations()); - } - - @Override - String getT() { - return UUID.randomUUID().toString(); - } -} -