Java 类io.dropwizard.validation.ConstraintViolations 实例源码

项目:dropwizard-xml    文件:JacksonXMLMessageBodyProviderTest.java   
@Test
public void throwsAnInvalidEntityExceptionForPartialValidatedRequestEntities() throws Exception {
    final Validated valid = Mockito.mock(Validated.class);
    Mockito.doReturn(Validated.class).when(valid).annotationType();
    Mockito.when(valid.value()).thenReturn(new Class[] {Partial1.class, Partial2.class});

    final ByteArrayInputStream entity = new ByteArrayInputStream("<Example xmlns=\"\"><id>1</id></Example>".getBytes());

    try {
        final Class<?> klass = PartialExample.class;
        provider.readFrom((Class<Object>) klass,
                PartialExample.class,
                new Annotation[] {valid},
                MediaType.APPLICATION_XML_TYPE,
                new MultivaluedHashMap<>(),
                entity);
        failBecauseExceptionWasNotThrown(ConstraintViolationException.class);
    } catch (ConstraintViolationException e) {
        assertThat(ConstraintViolations.formatUntyped(e.getConstraintViolations()))
                .containsOnly("text may not be null");
    }
}
项目:dropwizard-xml    文件:JacksonXMLMessageBodyProviderTest.java   
@Test
public void throwsAnInvalidEntityExceptionForInvalidRequestEntities() throws Exception {
    final Annotation valid = Mockito.mock(Annotation.class);
    Mockito.doReturn(Valid.class).when(valid).annotationType();

    final ByteArrayInputStream entity = new ByteArrayInputStream("<Example xmlns=\"\"><id>-1</id></Example>".getBytes());

    try {
        final Class<?> klass = Example.class;
        provider.readFrom((Class<Object>) klass,
                Example.class,
                new Annotation[] {valid},
                MediaType.APPLICATION_XML_TYPE,
                new MultivaluedHashMap<>(),
                entity);
        failBecauseExceptionWasNotThrown(ConstraintViolationException.class);
    } catch (ConstraintViolationException e) {
        assertThat(ConstraintViolations.formatUntyped(e.getConstraintViolations()))
                .containsOnly("id must be greater than or equal to 0");
    }
}
项目:dropwizard-jaxws    文件:ValidatingInvoker.java   
/**
 * Copied and modified from com.yammer.dropwizard.jersey.jackson#JacksonMessageBodyProvider.validate()
 * Notes on Hibernate Validator:
 * - when validating object graphs, null references are ignored.
 * - from version 5 on, Hibernate Validator throws IllegalArgumentException instead of ValidationException
 *   for null parameter values:
 *   java.lang.IllegalArgumentException: HV000116: The object to be validated must not be null.
 */
private Object validate(Annotation[] annotations, Object value) {
    final Class<?>[] classes = findValidationGroups(annotations);

    if (classes != null) {
        final ImmutableList<String> errors = ConstraintViolations.format(
                validator.validate(value, classes));

        if (!errors.isEmpty()) {
            String message = "\n";
            for (String error : errors) {
                message += "    " + error + "\n";
            }
            throw new ValidationException(message);
        }
    }

    return value;
}
项目:dropwizard-jaxb    文件:JaxbXMLValidatingMessageBodyProvider.java   
private Object validate(Annotation[] annotations, Object value) {
  final Class<?>[] classes = findValidationGroups(annotations);
  if (classes != null) {
    final Set<ConstraintViolation<Object>> violations = validator.validate(value, classes);
    if (!violations.isEmpty()) {
      throw new ConstraintViolationException("The request entity had the following errors:",
          ConstraintViolations.copyOf(violations));
    }
  }
  return value;
}
项目:dropwizard-xml    文件:JacksonXMLMessageBodyProvider.java   
private Object validate(Annotation[] annotations, Object value) {
    final Class<?>[] classes = findValidationGroups(annotations);

    if (classes != null) {
        final Set<ConstraintViolation<Object>> violations = validator.validate(value, classes);
        if (!violations.isEmpty()) {
            throw new ConstraintViolationException("The request entity had the following errors:",
                    ConstraintViolations.copyOf(violations));
        }
    }

    return value;
}
项目:codec    文件:CodecJackson.java   
public <T> T validate(T value) {
    Set<ConstraintViolation<T>> violations = validator.validate(value);
    if (!violations.isEmpty()) {
        throw new ValidationException(ConstraintViolations.<T>format(violations).toString());
    }
    return value;
}