Search in sources :

Example 11 with Validator

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

the class MessageInterpolationValidationTestCase method testCustomMessageInterpolation.

@Test
public void testCustomMessageInterpolation() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    final MessageInterpolator messageInterpolator = new CustomMessageInterpolator();
    configuration.messageInterpolator(messageInterpolator);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    // create employee
    Employee emp = new Employee();
    emp.setEmail("MADHUMITA");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals(CustomMessageInterpolator.MESSAGE, constraintViolations.iterator().next().getMessage());
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) MessageInterpolator(javax.validation.MessageInterpolator) Test(org.junit.Test)

Example 12 with Validator

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

the class ScriptAssertTestCase method testScriptAssert.

@Test
public void testScriptAssert() throws NamingException, SQLException {
    Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
    final Set<ConstraintViolation<ScriptAssertBean>> result = validator.validate(new ScriptAssertBean());
    Assert.assertEquals(1, result.size());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 13 with Validator

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

the class ConstraintValidationTestCase method testConstraintValidation.

@Test
public void testConstraintValidation() throws NamingException {
    Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
    UserBean user1 = new UserBean("MADHUMITA", "");
    user1.setEmail("madhumita_gmail");
    user1.setAddress("");
    final Set<ConstraintViolation<UserBean>> result = validator.validate(user1);
    Iterator<ConstraintViolation<UserBean>> it = result.iterator();
    String message = "";
    while (it.hasNext()) {
        ConstraintViolation<UserBean> cts = it.next();
        String mess = cts.getMessage();
        if (mess.contains("Please get a valid address")) {
            message = mess;
        }
    }
    assertEquals(3, result.size());
    assertTrue(message.contains("Please get a valid address"));
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 14 with Validator

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

the class AppComposerAsJUnitRuleTest method lookupValidator.

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

Example 15 with Validator

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

the class BuiltInEnvironmentEntries method add.

private void add(final JndiConsumer jndi, final DeploymentModule module, final DeploymentModule app, final boolean defaults) {
    // Standard names
    add(jndi.getEnvEntryMap(), new EnvEntry().name("java:module/ModuleName").value(module.getModuleId()).type(String.class));
    add(jndi.getEnvEntryMap(), new EnvEntry().name("java:app/AppName").value(app.getModuleId()).type(String.class));
    // Standard References to built-in objects
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/BeanManager").type(BeanManager.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/Validator").type(Validator.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/ValidatorFactory").type(ValidatorFactory.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/TransactionManager").type(TransactionManager.class));
    add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/TransactionSynchronizationRegistry").type(TransactionSynchronizationRegistry.class));
    if (defaults) {
        add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/DefaultManagedExecutorService").type(ManagedExecutorService.class));
        add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/DefaultManagedScheduledExecutorService").type(ManagedScheduledExecutorService.class));
        add(jndi.getResourceEnvRefMap(), new ResourceEnvRef().name("java:comp/DefaultManagedThreadFactory").type(ManagedThreadFactory.class));
        try {
            final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            contextClassLoader.loadClass("org.apache.activemq.ActiveMQSslConnectionFactory");
            final ResourceEnvRef ref = new ResourceEnvRef().name("java:comp/DefaultJMSConnectionFactory").type(contextClassLoader.loadClass("javax.jms.ConnectionFactory"));
            add(jndi.getResourceEnvRefMap(), ref);
        } catch (final ClassNotFoundException | NoClassDefFoundError notThere) {
        // no-op
        }
    }
    // OpenEJB specific feature
    add(jndi.getEnvEntryMap(), new EnvEntry().name("java:comp/ComponentName").value(jndi.getJndiConsumerName()).type(String.class));
}
Also used : ValidatorFactory(javax.validation.ValidatorFactory) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory) ManagedExecutorService(javax.enterprise.concurrent.ManagedExecutorService) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) BeanManager(javax.enterprise.inject.spi.BeanManager) ResourceEnvRef(org.apache.openejb.jee.ResourceEnvRef) Validator(javax.validation.Validator) ManagedScheduledExecutorService(javax.enterprise.concurrent.ManagedScheduledExecutorService) EnvEntry(org.apache.openejb.jee.EnvEntry)

Aggregations

Validator (javax.validation.Validator)42 Test (org.junit.Test)25 ConstraintViolation (javax.validation.ConstraintViolation)19 ValidatorFactory (javax.validation.ValidatorFactory)13 InitialContext (javax.naming.InitialContext)8 ArrayList (java.util.ArrayList)5 HibernateValidator (org.hibernate.validator.HibernateValidator)5 File (java.io.File)4 NamingException (javax.naming.NamingException)4 ConstraintViolationException (javax.validation.ConstraintViolationException)4 Title (net.thucydides.core.annotations.Title)4 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 PodamFactory (uk.co.jemos.podam.api.PodamFactory)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashSet (java.util.HashSet)3 Context (javax.naming.Context)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2