Skip to content

Commit 2c638fd

Browse files
committed
Allow @EnabledOnRedisDriver usage with @ParameterizedRedisTest.
We now enable tests without an instance in EnabledOnRedisDriverCondition assuming that we only use DriverQualifier on instance fields and not static ones.
1 parent dc0d98c commit 2c638fd

File tree

3 files changed

+37
-43
lines changed

3 files changed

+37
-43
lines changed

src/test/java/org/springframework/data/redis/cache/DefaultRedisCacheWriterTests.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.data.redis.cache;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.assertj.core.api.Assumptions.*;
2019
import static org.springframework.data.redis.cache.RedisCacheWriter.*;
2120

2221
import java.nio.charset.Charset;
@@ -33,8 +32,10 @@
3332
import org.springframework.data.redis.connection.RedisConnection;
3433
import org.springframework.data.redis.connection.RedisConnectionFactory;
3534
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
36-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
3735
import org.springframework.data.redis.core.types.Expiration;
36+
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver;
37+
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver.DriverQualifier;
38+
import org.springframework.data.redis.test.condition.RedisDriver;
3839
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
3940
import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
4041

@@ -55,7 +56,7 @@ public class DefaultRedisCacheWriterTests {
5556
private byte[] binaryCacheKey = cacheKey.getBytes(StandardCharsets.UTF_8);
5657
private byte[] binaryCacheValue = "value".getBytes(StandardCharsets.UTF_8);
5758

58-
private RedisConnectionFactory connectionFactory;
59+
private final @DriverQualifier RedisConnectionFactory connectionFactory;
5960

6061
public DefaultRedisCacheWriterTests(RedisConnectionFactory connectionFactory) {
6162
this.connectionFactory = connectionFactory;
@@ -147,10 +148,9 @@ void getShouldReturnNullWhenKeyDoesNotExist() {
147148
}
148149

149150
@ParameterizedRedisTest // GH-2650
151+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
150152
void cacheHitRetrieveShouldIncrementStatistics() throws ExecutionException, InterruptedException {
151153

152-
assumeThat(connectionFactory).isInstanceOf(LettuceConnectionFactory.class);
153-
154154
doWithConnection(connection -> connection.set(binaryCacheKey, binaryCacheValue));
155155

156156
RedisCacheWriter writer = nonLockingRedisCacheWriter(connectionFactory)
@@ -163,10 +163,9 @@ void cacheHitRetrieveShouldIncrementStatistics() throws ExecutionException, Inte
163163
}
164164

165165
@ParameterizedRedisTest // GH-2650
166+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
166167
void storeShouldIncrementStatistics() throws ExecutionException, InterruptedException {
167168

168-
assumeThat(connectionFactory).isInstanceOf(LettuceConnectionFactory.class);
169-
170169
RedisCacheWriter writer = nonLockingRedisCacheWriter(connectionFactory)
171170
.withStatisticsCollector(CacheStatisticsCollector.create());
172171

@@ -176,10 +175,9 @@ void storeShouldIncrementStatistics() throws ExecutionException, InterruptedExce
176175
}
177176

178177
@ParameterizedRedisTest // GH-2650
178+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
179179
void cacheMissRetrieveWithLoaderAsyncShouldIncrementStatistics() throws ExecutionException, InterruptedException {
180180

181-
assumeThat(connectionFactory).isInstanceOf(LettuceConnectionFactory.class);
182-
183181
RedisCacheWriter writer = nonLockingRedisCacheWriter(connectionFactory)
184182
.withStatisticsCollector(CacheStatisticsCollector.create());
185183

src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java

+12-32
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.data.redis.cache;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.assertj.core.api.Assumptions.*;
2019
import static org.awaitility.Awaitility.*;
2120

2221
import io.netty.util.concurrent.DefaultThreadFactory;
@@ -49,11 +48,12 @@
4948
import org.springframework.cache.support.NullValue;
5049
import org.springframework.data.redis.connection.RedisConnection;
5150
import org.springframework.data.redis.connection.RedisConnectionFactory;
52-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
53-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
5451
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
5552
import org.springframework.data.redis.serializer.RedisSerializer;
5653
import org.springframework.data.redis.test.condition.EnabledOnCommand;
54+
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver;
55+
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver.DriverQualifier;
56+
import org.springframework.data.redis.test.condition.RedisDriver;
5757
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
5858
import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
5959
import org.springframework.lang.Nullable;
@@ -80,7 +80,7 @@ public class RedisCacheTests {
8080

8181
private byte[] binaryNullValue = RedisSerializer.java().serialize(NullValue.INSTANCE);
8282

83-
private RedisConnectionFactory connectionFactory;
83+
private final @DriverQualifier RedisConnectionFactory connectionFactory;
8484
private RedisSerializer serializer;
8585
private RedisCache cache;
8686

@@ -284,13 +284,9 @@ void clearShouldClearCache() {
284284
}
285285

286286
@ParameterizedRedisTest // GH-1721
287+
@EnabledOnRedisDriver(RedisDriver.LETTUCE) // SCAN not supported via Jedis Cluster.
287288
void clearWithScanShouldClearCache() {
288289

289-
// SCAN not supported via Jedis Cluster.
290-
if (connectionFactory instanceof JedisConnectionFactory) {
291-
assumeThat(((JedisConnectionFactory) connectionFactory).isRedisClusterAware()).isFalse();
292-
}
293-
294290
RedisCache cache = new RedisCache("cache",
295291
RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory, BatchStrategies.scan(25)),
296292
RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(SerializationPair.fromSerializer(serializer)));
@@ -573,11 +569,9 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
573569
}
574570

575571
@ParameterizedRedisTest
572+
@EnabledOnRedisDriver(RedisDriver.JEDIS)
576573
void retrieveCacheValueUsingJedis() {
577574

578-
// TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.JEDIS) does not work!
579-
assumeThat(this.connectionFactory instanceof JedisConnectionFactory).isTrue();
580-
581575
assertThatExceptionOfType(UnsupportedOperationException.class)
582576
.isThrownBy(() -> this.cache.retrieve(this.binaryCacheKey)).withMessageContaining("RedisCache");
583577

@@ -587,12 +581,10 @@ void retrieveCacheValueUsingJedis() {
587581
}
588582

589583
@ParameterizedRedisTest // GH-2650
584+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
590585
@SuppressWarnings("unchecked")
591586
void retrieveReturnsCachedValue() throws Exception {
592587

593-
// TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
594-
assumeThat(this.connectionFactory instanceof LettuceConnectionFactory).isTrue();
595-
596588
doWithConnection(connection -> connection.stringCommands().set(this.binaryCacheKey, this.binarySample));
597589

598590
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
@@ -605,13 +597,10 @@ void retrieveReturnsCachedValue() throws Exception {
605597
}
606598

607599
@ParameterizedRedisTest // GH-2650
600+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
608601
@SuppressWarnings("unchecked")
609602
void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception {
610603

611-
// TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
612-
assumeThat(this.connectionFactory instanceof LettuceConnectionFactory).isTrue();
613-
614-
String mockValue = "MockValue";
615604
String testValue = "TestValue";
616605

617606
byte[] binaryCacheValue = this.serializer.serialize(testValue);
@@ -634,11 +623,9 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception {
634623
}
635624

636625
@ParameterizedRedisTest // GH-2650
626+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
637627
void retrieveReturnsLoadedValue() throws Exception {
638628

639-
// TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
640-
assumeThat(this.connectionFactory instanceof LettuceConnectionFactory).isTrue();
641-
642629
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
643630
AtomicBoolean loaded = new AtomicBoolean(false);
644631
Person jon = new Person("Jon", Date.from(Instant.now()));
@@ -658,11 +645,9 @@ void retrieveReturnsLoadedValue() throws Exception {
658645
}
659646

660647
@ParameterizedRedisTest // GH-2650
648+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
661649
void retrieveStoresLoadedValue() throws Exception {
662650

663-
// TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
664-
assumeThat(this.connectionFactory instanceof LettuceConnectionFactory).isTrue();
665-
666651
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
667652
Person jon = new Person("Jon", Date.from(Instant.now()));
668653
Supplier<CompletableFuture<Person>> valueLoaderSupplier = () -> CompletableFuture.completedFuture(jon);
@@ -675,11 +660,9 @@ void retrieveStoresLoadedValue() throws Exception {
675660
}
676661

677662
@ParameterizedRedisTest // GH-2650
663+
@EnabledOnRedisDriver(RedisDriver.LETTUCE)
678664
void retrieveReturnsNull() throws Exception {
679665

680-
// TODO: Is there a better way to do this? @EnableOnRedisDriver(RedisDriver.LETTUCE) does not work!
681-
assumeThat(this.connectionFactory instanceof LettuceConnectionFactory).isTrue();
682-
683666
doWithConnection(connection -> connection.stringCommands().set(this.binaryCacheKey, this.binaryNullValue));
684667

685668
RedisCache cache = new RedisCache("cache", usingLockingRedisCacheWriter(), usingRedisCacheConfiguration());
@@ -733,11 +716,8 @@ private Function<RedisCacheConfiguration, RedisCacheConfiguration> withTtiExpira
733716
}
734717

735718
void doWithConnection(Consumer<RedisConnection> callback) {
736-
RedisConnection connection = connectionFactory.getConnection();
737-
try {
719+
try (RedisConnection connection = connectionFactory.getConnection()) {
738720
callback.accept(connection);
739-
} finally {
740-
connection.close();
741721
}
742722
}
743723

src/test/java/org/springframework/data/redis/test/condition/EnabledOnRedisDriverCondition.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
5959

6060
if (annotatedFields.isEmpty()) {
6161
throw new IllegalStateException(
62-
"@EnabledOnRedisDriver requires a field of type RedisConnectionFactory annotated with @DriverQualifier");
62+
"@EnabledOnRedisDriver requires a field of type \"RedisConnectionFactory\" annotated with @DriverQualifier");
63+
}
64+
65+
if (context.getTestInstance().isEmpty()) {
66+
return ENABLED_BY_DEFAULT;
6367
}
6468

6569
for (Field field : annotatedFields) {
70+
6671
Try<Object> fieldValue = ReflectionUtils.tryToReadFieldValue(field, context.getRequiredTestInstance());
6772

6873
RedisConnectionFactory value = (RedisConnectionFactory) fieldValue
@@ -76,7 +81,8 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
7681
}
7782

7883
if (!foundMatch) {
79-
return disabled(String.format("Driver %s not supported; Supported driver(s): %s", value,
84+
return disabled(String.format("Driver %s not supported; Supported driver(s): %s",
85+
formatUnsupportedDriver(value),
8086
Arrays.toString(annotation.value())));
8187
}
8288
}
@@ -85,4 +91,14 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
8591

8692
}
8793

94+
private static String formatUnsupportedDriver(RedisConnectionFactory value) {
95+
96+
for (RedisDriver redisDriver : RedisDriver.values()) {
97+
if (redisDriver.matches(value)) {
98+
return redisDriver.toString();
99+
}
100+
}
101+
102+
return value.toString();
103+
}
88104
}

0 commit comments

Comments
 (0)