Search in sources :

Example 16 with MethodDescriptor

use of jakarta.validation.metadata.MethodDescriptor 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 17 with MethodDescriptor

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

the class BeanDescriptorTest method testGetConstraintsForMethodFromBaseType.

// A method descriptor can be retrieved by specifying a method from a base
// type (qax() is not defined on CustomerRepositoryExt, but only on
// CustomerRepository).
@Test
public void testGetConstraintsForMethodFromBaseType() throws Exception {
    BeanDescriptor descriptor = getBeanDescriptor(CustomerRepositoryExt.class);
    MethodDescriptor methodDescriptor = descriptor.getConstraintsForMethod("qax", Integer.class);
    assertNotNull(methodDescriptor);
}
Also used : ValidatorUtil.getBeanDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) Test(org.testng.annotations.Test)

Example 18 with MethodDescriptor

use of jakarta.validation.metadata.MethodDescriptor in project beanvalidation-tck by eclipse-ee4j.

the class IgnoreAnnotationsOnMethodTest method testIgnoreAnnotationsOnMethodLevel.

@Test
@SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "l"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "m"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "n"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "p") })
public void testIgnoreAnnotationsOnMethodLevel() {
    MethodDescriptor descriptor = TestUtil.getMethodDescriptor(IgnoreAnnotations.class, "foobar", String.class, String.class);
    CrossParameterDescriptor crossParameterDescriptor = descriptor.getCrossParameterDescriptor();
    assertFalse(crossParameterDescriptor.hasConstraints(), "Cross parameter constraints should be ignored.");
    ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor();
    assertFalse(returnValueDescriptor.hasConstraints(), "Return value constraints should be ignored.");
    assertTrue(returnValueDescriptor.getGroupConversions().isEmpty(), "Group conversions should be ignored");
    ParameterDescriptor parameterDescriptor = descriptor.getParameterDescriptors().get(0);
    assertFalse(parameterDescriptor.hasConstraints(), "First parameter constraints should be ignored.");
    assertTrue(parameterDescriptor.getGroupConversions().isEmpty(), "Group conversions should be ignored");
    parameterDescriptor = descriptor.getParameterDescriptors().get(1);
    assertTrue(parameterDescriptor.hasConstraints(), "Second parameter constraints should be applied.");
    assertEquals(parameterDescriptor.getGroupConversions().size(), 2, "All group conversions should be combined");
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test)

Example 19 with MethodDescriptor

use of jakarta.validation.metadata.MethodDescriptor in project beanvalidation-tck by eclipse-ee4j.

the class MethodValidationTest method testConstraintOnMethodReturnValueAndParameter.

@Test
@SpecAssertions({ @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "h") })
public void testConstraintOnMethodReturnValueAndParameter() throws Exception {
    MethodDescriptor descriptor = TestUtil.getMethodDescriptor(CustomerRepository.class, "notifyCustomer", Customer.class, String.class);
    assertNotNull(descriptor, "the specified method should be configured in xml");
    ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor();
    Set<ConstraintDescriptor<?>> constraintDescriptors = returnValueDescriptor.getConstraintDescriptors();
    assertTrue(constraintDescriptors.size() == 1);
    ConstraintDescriptor<?> constraintDescriptor = constraintDescriptors.iterator().next();
    assertEquals(constraintDescriptor.getAnnotation().annotationType(), NotNull.class, "Unexpected constraint type");
    List<ParameterDescriptor> parameterDescriptors = descriptor.getParameterDescriptors();
    assertTrue(parameterDescriptors.size() == 2);
    ParameterDescriptor parameterDescriptor = parameterDescriptors.get(0);
    constraintDescriptors = parameterDescriptor.getConstraintDescriptors();
    assertTrue(constraintDescriptors.size() == 1);
    constraintDescriptor = constraintDescriptors.iterator().next();
    assertEquals(constraintDescriptor.getAnnotation().annotationType(), NotNull.class, "Unexpected constraint type");
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test)

Example 20 with MethodDescriptor

use of jakarta.validation.metadata.MethodDescriptor in project beanvalidation-tck by eclipse-ee4j.

the class MethodValidationTest method testGroupConversionOnReturnValueAndParameter.

@Test
@SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "j")
public void testGroupConversionOnReturnValueAndParameter() throws Exception {
    MethodDescriptor descriptor = TestUtil.getMethodDescriptor(CustomerRepository.class, "findByExample", Customer.class);
    assertNotNull(descriptor, "the specified method should be configured in xml");
    ReturnValueDescriptor returnValueDescriptor = descriptor.getReturnValueDescriptor();
    Set<GroupConversionDescriptor> groupConversionDescriptors = returnValueDescriptor.getGroupConversions();
    assertTrue(groupConversionDescriptors.size() == 1);
    GroupConversionDescriptor groupConversionDescriptor = groupConversionDescriptors.iterator().next();
    assertEquals(groupConversionDescriptor.getFrom(), Default.class, "Wrong from class for group conversion");
    List<ParameterDescriptor> parameterDescriptors = descriptor.getParameterDescriptors();
    assertTrue(parameterDescriptors.size() == 1);
    ParameterDescriptor parameterDescriptor = parameterDescriptors.get(0);
    groupConversionDescriptors = parameterDescriptor.getGroupConversions();
    assertTrue(groupConversionDescriptors.size() == 1);
    groupConversionDescriptor = groupConversionDescriptors.iterator().next();
    assertEquals(groupConversionDescriptor.getFrom(), Default.class, "Wrong from class for group conversion");
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) GroupConversionDescriptor(jakarta.validation.metadata.GroupConversionDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) AbstractTCKTest(org.hibernate.beanvalidation.tck.tests.AbstractTCKTest) Test(org.testng.annotations.Test) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Aggregations

MethodDescriptor (jakarta.validation.metadata.MethodDescriptor)93 Test (org.testng.annotations.Test)86 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)49 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)44 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)19 ValidatorUtil.getMethodDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getMethodDescriptor)18 ParameterDescriptor (jakarta.validation.metadata.ParameterDescriptor)13 CrossParameterDescriptor (jakarta.validation.metadata.CrossParameterDescriptor)12 ReturnValueDescriptor (jakarta.validation.metadata.ReturnValueDescriptor)11 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)8 ValidatorUtil.getBeanDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor)7 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)5 ConstructorDescriptor (jakarta.validation.metadata.ConstructorDescriptor)4 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)4 Validator (jakarta.validation.Validator)3 TestUtil.getMethodDescriptor (org.hibernate.beanvalidation.tck.util.TestUtil.getMethodDescriptor)3 TestForIssue (org.hibernate.validator.testutil.TestForIssue)3 Test (org.junit.Test)3 ConstraintValidator (jakarta.validation.ConstraintValidator)2 ValidatorFactory (jakarta.validation.ValidatorFactory)2