Java Code Examples for org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#afterPropertiesSet()

The following examples show how to use org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#afterPropertiesSet() . 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 spring-analysis-note with MIT License 6 votes vote down vote up
@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 2
Source File: ValidatorFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testListValidation() {
	LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
	validator.afterPropertiesSet();

	ListContainer listContainer = new ListContainer();
	listContainer.addString("A");
	listContainer.addString("X");

	BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer");
	errors.initConversion(new DefaultConversionService());
	validator.validate(listContainer, errors);

	FieldError fieldError = errors.getFieldError("list[1]");
	assertNotNull(fieldError);
	assertEquals("X", fieldError.getRejectedValue());
	assertEquals("X", errors.getFieldValue("list[1]"));
}
 
Example 3
Source File: ValidatorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: ValidatorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@InitBinder
public void initBinder(WebDataBinder binder) {
	binder.initBeanPropertyAccess();
	binder.setRequiredFields("sex");
	LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
	vf.afterPropertiesSet();
	binder.setValidator(vf);
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(false);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
 
Example 6
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void initBinder(WebDataBinder binder) {
	LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
	vf.afterPropertiesSet();
	binder.setValidator(vf);
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(false);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
 
Example 7
Source File: ValidatorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 8
Source File: BlueprintRequestTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    underTest = new BlueprintV4Request();
    localValidatorFactory = new LocalValidatorFactoryBean();
    localValidatorFactory.setProviderClass(HibernateValidator.class);
    localValidatorFactory.afterPropertiesSet();
}
 
Example 9
Source File: HttpEntityMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public WebDataBinder createBinder(NativeWebRequest webRequest, Object target, String objectName) {
	LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
	validator.afterPropertiesSet();
	WebDataBinder dataBinder = new WebDataBinder(target, objectName);
	dataBinder.setValidator(validator);
	return dataBinder;
}
 
Example 10
Source File: ValidatorTests.java    From spring-graalvm-native with Apache License 2.0 5 votes vote down vote up
private Validator createValidator() {
	LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
	localValidatorFactoryBean
			.setMessageInterpolator(new MessageInterpolatorFactory().getObject());
	localValidatorFactoryBean.afterPropertiesSet();
	return localValidatorFactoryBean;
}
 
Example 11
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
@InitBinder({"myCommand", "date"})
private void initBinder(WebDataBinder binder, String date, @RequestParam("date") String[] date2) {
	LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
	vf.afterPropertiesSet();
	binder.setValidator(vf);
	assertEquals("2007-10-02", date);
	assertEquals(1, date2.length);
	assertEquals("2007-10-02", date2[0]);
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(false);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
 
Example 12
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@InitBinder({"myCommand", "date"})
public void initBinder(WebDataBinder binder, String date, @RequestParam("date") String[] date2) {
	LocalValidatorFactoryBean vf = new LocalValidatorFactoryBean();
	vf.afterPropertiesSet();
	binder.setValidator(vf);
	assertEquals("2007-10-02", date);
	assertEquals(1, date2.length);
	assertEquals("2007-10-02", date2[0]);
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(false);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
 
Example 13
Source File: ConfigurationServiceTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
LocalValidatorFactoryBean getValidator() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.refresh();
	LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
	validator.setApplicationContext(context);
	validator.setMessageInterpolator(new MessageInterpolatorFactory().getObject());
	validator.afterPropertiesSet();
	return validator;
}
 
Example 14
Source File: ValidatorTests.java    From DevOps-for-Web-Development with MIT License 4 votes vote down vote up
private Validator createValidator() {
    LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
    localValidatorFactoryBean.afterPropertiesSet();
    return localValidatorFactoryBean;
}
 
Example 15
Source File: CustomerTest.java    From bootiful-testing-online-training with Apache License 2.0 4 votes vote down vote up
@Before
public void before() throws Throwable {
	LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
	localValidatorFactoryBean.afterPropertiesSet();
	this.validator = localValidatorFactoryBean.getValidator();
}
 
Example 16
Source File: ExportParametersTest.java    From data-prep with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor that initialize the validator factory.
 */
public ExportParametersTest() {
    validator = new LocalValidatorFactoryBean();
    validator.setProviderClass(HibernateValidator.class);
    validator.afterPropertiesSet();
}
 
Example 17
Source File: PreviewAddInputTest.java    From data-prep with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor that initialize the validator factory.
 */
public PreviewAddInputTest() {
    validator = new LocalValidatorFactoryBean();
    validator.setProviderClass(HibernateValidator.class);
    validator.afterPropertiesSet();
}
 
Example 18
Source File: ValidatorTests.java    From DevOps-for-Web-Development with MIT License 4 votes vote down vote up
private Validator createValidator() {
    LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
    localValidatorFactoryBean.afterPropertiesSet();
    return localValidatorFactoryBean;
}
 
Example 19
Source File: TransactionCommandValidationTest.java    From kid-bank with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
  localValidatorFactory = new LocalValidatorFactoryBean();
  localValidatorFactory.setProviderClass(HibernateValidator.class);
  localValidatorFactory.afterPropertiesSet();
}
 
Example 20
Source File: AggregationParametersTest.java    From data-prep with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor that initialize the validator factory.
 */
public AggregationParametersTest() {
    validator = new LocalValidatorFactoryBean();
    validator.setProviderClass(HibernateValidator.class);
    validator.afterPropertiesSet();
}