Search in sources :

Example 41 with ValidatorFactory

use of javax.validation.ValidatorFactory in project tomee by apache.

the class BeanValidationTest method lookupValidatorFactory.

@Test
public void lookupValidatorFactory() throws Exception {
    final ValidatorFactory validatorFactory = (ValidatorFactory) new InitialContext().lookup("java:comp/ValidatorFactory");
    assertNotNull(validatorFactory);
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 42 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 43 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 44 with ValidatorFactory

use of javax.validation.ValidatorFactory in project keywhiz by square.

the class ValidX500NameValidatorTest method setUp.

@BeforeClass
public static void setUp() {
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    validator = factory.getValidator();
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) BeforeClass(org.junit.BeforeClass)

Example 45 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)

Aggregations

ValidatorFactory (javax.validation.ValidatorFactory)55 Validator (javax.validation.Validator)16 Test (org.junit.Test)12 ConstraintViolation (javax.validation.ConstraintViolation)10 Before (org.junit.Before)8 HashMap (java.util.HashMap)5 BeanManager (javax.enterprise.inject.spi.BeanManager)5 InitialContext (javax.naming.InitialContext)5 NamingException (javax.naming.NamingException)5 ArrayList (java.util.ArrayList)4 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)4 ValidationException (javax.validation.ValidationException)4 ValidatorContext (javax.validation.ValidatorContext)4 HibernateValidator (org.hibernate.validator.HibernateValidator)4 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 TransactionManager (javax.transaction.TransactionManager)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)3 ProxyBeanManager (org.jboss.as.jpa.beanmanager.ProxyBeanManager)3 PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)3