+ refactor integration tests through easy parametrization

- removed some of the shallow tests
This commit is contained in:
Costin Leau
2010-11-25 19:16:24 +02:00
parent b9430692fc
commit f738efff6d
8 changed files with 220 additions and 150 deletions

View File

@@ -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<T> {
protected AbstractRedisCollection<T> collection;
protected ObjectFactory<T> factory;
protected RedisTemplate template;
private static Set<RedisConnectionFactory> connFactories = new LinkedHashSet<RedisConnectionFactory>();
@Before
public void setUp() throws Exception {
@@ -54,22 +58,41 @@ public abstract class AbstractRedisCollectionTests<T> {
abstract AbstractRedisCollection<T> createCollection();
abstract void destroyCollection();
abstract RedisStore<T> copyStore(RedisStore<T> store);
public AbstractRedisCollectionTests(ObjectFactory<T> 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<T> {
}
@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<T> {
@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<T> {
List<T> list = (List<T>) Arrays.asList(expectedArray);
assertThat(collection.addAll(list), is(true));
Object[] array = collection.toArray();
assertArrayEquals(expectedArray, array);
}
@@ -264,4 +287,9 @@ public abstract class AbstractRedisCollectionTests<T> {
collection.add(getT());
assertEquals(name, collection.toString());
}
@Test
public void testGetKey() throws Exception {
assertNotNull(collection.getKey());
}
}

View File

@@ -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<T> extends AbstractRedisCollectionT
protected RedisList<T> list;
/**
* Constructs a new <code>AbstractRedisListTests</code> instance.
*
* @param factory
* @param template
*/
public AbstractRedisListTests(ObjectFactory<T> factory, RedisTemplate template) {
super(factory, template);
}
@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {

View File

@@ -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> {
T instance();
}

View File

@@ -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<Person> {
private int counter = 0;
@Override
public Person instance() {
String uuid = UUID.randomUUID().toString();
return new Person(uuid, uuid, ++counter, new Address(uuid, counter));
}
}

View File

@@ -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<Person> {
private JedisConnectionFactory factory;
private int counter = 0;
@Override
AbstractRedisCollection<Person> createCollection() {
String redisName = getClass().getName();
factory = new JedisConnectionFactory();
factory.setPooling(false);
factory.afterPropertiesSet();
RedisTemplate<String, Person> template = new RedisTemplate<String, Person>(factory);
return new DefaultRedisList<Person>(redisName, template);
}
@Override
void destroyCollection() {
factory.destroy();
}
@Override
RedisStore<Person> copyStore(RedisStore<Person> store) {
//return new DefaultRedisList<Person>(store.getKey(), (RedisOperations<String, Person>) store.getOperations());
return null;
}
@Override
Person getT() {
String uuid = UUID.randomUUID().toString();
return new Person(uuid, uuid, ++counter, new Address(uuid, counter));
}
}

View File

@@ -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<Object> {
/**
* Constructs a new <code>RedisListTests</code> instance.
*
* @param factory
* @param connFactory
*/
public RedisListTests(ObjectFactory<Object> factory, RedisTemplate template) {
super(factory, template);
}
@Parameters
public static Collection<Object[]> testParams() {
// create Jedis Factory
ObjectFactory<String> stringFactory = new StringObjectFactory();
ObjectFactory<Person> personFactory = new PersonObjectFactory();
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
jedisConnFactory.setPooling(false);
jedisConnFactory.afterPropertiesSet();
RedisTemplate<String, String> stringTemplate = new RedisTemplate<String, String>(jedisConnFactory);
RedisTemplate<String, Person> personTemplate = new RedisTemplate<String, Person>(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<Object> createCollection() {
String redisName = getClass().getName();
return new DefaultRedisList(redisName, template);
}
}

View File

@@ -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<String> {
@Override
public String instance() {
return UUID.randomUUID().toString();
}
}

View File

@@ -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<String> {
private JedisConnectionFactory factory;
@Override
AbstractRedisCollection<String> createCollection() {
String redisName = getClass().getName();
factory = new JedisConnectionFactory();
factory.setPooling(false);
factory.afterPropertiesSet();
RedisTemplate<String, String> template = new RedisTemplate<String, String>(factory);
return new DefaultRedisList<String>(redisName, template);
}
@Override
void destroyCollection() {
factory.destroy();
}
@Override
RedisStore<String> copyStore(RedisStore<String> store) {
return new DefaultRedisList<String>(store.getKey(), (RedisOperations<String, String>) store.getOperations());
}
@Override
String getT() {
return UUID.randomUUID().toString();
}
}