Search in sources :

Example 16 with Validator

use of org.springframework.validation.Validator in project spring-boot by spring-projects.

the class WebFluxAnnotationAutoConfigurationTests method validationJsr303ValidatorExposedAsSpringValidator.

@Test
public void validationJsr303ValidatorExposedAsSpringValidator() {
    load(Jsr303Validator.class);
    assertThat(this.context.getBeansOfType(ValidatorFactory.class)).isEmpty();
    assertThat(this.context.getBeansOfType(javax.validation.Validator.class)).hasSize(1);
    assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
    Validator validator = this.context.getBean(Validator.class);
    assertThat(validator).isInstanceOf(SpringValidator.class);
    SpringValidatorAdapter target = ((SpringValidator) validator).getTarget();
    assertThat(new DirectFieldAccessor(target).getPropertyValue("targetValidator")).isSameAs(this.context.getBean(javax.validation.Validator.class));
}
Also used : SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Example 17 with Validator

use of org.springframework.validation.Validator in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method validationJsr303ValidatorExposedAsSpringValidator.

@Test
public void validationJsr303ValidatorExposedAsSpringValidator() {
    load(Jsr303Validator.class);
    assertThat(this.context.getBeansOfType(ValidatorFactory.class)).isEmpty();
    assertThat(this.context.getBeansOfType(javax.validation.Validator.class)).hasSize(1);
    assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
    Validator validator = this.context.getBean(Validator.class);
    assertThat(validator).isInstanceOf(SpringValidator.class);
    SpringValidatorAdapter target = ((SpringValidator) validator).getTarget();
    assertThat(new DirectFieldAccessor(target).getPropertyValue("targetValidator")).isSameAs(this.context.getBean(javax.validation.Validator.class));
}
Also used : SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Example 18 with Validator

use of org.springframework.validation.Validator in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method validationCustomConfigurerTakesPrecedence.

@Test
public void validationCustomConfigurerTakesPrecedence() {
    load(MvcValidator.class);
    assertThat(this.context.getBeansOfType(ValidatorFactory.class)).isEmpty();
    assertThat(this.context.getBeansOfType(javax.validation.Validator.class)).isEmpty();
    assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
    Validator validator = this.context.getBean(Validator.class);
    assertThat(validator).isSameAs(this.context.getBean(MvcValidator.class).validator);
}
Also used : SpringValidator(org.springframework.boot.autoconfigure.validation.SpringValidator) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Example 19 with Validator

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

the class DefaultMessageHandlerMethodFactoryTests method customValidation.

@Test
public void customValidation() throws Exception {
    DefaultMessageHandlerMethodFactory instance = createInstance();
    instance.setValidator(new Validator() {

        @Override
        public boolean supports(Class<?> clazz) {
            return String.class.isAssignableFrom(clazz);
        }

        @Override
        public void validate(Object target, Errors errors) {
            String value = (String) target;
            if ("failure".equals(value)) {
                errors.reject("not a valid value");
            }
        }
    });
    instance.afterPropertiesSet();
    InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "payloadValidation", String.class);
    thrown.expect(MethodArgumentNotValidException.class);
    invocableHandlerMethod.invoke(MessageBuilder.withPayload("failure").build());
}
Also used : Errors(org.springframework.validation.Errors) InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Aggregations

Validator (org.springframework.validation.Validator)19 Test (org.junit.Test)15 SpringValidator (org.springframework.boot.autoconfigure.validation.SpringValidator)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Bean (org.springframework.context.annotation.Bean)3 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)3 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ConversionService (org.springframework.core.convert.ConversionService)2 SpringValidatorAdapter (org.springframework.validation.beanvalidation.SpringValidatorAdapter)2 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)2 WebBindingInitializer (org.springframework.web.bind.support.WebBindingInitializer)2 BindingContext (org.springframework.web.reactive.BindingContext)2 FAIL_ON_UNKNOWN_PROPERTIES (com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)1 DEFAULT_VIEW_INCLUSION (com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 List (java.util.List)1 Locale (java.util.Locale)1