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
This commit is contained in:
Andy Wilkinson 2016-03-08 11:05:31 +00:00
parent 6035fe4c48
commit ff509eecba
2 changed files with 118 additions and 0 deletions

View File

@ -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<ValidationException> {
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;
}
}

View File

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