本文整理匯總了Java中org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.validate方法的典型用法代碼示例。如果您正苦於以下問題:Java LocalValidatorFactoryBean.validate方法的具體用法?Java LocalValidatorFactoryBean.validate怎麽用?Java LocalValidatorFactoryBean.validate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
的用法示例。
在下文中一共展示了LocalValidatorFactoryBean.validate方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testSimpleValidation
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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();
}
示例2: testSimpleValidationWithCustomProvider
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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();
}
示例3: testSpringValidationWithClassLevel
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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"));
}
示例4: testSpringValidationWithAutowiredValidator
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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();
}
示例5: testSpringValidationWithErrorInListElement
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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());
}
示例6: testSpringValidationWithErrorInSetElement
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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());
}
示例7: testSimpleValidationWithClassLevel
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@Test
public void testSimpleValidationWithClassLevel() throws Exception {
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);
}
示例8: testSpringValidationFieldType
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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));
}
示例9: testSpringValidation
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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"));
}
示例10: testInnerBeanValidation
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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);
}
示例11: testValidationWithOptionalField
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; //導入方法依賴的package包/類
@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);
}