Upgrade to JPA 2.1+ and Bean Validation 1.1+; remove native support for Hibernate 3.6 and 4.x

Issue: SPR-13481
Issue: SPR-13827
This commit is contained in:
Juergen Hoeller
2016-07-04 23:37:23 +02:00
parent 69ec437fbc
commit 54004e0d78
128 changed files with 299 additions and 26731 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -31,10 +31,9 @@ import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*;
/**
* Tested against Hibernate Validator 4.3, as of Spring 4.0.
* Tested against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
* @since 3.0
*/
public class BeanValidationPostProcessorTests {
@@ -52,6 +51,7 @@ public class BeanValidationPostProcessorTests {
assertTrue(ex.getRootCause().getMessage().contains("testBean"));
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
}
ac.close();
}
@Test
@@ -63,6 +63,7 @@ public class BeanValidationPostProcessorTests {
bd.getPropertyValues().add("testBean", new TestBean());
ac.registerBeanDefinition("bean", bd);
ac.refresh();
ac.close();
}
@Test
@@ -74,6 +75,7 @@ public class BeanValidationPostProcessorTests {
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
ac.refresh();
ac.close();
}
@Test
@@ -92,6 +94,7 @@ public class BeanValidationPostProcessorTests {
assertTrue(ex.getRootCause().getMessage().contains("stringValue"));
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
}
ac.close();
}
@Test
@@ -103,6 +106,7 @@ public class BeanValidationPostProcessorTests {
bd.getPropertyValues().add("stringValue", "ss");
ac.registerBeanDefinition("bean", bd);
ac.refresh();
ac.close();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-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.
@@ -35,11 +35,11 @@ import org.springframework.validation.annotation.Validated;
import static org.junit.Assert.*;
/**
* Tested against Hibernate Validator 4.3, as of Spring 4.0.
* Tests against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
* @since 3.1
*/
@SuppressWarnings("rawtypes")
public class MethodValidationTests {
@Test
@@ -65,6 +65,7 @@ public class MethodValidationTests {
}
@SuppressWarnings("unchecked")
private void doTestProxyValidation(MyValidInterface proxy) {
assertNotNull(proxy.myValidMethod("value", 5));
try {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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,6 +38,10 @@ import javax.validation.constraints.NotNull;
import org.hibernate.validator.HibernateValidator;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
@@ -47,10 +51,9 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Tested against Hibernate Validator 4.3, as of Spring 4.0.
* Tests against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
* @since 3.0
*/
public class ValidatorFactoryTests {
@@ -58,6 +61,7 @@ public class ValidatorFactoryTests {
public void testSimpleValidation() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
assertEquals(2, result.size());
@@ -78,6 +82,7 @@ public class ValidatorFactoryTests {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setProviderClass(HibernateValidator.class);
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
Set<ConstraintViolation<ValidPerson>> result = validator.validate(person);
assertEquals(2, result.size());
@@ -112,6 +117,7 @@ public class ValidatorFactoryTests {
public void testSpringValidationFieldType() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Phil");
person.getAddress().setStreet("Phil's Street");
@@ -126,6 +132,7 @@ public class ValidatorFactoryTests {
public void testSpringValidation() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
@@ -153,6 +160,7 @@ public class ValidatorFactoryTests {
public void testSpringValidationWithClassLevel() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Juergen");
person.getAddress().setStreet("Juergen's Street");
@@ -166,10 +174,32 @@ public class ValidatorFactoryTests {
assertTrue(errorCodes.contains("NameAddressValid"));
}
@Test
public void testSpringValidationWithAutowiredValidator() throws Exception {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(
LocalValidatorFactoryBean.class);
LocalValidatorFactoryBean validator = ctx.getBean(LocalValidatorFactoryBean.class);
ValidPerson person = new ValidPerson();
person.expectsAutowiredValidator = true;
person.setName("Juergen");
person.getAddress().setStreet("Juergen's Street");
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
assertEquals(1, result.getErrorCount());
ObjectError globalError = result.getGlobalError();
List<String> errorCodes = Arrays.asList(globalError.getCodes());
assertEquals(2, errorCodes.size());
assertTrue(errorCodes.contains("NameAddressValid.person"));
assertTrue(errorCodes.contains("NameAddressValid"));
ctx.close();
}
@Test
public void testSpringValidationWithErrorInListElement() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.getAddressList().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
@@ -187,6 +217,7 @@ public class ValidatorFactoryTests {
public void testSpringValidationWithErrorInSetElement() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.getAddressSet().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
@@ -240,6 +271,8 @@ public class ValidatorFactoryTests {
@Valid
private Set<ValidAddress> addressSet = new LinkedHashSet<ValidAddress>();
public boolean expectsAutowiredValidator = false;
public String getName() {
return name;
}
@@ -304,16 +337,22 @@ public class ValidatorFactoryTests {
public static class NameAddressValidator implements ConstraintValidator<NameAddressValid, ValidPerson> {
@Autowired
private Environment environment;
@Override
public void initialize(NameAddressValid constraintAnnotation) {
}
@Override
public boolean isValid(ValidPerson value, ConstraintValidatorContext context) {
if (value.expectsAutowiredValidator) {
assertNotNull(this.environment);
}
boolean valid = (value.name == null || !value.address.street.contains(value.name));
if (!valid && "Phil".equals(value.name)) {
context.buildConstraintViolationWithTemplate(
context.getDefaultConstraintMessageTemplate()).addNode("address").addConstraintViolation().disableDefaultConstraintViolation();
context.getDefaultConstraintMessageTemplate()).addPropertyNode("address").addConstraintViolation().disableDefaultConstraintViolation();
}
return valid;
}
@@ -378,7 +417,7 @@ public class ValidatorFactoryTests {
public boolean isValid(InnerBean bean, ConstraintValidatorContext context) {
context.disableDefaultConstraintViolation();
if (bean.getValue() == null) {
context.buildConstraintViolationWithTemplate("NULL"). addNode("value").addConstraintViolation();
context.buildConstraintViolationWithTemplate("NULL").addPropertyNode("value").addConstraintViolation();
return false;
}
return true;