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);
}
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;
}
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[]
}
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);
}
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");
}
}
}
Aggregations