Automated tests against Hibernate ORM 4.3 and Hibernate Validator 5
Issue: SPR-11704
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -64,7 +64,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
* into any target dependency of type {@link org.springframework.validation.Validator}!
|
||||
*
|
||||
* <p><b>As of Spring 4.0, this class supports Bean Validation 1.0 and 1.1, with special support
|
||||
* for Hibernate Validator 4.3 and 5.0</b> (see {@link #setValidationMessageSource}).
|
||||
* for Hibernate Validator 4.3 and 5.x</b> (see {@link #setValidationMessageSource}).
|
||||
*
|
||||
* <p>Note that Bean Validation 1.1's {@code #forExecutables} method isn't supported: We do not
|
||||
* expect that method to be called by application code; consider {@link MethodValidationInterceptor}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -26,7 +26,7 @@ import org.springframework.context.support.MessageSourceResourceBundle;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation of Hibernate Validator 4.3/5.0's {@link ResourceBundleLocator} interface,
|
||||
* Implementation of Hibernate Validator 4.3/5.x's {@link ResourceBundleLocator} interface,
|
||||
* exposing a Spring {@link MessageSource} as localized {@link MessageSourceResourceBundle}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -18,7 +18,6 @@ package org.springframework.validation.beanvalidation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.Validation;
|
||||
@@ -28,6 +27,7 @@ import javax.validation.ValidatorFactory;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.hibernate.validator.HibernateValidator;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@@ -47,8 +47,8 @@ import org.springframework.validation.annotation.Validated;
|
||||
* of that class. By default, JSR-303 will validate against its default group only.
|
||||
*
|
||||
* <p>As of Spring 4.0, this functionality requires either a Bean Validation 1.1 provider
|
||||
* (such as Hibernate Validator 5.0) or the Bean Validation 1.0 API with Hibernate Validator
|
||||
* 4.2 or 4.3. The actual provider will be autodetected and automatically adapted.
|
||||
* (such as Hibernate Validator 5.x) or the Bean Validation 1.0 API with Hibernate Validator
|
||||
* 4.3. The actual provider will be autodetected and automatically adapted.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
@@ -147,7 +147,7 @@ public class MethodValidationInterceptor implements MethodInterceptor {
|
||||
|
||||
|
||||
/**
|
||||
* Inner class to avoid a hard-coded Hibernate Validator 4.2/4.3 dependency.
|
||||
* Inner class to avoid a hard-coded Hibernate Validator 4.3 dependency.
|
||||
*/
|
||||
private static class HibernateValidatorDelegate {
|
||||
|
||||
@@ -159,9 +159,11 @@ public class MethodValidationInterceptor implements MethodInterceptor {
|
||||
public static Object invokeWithinValidation(MethodInvocation invocation, Validator validator, Class<?>[] groups)
|
||||
throws Throwable {
|
||||
|
||||
org.hibernate.validator.method.MethodValidator methodValidator = validator.unwrap(org.hibernate.validator.method.MethodValidator.class);
|
||||
Set<org.hibernate.validator.method.MethodConstraintViolation<Object>> result = methodValidator.validateAllParameters(
|
||||
invocation.getThis(), invocation.getMethod(), invocation.getArguments(), groups);
|
||||
org.hibernate.validator.method.MethodValidator methodValidator =
|
||||
validator.unwrap(org.hibernate.validator.method.MethodValidator.class);
|
||||
Set<org.hibernate.validator.method.MethodConstraintViolation<Object>> result =
|
||||
methodValidator.validateAllParameters(
|
||||
invocation.getThis(), invocation.getMethod(), invocation.getArguments(), groups);
|
||||
if (!result.isEmpty()) {
|
||||
throw new org.hibernate.validator.method.MethodConstraintViolationException(result);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -49,8 +49,8 @@ import org.springframework.validation.annotation.Validated;
|
||||
* as well. By default, JSR-303 will validate against its default group only.
|
||||
*
|
||||
* <p>As of Spring 4.0, this functionality requires either a Bean Validation 1.1 provider
|
||||
* (such as Hibernate Validator 5.0) or the Bean Validation 1.0 API with Hibernate Validator
|
||||
* 4.2 or 4.3. The actual provider will be autodetected and automatically adapted.
|
||||
* (such as Hibernate Validator 5.x) or the Bean Validation 1.0 API with Hibernate Validator
|
||||
* 4.3. The actual provider will be autodetected and automatically adapted.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -30,6 +30,8 @@ import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
/**
|
||||
* Tested against Hibernate Validator 4.3, as of Spring 4.0.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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,6 +31,8 @@ import org.springframework.validation.annotation.Validated;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tested against Hibernate Validator 4.3, as of Spring 4.0.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
*/
|
||||
@@ -103,7 +105,6 @@ public class MethodValidationTests {
|
||||
@Validated({MyGroup.class, Default.class})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MyStereotype {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -46,6 +46,8 @@ import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tested against Hibernate Validator 4.3, as of Spring 4.0.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
@@ -67,6 +69,7 @@ public class ValidatorFactoryTests {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
}
|
||||
validator.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -86,6 +89,7 @@ public class ValidatorFactoryTests {
|
||||
fail("Invalid constraint violation with path '" + path + "'");
|
||||
}
|
||||
}
|
||||
validator.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -133,7 +137,6 @@ public class ValidatorFactoryTests {
|
||||
assertTrue(errorCodes.contains("NotNull.name"));
|
||||
assertTrue(errorCodes.contains("NotNull.java.lang.String"));
|
||||
assertTrue(errorCodes.contains("NotNull"));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
errorCodes = Arrays.asList(fieldError.getCodes());
|
||||
@@ -143,7 +146,6 @@ public class ValidatorFactoryTests {
|
||||
assertTrue(errorCodes.contains("NotNull.street"));
|
||||
assertTrue(errorCodes.contains("NotNull.java.lang.String"));
|
||||
assertTrue(errorCodes.contains("NotNull"));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -161,7 +163,6 @@ public class ValidatorFactoryTests {
|
||||
assertEquals(2, errorCodes.size());
|
||||
assertTrue(errorCodes.contains("NameAddressValid.person"));
|
||||
assertTrue(errorCodes.contains("NameAddressValid"));
|
||||
System.out.println(globalError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -175,16 +176,10 @@ public class ValidatorFactoryTests {
|
||||
assertEquals(3, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("addressList[0].street");
|
||||
assertEquals("addressList[0].street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -198,16 +193,10 @@ public class ValidatorFactoryTests {
|
||||
assertEquals(3, result.getErrorCount());
|
||||
FieldError fieldError = result.getFieldError("name");
|
||||
assertEquals("name", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("address.street");
|
||||
assertEquals("address.street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
fieldError = result.getFieldError("addressSet[].street");
|
||||
assertEquals("addressSet[].street", fieldError.getField());
|
||||
System.out.println(Arrays.asList(fieldError.getCodes()));
|
||||
System.out.println(fieldError.getDefaultMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -271,6 +260,7 @@ public class ValidatorFactoryTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ValidAddress {
|
||||
|
||||
@NotNull
|
||||
@@ -285,6 +275,7 @@ public class ValidatorFactoryTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = NameAddressValidator.class)
|
||||
@@ -297,6 +288,7 @@ public class ValidatorFactoryTests {
|
||||
Class<?>[] payload() default {};
|
||||
}
|
||||
|
||||
|
||||
public static class NameAddressValidator implements ConstraintValidator<NameAddressValid, ValidPerson> {
|
||||
|
||||
@Override
|
||||
@@ -325,6 +317,7 @@ public class ValidatorFactoryTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class InnerBean {
|
||||
|
||||
private String value;
|
||||
@@ -337,15 +330,20 @@ public class ValidatorFactoryTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@Constraint(validatedBy=InnerValidator.class)
|
||||
public static @interface InnerValid {
|
||||
|
||||
String message() default "NOT VALID";
|
||||
|
||||
Class<?>[] groups() default { };
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
||||
|
||||
|
||||
public static class InnerValidator implements ConstraintValidator<InnerValid, InnerBean> {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user