1616package org .springframework .data .redis .cache ;
1717
1818import static org .assertj .core .api .Assertions .*;
19- import static org .assertj .core .api .Assumptions .*;
2019import static org .awaitility .Awaitility .*;
2120
2221import io .netty .util .concurrent .DefaultThreadFactory ;
4948import org .springframework .cache .support .NullValue ;
5049import org .springframework .data .redis .connection .RedisConnection ;
5150import org .springframework .data .redis .connection .RedisConnectionFactory ;
52- import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
53- import org .springframework .data .redis .connection .lettuce .LettuceConnectionFactory ;
5451import org .springframework .data .redis .serializer .RedisSerializationContext .SerializationPair ;
5552import org .springframework .data .redis .serializer .RedisSerializer ;
5653import 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 ;
5757import org .springframework .data .redis .test .extension .parametrized .MethodSource ;
5858import org .springframework .data .redis .test .extension .parametrized .ParameterizedRedisTest ;
5959import 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 // GH-2650
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
0 commit comments