Search in sources :

Example 6 with BeanDescriptor

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();
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)

Example 7 with BeanDescriptor

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);
}
Also used : BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) Test(org.testng.annotations.Test)

Example 8 with BeanDescriptor

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[]
}
Also used : BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) Validator(jakarta.validation.Validator) Test(org.junit.Test)

Example 9 with BeanDescriptor

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[]
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) GroupConversionDescriptor(jakarta.validation.metadata.GroupConversionDescriptor) Test(org.junit.Test)

Example 10 with BeanDescriptor

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[]
}
Also used : BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) Test(org.junit.Test)

Aggregations

BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)116 Test (org.testng.annotations.Test)96 AbstractTCKTest (org.hibernate.beanvalidation.tck.tests.AbstractTCKTest)55 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)40 PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)39 Validator (jakarta.validation.Validator)35 ValidatorUtil.getBeanDescriptor (org.hibernate.validator.testutils.ValidatorUtil.getBeanDescriptor)31 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)24 MethodDescriptor (jakarta.validation.metadata.MethodDescriptor)19 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)15 ConstructorDescriptor (jakarta.validation.metadata.ConstructorDescriptor)10 Test (org.junit.Test)10 CrossParameterDescriptor (jakarta.validation.metadata.CrossParameterDescriptor)7 TestForIssue (org.hibernate.validator.testutil.TestForIssue)7 NotNull (jakarta.validation.constraints.NotNull)6 Default (jakarta.validation.groups.Default)6 TestUtil.getValidatorUnderTest (org.hibernate.beanvalidation.tck.util.TestUtil.getValidatorUnderTest)6 ConstraintValidator (jakarta.validation.ConstraintValidator)4 ConstraintViolation (jakarta.validation.ConstraintViolation)4 Annotation (java.lang.annotation.Annotation)4