Skip to content

Commit da8e5fb

Browse files
vpavicrwinch
authored andcommitted
Ensure configuration classes can be used with @import
This commit adds tests that verify that all Spring Session configuration classes can be used with @import, and fixes JDBC and Hazelcast HttpSession configurations and Redis WebSession configuration.
1 parent 4b34f35 commit da8e5fb

File tree

10 files changed

+149
-0
lines changed

10 files changed

+149
-0
lines changed

spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfigurationTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ void customIndexResolverConfigurationWithProvidedMongoSessionConverter() {
194194
indexResolver);
195195
}
196196

197+
@Test
198+
void importConfigAndCustomize() {
199+
registerAndRefresh(ImportConfigAndCustomizeConfiguration.class);
200+
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
201+
assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0);
202+
}
203+
197204
private void registerAndRefresh(Class<?>... annotatedClasses) {
198205

199206
this.context.register(annotatedClasses);
@@ -333,4 +340,15 @@ IndexResolver<MongoSession> indexResolver() {
333340

334341
}
335342

343+
@Configuration(proxyBeanMethods = false)
344+
@Import(MongoHttpSessionConfiguration.class)
345+
static class ImportConfigAndCustomizeConfiguration extends BaseConfiguration {
346+
347+
@Bean
348+
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizer() {
349+
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
350+
}
351+
352+
}
353+
336354
}

spring-session-data-mongodb/src/test/java/org/springframework/session/data/mongo/config/annotation/web/reactive/ReactiveMongoWebSessionConfigurationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
3031
import org.springframework.data.mongodb.core.MongoOperations;
3132
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
3233
import org.springframework.data.mongodb.core.index.IndexOperations;
@@ -220,6 +221,15 @@ void customIndexResolverConfigurationWithProvidedMongoSessionConverter() {
220221
indexResolver);
221222
}
222223

