Java Code Examples for org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#validate()
The following examples show how to use
org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#validate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test 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"); validator.validate(person, result); assertEquals(3, result.getErrorCount()); FieldError fieldError = result.getFieldError("name"); assertEquals("name", fieldError.getField()); fieldError = result.getFieldError("address.street"); assertEquals("address.street", fieldError.getField()); fieldError = result.getFieldError("addressSet[].street"); assertEquals("addressSet[].street", fieldError.getField()); }
Example 2
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test 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()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } validator.destroy(); }
Example 3
Source File: ValidatorFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testSpringValidationWithAutowiredValidator() { 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(); }
Example 4
Source File: ValidatorFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@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); 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")); }
Example 5
Source File: ValidatorFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testSpringValidationWithErrorInSetElement() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); person.getAddressSet().add(new ValidAddress()); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); assertEquals(3, result.getErrorCount()); FieldError fieldError = result.getFieldError("name"); assertEquals("name", fieldError.getField()); fieldError = result.getFieldError("address.street"); assertEquals("address.street", fieldError.getField()); fieldError = result.getFieldError("addressSet[].street"); assertEquals("addressSet[].street", fieldError.getField()); }
Example 6
Source File: ValidatorFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testSimpleValidationWithCustomProvider() { 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()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } Validator nativeValidator = validator.unwrap(Validator.class); assertTrue(nativeValidator.getClass().getName().startsWith("org.hibernate")); assertTrue(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory); assertTrue(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory); validator.destroy(); }
Example 7
Source File: ValidatorFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testSimpleValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); Set<ConstraintViolation<ValidPerson>> result = validator.validate(person); assertEquals(2, result.size()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } Validator nativeValidator = validator.unwrap(Validator.class); assertTrue(nativeValidator.getClass().getName().startsWith("org.hibernate")); assertTrue(validator.unwrap(ValidatorFactory.class) instanceof HibernateValidatorFactory); assertTrue(validator.unwrap(HibernateValidatorFactory.class) instanceof HibernateValidatorFactory); validator.destroy(); }
Example 8
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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"); validator.validate(person, result); assertEquals(3, result.getErrorCount()); FieldError fieldError = result.getFieldError("name"); assertEquals("name", fieldError.getField()); fieldError = result.getFieldError("address.street"); assertEquals("address.street", fieldError.getField()); fieldError = result.getFieldError("addressList[0].street"); assertEquals("addressList[0].street", fieldError.getField()); }
Example 9
Source File: ValidatorFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testSpringValidationWithErrorInSetElement() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); person.getAddressSet().add(new ValidAddress()); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); assertEquals(3, result.getErrorCount()); FieldError fieldError = result.getFieldError("name"); assertEquals("name", fieldError.getField()); fieldError = result.getFieldError("address.street"); assertEquals("address.street", fieldError.getField()); fieldError = result.getFieldError("addressSet[].street"); assertEquals("addressSet[].street", fieldError.getField()); }
Example 10
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testSpringValidationWithClassLevel() throws Exception { 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); 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")); }
Example 11
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testSimpleValidationWithCustomProvider() throws Exception { 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()); for (ConstraintViolation<ValidPerson> cv : result) { String path = cv.getPropertyPath().toString(); if ("name".equals(path) || "address.street".equals(path)) { assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull); } else { fail("Invalid constraint violation with path '" + path + "'"); } } validator.destroy(); }
Example 12
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testSpringValidationFieldType() throws Exception { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); person.setName("Phil"); person.getAddress().setStreet("Phil's Street"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person"); validator.validate(person, errors); assertEquals(1, errors.getErrorCount()); assertThat("Field/Value type mismatch", errors.getFieldError("address").getRejectedValue(), instanceOf(ValidAddress.class)); }
Example 13
Source File: ValidatorFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@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"); assertNull(rejected); }
Example 14
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testValidationWithOptionalField() throws Exception { 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"); assertNull(rejected); }
Example 15
Source File: ValidatorFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testInnerBeanValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); MainBean mainBean = new MainBean(); Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); assertNull(rejected); }
Example 16
Source File: ValidatorFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testSpringValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person"); validator.validate(person, result); assertEquals(2, result.getErrorCount()); FieldError fieldError = result.getFieldError("name"); assertEquals("name", fieldError.getField()); List<String> errorCodes = Arrays.asList(fieldError.getCodes()); assertEquals(4, errorCodes.size()); assertTrue(errorCodes.contains("NotNull.person.name")); assertTrue(errorCodes.contains("NotNull.name")); assertTrue(errorCodes.contains("NotNull.java.lang.String")); assertTrue(errorCodes.contains("NotNull")); fieldError = result.getFieldError("address.street"); assertEquals("address.street", fieldError.getField()); errorCodes = Arrays.asList(fieldError.getCodes()); assertEquals(5, errorCodes.size()); assertTrue(errorCodes.contains("NotNull.person.address.street")); assertTrue(errorCodes.contains("NotNull.address.street")); assertTrue(errorCodes.contains("NotNull.street")); assertTrue(errorCodes.contains("NotNull.java.lang.String")); assertTrue(errorCodes.contains("NotNull")); }
Example 17
Source File: ValidatorFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testSpringValidationFieldType() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); person.setName("Phil"); person.getAddress().setStreet("Phil's Street"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person"); validator.validate(person, errors); assertEquals(1, errors.getErrorCount()); assertThat("Field/Value type mismatch", errors.getFieldError("address").getRejectedValue(), instanceOf(ValidAddress.class)); }
Example 18
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test 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); assertEquals(2, result.getErrorCount()); FieldError fieldError = result.getFieldError("name"); assertEquals("name", fieldError.getField()); List<String> errorCodes = Arrays.asList(fieldError.getCodes()); assertEquals(4, errorCodes.size()); assertTrue(errorCodes.contains("NotNull.person.name")); assertTrue(errorCodes.contains("NotNull.name")); assertTrue(errorCodes.contains("NotNull.java.lang.String")); assertTrue(errorCodes.contains("NotNull")); fieldError = result.getFieldError("address.street"); assertEquals("address.street", fieldError.getField()); errorCodes = Arrays.asList(fieldError.getCodes()); assertEquals(5, errorCodes.size()); assertTrue(errorCodes.contains("NotNull.person.address.street")); assertTrue(errorCodes.contains("NotNull.address.street")); assertTrue(errorCodes.contains("NotNull.street")); assertTrue(errorCodes.contains("NotNull.java.lang.String")); assertTrue(errorCodes.contains("NotNull")); }
Example 19
Source File: ValidatorFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testSimpleValidationWithClassLevel() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); ValidPerson person = new ValidPerson(); person.setName("Juergen"); person.getAddress().setStreet("Juergen's Street"); Set<ConstraintViolation<ValidPerson>> result = validator.validate(person); assertEquals(1, result.size()); Iterator<ConstraintViolation<ValidPerson>> iterator = result.iterator(); ConstraintViolation<?> cv = iterator.next(); assertEquals("", cv.getPropertyPath().toString()); assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NameAddressValid); }
Example 20
Source File: ValidatorFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testInnerBeanValidation() throws Exception { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); MainBean mainBean = new MainBean(); Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); assertNull(rejected); }