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 ;
26
31
import org .springframework .data .redis .connection .RedisConnection ;
27
32
import org .springframework .data .redis .connection .RedisConnectionFactory ;
33
+ import org .springframework .data .redis .serializer .JdkSerializationRedisSerializer ;
34
+ import org .springframework .instrument .classloading .ShadowingClassLoader ;
28
35
29
36
/**
30
37
* @author Christoph Strobl
31
38
*/
32
39
@ RunWith (MockitoJUnitRunner .class )
33
40
public class RedisTemplateUnitTests {
34
41
35
- private RedisTemplate <String , String > template ;
42
+ private RedisTemplate <Object , Object > template ;
36
43
private @ Mock RedisConnectionFactory connectionFactoryMock ;
37
44
private @ Mock RedisConnection redisConnectionMock ;
38
45
39
46
@ Before
40
47
public void setUp () {
41
48
42
- template = new RedisTemplate <String , String >();
49
+ template = new RedisTemplate <Object , Object >();
43
50
template .setConnectionFactory (connectionFactoryMock );
44
51
when (connectionFactoryMock .getConnection ()).thenReturn (redisConnectionMock );
45
52
@@ -66,4 +73,29 @@ public void slaveOfNoOneIsDelegatedToConnectionCorrectly() {
66
73
verify (redisConnectionMock , times (1 )).slaveOfNoOne ();
67
74
}
68
75
76
+ /**
77
+ * @see DATAREDIS-501
78
+ */
79
+ @ Test
80
+ public void templateShouldPassOnAndUseResoureLoaderClassLoaderToDefaultJdkSerializerWhenNotAlreadySet () {
81
+
82
+ ShadowingClassLoader scl = new ShadowingClassLoader (ClassLoader .getSystemClassLoader ());
83
+
84
+ template = new RedisTemplate <Object , Object >();
85
+ template .setConnectionFactory (connectionFactoryMock );
86
+ template .setBeanClassLoader (scl );
87
+ template .afterPropertiesSet ();
88
+
89
+ when (redisConnectionMock .get (any (byte [].class )))
90
+ .thenReturn (new JdkSerializationRedisSerializer ().serialize (new SomeArbitrarySeriaizableObject ()));
91
+
92
+ Object deserialized = template .opsForValue ().get ("spring" );
93
+ assertThat (deserialized , notNullValue ());
94
+ assertThat (deserialized .getClass ().getClassLoader (), is ((ClassLoader ) scl ));
95
+ }
96
+
97
+ static class SomeArbitrarySeriaizableObject implements Serializable {
98
+ private static final long serialVersionUID = -5973659324040506423L ;
99
+ }
100
+
69
101
}
0 commit comments