224+
@Test
225+
void importConfigAndCustomize() {
226+
this.context = new AnnotationConfigApplicationContext();
227+
this.context.register(ImportConfigAndCustomizeConfiguration.class);
228+
this.context.refresh();
229+
ReactiveMongoSessionRepository sessionRepository = this.context.getBean(ReactiveMongoSessionRepository.class);
230+
assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0);
231+
}
232+
223233
/**
224234
* Reflectively extract the {@link AbstractMongoSessionConverter} from the
225235
* {@link ReactiveMongoSessionRepository}. This is to avoid expanding the surface area
@@ -393,4 +403,20 @@ IndexResolver<MongoSession> indexResolver() {
393403

394404
}
395405

406+
@Configuration(proxyBeanMethods = false)
407+
@Import(ReactiveMongoWebSessionConfiguration.class)
408+
static class ImportConfigAndCustomizeConfiguration {
409+
410+
@Bean
411+
ReactiveMongoOperations operations() {
412+
return mock(ReactiveMongoOperations.class);
413+
}
414+
415+
@Bean
416+
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizer() {
417+
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
418+
}
419+
420+
}
421+
396422
}

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
140140
Map<String, Object> attributeMap = importMetadata
141141
.getAnnotationAttributes(EnableRedisWebSession.class.getName());
142142
AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
143+
if (attributes == null) {
144+
return;
145+
}
143146
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
144147
String redisNamespaceValue = attributes.getString("redisNamespace");
145148
if (StringUtils.hasText(redisNamespaceValue)) {

spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisHttpsSessionConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
3031
import org.springframework.context.annotation.Primary;
3132
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
3233
import org.springframework.core.annotation.Order;
@@ -198,6 +199,13 @@ void sessionRepositoryCustomizer() {
198199
Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
199200
}
200201

202+
@Test
203+
void importConfigAndCustomize() {
204+
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
205+
RedisSessionRepository sessionRepository = this.context.getBean(RedisSessionRepository.class);
206+
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
207+
}
208+
201209
private void registerAndRefresh(Class<?>... annotatedClasses) {
202210
this.context.register(annotatedClasses);
203211
this.context.refresh();
@@ -362,4 +370,15 @@ SessionRepositoryCustomizer<RedisSessionRepository> sessionRepositoryCustomizerT
362370

363371
}
364372

373+
@Configuration(proxyBeanMethods = false)
374+
@Import(RedisHttpSessionConfiguration.class)
375+
static class ImportConfigAndCustomizeConfiguration {
376+
377+
@Bean
378+
SessionRepositoryCustomizer<RedisSessionRepository> sessionRepositoryCustomizer() {
379+
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
380+
}
381+
382+
}
383+
365384
}

spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/http/RedisIndexedHttpSessionConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
3031
import org.springframework.context.annotation.Primary;
3132
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
3233
import org.springframework.core.annotation.Order;
@@ -231,6 +232,13 @@ void sessionRepositoryCustomizer() {
231232
MAX_INACTIVE_INTERVAL_IN_SECONDS);
232233
}
233234

235+
@Test
236+
void importConfigAndCustomize() {
237+
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
238+
RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class);
239+
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
240+
}
241+
234242
private void registerAndRefresh(Class<?>... annotatedClasses) {
235243
this.context.register(annotatedClasses);
236244
this.context.refresh();
@@ -424,4 +432,15 @@ SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCust
424432

425433
}
426434

435+
@Configuration(proxyBeanMethods = false)
436+
@Import(RedisIndexedHttpSessionConfiguration.class)
437+
static class ImportConfigAndCustomizeConfiguration {
438+
439+
@Bean
440+
SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizer() {
441+
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
442+
}
443+
444+
}
445+
427446
}

spring-session-data-redis/src/test/java/org/springframework/session/data/redis/config/annotation/web/server/RedisWebSessionConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Import;
2829
import org.springframework.context.annotation.Primary;
2930
import org.springframework.core.annotation.Order;
3031
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
@@ -225,6 +226,13 @@ void sessionRepositoryCustomizer() {
225226
MAX_INACTIVE_INTERVAL_IN_SECONDS);
226227
}
227228

229+
@Test
230+
void importConfigAndCustomize() {
231+
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
232+
ReactiveRedisSessionRepository sessionRepository = this.context.getBean(ReactiveRedisSessionRepository.class);
233+
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
234+
}
235+
228236
private void registerAndRefresh(Class<?>... annotatedClasses) {
229237
this.context.register(annotatedClasses);
230238
this.context.refresh();
@@ -381,4 +389,15 @@ ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepos
381389

382390
}
383391

392+
@Configuration(proxyBeanMethods = false)
393+
@Import(RedisWebSessionConfiguration.class)
394+
static class ImportConfigAndCustomizeConfiguration {
395+
396+
@Bean
397+
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizer() {
398+
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
399+
}
400+
401+
}
402+
384403
}

spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
125125
Map<String, Object> attributeMap = importMetadata
126126
.getAnnotationAttributes(EnableHazelcastHttpSession.class.getName());
127127
AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
128+
if (attributes == null) {
129+
return;
130+
}
128131
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
129132
String sessionMapNameValue = attributes.getString("sessionMapName");
130133
if (StringUtils.hasText(sessionMapNameValue)) {

spring-session-hazelcast/src/test/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfigurationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
3031
import org.springframework.context.annotation.Primary;
3132
import org.springframework.core.annotation.Order;
3233
import org.springframework.session.FlushMode;
@@ -230,6 +231,14 @@ void sessionRepositoryCustomizer() {
230231
MAX_INACTIVE_INTERVAL_IN_SECONDS);
231232
}
232233

234+
@Test
235+
void importConfigAndCustomize() {
236+
registerAndRefresh(ImportConfigAndCustomizeConfiguration.class);
237+
HazelcastIndexedSessionRepository sessionRepository = this.context
238+
.getBean(HazelcastIndexedSessionRepository.class);
239+
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
240+
}
241+
233242
private void registerAndRefresh(Class<?>... annotatedClasses) {
234243
this.context.register(annotatedClasses);
235244
this.context.refresh();
@@ -446,4 +455,15 @@ SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepository
446455

447456
}
448457

458+
@Configuration(proxyBeanMethods = false)
459+
@Import(HazelcastHttpSessionConfiguration.class)
460+
static class ImportConfigAndCustomizeConfiguration extends BaseConfiguration {
461+
462+
@Bean
463+
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizer() {
464+
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
465+
}
466+
467+
}
468+
449469
}

spring-session-jdbc/src/main/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
243243
Map<String, Object> attributeMap = importMetadata
244244
.getAnnotationAttributes(EnableJdbcHttpSession.class.getName());
245245
AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
246+
if (attributes == null) {
247+
return;
248+
}
246249
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
247250
String tableNameValue = attributes.getString("tableName");
248251
if (StringUtils.hasText(tableNameValue)) {

spring-session-jdbc/src/test/java/org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
3031
import org.springframework.context.annotation.Primary;
3132
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
3233
import org.springframework.core.annotation.Order;
@@ -310,6 +311,13 @@ void defaultConfigurationJdbcTemplateHasExpectedExceptionTranslator() {
310311
assertThat(jdbcTemplate.getExceptionTranslator()).isInstanceOf(SQLErrorCodeSQLExceptionTranslator.class);
311312
}
312313

314+
@Test
315+
void importConfigAndCustomize() {
316+
registerAndRefresh(DataSourceConfiguration.class, ImportConfigAndCustomizeConfiguration.class);
317+
JdbcIndexedSessionRepository sessionRepository = this.context.getBean(JdbcIndexedSessionRepository.class);
318+
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
319+
}
320+
313321
private void registerAndRefresh(Class<?>... annotatedClasses) {
314322
this.context.register(annotatedClasses);
315323
this.context.refresh();
@@ -556,4 +564,15 @@ SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCusto
556564

557565
}
558566

567+
@Configuration(proxyBeanMethods = false)
568+
@Import(JdbcHttpSessionConfiguration.class)
569+
static class ImportConfigAndCustomizeConfiguration {
570+
571+
@Bean
572+
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizer() {
573+
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
574+
}
575+
576+
}
577+
559578
}

0 commit comments

Comments
 (0)