Java源码示例:io.dropwizard.validation.Validated

示例1
@Test
public void returnsPartialValidatedRequestEntities() 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("<PartialExample xmlns=\"\"><id>1</id><text>hello Cemo</text></PartialExample>".getBytes());
    final Class<?> klass = PartialExample.class;

    final Object obj = provider.readFrom((Class<Object>) klass,
            PartialExample.class,
            new Annotation[] {valid},
            MediaType.APPLICATION_XML_TYPE,
            new MultivaluedHashMap<>(),
            entity);

    assertThat(obj)
            .isInstanceOf(PartialExample.class);

    assertThat(((PartialExample) obj).id)
            .isEqualTo(1);
}
 
示例2
@Test
public void returnsPartialValidatedByGroupRequestEntities() 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});

    final ByteArrayInputStream entity = new ByteArrayInputStream("<Example xmlns=\"\"><id>1</id></Example>".getBytes());
    final Class<?> klass = PartialExample.class;

    final Object obj = provider.readFrom((Class<Object>) klass,
            PartialExample.class,
            new Annotation[] {valid},
            MediaType.APPLICATION_XML_TYPE,
            new MultivaluedHashMap<>(),
            entity);

    assertThat(obj)
            .isInstanceOf(PartialExample.class);

    assertThat(((PartialExample) obj).id)
            .isEqualTo(1);
}
 
示例3
@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");
    }
}
 
示例4
private Class<?>[] findValidationGroups(Annotation[] annotations) {
    for (Annotation annotation : annotations) {
        if (annotation.annotationType() == Valid.class) {
            return DEFAULT_GROUP_ARRAY;
        } else if (annotation.annotationType() == Validated.class) {
            return  ((Validated) annotation).value();
        }
    }
    return null;
}
 
示例5
/**
 * Copied from com.yammer.dropwizard.jersey.jackson.JacksonMessageBodyProvider#findValidationGroups()
 */
private Class<?>[] findValidationGroups(Annotation[] annotations) {
    for (Annotation annotation : annotations) {
        if (annotation.annotationType() == Valid.class) {
            return DEFAULT_GROUP_ARRAY;
        } else if (annotation.annotationType() == Validated.class) {
            return  ((Validated) annotation).value();
        }
    }
    return null;
}
 
示例6
public void withDropwizardValidation(@Validated() String foo) {
}