Search in sources :

Example 46 with Validator

use of javax.validation.Validator 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 47 with Validator

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

the class ConstraintValidationTestCase method testObjectGraphValidation.

@Test
public void testObjectGraphValidation() throws NamingException {
    Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
    // create first passenger
    UserBean user1 = new UserBean("MADHUMITA", "SADHUKHAN");
    user1.setEmail("madhu@gmail.com");
    user1.setAddress("REDHAT Brno");
    // create second passenger
    UserBean user2 = new UserBean("Mickey", "Mouse");
    user2.setAddress("");
    List<UserBean> passengers = new ArrayList<UserBean>();
    passengers.add(user1);
    passengers.add(user2);
    // Create a Car
    Car car = new Car("CET5678", passengers);
    car.setModel("SKODA Octavia");
    final Set<ConstraintViolation<Car>> errorresult = validator.validate(car);
    Iterator<ConstraintViolation<Car>> it1 = errorresult.iterator();
    String message = "";
    while (it1.hasNext()) {
        ConstraintViolation<Car> cts = it1.next();
        String mess = cts.getMessage();
        if (mess.contains("Please get a valid address")) {
            message = mess;
        }
    }
    assertEquals(2, errorresult.size());
    assertTrue(message.contains("Please get a valid address"));
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) Validator(javax.validation.Validator) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 48 with Validator

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

the class ExpressionLanguageTestCase method testValidationUsingExpressionLanguage.

@Test
public void testValidationUsingExpressionLanguage() {
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<TestBean>> violations = validator.validate(new TestBean());
    assertEquals(1, violations.size());
    assertEquals("'Bob' is too short, it should at least be 5 characters long.", violations.iterator().next().getMessage());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 49 with Validator

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

the class LazyValidatorFactoryTestCase method testHibernateValidatorIsUsedAsProviderByDefault.

@Test
public void testHibernateValidatorIsUsedAsProviderByDefault() {
    HibernateValidatorFactory hibernateValidatorFactory = validatorFactory.unwrap(HibernateValidatorFactory.class);
    assertNotNull("LazyValidatorFactory should delegate to the HV factory by default", hibernateValidatorFactory);
    Validator validator = validatorFactory.getValidator();
    assertNotNull("LazyValidatorFactory should provide a validator", validator);
}
Also used : HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 50 with Validator

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

the class LazyValidatorFactoryTestCase method testSpecificProviderCanBeConfiguredInValidationXml.

@Test
@WithContextClassLoader(TestClassLoader.class)
public void testSpecificProviderCanBeConfiguredInValidationXml() {
    Validator validator = validatorFactory.getValidator();
    assertNotNull("LazyValidatorFactory should provide a validator", validator);
    assertTrue("Validator should be of type created by XML-configured provider", validator instanceof MyValidatorImpl);
}
Also used : MyValidatorImpl(org.jboss.as.ee.beanvalidation.testprovider.MyValidatorImpl) Validator(javax.validation.Validator) Test(org.junit.Test) WithContextClassLoader(org.jboss.as.ee.beanvalidation.testutil.WithContextClassLoader)

Aggregations

Validator (javax.validation.Validator)54 Test (org.junit.Test)30 ConstraintViolation (javax.validation.ConstraintViolation)26 ValidatorFactory (javax.validation.ValidatorFactory)17 InitialContext (javax.naming.InitialContext)8 ArrayList (java.util.ArrayList)5 NamingException (javax.naming.NamingException)5 ConstraintViolationException (javax.validation.ConstraintViolationException)5 HibernateValidator (org.hibernate.validator.HibernateValidator)5 File (java.io.File)4 Date (java.util.Date)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 BeanManager (javax.enterprise.inject.spi.BeanManager)3 Context (javax.naming.Context)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)3