Search in sources :

Example 1 with ParameterDescriptor

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

the class MethodDescriptorTest method testGetParameterConstraintsForParameterlessMethod.

@Test
public void testGetParameterConstraintsForParameterlessMethod() {
    MethodDescriptor methodDescriptor = getMethodDescriptor(CustomerRepositoryExt.class, "baz");
    List<ParameterDescriptor> parameterConstraints = methodDescriptor.getParameterDescriptors();
    assertNotNull(parameterConstraints);
    assertEquals(parameterConstraints.size(), 0);
}
Also used : ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) ValidatorUtil.getMethodDescriptor(org.hibernate.validator.testutils.ValidatorUtil.getMethodDescriptor) Test(org.testng.annotations.Test)

Example 2 with ParameterDescriptor

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

the class BeanDescriptorTest method getSignatures.

private Set<List<Class<?>>> getSignatures(Set<ConstructorDescriptor> descriptors) {
    Set<List<Class<?>>> signatures = newHashSet();
    for (ConstructorDescriptor methodDescriptor : descriptors) {
        List<Class<?>> parameterTypes = newArrayList();
        for (ParameterDescriptor oneParameter : methodDescriptor.getParameterDescriptors()) {
            parameterTypes.add(oneParameter.getElementClass());
        }
        signatures.add(parameterTypes);
    }
    return signatures;
}
Also used : ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) ConstructorDescriptor(jakarta.validation.metadata.ConstructorDescriptor) CollectionHelper.newArrayList(org.hibernate.validator.internal.util.CollectionHelper.newArrayList) List(java.util.List)

Example 3 with ParameterDescriptor

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

the class CarTest method testMethodAndConstructorDescriptor.

@Test
public void testMethodAndConstructorDescriptor() {
    BeanDescriptor carDescriptor = validator.getConstraintsForClass(Car.class);
    // tag::testMethodAndConstructorDescriptor[]
    // driveAway(int) has a constrained parameter and an unconstrained return value
    MethodDescriptor driveAwayDescriptor = carDescriptor.getConstraintsForMethod("driveAway", int.class);
    assertEquals("driveAway", driveAwayDescriptor.getName());
    assertTrue(driveAwayDescriptor.hasConstrainedParameters());
    assertFalse(driveAwayDescriptor.hasConstrainedReturnValue());
    // always returns an empty set; constraints are retrievable by navigating to
    // one of the sub-descriptors, e.g. for the return value
    assertTrue(driveAwayDescriptor.getConstraintDescriptors().isEmpty());
    ParameterDescriptor speedDescriptor = driveAwayDescriptor.getParameterDescriptors().get(0);
    // The "speed" parameter is located at index 0, has one constraint and is not cascaded
    // nor does it define group conversions
    assertEquals("speed", speedDescriptor.getName());
    assertEquals(0, speedDescriptor.getIndex());
    assertEquals(1, speedDescriptor.getConstraintDescriptors().size());
    assertFalse(speedDescriptor.isCascaded());
    assert speedDescriptor.getGroupConversions().isEmpty();
    // getDriver() has no constrained parameters but its return value is marked for cascaded
    // validation and declares one group conversion
    MethodDescriptor getDriverDescriptor = carDescriptor.getConstraintsForMethod("getDriver");
    assertFalse(getDriverDescriptor.hasConstrainedParameters());
    assertTrue(getDriverDescriptor.hasConstrainedReturnValue());
    ReturnValueDescriptor returnValueDescriptor = getDriverDescriptor.getReturnValueDescriptor();
    assertTrue(returnValueDescriptor.getConstraintDescriptors().isEmpty());
    assertTrue(returnValueDescriptor.isCascaded());
    assertEquals(1, returnValueDescriptor.getGroupConversions().size());
    // load(List<Person>, List<PieceOfLuggage>) has one cross-parameter constraint
    MethodDescriptor loadDescriptor = carDescriptor.getConstraintsForMethod("load", List.class, List.class);
    assertTrue(loadDescriptor.hasConstrainedParameters());
    assertFalse(loadDescriptor.hasConstrainedReturnValue());
    assertEquals(1, loadDescriptor.getCrossParameterDescriptor().getConstraintDescriptors().size());
    // Car(String, String, Person, String) has one constrained parameter
    ConstructorDescriptor constructorDescriptor = carDescriptor.getConstraintsForConstructor(String.class, String.class, Person.class, String.class);
    assertEquals("Car", constructorDescriptor.getName());
    assertFalse(constructorDescriptor.hasConstrainedReturnValue());
    assertTrue(constructorDescriptor.hasConstrainedParameters());
    assertEquals(1, constructorDescriptor.getParameterDescriptors().get(0).getConstraintDescriptors().size());
// end::testMethodAndConstructorDescriptor[]
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) ConstructorDescriptor(jakarta.validation.metadata.ConstructorDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) Test(org.junit.Test)

