Search in sources :

Example 76 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class SpringValidatorAdapterTests method testMapEntryConstraint.

// SPR-15839
@Test
public void testMapEntryConstraint() {
    Map<String, String> property = new HashMap<>();
    property.put(null, null);
    BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint();
    bean.setProperty(property);
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
    validatorAdapter.validate(bean, errors);
    assertThat(errors.hasFieldErrors("property[]")).isTrue();
    assertThat(errors.getFieldValue("property[]")).isNull();
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 77 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class SpringValidatorAdapterTests method testWithSet.

// SPR-16177
@Test
public void testWithSet() {
    Parent parent = new Parent();
    parent.setName("Parent with set");
    parent.getChildSet().addAll(createChildren(parent));
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent");
    validatorAdapter.validate(parent, errors);
    assertThat(errors.getErrorCount() > 0).isTrue();
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Test(org.junit.jupiter.api.Test)

Example 78 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testSpringValidationWithClassLevel.

@Test
public void testSpringValidationWithClassLevel() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    person.setName("Juergen");
    person.getAddress().setStreet("Juergen's Street");
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertThat(result.getErrorCount()).isEqualTo(1);
    ObjectError globalError = result.getGlobalError();
    List<String> errorCodes = Arrays.asList(globalError.getCodes());
    assertThat(errorCodes.size()).isEqualTo(2);
    assertThat(errorCodes.contains("NameAddressValid.person")).isTrue();
    assertThat(errorCodes.contains("NameAddressValid")).isTrue();
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ObjectError(org.springframework.validation.ObjectError) Test(org.junit.jupiter.api.Test)

Example 79 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testValidationWithOptionalField.

@Test
public void testValidationWithOptionalField() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    MainBeanWithOptional mainBean = new MainBeanWithOptional();
    Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean");
    validator.validate(mainBean, errors);
    Object rejected = errors.getFieldValue("inner.value");
    assertThat(rejected).isNull();
}
Also used : Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Test(org.junit.jupiter.api.Test)

Example 80 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testSpringValidation.

@Test
public void testSpringValidation() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertThat(result.getErrorCount()).isEqualTo(2);
    FieldError fieldError = result.getFieldError("name");
    assertThat(fieldError.getField()).isEqualTo("name");
    List<String> errorCodes = Arrays.asList(fieldError.getCodes());
    assertThat(errorCodes.size()).isEqualTo(4);
    assertThat(errorCodes.contains("NotNull.person.name")).isTrue();
    assertThat(errorCodes.contains("NotNull.name")).isTrue();
    assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue();
    assertThat(errorCodes.contains("NotNull")).isTrue();
    fieldError = result.getFieldError("address.street");
    assertThat(fieldError.getField()).isEqualTo("address.street");
    errorCodes = Arrays.asList(fieldError.getCodes());
    assertThat(errorCodes.size()).isEqualTo(5);
    assertThat(errorCodes.contains("NotNull.person.address.street")).isTrue();
    assertThat(errorCodes.contains("NotNull.address.street")).isTrue();
    assertThat(errorCodes.contains("NotNull.street")).isTrue();
    assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue();
    assertThat(errorCodes.contains("NotNull")).isTrue();
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)153 Test (org.junit.jupiter.api.Test)71 RestServerError (org.entando.entando.aps.system.exception.RestServerError)28 Errors (org.springframework.validation.Errors)24 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)23 BindingResult (org.springframework.validation.BindingResult)23 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)21 StringReader (java.io.StringReader)20 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 List (java.util.List)17 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 ResourceNotFoundException (org.entando.entando.aps.system.exception.ResourceNotFoundException)14 ArrayList (java.util.ArrayList)13 ValidationConflictException (org.entando.entando.web.common.exceptions.ValidationConflictException)13 FieldError (org.springframework.validation.FieldError)12 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 Test (org.junit.Test)9