use of jakarta.validation.metadata.BeanDescriptor in project hibernate-validator by hibernate.
the class ConstraintValidatorManagerTest method getSingleConstraintDescriptorForProperty.
private ConstraintDescriptorImpl<?> getSingleConstraintDescriptorForProperty(Validator validator, Class<?> clazz, String propertyName) {
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(clazz);
PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(propertyName);
Set<ConstraintDescriptor<?>> constraintDescriptorSet = propertyDescriptor.getConstraintDescriptors();
assertEquals(constraintDescriptorSet.size(), 1, "There should be only one constraint descriptor");
return (ConstraintDescriptorImpl<?>) constraintDescriptorSet.iterator().next();
}
use of jakarta.validation.metadata.BeanDescriptor 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);
}
use of jakarta.validation.metadata.BeanDescriptor in project hibernate-validator by hibernate.
the class CarTest method testConstraintDescriptor.
@Test
public void testConstraintDescriptor() {
BeanDescriptor carDescriptor = validator.getConstraintsForClass(Car.class);
// tag::testConstraintDescriptor[]
// descriptor for the @LuggageCountMatchesPassengerCount constraint on the
// load(List<Person>, List<PieceOfLuggage>) method
ConstraintDescriptor<?> constraintDescriptor = carDescriptor.getConstraintsForMethod("load", List.class, List.class).getCrossParameterDescriptor().getConstraintDescriptors().iterator().next();
// constraint type
assertEquals(LuggageCountMatchesPassengerCount.class, constraintDescriptor.getAnnotation().annotationType());
// standard constraint attributes
assertEquals(SeverityInfo.class, constraintDescriptor.getPayload().iterator().next());
assertEquals(ConstraintTarget.PARAMETERS, constraintDescriptor.getValidationAppliesTo());
assertEquals(Default.class, constraintDescriptor.getGroups().iterator().next());
assertEquals("There must not be more than {piecesOfLuggagePerPassenger} pieces of luggage per " + "passenger.", constraintDescriptor.getMessageTemplate());
// custom constraint attribute
assertEquals(2, constraintDescriptor.getAttributes().get("piecesOfLuggagePerPassenger"));
// no composing constraints
assertTrue(constraintDescriptor.getComposingConstraints().isEmpty());
// validator class
assertEquals(Arrays.<Class<?>>asList(LuggageCountMatchesPassengerCount.Validator.class), constraintDescriptor.getConstraintValidatorClasses());
// end::testConstraintDescriptor[]
}
use of jakarta.validation.metadata.BeanDescriptor in project hibernate-validator by hibernate.
the class CarTest method testGroupConversionDescriptor.
@Test
public void testGroupConversionDescriptor() {
BeanDescriptor carDescriptor = validator.getConstraintsForClass(Car.class);
// tag::testGroupConversionDescriptor[]
PropertyDescriptor driverDescriptor = carDescriptor.getConstraintsForProperty("driver");
Set<GroupConversionDescriptor> groupConversions = driverDescriptor.getGroupConversions();
assertEquals(1, groupConversions.size());
GroupConversionDescriptor groupConversionDescriptor = groupConversions.iterator().next();
assertEquals(Default.class, groupConversionDescriptor.getFrom());
assertEquals(Person.Basic.class, groupConversionDescriptor.getTo());
// end::testGroupConversionDescriptor[]
}
use of jakarta.validation.metadata.BeanDescriptor in project hibernate-validator by hibernate.
the class CarTest method testBeanDescriptor.
@Test
public void testBeanDescriptor() {
// tag::testBeanDescriptor[]
BeanDescriptor carDescriptor = validator.getConstraintsForClass(Car.class);
assertTrue(carDescriptor.isBeanConstrained());
// one class-level constraint
assertEquals(1, carDescriptor.getConstraintDescriptors().size());
// manufacturer, licensePlate, driver
assertEquals(3, carDescriptor.getConstrainedProperties().size());
// property has constraint
assertNotNull(carDescriptor.getConstraintsForProperty("licensePlate"));
// property is marked with @Valid
assertNotNull(carDescriptor.getConstraintsForProperty("driver"));
// constraints from getter method in interface and implementation class are returned
assertEquals(2, carDescriptor.getConstraintsForProperty("manufacturer").getConstraintDescriptors().size());
// property is not constrained
assertNull(carDescriptor.getConstraintsForProperty("modelName"));
// driveAway(int), load(List<Person>, List<PieceOfLuggage>)
assertEquals(2, carDescriptor.getConstrainedMethods(MethodType.NON_GETTER).size());
// driveAway(int), getManufacturer(), getDriver(), load(List<Person>, List<PieceOfLuggage>)
assertEquals(4, carDescriptor.getConstrainedMethods(MethodType.NON_GETTER, MethodType.GETTER).size());
// driveAway(int)
assertNotNull(carDescriptor.getConstraintsForMethod("driveAway", int.class));
// getManufacturer()
assertNotNull(carDescriptor.getConstraintsForMethod("getManufacturer"));
// setManufacturer() is not constrained
assertNull(carDescriptor.getConstraintsForMethod("setManufacturer", String.class));
// Car(String, String, Person, String)
assertEquals(1, carDescriptor.getConstrainedConstructors().size());
// Car(String, String, Person, String)
assertNotNull(carDescriptor.getConstraintsForConstructor(String.class, String.class, Person.class, String.class));
// end::testBeanDescriptor[]
}
Aggregations