|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* Copyright 2002-2019 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -34,6 +34,7 @@ import java.util.Set; |
|
|
|
import javax.validation.Constraint; |
|
|
|
import javax.validation.Constraint; |
|
|
|
import javax.validation.ConstraintValidator; |
|
|
|
import javax.validation.ConstraintValidator; |
|
|
|
import javax.validation.ConstraintValidatorContext; |
|
|
|
import javax.validation.ConstraintValidatorContext; |
|
|
|
|
|
|
|
import javax.validation.ConstraintViolation; |
|
|
|
import javax.validation.Payload; |
|
|
|
import javax.validation.Payload; |
|
|
|
import javax.validation.Valid; |
|
|
|
import javax.validation.Valid; |
|
|
|
import javax.validation.Validation; |
|
|
|
import javax.validation.Validation; |
|
|
|
@ -50,6 +51,7 @@ import org.springframework.beans.BeanWrapperImpl; |
|
|
|
import org.springframework.context.support.StaticMessageSource; |
|
|
|
import org.springframework.context.support.StaticMessageSource; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.validation.BeanPropertyBindingResult; |
|
|
|
import org.springframework.validation.BeanPropertyBindingResult; |
|
|
|
|
|
|
|
import org.springframework.validation.FieldError; |
|
|
|
import org.springframework.validation.beanvalidation.SpringValidatorAdapter; |
|
|
|
import org.springframework.validation.beanvalidation.SpringValidatorAdapter; |
|
|
|
|
|
|
|
|
|
|
|
import static java.lang.annotation.ElementType.*; |
|
|
|
import static java.lang.annotation.ElementType.*; |
|
|
|
@ -73,7 +75,7 @@ public class SpringValidatorAdapterTests { |
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setupSpringValidatorAdapter() { |
|
|
|
public void setupSpringValidatorAdapter() { |
|
|
|
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}"); |
|
|
|
messageSource.addMessage("Size", Locale.ENGLISH, "Size of {0} is must be between {2} and {1}"); |
|
|
|
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value with {1}"); |
|
|
|
messageSource.addMessage("Same", Locale.ENGLISH, "{2} must be same value as {1}"); |
|
|
|
messageSource.addMessage("password", Locale.ENGLISH, "Password"); |
|
|
|
messageSource.addMessage("password", Locale.ENGLISH, "Password"); |
|
|
|
messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)"); |
|
|
|
messageSource.addMessage("confirmPassword", Locale.ENGLISH, "Password(Confirm)"); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -96,8 +98,11 @@ public class SpringValidatorAdapterTests { |
|
|
|
|
|
|
|
|
|
|
|
assertThat(errors.getFieldErrorCount("password"), is(1)); |
|
|
|
assertThat(errors.getFieldErrorCount("password"), is(1)); |
|
|
|
assertThat(errors.getFieldValue("password"), is("pass")); |
|
|
|
assertThat(errors.getFieldValue("password"), is("pass")); |
|
|
|
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), |
|
|
|
FieldError error = errors.getFieldError("password"); |
|
|
|
is("Size of Password is must be between 8 and 128")); |
|
|
|
assertNotNull(error); |
|
|
|
|
|
|
|
assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Size of Password is must be between 8 and 128")); |
|
|
|
|
|
|
|
assertTrue(error.contains(ConstraintViolation.class)); |
|
|
|
|
|
|
|
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // SPR-13406
|
|
|
|
@Test // SPR-13406
|
|
|
|
@ -111,8 +116,11 @@ public class SpringValidatorAdapterTests { |
|
|
|
|
|
|
|
|
|
|
|
assertThat(errors.getFieldErrorCount("password"), is(1)); |
|
|
|
assertThat(errors.getFieldErrorCount("password"), is(1)); |
|
|
|
assertThat(errors.getFieldValue("password"), is("password")); |
|
|
|
assertThat(errors.getFieldValue("password"), is("password")); |
|
|
|
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), |
|
|
|
FieldError error = errors.getFieldError("password"); |
|
|
|
is("Password must be same value with Password(Confirm)")); |
|
|
|
assertNotNull(error); |
|
|
|
|
|
|
|
assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Password must be same value as Password(Confirm)")); |
|
|
|
|
|
|
|
assertTrue(error.contains(ConstraintViolation.class)); |
|
|
|
|
|
|
|
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("password")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // SPR-13406
|
|
|
|
@Test // SPR-13406
|
|
|
|
@ -127,10 +135,16 @@ public class SpringValidatorAdapterTests { |
|
|
|
assertThat(errors.getFieldErrorCount("email"), is(1)); |
|
|
|
assertThat(errors.getFieldErrorCount("email"), is(1)); |
|
|
|
assertThat(errors.getFieldValue("email"), is("test@example.com")); |
|
|
|
assertThat(errors.getFieldValue("email"), is("test@example.com")); |
|
|
|
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); |
|
|
|
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); |
|
|
|
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), |
|
|
|
FieldError error1 = errors.getFieldError("email"); |
|
|
|
is("email must be same value with confirmEmail")); |
|
|
|
FieldError error2 = errors.getFieldError("confirmEmail"); |
|
|
|
assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), |
|
|
|
assertNotNull(error1); |
|
|
|
is("Email required")); |
|
|
|
assertNotNull(error2); |
|
|
|
|
|
|
|
assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail")); |
|
|
|
|
|
|
|
assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required")); |
|
|
|
|
|
|
|
assertTrue(error1.contains(ConstraintViolation.class)); |
|
|
|
|
|
|
|
assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email")); |
|
|
|
|
|
|
|
assertTrue(error2.contains(ConstraintViolation.class)); |
|
|
|
|
|
|
|
assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // SPR-15123
|
|
|
|
@Test // SPR-15123
|
|
|
|
@ -147,10 +161,16 @@ public class SpringValidatorAdapterTests { |
|
|
|
assertThat(errors.getFieldErrorCount("email"), is(1)); |
|
|
|
assertThat(errors.getFieldErrorCount("email"), is(1)); |
|
|
|
assertThat(errors.getFieldValue("email"), is("test@example.com")); |
|
|
|
assertThat(errors.getFieldValue("email"), is("test@example.com")); |
|
|
|
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); |
|
|
|
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); |
|
|
|
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), |
|
|
|
FieldError error1 = errors.getFieldError("email"); |
|
|
|
is("email must be same value with confirmEmail")); |
|
|
|
FieldError error2 = errors.getFieldError("confirmEmail"); |
|
|
|
assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), |
|
|
|
assertNotNull(error1); |
|
|
|
is("Email required")); |
|
|
|
assertNotNull(error2); |
|
|
|
|
|
|
|
assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value as confirmEmail")); |
|
|
|
|
|
|
|
assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required")); |
|
|
|
|
|
|
|
assertTrue(error1.contains(ConstraintViolation.class)); |
|
|
|
|
|
|
|
assertThat(error1.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("email")); |
|
|
|
|
|
|
|
assertTrue(error2.contains(ConstraintViolation.class)); |
|
|
|
|
|
|
|
assertThat(error2.unwrap(ConstraintViolation.class).getPropertyPath().toString(), is("confirmEmail")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test // SPR-16177
|
|
|
|
@Test // SPR-16177
|
|
|
|
@ -403,13 +423,13 @@ public class SpringValidatorAdapterTests { |
|
|
|
|
|
|
|
|
|
|
|
private Integer id; |
|
|
|
private Integer id; |
|
|
|
|
|
|
|
|
|
|
|
@javax.validation.constraints.NotNull |
|
|
|
@NotNull |
|
|
|
private String name; |
|
|
|
private String name; |
|
|
|
|
|
|
|
|
|
|
|
@javax.validation.constraints.NotNull |
|
|
|
@NotNull |
|
|
|
private Integer age; |
|
|
|
private Integer age; |
|
|
|
|
|
|
|
|
|
|
|
@javax.validation.constraints.NotNull |
|
|
|
@NotNull |
|
|
|
private Parent parent; |
|
|
|
private Parent parent; |
|
|
|
|
|
|
|
|
|
|
|
public Integer getId() { |
|
|
|
public Integer getId() { |
|
|
|
|