25
25
26
26
import org .springframework .boot .autoconfigure .AutoConfigurations ;
27
27
import org .springframework .boot .autoconfigure .data .redis .RedisAutoConfiguration ;
28
- import org .springframework .boot .autoconfigure .session .RedisSessionConfiguration .SpringBootRedisHttpSessionConfiguration ;
28
+ import org .springframework .boot .autoconfigure .session .RedisSessionConfiguration .IndexedRedisSessionConfiguration . SpringBootRedisIndexedHttpSessionConfiguration ;
29
29
import org .springframework .boot .autoconfigure .web .ServerProperties ;
30
+ import org .springframework .boot .context .properties .source .InvalidConfigurationPropertyValueException ;
30
31
import org .springframework .boot .test .context .FilteredClassLoader ;
31
32
import org .springframework .boot .test .context .assertj .AssertableWebApplicationContext ;
32
33
import org .springframework .boot .test .context .runner .ContextConsumer ;
38
39
import org .springframework .session .SaveMode ;
39
40
import org .springframework .session .data .mongo .MongoIndexedSessionRepository ;
40
41
import org .springframework .session .data .redis .RedisIndexedSessionRepository ;
42
+ import org .springframework .session .data .redis .RedisSessionRepository ;
41
43
import org .springframework .session .data .redis .config .ConfigureNotifyKeyspaceEventsAction ;
42
44
import org .springframework .session .data .redis .config .ConfigureRedisAction ;
43
45
import org .springframework .session .hazelcast .HazelcastIndexedSessionRepository ;
@@ -70,17 +72,28 @@ void defaultConfig() {
70
72
.withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
71
73
"spring.data.redis.port=" + redis .getFirstMappedPort ())
72
74
.withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
73
- .run (validateSpringSessionUsesRedis ("spring:session:event:0:created:" , FlushMode .ON_SAVE ,
74
- SaveMode .ON_SET_ATTRIBUTE , "0 * * * * *" ));
75
+ .run (validateSpringSessionUsesDefaultRedis ("spring:session:" , FlushMode .ON_SAVE ,
76
+ SaveMode .ON_SET_ATTRIBUTE ));
77
+ }
78
+
79
+ @ Test
80
+ void invalidConfigurationPropertyValueWhenDefaultConfigIsUsedWithCustomCronCleanup () {
81
+ this .contextRunner .withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
82
+ "spring.data.redis.port=" + redis .getFirstMappedPort (), "spring.session.redis.cleanup-cron=0 0 * * * *" )
83
+ .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class )).run ((context ) -> {
84
+ assertThat (context ).hasFailed ();
85
+ assertThat (context .getStartupFailure ())
86
+ .hasRootCauseExactlyInstanceOf (InvalidConfigurationPropertyValueException .class );
87
+ });
75
88
}
76
89
77
90
@ Test
78
91
void redisTakesPrecedenceMultipleImplementations () {
79
92
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
80
93
.withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
81
94
"spring.data.redis.port=" + redis .getFirstMappedPort ())
82
- .run (validateSpringSessionUsesRedis ("spring:session:event:0:created :" , FlushMode .ON_SAVE ,
83
- SaveMode .ON_SET_ATTRIBUTE , "0 * * * * *" ));
95
+ .run (validateSpringSessionUsesDefaultRedis ("spring:session:" , FlushMode .ON_SAVE ,
96
+ SaveMode .ON_SET_ATTRIBUTE ));
84
97
}
85
98
86
99
@ Test
@@ -89,64 +102,98 @@ void defaultConfigWithCustomTimeout() {
89
102
.withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
90
103
"spring.data.redis.port=" + redis .getFirstMappedPort (), "spring.session.timeout=1m" )
91
104
.withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class )).run ((context ) -> {
92
- RedisIndexedSessionRepository repository = validateSessionRepository (context ,
93
- RedisIndexedSessionRepository .class );
94
- assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" , 60 );
105
+ RedisSessionRepository repository = validateSessionRepository (context ,
106
+ RedisSessionRepository .class );
107
+ assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" ,
108
+ Duration .ofMinutes (1 ));
95
109
});
96
110
}
97
111
98
112
@ Test
99
- void redisSessionStoreWithCustomizations () {
113
+ void defaultRedisSessionStoreWithCustomizations () {
100
114
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
101
115
.withPropertyValues ("spring.session.redis.namespace=foo" , "spring.session.redis.flush-mode=immediate" ,
116
+ "spring.session.redis.save-mode=on-get-attribute" , "spring.data.redis.host=" + redis .getHost (),
117
+ "spring.data.redis.port=" + redis .getFirstMappedPort ())
118
+ .run (validateSpringSessionUsesDefaultRedis ("foo:" , FlushMode .IMMEDIATE , SaveMode .ON_GET_ATTRIBUTE ));
119
+ }
120
+
121
+ @ Test
122
+ void indexedRedisSessionDefaultConfig () {
123
+ this .contextRunner .withPropertyValues ("spring.session.redis.repository-type=indexed" ,
124
+ "spring.data.redis.host=" + redis .getHost (), "spring.data.redis.port=" + redis .getFirstMappedPort ())
125
+ .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
126
+ .run (validateSpringSessionUsesIndexedRedis ("spring:session:" , FlushMode .ON_SAVE ,
127
+ SaveMode .ON_SET_ATTRIBUTE , "0 * * * * *" ));
128
+ }
129
+
130
+ @ Test
131
+ void indexedRedisSessionStoreWithCustomizations () {
132
+ this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
133
+ .withPropertyValues ("spring.session.redis.repository-type=indexed" ,
134
+ "spring.session.redis.namespace=foo" , "spring.session.redis.flush-mode=immediate" ,
102
135
"spring.session.redis.save-mode=on-get-attribute" ,
103
136
"spring.session.redis.cleanup-cron=0 0 12 * * *" , "spring.data.redis.host=" + redis .getHost (),
104
137
"spring.data.redis.port=" + redis .getFirstMappedPort ())
105
- .run (validateSpringSessionUsesRedis ("foo:event:0:created: " , FlushMode .IMMEDIATE ,
106
- SaveMode . ON_GET_ATTRIBUTE , "0 0 12 * * *" ));
138
+ .run (validateSpringSessionUsesIndexedRedis ("foo:" , FlushMode .IMMEDIATE , SaveMode . ON_GET_ATTRIBUTE ,
139
+ "0 0 12 * * *" ));
107
140
}
108
141
109
142
@ Test
110
- void redisSessionWithConfigureActionNone () {
143
+ void indexedRedisSessionWithConfigureActionNone () {
111
144
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
112
- .withPropertyValues ("spring.session.redis.configure-action=none " ,
113
- "spring.data.redis.host=" + redis .getHost (),
145
+ .withPropertyValues ("spring.session.redis.repository-type=indexed " ,
146
+ "spring.session.redis.configure-action=none" , "spring. data.redis.host=" + redis .getHost (),
114
147
"spring.data.redis.port=" + redis .getFirstMappedPort ())
115
148
.run (validateStrategy (ConfigureRedisAction .NO_OP .getClass ()));
116
149
}
117
150
118
151
@ Test
119
- void redisSessionWithDefaultConfigureActionNone () {
152
+ void indexedRedisSessionWithDefaultConfigureActionNone () {
120
153
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
121
- .withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
154
+ .withPropertyValues ("spring.session.redis.repository-type=indexed" ,
155
+ "spring.data.redis.host=" + redis .getHost (),
122
156
"spring.data.redis.port=" + redis .getFirstMappedPort ())
123
157
.run (validateStrategy (ConfigureNotifyKeyspaceEventsAction .class ,
124
158
entry ("notify-keyspace-events" , "gxE" )));
125
159
}
126
160
127
161
@ Test
128
- void redisSessionWithCustomConfigureRedisActionBean () {
162
+ void indexedRedisSessionWithCustomConfigureRedisActionBean () {
129
163
this .contextRunner .withConfiguration (AutoConfigurations .of (RedisAutoConfiguration .class ))
130
164
.withUserConfiguration (MaxEntriesRedisAction .class )
131
- .withPropertyValues ("spring.data.redis.host=" + redis .getHost (),
165
+ .withPropertyValues ("spring.session.redis.repository-type=indexed" ,
166
+ "spring.data.redis.host=" + redis .getHost (),
132
167
"spring.data.redis.port=" + redis .getFirstMappedPort ())
133
168
.run (validateStrategy (MaxEntriesRedisAction .class , entry ("set-max-intset-entries" , "1024" )));
134
169
135
170
}
136
171
137
- private ContextConsumer <AssertableWebApplicationContext > validateSpringSessionUsesRedis (
138
- String sessionCreatedChannelPrefix , FlushMode flushMode , SaveMode saveMode , String cleanupCron ) {
172
+ private ContextConsumer <AssertableWebApplicationContext > validateSpringSessionUsesDefaultRedis (String keyNamespace ,
173
+ FlushMode flushMode , SaveMode saveMode ) {
174
+ return (context ) -> {
175
+ RedisSessionRepository repository = validateSessionRepository (context , RedisSessionRepository .class );
176
+ assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" ,
177
+ new ServerProperties ().getServlet ().getSession ().getTimeout ());
178
+ assertThat (repository ).hasFieldOrPropertyWithValue ("keyNamespace" , keyNamespace );
179
+ assertThat (repository ).hasFieldOrPropertyWithValue ("flushMode" , flushMode );
180
+ assertThat (repository ).hasFieldOrPropertyWithValue ("saveMode" , saveMode );
181
+ };
182
+ }
183
+
184
+ private ContextConsumer <AssertableWebApplicationContext > validateSpringSessionUsesIndexedRedis (String keyNamespace ,
185
+ FlushMode flushMode , SaveMode saveMode , String cleanupCron ) {
139
186
return (context ) -> {
140
187
RedisIndexedSessionRepository repository = validateSessionRepository (context ,
141
188
RedisIndexedSessionRepository .class );
142
189
assertThat (repository ).hasFieldOrPropertyWithValue ("defaultMaxInactiveInterval" ,
143
190
(int ) new ServerProperties ().getServlet ().getSession ().getTimeout ().getSeconds ());
144
- assertThat (repository . getSessionCreatedChannelPrefix ()). isEqualTo ( sessionCreatedChannelPrefix );
191
+ assertThat (repository ). hasFieldOrPropertyWithValue ( "namespace" , keyNamespace );
145
192
assertThat (repository ).hasFieldOrPropertyWithValue ("flushMode" , flushMode );
146
- SpringBootRedisHttpSessionConfiguration configuration = context
147
- .getBean (SpringBootRedisHttpSessionConfiguration .class );
148
- assertThat (configuration ).hasFieldOrPropertyWithValue ("cleanupCron" , cleanupCron );
149
193
assertThat (repository ).hasFieldOrPropertyWithValue ("saveMode" , saveMode );
194
+ SpringBootRedisIndexedHttpSessionConfiguration configuration = context
195
+ .getBean (SpringBootRedisIndexedHttpSessionConfiguration .class );
196
+ assertThat (configuration ).hasFieldOrPropertyWithValue ("cleanupCron" , cleanupCron );
150
197
};
151
198
}
152
199
0 commit comments