Search in sources :

Example 6 with ConstraintDescriptor

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

the class ElementDescriptorTest method testThatMethodLevelConstraintsAreNotReflectedByBeanDescriptor.

@Test
public void testThatMethodLevelConstraintsAreNotReflectedByBeanDescriptor() {
    BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass(CustomerRepository.class);
    Set<ConstraintDescriptor<?>> constraintDescriptors = beanDescriptor.getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 1, "Only the class-level @ScriptAssert is expected.");
    constraintDescriptors = beanDescriptor.findConstraints().declaredOn(ElementType.PARAMETER).getConstraintDescriptors();
    assertEquals(constraintDescriptors.size(), 0);
}
Also used : BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Test(org.testng.annotations.Test)

Example 7 with ConstraintDescriptor

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

the class LibraryTest method testContainerElementTypeDescriptor.

@Test
public void testContainerElementTypeDescriptor() {
    BeanDescriptor libraryDescriptor = validator.getConstraintsForClass(Library.class);
    // tag::testContainerElementTypeDescriptor[]
    PropertyDescriptor booksDescriptor = libraryDescriptor.getConstraintsForProperty("books");
    Set<ContainerElementTypeDescriptor> booksContainerElementTypeDescriptors = booksDescriptor.getConstrainedContainerElementTypes();
    ContainerElementTypeDescriptor booksContainerElementTypeDescriptor = booksContainerElementTypeDescriptors.iterator().next();
    assertTrue(booksContainerElementTypeDescriptor.hasConstraints());
    assertTrue(booksContainerElementTypeDescriptor.isCascaded());
    assertEquals(0, booksContainerElementTypeDescriptor.getTypeArgumentIndex().intValue());
    assertEquals(List.class, booksContainerElementTypeDescriptor.getContainerClass());
    Set<ConstraintDescriptor<?>> constraintDescriptors = booksContainerElementTypeDescriptor.getConstraintDescriptors();
    ConstraintDescriptor<?> constraintDescriptor = constraintDescriptors.iterator().next();
    assertEquals(NotNull.class, constraintDescriptor.getAnnotation().annotationType());
// end::testContainerElementTypeDescriptor[]
}
Also used : ContainerElementTypeDescriptor(jakarta.validation.metadata.ContainerElementTypeDescriptor) PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Test(org.junit.Test)

Example 8 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 9 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 10 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)

Aggregations

ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)59 Test (org.testng.annotations.Test)45 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)34 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)25 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)24 Validator (jakarta.validation.Validator)18 CrossParameterDescriptor (jakarta.validation.metadata.CrossParameterDescriptor)18 PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)12 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)9 NotNull (jakarta.validation.constraints.NotNull)5 MethodDescriptor (jakarta.validation.metadata.MethodDescriptor)5 ParameterDescriptor (jakarta.validation.metadata.ParameterDescriptor)5 ReturnValueDescriptor (jakarta.validation.metadata.ReturnValueDescriptor)5 TestUtil.getValidatorUnderTest (org.hibernate.beanvalidation.tck.util.TestUtil.getValidatorUnderTest)5 Assert.assertNotNull (org.testng.Assert.assertNotNull)5 ConstructorDescriptor (jakarta.validation.metadata.ConstructorDescriptor)4 Column (org.hibernate.mapping.Column)4 TestForIssue (org.hibernate.validator.testutil.TestForIssue)4 ConstraintValidator (jakarta.validation.ConstraintValidator)3 ConstraintViolation (jakarta.validation.ConstraintViolation)3