java类javax.validation.constraints.NotEmpty的实例源码

MinijaxConstraintDescriptor.java 文件源码 项目:minijax 阅读 23 收藏 0 点赞 0 评论 0
private static MinijaxConstraintDescriptor<NotEmpty> buildNotEmptyValidator(final NotEmpty notEmpty, final Class<?> valueClass) {
    if (valueClass.isArray()) {
        return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForArray.INSTANCE);
    }

    if (CharSequence.class.isAssignableFrom(valueClass)) {
        return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForCharSequence.INSTANCE);
    }

    if (Collection.class.isAssignableFrom(valueClass)) {
        return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForCollection.INSTANCE);
    }

    if (Map.class.isAssignableFrom(valueClass)) {
        return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForMap.INSTANCE);
    }

    throw new ValidationException("Unsupported type for @NotEmpty annotation: " + valueClass);
}
JAXRSBeanValidationImplicitInInterceptorTest.java 文件源码 项目:bootstrap 阅读 21 收藏 0 点赞 0 评论 0
/**
 * Check not valid parameter operation failed.
 */
@Test
public void objectInvalid() {
    final SystemUser userDto = new SystemUser();
    userDto.setLogin("");
    try {
        validationInInterceptor.handleValidation(MESSAGE, INSTANCE, fromName("object"), Arrays.asList(userDto));
        Assert.fail("Expected validation errors");
    } catch (final ConstraintViolationException cve) {

        // Check all expected errors are there.
        final Set<ConstraintViolation<?>> constraintViolations = cve.getConstraintViolations();
        Assert.assertNotNull(constraintViolations);
        Assert.assertEquals(1, constraintViolations.size());

        // Check expected errors
        final ConstraintViolation<?> error1 = constraintViolations.iterator().next();
        Assert.assertEquals(NotEmpty.class, error1.getConstraintDescriptor().getAnnotation().annotationType());
        Assert.assertEquals("", error1.getInvalidValue());
        Assert.assertEquals("login", error1.getPropertyPath().toString());
    }
}
ValidationJsonExceptionTest.java 文件源码 项目:bootstrap 阅读 24 收藏 0 点赞 0 评论 0
@Test
public void testConstraintViolationExceptionParameter() {
    final Wine bean = new Wine();
    final Set<ConstraintViolation<?>> violations = new LinkedHashSet<>();

    final ConstraintHelper helper = new ConstraintHelper();

    final ConstraintDescriptor<NotEmpty> notEmptyNameDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null,
            getAnnotation("name", NotEmpty.class), ElementType.FIELD);
    PathImpl path = PathImpl.createPathFromString("name");
    violations.add(ConstraintViolationImpl.<Wine> forParameterValidation("name-Empty", null, null, "interpolated", Wine.class, bean, new Object(),
            "value", path, notEmptyNameDescriptor, ElementType.PARAMETER, null, null));
    path.addParameterNode("parameter1", 0);

    final ConstraintViolationException violationException = Mockito.mock(ConstraintViolationException.class);
    Mockito.when(violationException.getConstraintViolations()).thenReturn(violations);

    final ValidationJsonException validationJsonException = new ValidationJsonException(violationException);
    Assert.assertFalse(validationJsonException.getErrors().isEmpty());
    Assert.assertEquals("{parameter1=[{rule=name-Empty}]}", validationJsonException.getErrors().toString());
}
ValidationJsonExceptionTest.java 文件源码 项目:bootstrap 阅读 26 收藏 0 点赞 0 评论 0
@Test
public void testConstraintViolationException() {
    final Wine bean = new Wine();
    final Set<ConstraintViolation<?>> violations = new LinkedHashSet<>();

    final ConstraintHelper helper = new ConstraintHelper();

    final ConstraintDescriptor<NotEmpty> notEmptyNameDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null,
            getAnnotation("name", NotEmpty.class), ElementType.FIELD);
    final ConstraintDescriptor<NotEmpty> notEmptyGrapesDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null,
            getAnnotation("grapes", NotEmpty.class), ElementType.FIELD);
    final ConstraintDescriptor<Length> lengthNameDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null,
            getAnnotation("name", Length.class), ElementType.FIELD);
    violations.add(ConstraintViolationImpl.<Wine> forBeanValidation("name-Empty", null, null, "interpolated", Wine.class, bean, new Object(),
            "value", PathImpl.createPathFromString("name"), notEmptyNameDescriptor, ElementType.FIELD, null));
    violations.add(ConstraintViolationImpl.<Wine> forBeanValidation("name-length", null, null, "interpolated", Wine.class, bean, new Object(),
            "value", PathImpl.createPathFromString("name"), lengthNameDescriptor, ElementType.FIELD, null));
    violations.add(ConstraintViolationImpl.<Wine> forBeanValidation("grapes-Empty", null, null, "interpolated", Wine.class, bean, new Object(),
            "value", PathImpl.createPathFromString("grapes"), notEmptyGrapesDescriptor, ElementType.FIELD, null));

    final ConstraintViolationException violationException = Mockito.mock(ConstraintViolationException.class);
    Mockito.when(violationException.getConstraintViolations()).thenReturn(violations);

    final ValidationJsonException validationJsonException = new ValidationJsonException(violationException);
    Assert.assertFalse(validationJsonException.getErrors().isEmpty());
    Assert.assertEquals("{name=[{rule=name-Empty}, {rule=name-length, parameters={min=0, max=50}}], grapes=[{rule=grapes-Empty}]}",
            validationJsonException.getErrors().toString());
}
MinijaxConstraintDescriptor.java 文件源码 项目:minijax 阅读 25 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
public static <T extends Annotation> MinijaxConstraintDescriptor<T> build(final AnnotatedType annotatedType, final T annotation) {
    final Constraint constraint = annotation.annotationType().getAnnotation(Constraint.class);
    if (constraint == null) {
        return null;
    }

    final Class<?> valueClass = ReflectionUtils.getRawType(annotatedType);
    final Class<?> annotationClass = annotation.annotationType();

    if (constraint.validatedBy().length > 0) {
        return buildDeclaredValidator(annotation, constraint.validatedBy()[0]);

    } else if (annotationClass == AssertFalse.class) {
        return (MinijaxConstraintDescriptor<T>) buildAssertFalseValidator((AssertFalse) annotation, valueClass);

    } else if (annotationClass == AssertTrue.class) {
        return (MinijaxConstraintDescriptor<T>) buildAssertTrueValidator((AssertTrue) annotation, valueClass);

    } else if (annotationClass == Max.class) {
        return (MinijaxConstraintDescriptor<T>) buildMaxValidator((Max) annotation, valueClass);

    } else if (annotationClass == Min.class) {
        return (MinijaxConstraintDescriptor<T>) buildMinValidator((Min) annotation, valueClass);

    } else if (annotationClass == NotBlank.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotBlankValidator((NotBlank) annotation, valueClass);

    } else if (annotationClass == NotEmpty.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotEmptyValidator((NotEmpty) annotation, valueClass);

    } else if (annotationClass == NotNull.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotNullValidator((NotNull) annotation);

    } else if (annotationClass == Pattern.class) {
        return (MinijaxConstraintDescriptor<T>) buildPatternValidator((Pattern) annotation, valueClass);

    } else if (annotationClass == Size.class) {
        return (MinijaxConstraintDescriptor<T>) buildSizeValidator((Size) annotation, valueClass);

    } else {
        throw new ValidationException("Unrecognized constraint annotation: " + annotation);
    }
}
JAXRSBeanValidationImplicitInInterceptorTest.java 文件源码 项目:bootstrap 阅读 21 收藏 0 点赞 0 评论 0
/**
 * @param params Collection parameter with several constraints.
 */