Example 4 with ParameterDescriptor

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

the class XMLConfigurationTest method testXMLConstraintsApplied.

@Test
public void testXMLConstraintsApplied() {
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(RentalStation.class);
    assertTrue(beanDescriptor.isBeanConstrained());
    ConstructorDescriptor constructorDescriptor = beanDescriptor.getConstraintsForConstructor();
    ReturnValueDescriptor returnValueDescriptor = constructorDescriptor.getReturnValueDescriptor();
    Set<ConstraintDescriptor<?>> constraintDescriptors = returnValueDescriptor.getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, ValidRentalStation.class);
    constructorDescriptor = beanDescriptor.getConstraintsForConstructor(String.class);
    List<ParameterDescriptor> parameterDescriptors = constructorDescriptor.getParameterDescriptors();
    constraintDescriptors = parameterDescriptors.get(0).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class);
    MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod("getCustomers");
    returnValueDescriptor = methodDescriptor.getReturnValueDescriptor();
    constraintDescriptors = returnValueDescriptor.getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class, Size.class);
    methodDescriptor = beanDescriptor.getConstraintsForMethod("rentCar", Customer.class, Date.class, int.class);
    parameterDescriptors = methodDescriptor.getParameterDescriptors();
    constraintDescriptors = parameterDescriptors.get(0).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class);
    constraintDescriptors = parameterDescriptors.get(1).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, NotNull.class, Future.class);
    constraintDescriptors = parameterDescriptors.get(2).getConstraintDescriptors();
    assertCorrectConstraintTypes(constraintDescriptors, Min.class);
}
Also used : ReturnValueDescriptor(jakarta.validation.metadata.ReturnValueDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) CrossParameterDescriptor(jakarta.validation.metadata.CrossParameterDescriptor) ConstructorDescriptor(jakarta.validation.metadata.ConstructorDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) Date(java.util.Date) Test(org.junit.Test)

Example 5 with ParameterDescriptor

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

the class DefaultPackageInVarargParameterTest method testArrayParametersAreConfigurableWithAndWithoutDefaultPackage.

@Test
public void testArrayParametersAreConfigurableWithAndWithoutDefaultPackage() {
    Configuration<?> configuration = ValidatorUtil.getConfiguration();
    configuration.addMapping(DefaultPackageInVarargParameterTest.class.getResourceAsStream("array-and-vararg-parameter-mapping.xml"));
    ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
    validator = validatorFactory.getValidator();
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(Foo.class);
    Set<MethodDescriptor> methodDescriptors = beanDescriptor.getConstrainedMethods(MethodType.NON_GETTER);
    assertTrue(methodDescriptors.size() == 2, "There should be two constrained methods");
    for (MethodDescriptor methodDescriptor : methodDescriptors) {
        assertTrue(methodDescriptor.hasConstrainedParameters(), "Parameter should be constrained");
        List<ParameterDescriptor> parameterDescriptorList = methodDescriptor.getParameterDescriptors();
        for (ParameterDescriptor parameterDescriptor : parameterDescriptorList) {
            assertTrue(parameterDescriptor.isCascaded(), "Parameter should be constrained");
        }
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ParameterDescriptor(jakarta.validation.metadata.ParameterDescriptor) MethodDescriptor(jakarta.validation.metadata.MethodDescriptor) Test(org.testng.annotations.Test)

Aggregations

ParameterDescriptor (jakarta.validation.metadata.ParameterDescriptor)10 Test (org.testng.annotations.Test)7 MethodDescriptor (jakarta.validation.metadata.MethodDescriptor)5 ConstructorDescriptor (jakarta.validation.metadata.ConstructorDescriptor)4 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)3 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)3 CrossParameterDescriptor (jakarta.validation.metadata.CrossParameterDescriptor)2 ReturnValueDescriptor (jakarta.validation.metadata.ReturnValueDescriptor)2 ValidatorUtil.getMethodDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getMethodDescriptor)2 ValidatorUtil.getParameterDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getParameterDescriptor)2 Test (org.junit.Test)2 ValidatorFactory (jakarta.validation.ValidatorFactory)1 Date (java.util.Date)1 List (java.util.List)1 CollectionHelper.newArrayList (org.hibernate.validator.internal.util.CollectionHelper.newArrayList)1 TestForIssue (org.hibernate.validator.testutil.TestForIssue)1 ValidatorUtil.getConstructorDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getConstructorDescriptor)1