Search in sources :

Example 11 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.

the class ConstraintDescriptorTest method testValidationAppliesTo.

@Test
public void testValidationAppliesTo() {
    BeanDescriptor beanDescriptor = getBeanDescriptor(Bar.class);
    Set<MethodDescriptor> methodDescriptors = beanDescriptor.getConstrainedMethods(MethodType.NON_GETTER);
    assertEquals(methodDescriptors.size(), 1);
    CrossParameterDescriptor crossParameterDescriptor = methodDescriptors.iterator().next().getCrossParameterDescriptor();
    Set<ConstraintDescriptor<?>> constraintDescriptors = crossParameterDescriptor.getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 1);
    ConstraintDescriptor<?> constraintDescriptor = constraintDescriptors.iterator().next();
    assertEquals(constraintDescriptor.getValidationAppliesTo(), ConstraintTarget.PARAMETERS, "wrong constraint targets");
}
Also used : ValidatorUtil.getBeanDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) Test(org.testng.annotations.Test)

Example 12 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.

the class ConstraintDescriptorTest method testBasicDescriptorAttributes.

@Test
public void testBasicDescriptorAttributes() {
    BeanDescriptor beanDescriptor = getBeanDescriptor(Foo.class);
    PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty("fubar");
    Set<ConstraintDescriptor<?>> constraintDescriptors = propertyDescriptor.getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 1);
    ConstraintDescriptor<?> constraintDescriptor = constraintDescriptors.iterator().next();
    assertEquals(constraintDescriptor.getMessageTemplate(), "bar", "Wrong message");
    Set<Class<?>> groups = newHashSet();
    groups.add(SnafuGroup.class);
    assertEquals(constraintDescriptor.getGroups(), groups, "Wrong groups");
    Set<Class<?>> payloads = newHashSet();
    payloads.add(Payload22.class);
    assertEquals(constraintDescriptor.getPayload(), payloads, "Wrong payload");
    assertNull(constraintDescriptor.getValidationAppliesTo(), "There is no validationAppliedTo attribute");
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) ValidatorUtil.getBeanDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Test(org.testng.annotations.Test)

Example 13 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.

the class ConstructorDescriptorTest method testGetReturnValueDescriptor.

@Test
public void testGetReturnValueDescriptor() {
    ConstructorDescriptor constructorDescriptor = getConstructorDescriptor(CustomerRepositoryExt.class, String.class);
    ReturnValueDescriptor returnValueDescriptor = constructorDescriptor.getReturnValueDescriptor();
    assertThat(returnValueDescriptor).isNotNull();
    assertThat(returnValueDescriptor.getElementClass()).isEqualTo(CustomerRepositoryExt.class);
    assertThat(returnValueDescriptor.hasConstraints()).isTrue();
    assertThat(returnValueDescriptor.isCascaded()).isTrue();
    Set<ConstraintDescriptor<?>> constraintDescriptors = returnValueDescriptor.getConstraintDescriptors();
    assertThat(constraintDescriptors).hasSize(1);
    ConstraintDescriptor<?> descriptor = constraintDescriptors.iterator().next();
    assertThat(descriptor.getAnnotation().annotationType()).isEqualTo(ValidB2BRepository.class);
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ConstructorDescriptor(jakarta.validation.metadata.ConstructorDescriptor) ValidatorUtil.getConstructorDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getConstructorDescriptor) Test(org.testng.annotations.Test)

Example 14 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.

the class CrossParameterDescriptorTest method testFindConstraintsLookingAt.

@Test
public void testFindConstraintsLookingAt() {
    CrossParameterDescriptor descriptor = getMethodDescriptor(CustomerRepositoryExt.class, "methodWithCrossParameterConstraint", LocalDate.class, LocalDate.class).getCrossParameterDescriptor();
    Set<ConstraintDescriptor<?>> constraintDescriptors = descriptor.findConstraints().lookingAt(Scope.LOCAL_ELEMENT).getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 0);
    constraintDescriptors = descriptor.findConstraints().lookingAt(Scope.HIERARCHY).getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 1);
    assertEquals(constraintDescriptors.iterator().next().getAnnotation().annotationType(), ConsistentDateParameters.class);
}
Also used : ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) CustomerRepositoryExt(org.hibernate.validator.test.internal.metadata.CustomerRepositoryExt) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 15 with ConstraintDescriptor

use of jakarta.validation.metadata.ConstraintDescriptor in project minijax by minijax.

the class MinijaxContainerElementTypeDescriptor method build.

public static Set<ContainerElementTypeDescriptor> build(final Class<?> elementClass, final AnnotatedParameterizedType annotatedType) {
    final Set<ContainerElementTypeDescriptor> result = new HashSet<>();
    final Class<?> containerClass = ReflectionUtils.getRawType(annotatedType);
    int argIndex = 0;
    for (final AnnotatedType typeArg : annotatedType.getAnnotatedActualTypeArguments()) {
        final Set<ConstraintDescriptor<?>> constraintDescriptors = new HashSet<>();
        for (final Annotation annotation : typeArg.getAnnotations()) {
            final MinijaxConstraintDescriptor<?> constraintDescriptor = MinijaxConstraintDescriptor.build(typeArg, annotation);
            if (constraintDescriptor != null) {
                constraintDescriptors.add(constraintDescriptor);
            }
        }
        if (!constraintDescriptors.isEmpty()) {
            result.add(new MinijaxContainerElementTypeDescriptor(elementClass, containerClass, argIndex, constraintDescriptors));
        }
        argIndex++;
    }
    return result;
}
Also used : ContainerElementTypeDescriptor(jakarta.validation.metadata.ContainerElementTypeDescriptor) AnnotatedType(java.lang.reflect.AnnotatedType) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Aggregations

ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)25 Test (org.testng.annotations.Test)11 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)10 CrossParameterDescriptor (jakarta.validation.metadata.CrossParameterDescriptor)4 PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)4 Column (org.hibernate.mapping.Column)4 TestForIssue (org.hibernate.validator.testutil.TestForIssue)4 MethodDescriptor (jakarta.validation.metadata.MethodDescriptor)3 ParameterDescriptor (jakarta.validation.metadata.ParameterDescriptor)3 ReturnValueDescriptor (jakarta.validation.metadata.ReturnValueDescriptor)3 Selectable (org.hibernate.mapping.Selectable)3 ValidatorUtil.getBeanDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor)3 Test (org.junit.Test)3 MessageInterpolator (jakarta.validation.MessageInterpolator)2 Validator (jakarta.validation.Validator)2 ConstructorDescriptor (jakarta.validation.metadata.ConstructorDescriptor)2 ContainerElementTypeDescriptor (jakarta.validation.metadata.ContainerElementTypeDescriptor)2 ElementDescriptor (jakarta.validation.metadata.ElementDescriptor)2 ConstraintDescriptorImpl (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)2 HibernateMessageInterpolatorContext (org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext)2