From 50876382db176d5e8d427b70dfa0445e1217ecfb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 23 May 2017 11:37:26 +0200 Subject: [PATCH] Improve Spring Session validation message Closes gh-9284 --- .../session/SessionAutoConfiguration.java | 19 +++++++++++------- .../SessionAutoConfigurationTests.java | 20 +++++++++---------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java index a9aa7341f6..6b1e4ca121 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,6 @@ import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; import org.springframework.session.Session; import org.springframework.session.SessionRepository; -import org.springframework.util.Assert; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Session. @@ -96,11 +95,17 @@ public class SessionAutoConfiguration { @PostConstruct public void checkSessionRepository() { StoreType storeType = this.sessionProperties.getStoreType(); - if (storeType != StoreType.NONE) { - Assert.notNull(this.sessionRepositoryProvider.getIfAvailable(), - "No session repository could be auto-configured, check your " - + "configuration (session store type is '" + storeType - + "')"); + if (storeType != StoreType.NONE + && this.sessionRepositoryProvider.getIfAvailable() == null) { + if (storeType != null) { + throw new IllegalArgumentException("No session repository could be " + + "auto-configured, check your configuration (session store " + + "type is '" + storeType.name().toLowerCase() + "')"); + } + else { + throw new IllegalArgumentException("No Spring Session store is " + + "configured: set the 'spring.session.store-type' property"); + } } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java index f8b3b46b9f..ba9d029cdc 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -52,11 +52,19 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat @Test public void contextFailsIfStoreTypeNotSet() { this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("No session repository could be auto-configured"); - this.thrown.expectMessage("session store type is 'null'"); + this.thrown.expectMessage("No Spring Session store is configured"); + this.thrown.expectMessage("set the 'spring.session.store-type' property"); load(); } + @Test + public void contextFailsIfStoreTypeNotAvailable() { + this.thrown.expect(BeanCreationException.class); + this.thrown.expectMessage("No session repository could be auto-configured"); + this.thrown.expectMessage("session store type is 'mongo'"); + load("spring.session.store-type=mongo"); + } + @Test public void autoConfigurationDisabledIfStoreTypeSetToNone() { load("spring.session.store-type=none"); @@ -116,14 +124,6 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat .isEqualTo("foobar"); } - @Test - public void validationFailsIfSessionRepositoryIsNotConfigured() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("No session repository could be auto-configured"); - this.thrown.expectMessage("session store type is 'JDBC'"); - load("spring.session.store-type=jdbc"); - } - @Configuration static class SessionRepositoryConfiguration {