15
15
*/
16
16
package org .springframework .data .redis .core ;
17
17
18
+ import static org .hamcrest .Matchers .*;
18
19
import static org .junit .Assert .*;
19
20
import static org .junit .Assume .*;
20
21
import static org .springframework .data .redis .SpinBarrier .*;
54
55
import org .springframework .data .redis .connection .StringRedisConnection ;
55
56
import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
56
57
import org .springframework .data .redis .connection .jredis .JredisConnectionFactory ;
58
+ import org .springframework .data .redis .connection .lettuce .LettuceConnectionFactory ;
57
59
import org .springframework .data .redis .connection .srp .SrpConnectionFactory ;
58
60
import org .springframework .data .redis .core .ZSetOperations .TypedTuple ;
59
61
import org .springframework .data .redis .core .query .SortQueryBuilder ;
65
67
66
68
/**
67
69
* Integration test of {@link RedisTemplate}
68
- *
70
+ *
69
71
* @author Jennifer Hickey
70
72
* @author Christoph Strobl
71
73
* @author Anqing Shao
74
+ * @author Duobiao Ou
72
75
*/
73
76
@ RunWith (Parameterized .class )
74
77
public class RedisTemplateTests <K , V > {
@@ -302,9 +305,9 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
302
305
@ SuppressWarnings ("rawtypes" )
303
306
@ Test
304
307
public void testExecutePipelinedCustomSerializer () {
305
-
308
+
306
309
assumeTrue (redisTemplate instanceof StringRedisTemplate );
307
-
310
+
308
311
List <Object > results = redisTemplate .executePipelined (new RedisCallback () {
309
312
public Object doInRedis (RedisConnection connection ) throws DataAccessException {
310
313
StringRedisConnection stringRedisConn = (StringRedisConnection ) connection ;
@@ -316,7 +319,7 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
316
319
return null ;
317
320
}
318
321
}, new GenericToStringSerializer <Long >(Long .class ));
319
-
322
+
320
323
assertEquals (Arrays .asList (new Object [] { 5l , 1l , 2l , Arrays .asList (new Long [] { 10l , 11l }) }), results );
321
324
}
322
325
@@ -325,24 +328,24 @@ public Object doInRedis(RedisConnection connection) throws DataAccessException {
325
328
*/
326
329
@ Test
327
330
public void testExecutePipelinedWidthDifferentHashKeySerializerAndHashValueSerializer () {
328
-
331
+
329
332
assumeTrue (redisTemplate instanceof StringRedisTemplate );
330
-
333
+
331
334
redisTemplate .setKeySerializer (new StringRedisSerializer ());
332
335
redisTemplate .setHashKeySerializer (new GenericToStringSerializer <Long >(Long .class ));
333
336
redisTemplate .setHashValueSerializer (new Jackson2JsonRedisSerializer <Person >(Person .class ));
334
337
335
338
Person person = new Person ("Homer" , "Simpson" , 38 );
336
-
339
+
337
340
redisTemplate .opsForHash ().put ((K ) "foo" , 1L , person );
338
-
341
+
339
342
List <Object > results = redisTemplate .executePipelined (new RedisCallback () {
340
343
public Object doInRedis (RedisConnection connection ) throws DataAccessException {
341
344
connection .hGetAll (((StringRedisSerializer ) redisTemplate .getKeySerializer ()).serialize ("foo" ));
342
345
return null ;
343
346
}
344
347
});
345
-
348
+
346
349
assertEquals (((Map ) results .get (0 )).get (1L ), person );
347
350
}
348
351
@@ -530,37 +533,70 @@ public void testGetExpireSeconds() {
530
533
assertEquals (Long .valueOf (1 ), redisTemplate .getExpire (key1 , TimeUnit .SECONDS ));
531
534
}
532
535
536
+ /**
537
+ * @see DATAREDIS-526
538
+ */
533
539
@ Test
534
540
public void testGetExpireSecondsForKeyDoesNotExist () {
535
- final K key1 = keyFactory . instance ();
536
- Long expire = redisTemplate .getExpire (key1 , TimeUnit .SECONDS );
537
- assertTrue (expire < 0l );
541
+
542
+ Long expire = redisTemplate .getExpire (keyFactory . instance () , TimeUnit .SECONDS );
543
+ assertTrue (expire < 0L );
538
544
}
539
545
546
+ /**
547
+ * @see DATAREDIS-526
548
+ */
540
549
@ Test
541
550
public void testGetExpireSecondsForKeyExistButHasNoAssociatedExpire () {
542
- final K key1 = keyFactory .instance ();
543
- V value1 = valueFactory .instance ();
544
- redisTemplate .boundValueOps (key1 ).set (value1 );
545
- Long expire = redisTemplate .getExpire (key1 , TimeUnit .SECONDS );
546
- assertTrue (expire < 0l );
547
- }
548
551
552
+ K key = keyFactory .instance ();
553
+ Long expire = redisTemplate .getExpire (key , TimeUnit .SECONDS );
549
554
555
+ assertTrue (expire < 0L );
556
+ }
557
+
558
+ /**
559
+ * @see DATAREDIS-526
560
+ */
550
561
@ Test
551
562
public void testGetExpireMillisForKeyDoesNotExist () {
552
- final K key1 = keyFactory .instance ();
553
- Long expire = redisTemplate .getExpire (key1 , TimeUnit .MILLISECONDS );
554
- assertTrue (expire < 0l );
563
+
564
+ Long expire = redisTemplate .getExpire (keyFactory .instance (), TimeUnit .MILLISECONDS );
565
+
566
+ assertTrue (expire < 0L );
555
567
}
556
568
569
+ /**
570
+ * @see DATAREDIS-526
571
+ */
557
572
@ Test
558
573
public void testGetExpireMillisForKeyExistButHasNoAssociatedExpire () {
559
- final K key1 = keyFactory .instance ();
560
- V value1 = valueFactory .instance ();
561
- redisTemplate .boundValueOps (key1 ).set (value1 );
562
- Long expire = redisTemplate .getExpire (key1 , TimeUnit .MILLISECONDS );
563
- assertTrue (expire < 0l );
574
+
575
+ K key = keyFactory .instance ();
576
+ redisTemplate .boundValueOps (key ).set (valueFactory .instance ());
577
+
578
+ Long expire = redisTemplate .getExpire (key , TimeUnit .MILLISECONDS );
579
+
580
+ assertTrue (expire < 0L );
581
+ }
582
+
583
+ /**
584
+ * @see DATAREDIS-526
585
+ */
586
+ @ Test
587
+ public void testGetExpireMillis () {
588
+
589
+ assumeTrue (redisTemplate .getConnectionFactory () instanceof JedisConnectionFactory
590
+ || redisTemplate .getConnectionFactory () instanceof LettuceConnectionFactory );
591
+
592
+ final K key = keyFactory .instance ();
593
+ redisTemplate .boundValueOps (key ).set (valueFactory .instance ());
594
+ redisTemplate .expire (key , 1 , TimeUnit .DAYS );
595
+
596
+ Long ttl = redisTemplate .getExpire (key , TimeUnit .HOURS );
597
+
598
+ assertThat (ttl , greaterThanOrEqualTo (23L ));
599
+ assertThat (ttl , lessThan (25L ));
564
600
}
565
601
566
602
@ Test
@@ -771,10 +807,8 @@ public void testExecuteScriptCustomSerializers() {
771
807
final DefaultRedisScript <String > script = new DefaultRedisScript <String >();
772
808
script .setScriptText ("return 'Hey'" );
773
809
script .setResultType (String .class );
774
- assertEquals (
775
- "Hey" ,
776
- redisTemplate .execute (script , redisTemplate .getValueSerializer (), new StringRedisSerializer (),
777
- Collections .singletonList (key1 )));
810
+ assertEquals ("Hey" , redisTemplate .execute (script , redisTemplate .getValueSerializer (), new StringRedisSerializer (),
811
+ Collections .singletonList (key1 )));
778
812
}
779
813
780
814
@ Test
0 commit comments