DATAREDIS-288 - Polishing.

Added javadoc and assertions to ensure failing before actually sending data to redis and backed those with tests.

Original pull request: #114.
This commit is contained in:
Christoph Strobl
2014-11-17 11:43:05 +01:00
parent d89773387a
commit 57fc58fcff
4 changed files with 106 additions and 34 deletions

View File

@@ -121,8 +121,16 @@ abstract class AbstractOperations<K, V> {
return rawValues;
}
/**
* @param values must not be {@literal empty} nor contain {@literal null} values.
* @return
* @since 1.5
*/
byte[][] rawValues(Collection<V> values) {
Assert.notEmpty(values, "Values must not be 'null' or empty.");
Assert.noNullElements(values.toArray(), "Values must not contain 'null' value.");
byte[][] rawValues = new byte[values.size()][];
int i = 0;
for (V value : values) {

View File

@@ -29,6 +29,7 @@ import org.springframework.util.CollectionUtils;
* @author Costin Leau
* @author David Liu
* @author Thomas Darimont
* @author Christoph Strobl
*/
class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements ListOperations<K, V> {
@@ -86,6 +87,23 @@ class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements Li
}, true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ListOperations#leftPushAll(java.lang.Object, java.util.Collection)
*/
@Override
public Long leftPushAll(K key, Collection<V> values) {
final byte[] rawKey = rawKey(key);
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.lPush(rawKey, rawValues);
}
}, true);
}
public Long leftPushIfPresent(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
@@ -181,6 +199,23 @@ class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements Li
}, true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ListOperations#rightPushAll(java.lang.Object, java.util.Collection)
*/
@Override
public Long rightPushAll(K key, Collection<V> values) {
final byte[] rawKey = rawKey(key);
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.rPush(rawKey, rawValues);
}
}, true);
}
public Long rightPushIfPresent(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
@@ -249,30 +284,4 @@ class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements Li
}, true);
}
@Override
public Long rightPushAll(K key, Collection<V> values) {
final byte[] rawKey = rawKey(key);
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.rPush(rawKey, rawValues);
}
}, true);
}
@Override
public Long leftPushAll(K key, Collection<V> values) {
final byte[] rawKey = rawKey(key);
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.lPush(rawKey, rawValues);
}
}, true);
}
}

View File

@@ -25,6 +25,7 @@ import java.util.concurrent.TimeUnit;
* @author Costin Leau
* @author David Liu
* @author Thomas Darimont
* @author Christoph Strobl
*/
public interface ListOperations<K, V> {
@@ -39,8 +40,10 @@ public interface ListOperations<K, V> {
Long leftPushAll(K key, V... values);
/**
* @param key
* @param values
* Insert all {@literal values} at the head of the list stored at {@literal key}.
*
* @param key must not be {@literal null}.
* @param values must not be {@literal empty} nor contain {@literal null} values.
* @return
* @since 1.5
*/
@@ -55,8 +58,10 @@ public interface ListOperations<K, V> {
Long rightPushAll(K key, V... values);
/**
* @param key
* @param values
* Insert all {@literal values} at the tail of the list stored at {@literal key}.
*
* @param key must not be {@literal null}.
* @param values must not be {@literal empty} nor contain {@literal null} values.
* @return
* @since 1.5
*/

View File

@@ -21,6 +21,7 @@ import static org.springframework.data.redis.matcher.RedisTestMatchers.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.junit.After;
@@ -38,6 +39,7 @@ import org.springframework.data.redis.connection.RedisConnection;
*
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @param <K> Key test
* @param <V> Value test
*/
@@ -178,8 +180,7 @@ public class DefaultListOperationsTests<K, V> {
* @see DATAREDIS-288
*/
@Test
@SuppressWarnings("all")
// get rid of varargs warning
@SuppressWarnings("unchecked")
public void testRightPushAllCollection() {
K key = keyFactory.instance();
@@ -192,12 +193,36 @@ public class DefaultListOperationsTests<K, V> {
assertThat(listOps.range(key, 0, -1), isEqual(Arrays.asList(new Object[] { v1, v2, v3 })));
}
/**
* @see DATAREDIS-288
*/
@Test(expected = IllegalArgumentException.class)
public void rightPushAllShouldThrowExceptionWhenCalledWithEmptyCollection() {
listOps.rightPushAll(keyFactory.instance(), Collections.<V> emptyList());
}
/**
* @see DATAREDIS-288
*/
@SuppressWarnings("unchecked")
@Test(expected = IllegalArgumentException.class)
public void rightPushAllShouldThrowExceptionWhenCollectionContainsNullValue() {
listOps.rightPushAll(keyFactory.instance(), Arrays.asList(valueFactory.instance(), null));
}
/**
* @see DATAREDIS-288
*/
@Test(expected = IllegalArgumentException.class)
public void rightPushAllShouldThrowExceptionWhenCalledWithNull() {
listOps.rightPushAll(keyFactory.instance(), (Collection<V>) null);
}
/**
* @see DATAREDIS-288
*/
@Test
@SuppressWarnings("all")
// get rid of varargs warning
@SuppressWarnings("unchecked")
public void testLeftPushAllCollection() {
K key = keyFactory.instance();
@@ -209,4 +234,29 @@ public class DefaultListOperationsTests<K, V> {
assertEquals(Long.valueOf(3), listOps.leftPushAll(key, Arrays.<V> asList(v1, v2, v3)));
assertThat(listOps.range(key, 0, -1), isEqual(Arrays.asList(new Object[] { v3, v2, v1 })));
}
/**
* @see DATAREDIS-288
*/
@Test(expected = IllegalArgumentException.class)
public void leftPushAllShouldThrowExceptionWhenCalledWithEmptyCollection() {
listOps.leftPushAll(keyFactory.instance(), Collections.<V> emptyList());
}
/**
* @see DATAREDIS-288
*/
@SuppressWarnings("unchecked")
@Test(expected = IllegalArgumentException.class)
public void leftPushAllShouldThrowExceptionWhenCollectionContainsNullValue() {
listOps.leftPushAll(keyFactory.instance(), Arrays.asList(valueFactory.instance(), null));
}
/**
* @see DATAREDIS-288
*/
@Test(expected = IllegalArgumentException.class)
public void leftPushAllShouldThrowExceptionWhenCalledWithNull() {
listOps.leftPushAll(keyFactory.instance(), (Collection<V>) null);
}
}