Search in sources :

Example 66 with ValidatorFactory

use of javax.validation.ValidatorFactory in project hibernate-orm by hibernate.

the class BeanValidationProvidedFactoryTest method configure.

@Override
protected void configure(Configuration cfg) {
    super.configure(cfg);
    final MessageInterpolator messageInterpolator = new MessageInterpolator() {

        public String interpolate(String s, Context context) {
            return "Oops";
        }

        public String interpolate(String s, Context context, Locale locale) {
            return interpolate(s, context);
        }
    };
    final javax.validation.Configuration<?> configuration = Validation.byDefaultProvider().configure();
    configuration.messageInterpolator(messageInterpolator);
    ValidatorFactory vf = configuration.buildValidatorFactory();
    cfg.getProperties().put("javax.persistence.validation.factory", vf);
    cfg.setProperty("javax.persistence.validation.mode", "AUTO");
}
Also used : Locale(java.util.Locale) ValidatorFactory(javax.validation.ValidatorFactory) MessageInterpolator(javax.validation.MessageInterpolator)

Example 67 with ValidatorFactory

use of javax.validation.ValidatorFactory in project hibernate-orm by hibernate.

the class BeanValidationEventListener method initialize.

public void initialize(Map settings, ClassLoaderService classLoaderService) {
    if (!initialized) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        init(factory, settings, classLoaderService);
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory)

Example 68 with ValidatorFactory

use of javax.validation.ValidatorFactory in project wildfly by wildfly.

the class BootStrapValidationTestCase method testCustomConstraintValidatorFactory.

@Test
public void testCustomConstraintValidatorFactory() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Employee emp = new Employee();
    // create employee
    emp.setEmpId("M1234");
    emp.setFirstName("MADHUMITA");
    emp.setLastName("SADHUKHAN");
    emp.setEmail("madhu@redhat.com");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals("Created by default factory", constraintViolations.iterator().next().getMessage());
    // get a new factory using a custom configuration
    configuration.constraintValidatorFactory(new CustomConstraintValidatorFactory(configuration.getDefaultConstraintValidatorFactory()));
    factory = configuration.buildValidatorFactory();
    validator = factory.getValidator();
    constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals("Created by custom factory", constraintViolations.iterator().next().getMessage());
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) ConstraintValidator(javax.validation.ConstraintValidator) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Example 69 with ValidatorFactory

use of javax.validation.ValidatorFactory in project wildfly by wildfly.

the class BootStrapValidationTestCase method testSafeHTML.

/**
     * Ensure that including a cross-site script attack triggers the @SafeHtml validation rule
     */
@Test
public void testSafeHTML() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Employee emp = new Employee();
    // create employee
    emp.setFirstName("Joe");
    emp.setLastName("Cocker");
    emp.setEmail("none@jboss.org");
    emp.setWebsite("<script> Cross-site scripting http://en.wikipedia.org/wiki/Joe_Cocker <script/>.");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) ConstraintValidator(javax.validation.ConstraintValidator) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Example 70 with ValidatorFactory

use of javax.validation.ValidatorFactory in project wildfly by wildfly.

the class JSFBeanValidationFactoryProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    // Get the CDI-enabled ValidatorFactory and add it to the servlet context
    ValidatorFactory validatorFactory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
    if (validatorFactory != null) {
        deploymentUnit.addToAttachmentList(ServletContextAttribute.ATTACHMENT_KEY, new ServletContextAttribute(VALIDATOR_FACTORY_KEY, validatorFactory));
    }
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) ServletContextAttribute(org.jboss.as.web.common.ServletContextAttribute)

Aggregations

ValidatorFactory (javax.validation.ValidatorFactory)106 Validator (javax.validation.Validator)31 Before (org.junit.Before)28 ConstraintViolation (javax.validation.ConstraintViolation)25 Test (org.junit.Test)17 BeforeClass (org.junit.BeforeClass)13 ConstraintViolationException (javax.validation.ConstraintViolationException)7 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)6 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)6 PlatformResourceBundleLocator (org.hibernate.validator.resourceloading.PlatformResourceBundleLocator)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 BeanManager (javax.enterprise.inject.spi.BeanManager)5 InitialContext (javax.naming.InitialContext)5 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)5 HashSet (java.util.HashSet)4 NamingException (javax.naming.NamingException)4 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)4 ValidatorContext (javax.validation.ValidatorContext)4 HibernateValidator (org.hibernate.validator.HibernateValidator)4