public void jsr349Collection(final @NotEmpty @Size(max = 2) Collection<Object> params) {
    //
}
ExecuteMethodValidatorChecker.java 文件源码 项目:lastaflute 阅读 19 收藏 0 点赞 0 评论 0
protected void doCheckMismatchedValidatorAnnotation(Field field, Map<String, Class<?>> genericMap) { // recursive point
    pathDeque.push(field.getName());
    checkedTypeSet.add(field.getDeclaringClass());
    final Class<?> fieldType = deriveFieldType(field, genericMap);
    // *depends on JSON rule so difficult, check only physical mismatch here
    //if (isFormPropertyCannotNotNullType(fieldType)) {
    //    final Class<NotNull> notNullType = NotNull.class;
    //    if (field.getAnnotation(notNullType) != null) {
    //        throwExecuteMethodFormPropertyValidationMismatchException(property, notNullType);
    //    }
    //}
    if (isCannotNotEmptyType(fieldType)) {
        final Class<NotEmpty> notEmptyType = NotEmpty.class;
        if (field.getAnnotation(notEmptyType) != null) {
            throwExecuteMethodNotEmptyValidationMismatchException(field, notEmptyType);
        }
    }
    if (isCannotNotBlankType(fieldType)) {
        final Class<NotBlank> notBlankType = NotBlank.class;
        if (field.getAnnotation(notBlankType) != null) {
            throwExecuteMethodNotEmptyValidationMismatchException(field, notBlankType);
        }
    }
    if (isFormPropertyCannotRequiredPrimitiveType(fieldType)) {
        final Class<Required> requiredType = Required.class;
        if (field.getAnnotation(requiredType) != null) {
            throwExecuteMethodPrimitiveValidationMismatchException(field, requiredType);
        }
        final Class<NotNull> notNullType = NotNull.class;
        if (field.getAnnotation(notNullType) != null) {
            throwExecuteMethodPrimitiveValidationMismatchException(field, notNullType);
        }
    }
    if (Collection.class.isAssignableFrom(fieldType)) { // only collection, except array and map, simply
        doCheckGenericBeanValidationMismatch(field);
    } else if (mayBeNestedBeanType(fieldType)) {
        doCheckNestedValidationMismatch(fieldType);
        doCheckGenericBeanValidationMismatch(field);
    }
    pathDeque.pop();
}
GenerateAlgorithmRequest.java 文件源码 项目:molgenis 阅读 19 收藏 0 点赞 0 评论 0
@NotEmpty
public abstract List<String> getSourceAttributes();
AutoTagRequest.java 文件源码 项目:molgenis 阅读 20 收藏 0 点赞 0 评论 0
@NotEmpty
public abstract List<String> getOntologyIds();
GetOntologyTermRequest.java 文件源码 项目:molgenis 阅读 21 收藏 0 点赞 0 评论 0
@NotEmpty
public abstract List<String> getOntologyIds();
RemoveTagRequest.java 文件源码 项目:molgenis 阅读 18 收藏 0 点赞 0 评论 0
@NotEmpty
public abstract String getOntologyTermIRI();
AddTagRequest.java 文件源码 项目:molgenis 阅读 20 收藏 0 点赞 0 评论 0
@NotEmpty
public abstract List<String> getOntologyTermIRIs();
EntityCollectionBatchRequestV2.java 文件源码 项目:molgenis 阅读 19 收藏 0 点赞 0 评论 0
@Nullable // See #3897. If hashCode fails, the validation throws an exception
@NotEmpty(message = "Please provide at least one entity in the entities property.")
@Size(max = RestControllerV2.MAX_ENTITIES, message = "Number of entities cannot be more than {max}.")
public abstract List<Map<String, Object>> getEntities();
EntityCollectionDeleteRequestV2.java 文件源码 项目:molgenis 阅读 20 收藏 0 点赞 0 评论 0
@NotEmpty(message = "Please provide at least one entity in the entityIds property.")
@Size(max = RestControllerV2.MAX_ENTITIES, message = "Number of entity identifiers cannot be more than {max}.")
public abstract List<String> getEntityIds();
UserClass.java 文件源码 项目:extclassgenerator 阅读 21 收藏 0 点赞 0 评论 0
@NotEmpty
@Email
@Size(max = 128)
public void setEmail(String email) {
    this.email = email;
}
User.java 文件源码 项目:extclassgenerator 阅读 18 收藏 0 点赞 0 评论 0
@NotEmpty
@Email
@Size(max = 128)
String getEmail();


问题


面经


文章

微信
公众号

扫码关注公众号