1
1
/*
2
- * Copyright 2014 the original author or authors.
2
+ * Copyright 2014-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .redis .core ;
17
17
18
+ import static org .hamcrest .core .Is .*;
19
+ import static org .hamcrest .core .IsNull .*;
20
+ import static org .junit .Assert .*;
18
21
import static org .mockito .Matchers .*;
19
22
import static org .mockito .Mockito .*;
20
23
24
+ import java .io .Serializable ;
25
+
21
26
import org .junit .Before ;
22
27
import org .junit .Test ;
23
28
import org .junit .runner .RunWith ;
24
29
import org .mockito .Mock ;
25
30
import org .mockito .runners .MockitoJUnitRunner ;
31
+ import org .springframework .core .io .DefaultResourceLoader ;
26
32
import org .springframework .data .redis .connection .RedisConnection ;
27
33
import org .springframework .data .redis .connection .RedisConnectionFactory ;
34
+ import org .springframework .data .redis .serializer .JdkSerializationRedisSerializer ;
35
+ import org .springframework .instrument .classloading .ShadowingClassLoader ;
28
36
29
37
/**
30
38
* @author Christoph Strobl
31
39
*/
32
40
@ RunWith (MockitoJUnitRunner .class )
33
41
public class RedisTemplateUnitTests {
34
42
35
- private RedisTemplate <String , String > template ;
43
+ private RedisTemplate <Object , Object > template ;
36
44
private @ Mock RedisConnectionFactory connectionFactoryMock ;
37
45
private @ Mock RedisConnection redisConnectionMock ;
38
46
39
47
@ Before
40
48
public void setUp () {
41
49
42
- template = new RedisTemplate <String , String >();
50
+ template = new RedisTemplate <Object , Object >();
43
51
template .setConnectionFactory (connectionFactoryMock );
44
52
when (connectionFactoryMock .getConnection ()).thenReturn (redisConnectionMock );
45
53
@@ -66,4 +74,29 @@ public void slaveOfNoOneIsDelegatedToConnectionCorrectly() {
66
74
verify (redisConnectionMock , times (1 )).slaveOfNoOne ();
67
75
}
68
76
77
+ /**
78
+ * @see DATAREDIS-501
79
+ */
80
+ @ Test
81
+ public void templateShouldPassOnAndUseResoureLoaderClassLoaderToDefaultJdkSerializerWhenNotAlreadySet () {
82
+
83
+ ShadowingClassLoader scl = new ShadowingClassLoader (ClassLoader .getSystemClassLoader ());
84
+
85
+ template = new RedisTemplate <Object , Object >();
86
+ template .setConnectionFactory (connectionFactoryMock );
87
+ template .setResourceLoader (new DefaultResourceLoader (scl ));
88
+ template .afterPropertiesSet ();
89
+
90
+ when (redisConnectionMock .get (any (byte [].class )))
91
+ .thenReturn (new JdkSerializationRedisSerializer ().serialize (new SomeArbitrarySeriaizableObject ()));
92
+
93
+ Object deserialized = template .opsForValue ().get ("spring" );
94
+ assertThat (deserialized , notNullValue ());
95
+ assertThat (deserialized .getClass ().getClassLoader (), is ((ClassLoader ) scl ));
96
+ }
97
+
98
+ static class SomeArbitrarySeriaizableObject implements Serializable {
99
+ private static final long serialVersionUID = -5973659324040506423L ;
100
+ }
101
+
69
102
}
0 commit comments