From ff509eecba0cc56a174aa690227d841572ad2e0b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 8 Mar 2016 11:05:31 +0000 Subject: [PATCH] Improve failure diagnostics for messing Bean Validation provider When the bean validation API is on the class path, but there is no provider available, the stack trace that results is pretty much useless. All the user needs to know is that the have the Bean Validation API on the class path and that they should also add an implementation, such as Hibernate Validator, if they want to use validation. This commit introduces a FailureAnalyser for ValidationException, that returns a FailureAnalysis when the ValidationException is a result of the situation described above. Closes gh-5332 --- .../ValidationExceptionFailureAnalzer.java | 50 ++++++++++++++ ...lidationExceptionFailureAnalyzerTests.java | 68 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalzer.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java diff --git a/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalzer.java b/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalzer.java new file mode 100644 index 0000000000..ec9bc371c2 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalzer.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.diagnostics.analyzer; + +import javax.validation.ValidationException; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; +import org.springframework.boot.diagnostics.FailureAnalyzer; + +/** + * A {@link FailureAnalyzer} that performs analysis of failures caused by a + * {@link ValidationException}. + * + * @author Andy Wilkinson + */ +class ValidationExceptionFailureAnalzer + extends AbstractFailureAnalyzer { + + private static final String MISSING_IMPLEMENTATION_MESSAGE = "Unable to create a " + + "Configuration, because no Bean Validation provider could be found"; + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, ValidationException cause) { + if (cause.getMessage().startsWith(MISSING_IMPLEMENTATION_MESSAGE)) { + return new FailureAnalysis( + "The Bean Validation API is on the classpath but no implementation" + + " could be found", + "Add an implementation, such as Hibernate Validator, to the" + + " classpath", + cause); + } + return null; + } + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java b/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java new file mode 100644 index 0000000000..fb61ba19d1 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.diagnostics.analyzer; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.diagnostics.FailureAnalysis; +import org.springframework.boot.testutil.ClassPathExclusions; +import org.springframework.boot.testutil.FilteredClassPathRunner; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; + +/** + * Tests for {@link ValidationExceptionFailureAnalzer} + * + * @author Andy Wilkinson + */ +@RunWith(FilteredClassPathRunner.class) +@ClassPathExclusions("hibernate-validator-*.jar") +public class ValidationExceptionFailureAnalyzerTests { + + @Test + public void test() throws Exception { + try { + new AnnotationConfigApplicationContext(TestConfiguration.class).close(); + fail("Expected failure did not occur"); + } + catch (Exception ex) { + FailureAnalysis analysis = new ValidationExceptionFailureAnalzer() + .analyze(ex); + assertThat(analysis).isNotNull(); + } + } + + @EnableConfigurationProperties(TestProperties.class) + static class TestConfiguration { + + TestConfiguration(TestProperties testProperties) { + + } + + } + + @ConfigurationProperties("test") + private static class TestProperties { + + } + +}