Skip to content

Commit 09fde31

Browse files
committed
Support for Hibernate Validator 5.2
Issue: SPR-12758
1 parent 626748d commit 09fde31

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -222,6 +222,17 @@ public void afterPropertiesSet() {
222222
Validation.byProvider(this.providerClass).configure() :
223223
Validation.byDefaultProvider().configure());
224224

225+
// Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method
226+
if (this.applicationContext != null) {
227+
try {
228+
Method eclMethod = configuration.getClass().getMethod("externalClassLoader", ClassLoader.class);
229+
ReflectionUtils.invokeMethod(eclMethod, configuration, this.applicationContext.getClassLoader());
230+
}
231+
catch (NoSuchMethodException ex) {
232+
// Ignore - no Hibernate Validator 5.2+ or similar provider
233+
}
234+
}
235+
225236
MessageInterpolator targetInterpolator = this.messageInterpolator;
226237
if (targetInterpolator == null) {
227238
targetInterpolator = configuration.getDefaultMessageInterpolator();

spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/ValidatorFactoryTests.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,10 @@
3838
import org.hibernate.validator.HibernateValidator;
3939
import org.junit.Test;
4040

41+
import org.springframework.beans.factory.annotation.Autowired;
42+
import org.springframework.context.ApplicationContext;
43+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
44+
import org.springframework.core.env.Environment;
4145
import org.springframework.validation.BeanPropertyBindingResult;
4246
import org.springframework.validation.Errors;
4347
import org.springframework.validation.FieldError;
@@ -60,6 +64,7 @@ public class ValidatorFactoryTests {
6064
public void testSimpleValidation() throws Exception {
6165
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
6266
validator.afterPropertiesSet();
67+
6368
ValidPerson person = new ValidPerson();
6469
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
6570
assertEquals(2, result.size());
@@ -80,6 +85,7 @@ public void testSimpleValidationWithCustomProvider() throws Exception {
8085
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
8186
validator.setProviderClass(HibernateValidator.class);
8287
validator.afterPropertiesSet();
88+
8389
ValidPerson person = new ValidPerson();
8490
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
8591
assertEquals(2, result.size());
@@ -114,6 +120,7 @@ public void testSimpleValidationWithClassLevel() throws Exception {
114120
public void testSpringValidationFieldType() throws Exception {
115121
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
116122
validator.afterPropertiesSet();
123+
117124
ValidPerson person = new ValidPerson();
118125
person.setName("Phil");
119126
person.getAddress().setStreet("Phil's Street");
@@ -128,6 +135,7 @@ public void testSpringValidationFieldType() throws Exception {
128135
public void testSpringValidation() throws Exception {
129136
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
130137
validator.afterPropertiesSet();
138+
131139
ValidPerson person = new ValidPerson();
132140
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
133141
validator.validate(person, result);
@@ -155,6 +163,7 @@ public void testSpringValidation() throws Exception {
155163
public void testSpringValidationWithClassLevel() throws Exception {
156164
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
157165
validator.afterPropertiesSet();
166+
158167
ValidPerson person = new ValidPerson();
159168
person.setName("Juergen");
160169
person.getAddress().setStreet("Juergen's Street");
@@ -168,10 +177,30 @@ public void testSpringValidationWithClassLevel() throws Exception {
168177
assertTrue(errorCodes.contains("NameAddressValid"));
169178
}
170179

180+
@Test
181+
public void testSpringValidationWithAutowiredValidator() throws Exception {
182+
ApplicationContext ctx = new AnnotationConfigApplicationContext(LocalValidatorFactoryBean.class);
183+
LocalValidatorFactoryBean validator = ctx.getBean(LocalValidatorFactoryBean.class);
184+
185+
ValidPerson person = new ValidPerson();
186+
person.expectsAutowiredValidator = true;
187+
person.setName("Juergen");
188+
person.getAddress().setStreet("Juergen's Street");
189+
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
190+
validator.validate(person, result);
191+
assertEquals(1, result.getErrorCount());
192+
ObjectError globalError = result.getGlobalError();
193+
List<String> errorCodes = Arrays.asList(globalError.getCodes());
194+
assertEquals(2, errorCodes.size());
195+
assertTrue(errorCodes.contains("NameAddressValid.person"));
196+
assertTrue(errorCodes.contains("NameAddressValid"));
197+
}
198+
171199
@Test
172200
public void testSpringValidationWithErrorInListElement() throws Exception {
173201
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
174202
validator.afterPropertiesSet();
203+
175204
ValidPerson person = new ValidPerson();
176205
person.getAddressList().add(new ValidAddress());
177206
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
@@ -189,6 +218,7 @@ public void testSpringValidationWithErrorInListElement() throws Exception {
189218
public void testSpringValidationWithErrorInSetElement() throws Exception {
190219
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
191220
validator.afterPropertiesSet();
221+
192222
ValidPerson person = new ValidPerson();
193223
person.getAddressSet().add(new ValidAddress());
194224
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
@@ -242,6 +272,8 @@ public static class ValidPerson {
242272
@Valid
243273
private Set<ValidAddress> addressSet = new LinkedHashSet<ValidAddress>();
244274

275+
public boolean expectsAutowiredValidator = false;
276+
245277
public String getName() {
246278
return name;
247279
}
@@ -306,12 +338,18 @@ public void setStreet(String street) {
306338

307339
public static class NameAddressValidator implements ConstraintValidator<NameAddressValid, ValidPerson> {
308340

341+
@Autowired
342+
private Environment environment;
343+
309344
@Override
310345
public void initialize(NameAddressValid constraintAnnotation) {
311346
}
312347

313348
@Override
314349
public boolean isValid(ValidPerson value, ConstraintValidatorContext context) {
350+
if (value.expectsAutowiredValidator) {
351+
assertNotNull(this.environment);
352+
}
315353
boolean valid = (value.name == null || !value.address.street.contains(value.name));
316354
if (!valid && "Phil".equals(value.name)) {
317355
context.buildConstraintViolationWithTemplate(

0 commit comments

Comments
 (0)