Search in sources :

Example 31 with Validator

use of javax.validation.Validator in project javaee7-firstcup by ecabrerar.

the class BooksCollectionTest method shouldReturnAValidationError.

@Test
public void shouldReturnAValidationError() {
    Book book = new Book("Effective Java", "2234555568", "");
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Book>> violations = validator.validate(book);
    Assert.assertEquals(2, violations.size());
    System.out.println(violations.stream().map(error -> error.getMessage()).collect(Collectors.joining(", ")));
}
Also used : Book(org.ecabrerar.examples.javax.rs.validation.domain.Book) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 32 with Validator

use of javax.validation.Validator in project opennms by OpenNMS.

the class EventValidationTest method testBadState.

@Test
public void testBadState() throws Exception {
    final Validator validator = factory.getValidator();
    final Event event = new Event();
    event.setSource("tests");
    event.setTime(new Date());
    final Operaction action = new Operaction();
    action.setState("monkey");
    event.addOperaction(action);
    final Set<ConstraintViolation<Event>> errors = validator.validate(event);
    LOG.debug("errors: {}", errors);
    assertEquals(2, errors.size());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Date(java.util.Date) Test(org.junit.Test)

Example 33 with Validator

use of javax.validation.Validator in project opennms by OpenNMS.

the class EventValidationTest method testBadDbid.

@Test
public void testBadDbid() throws Exception {
    final Validator validator = factory.getValidator();
    final Event event = new Event();
    event.setSource("tests");
    event.setTime(new Date());
    event.setDbid(-1);
    final Set<ConstraintViolation<Event>> errors = validator.validate(event);
    assertEquals(1, errors.size());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Date(java.util.Date) Test(org.junit.Test)

Example 34 with Validator

use of javax.validation.Validator in project opennms by OpenNMS.

the class EventValidationTest method testEmptySource.

@Test
public void testEmptySource() throws Exception {
    final Validator validator = factory.getValidator();
    final Event event = new Event();
    event.setTime(new Date());
    final Set<ConstraintViolation<Event>> errors = validator.validate(event);
    assertNull(event.getSource());
    assertEquals(1, errors.size());
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Date(java.util.Date) Test(org.junit.Test)

Example 35 with Validator

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

the class LazyValidatorTest method serialize.

@Test
public void serialize() {
    final Serializable obj = Serializable.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ValidatorFactory.class }, new LazyValidator(Validation.buildDefaultValidatorFactory())));
    final LazyValidator deserialized = LazyValidator.class.cast(Proxy.getInvocationHandler(SerializationUtils.deserialize(SerializationUtils.serialize(obj))));
    final Validator validator = deserialized.getValidator();
    assertNotNull(validator);
}
Also used : Serializable(java.io.Serializable) ValidatorFactory(javax.validation.ValidatorFactory) Validator(javax.validation.Validator) Test(org.junit.Test)

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