1515 */
1616package org .springframework .data .redis .cache ;
1717
18- import static org .assertj .core .api .Assertions .assertThat ;
19- import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20- import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
21- import static org .awaitility .Awaitility .await ;
18+ import static org .assertj .core .api .Assertions .*;
19+ import static org .awaitility .Awaitility .*;
2220
2321import io .netty .util .concurrent .DefaultThreadFactory ;
2422
@@ -575,8 +573,7 @@ void cacheGetWithTimeToIdleExpirationAfterEntryExpiresShouldReturnNull() {
575573 void retrieveCacheValueUsingJedis () {
576574
577575 assertThatExceptionOfType (UnsupportedOperationException .class )
578- .isThrownBy (() -> this .cache .retrieve (this .binaryCacheKey ))
579- .withMessageContaining ("RedisCache" );
576+ .isThrownBy (() -> this .cache .retrieve (this .binaryCacheKey )).withMessageContaining ("RedisCache" );
580577 }
581578
582579 @ ParameterizedRedisTest // GH-2650
@@ -590,23 +587,51 @@ void retrieveLoadedValueUsingJedis() {
590587
591588 @ ParameterizedRedisTest // GH-2650
592589 @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
593- @ SuppressWarnings ("unchecked" )
594590 void retrieveReturnsCachedValue () throws Exception {
595591
596592 doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binarySample ));
597593
594+ RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (),
595+ usingRedisCacheConfiguration ().disableCachingNullValues ());
596+
597+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
598+
599+ assertThat (value ).isNotNull ();
600+ assertThat (value .get (5 , TimeUnit .SECONDS )).isNotNull ();
601+ assertThat (value .get ().get ()).isEqualTo (this .sample );
602+ assertThat (value ).isDone ();
603+ }
604+
605+ @ ParameterizedRedisTest // GH-2650
606+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
607+ void retrieveReturnsCachedNullableValue () throws Exception {
608+
609+ doWithConnection (connection -> connection .stringCommands ().set (this .binaryCacheKey , this .binarySample ));
610+
598611 RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
599612
600- CompletableFuture <Person > value = ( CompletableFuture < Person >) cache .retrieve (this .key );
613+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
601614
602615 assertThat (value ).isNotNull ();
603- assertThat (value .get ()).isEqualTo (this .sample );
616+ assertThat (value .get (). get () ).isEqualTo (this .sample );
604617 assertThat (value ).isDone ();
605618 }
606619
620+ @ ParameterizedRedisTest // GH-2783
621+ @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
622+ void retrieveReturnsCachedNullValue () throws Exception {
623+
624+ doWithConnection (connection -> connection .set (binaryCacheKey , binaryNullValue ));
625+
626+ CompletableFuture <ValueWrapper > value = (CompletableFuture <ValueWrapper >) cache .retrieve (this .key );
627+ ValueWrapper wrapper = value .get (5 , TimeUnit .SECONDS );
628+
629+ assertThat (wrapper ).isNotNull ();
630+ assertThat (wrapper .get ()).isNull ();
631+ }
632+
607633 @ ParameterizedRedisTest // GH-2650
608634 @ EnabledOnRedisDriver (RedisDriver .LETTUCE )
609- @ SuppressWarnings ("unchecked" )
610635 void retrieveReturnsCachedValueWhenLockIsReleased () throws Exception {
611636
612637 String testValue = "TestValue" ;
@@ -622,13 +647,12 @@ void retrieveReturnsCachedValueWhenLockIsReleased() throws Exception {
622647
623648 cacheWriter .lock ("cache" );
624649
625- CompletableFuture <String > value = (CompletableFuture <String >) cache .retrieve (this .key );
626-
650+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
627651 assertThat (value ).isNotDone ();
628652
629653 cacheWriter .unlock ("cache" );
630654
631- assertThat (value .get (15L , TimeUnit .MILLISECONDS )).isEqualTo (testValue );
655+ assertThat (value .get (15L , TimeUnit .MILLISECONDS ). get () ).isEqualTo (testValue );
632656 assertThat (value ).isDone ();
633657 }
634658
@@ -665,8 +689,9 @@ void retrieveStoresLoadedValue() throws Exception {
665689
666690 cache .retrieve (this .key , valueLoaderSupplier ).get ();
667691
668- doWithConnection (connection ->
669- assertThat (connection .keyCommands ().exists ("cache::key-1" .getBytes (StandardCharsets .UTF_8 ))).isTrue ());
692+ doWithConnection (
693+ connection -> assertThat (connection .keyCommands ().exists ("cache::key-1" .getBytes (StandardCharsets .UTF_8 )))
694+ .isTrue ());
670695 }
671696
672697 @ ParameterizedRedisTest // GH-2650
@@ -677,11 +702,18 @@ void retrieveReturnsNull() throws Exception {
677702
678703 RedisCache cache = new RedisCache ("cache" , usingLockingRedisCacheWriter (), usingRedisCacheConfiguration ());
679704
680- CompletableFuture <? > value = cache .retrieve (this .key );
705+ CompletableFuture <ValueWrapper > value = cache .retrieve (this .key );
681706
682707 assertThat (value ).isNotNull ();
683- assertThat (value .get ()).isNull ();
708+ assertThat (value .get (5 , TimeUnit . SECONDS ). get ( )).isNull ();
684709 assertThat (value ).isDone ();
710+
711+ doWithConnection (connection -> connection .keyCommands ().del (this .binaryCacheKey ));
712+
713+ value = cache .retrieve (this .key );
714+
715+ assertThat (value ).isNotNull ();
716+ assertThat (value .get (5 , TimeUnit .SECONDS )).isNull ();
685717 }
686718
687719 private <T > CompletableFuture <T > usingCompletedFuture (T value ) {
0 commit comments