From c72a84885c650731ec216dbddd34d30ebb51122d Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 16 Sep 2020 09:27:54 +0200 Subject: [PATCH] DATAREDIS-1196 - Polishing. Tweak Redis 6.0.6 requirement wording. Join assertions where possible and use more concise assertions. Original pull request: #563. --- .../redis/connection/RedisListCommands.java | 4 +- .../connection/StringRedisConnection.java | 6 +-- .../data/redis/core/BoundListOperations.java | 4 +- .../data/redis/core/ListOperations.java | 4 +- .../redis/core/ReactiveListOperations.java | 4 +- .../collections/AbstractRedisListTests.java | 51 ++++++++----------- .../support/collections/RedisListTests.java | 6 +-- 7 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java b/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java index f55ef55719..e8c4e85f20 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisListCommands.java @@ -49,7 +49,7 @@ enum Position { /** * Returns the index of matching elements inside the list stored at given {@literal key}.
- * Requires Redis 6.0.6. + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param element must not be {@literal null}. @@ -64,7 +64,7 @@ default Long lPos(byte[] key, byte[] element) { /** * Returns the index of matching elements inside the list stored at given {@literal key}.
- * Requires Redis 6.0.6. + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param element must not be {@literal null}. diff --git a/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java index 8f6be25cd1..5378048734 100644 --- a/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java @@ -688,7 +688,7 @@ default Long bitPos(String key, boolean bit) { /** * Returns the index of matching elements inside the list stored at given {@literal key}.
- * Requires Redis 6.0.6. + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param element must not be {@literal null}. @@ -703,7 +703,7 @@ default Long lPos(String key, String element) { /** * Returns the index of matching elements inside the list stored at given {@literal key}.
- * Requires Redis 6.0.6. + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param element must not be {@literal null}. @@ -2059,7 +2059,7 @@ default RecordId xAdd(StringRecord record) { /** * Append the given {@link StringRecord} to the stream stored at {@link StringRecord#getStream()}. - * + * * @param record must not be {@literal null}. * @param options must not be {@literal null}, use {@link XAddOptions#none()} instead. * @return the record Id. {@literal null} when used in pipeline / transaction. diff --git a/src/main/java/org/springframework/data/redis/core/BoundListOperations.java b/src/main/java/org/springframework/data/redis/core/BoundListOperations.java index 7482da4752..94f4a9b647 100644 --- a/src/main/java/org/springframework/data/redis/core/BoundListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/BoundListOperations.java @@ -171,7 +171,7 @@ public interface BoundListOperations extends BoundKeyOperations { /** * Returns the index of the first occurrence of the specified value in the list at at {@code key}.
- * Requires Redis 6.0.6 + * Requires Redis 6.0.6 or newer. * * @param value must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction or when not contained in list. @@ -182,7 +182,7 @@ public interface BoundListOperations extends BoundKeyOperations { /** * Returns the index of the last occurrence of the specified value in the list at at {@code key}.
- * Requires Redis 6.0.6 + * Requires Redis 6.0.6 or newer. * * @param value must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction or when not contained in list. diff --git a/src/main/java/org/springframework/data/redis/core/ListOperations.java b/src/main/java/org/springframework/data/redis/core/ListOperations.java index 851b989306..9c1bce09fc 100644 --- a/src/main/java/org/springframework/data/redis/core/ListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ListOperations.java @@ -215,7 +215,7 @@ public interface ListOperations { /** * Returns the index of the first occurrence of the specified value in the list at at {@code key}.
- * Requires Redis 6.0.6 + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param value must not be {@literal null}. @@ -227,7 +227,7 @@ public interface ListOperations { /** * Returns the index of the last occurrence of the specified value in the list at at {@code key}.
- * Requires Redis 6.0.6 + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param value must not be {@literal null}. diff --git a/src/main/java/org/springframework/data/redis/core/ReactiveListOperations.java b/src/main/java/org/springframework/data/redis/core/ReactiveListOperations.java index 61204eee3b..bbbd1d0522 100644 --- a/src/main/java/org/springframework/data/redis/core/ReactiveListOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ReactiveListOperations.java @@ -198,7 +198,7 @@ public interface ReactiveListOperations { /** * Returns the index of the first occurrence of the specified value in the list at at {@code key}.
- * Requires Redis 6.0.6 + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param value must not be {@literal null}. @@ -210,7 +210,7 @@ public interface ReactiveListOperations { /** * Returns the index of the last occurrence of the specified value in the list at at {@code key}.
- * Requires Redis 6.0.6 + * Requires Redis 6.0.6 or newer. * * @param key must not be {@literal null}. * @param value must not be {@literal null}. diff --git a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java index f02c1f1cc5..c747698668 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/AbstractRedisListTests.java @@ -16,6 +16,7 @@ package org.springframework.data.redis.support.collections; import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assumptions.*; import java.util.ArrayList; import java.util.Arrays; @@ -24,10 +25,10 @@ import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; -import org.assertj.core.api.Assumptions; import org.junit.Before; import org.junit.Rule; import org.junit.Test; + import org.springframework.data.redis.ObjectFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @@ -168,7 +169,7 @@ public void addAllIndexCollectionMiddle() { @IfProfileValue(name = "redisVersion", value = "6.0.6+") public void testIndexOfObject() { - Assumptions.assumeThat(template.getConnectionFactory()).isInstanceOf(LettuceConnectionFactory.class); + assumeThat(template.getConnectionFactory()).isInstanceOf(LettuceConnectionFactory.class); T t1 = getT(); T t2 = getT(); @@ -249,10 +250,10 @@ public void testRange() { T t1 = getT(); T t2 = getT(); - assertThat(list.range(0, -1).isEmpty()).isTrue(); + assertThat(list.range(0, -1)).isEmpty(); list.add(t1); list.add(t2); - assertThat(list.range(0, -1).size()).isEqualTo(2); + assertThat(list.range(0, -1)).hasSize(2); assertThat(list.range(0, 0).get(0)).isEqualTo(t1); assertThat(list.range(1, 1).get(0)).isEqualTo(t2); } @@ -285,12 +286,12 @@ public void testTrim() { T t1 = getT(); T t2 = getT(); - assertThat(list.trim(0, 0).isEmpty()).isTrue(); + assertThat(list.trim(0, 0)).isEmpty(); list.add(t1); list.add(t2); - assertThat(list.size()).isEqualTo(2); - assertThat(list.trim(0, 0).size()).isEqualTo(1); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(2); + assertThat(list.trim(0, 0)).hasSize(1); + assertThat(list).hasSize(1); assertThat(list.get(0)).isEqualTo(t1); } @@ -300,12 +301,12 @@ public void testCappedCollection() throws Exception { RedisList cappedList = new DefaultRedisList(template.boundListOps(collection.getKey() + ":capped"), 1); T first = getT(); cappedList.offer(first); - assertThat(cappedList.size()).isEqualTo(1); + assertThat(cappedList).hasSize(1); cappedList.add(getT()); - assertThat(cappedList.size()).isEqualTo(1); + assertThat(cappedList).hasSize(1); T last = getT(); cappedList.add(last); - assertThat(cappedList.size()).isEqualTo(1); + assertThat(cappedList).hasSize(1); assertThat(cappedList.get(0)).isEqualTo(first); } @@ -361,10 +362,8 @@ public void testDrainToCollectionWithMaxElements() { List c = new ArrayList<>(); list.drainTo(c, 2); - assertThat(list.size()).isEqualTo(1); - assertThat(list).contains(t3); - assertThat(c.size()).isEqualTo(2); - assertThat(c).contains(t1, t2); + assertThat(list).hasSize(1).contains(t3); + assertThat(c).hasSize(2).contains(t1, t2); } @SuppressWarnings("unchecked") @@ -381,9 +380,8 @@ public void testDrainToCollection() { List c = new ArrayList<>(); list.drainTo(c); - assertThat(list.isEmpty()).isTrue(); - assertThat(c.size()).isEqualTo(3); - assertThat(c).contains(t1, t2, t3); + assertThat(list).isEmpty(); + assertThat(c).hasSize(3).contains(t1, t2, t3); } @Test @@ -426,7 +424,7 @@ public void testPeekLast() { list.add(t2); assertThat(list.peekLast()).isEqualTo(t2); - assertThat(list.size()).isEqualTo(2); + assertThat(list).hasSize(2); } @Test @@ -444,8 +442,7 @@ public void testPollLast() { T last = list.pollLast(); assertThat(last).isEqualTo(t2); - assertThat(list.size()).isEqualTo(1); - assertThat(list).contains(t1); + assertThat(list).hasSize(1).contains(t1); } @Test @@ -459,8 +456,7 @@ public void testPollLastTimeout() throws InterruptedException { T last = list.pollLast(1, TimeUnit.MILLISECONDS); assertThat(last).isEqualTo(t2); - assertThat(list.size()).isEqualTo(1); - assertThat(list).contains(t1); + assertThat(list).hasSize(1).contains(t1); } @Test @@ -500,6 +496,7 @@ public void testRemoveLast() { @Test public void testRmoveLastOccurrence() { + T t1 = getT(); T t2 = getT(); @@ -509,11 +506,7 @@ public void testRmoveLastOccurrence() { list.add(t2); list.removeLastOccurrence(t2); - assertThat(list.size()).isEqualTo(3); - Iterator iterator = list.iterator(); - assertThat(iterator.next()).isEqualTo(t1); - assertThat(iterator.next()).isEqualTo(t2); - assertThat(iterator.next()).isEqualTo(t1); + assertThat(list).hasSize(3).containsExactly(t1, t2, t1); } @Test @@ -535,7 +528,7 @@ public void testTakeLast() { @IfProfileValue(name = "redisVersion", value = "6.0.6+") public void lastIndexOf() { - Assumptions.assumeThat(template.getConnectionFactory()).isInstanceOf(LettuceConnectionFactory.class); + assumeThat(template.getConnectionFactory()).isInstanceOf(LettuceConnectionFactory.class); T t1 = getT(); T t2 = getT(); diff --git a/src/test/java/org/springframework/data/redis/support/collections/RedisListTests.java b/src/test/java/org/springframework/data/redis/support/collections/RedisListTests.java index 69433c905c..eeb3497bb8 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/RedisListTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/RedisListTests.java @@ -31,16 +31,16 @@ public class RedisListTests extends AbstractRedisListTests { * @param factory * @param connFactory */ - public RedisListTests(ObjectFactory factory, RedisTemplate template) { + public RedisListTests(ObjectFactory factory, RedisTemplate template) { super(factory, template); } RedisStore copyStore(RedisStore store) { - return new DefaultRedisList(store.getKey().toString(), store.getOperations()); + return new DefaultRedisList<>(store.getKey(), store.getOperations()); } AbstractRedisCollection createCollection() { String redisName = getClass().getName(); - return new DefaultRedisList(redisName, template); + return new DefaultRedisList<>(redisName, template); } }