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 { + + } + +}