public static PropertyDescriptor extractPropertyDescriptor(FacesContext context, PrimeApplicationContext applicationContext, ValueExpression ve) { if (ve != null) { ELContext elContext = context.getELContext(); ValueReference vr = ValueExpressionAnalyzer.getReference(elContext, ve); if (vr != null) { Validator validator = applicationContext.getValidator(); Object base = vr.getBase(); Object property = vr.getProperty(); if (base != null && property != null) { BeanDescriptor beanDescriptor = validator.getConstraintsForClass(base.getClass()); if (beanDescriptor != null) { return beanDescriptor.getConstraintsForProperty(property.toString()); } } } } return null; } }
@Override public List<Constraint> resolveForProperty(String property, Class<?> clazz) { List<Constraint> constraints = new ArrayList<>(); BeanDescriptor beanDescriptor = this.validator.getConstraintsForClass(clazz); PropertyDescriptor propertyDescriptor = beanDescriptor .getConstraintsForProperty(property); if (propertyDescriptor != null) { for (ConstraintDescriptor<?> constraintDescriptor : propertyDescriptor .getConstraintDescriptors()) { constraints.add(new Constraint( constraintDescriptor.getAnnotation().annotationType().getName(), constraintDescriptor.getAttributes())); } } return constraints; }
private boolean isGetterConstrained(BeanDescriptor beanDescriptor, Method method, String property) { PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( property ); return propertyDescriptor != null && propertyDescriptor.findConstraints() .declaredOn( ElementType.METHOD ) .hasConstraints(); }
private void configureRequired(BindingBuilder<BEAN, ?> binding, PropertyDefinition<BEAN, ?> definition, BeanValidator validator) { assert requiredConfigurator != null; Class<?> propertyHolderType = definition.getPropertyHolderType(); BeanDescriptor descriptor = validator.getJavaxBeanValidator() .getConstraintsForClass(propertyHolderType); PropertyDescriptor propertyDescriptor = descriptor .getConstraintsForProperty(definition.getTopLevelName()); if (propertyDescriptor == null) { return; } if (propertyDescriptor.getConstraintDescriptors().stream() .map(ConstraintDescriptor::getAnnotation) .anyMatch(requiredConfigurator)) { binding.getField().setRequiredIndicatorVisible(true); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_INHERITANCE, id = "b") public void testConstraintsOnSuperClassAreInherited() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( Bar.class ); String propertyName = "foo"; assertTrue( beanDescriptor.getConstraintsForProperty( propertyName ) != null ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( propertyName ); Annotation constraintAnnotation = propDescriptor.getConstraintDescriptors() .iterator() .next().getAnnotation(); assertTrue( constraintAnnotation.annotationType() == NotNull.class ); }
@Test @SpecAssertion(section = "3.4", id = "c") public void testConstraintCanBelongToMoreThanOneGroup() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertTrue( beanDescriptor.isBeanConstrained() ); PropertyDescriptor propDesc = beanDescriptor.getConstraintsForProperty( "defaultCreditCard" ); assertTrue( propDesc.getConstraintDescriptors().size() == 1 ); ConstraintDescriptor descriptor = propDesc.getConstraintDescriptors().iterator().next(); assertTrue( descriptor.getGroups().size() == 2 ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.CONSTRAINTMETADATA_BEANDESCRIPTOR, id = "c") public void testGetConstraintsForNullProperty() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( Order.class ); beanDescriptor.getConstraintsForProperty( null ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "a") public void testGetElementClass() { Validator validator = getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( SuperClass.class ); assertEquals( beanDescriptor.getElementClass(), SuperClass.class, "Wrong element class" ); ElementDescriptor elementDescriptor = beanDescriptor.getConstraintsForProperty( "myField" ); assertEquals( elementDescriptor.getElementClass(), String.class, "Wrong element class" ); }
@Test @SpecAssertions({ @SpecAssertion(section = "7.1.1.2", id = "a"), @SpecAssertion(section = "7.1.1.2", id = "c") }) public void testIgnoreAnnotations() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertNotNull( beanDescriptor ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstname" ); assertNull( propDescriptor, "The annotation defined constraints should be ignored." ); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_FIELDLEVELOVERRIDING, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_FIELDLEVELOVERRIDING, id = "c") }) public void testIgnoreAnnotations() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertNotNull( beanDescriptor ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstname" ); assertNull( propDescriptor, "The annotation defined constraints should be ignored." ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_VALIDATOR, id = "a") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_BEANDESCRIPTOR, id = "b") public void testGetConstraintsForNonExistingProperty() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( Order.class ); assertNull( beanDescriptor.getConstraintsForProperty( "foobar" ), "There should be no descriptor" ); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_PROPERTYLEVELOVERRIDING, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_PROPERTYLEVELOVERRIDING, id = "c") }) public void testIgnoreAnnotations() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertNotNull( beanDescriptor ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstname" ); assertNull( propDescriptor, "The annotation defined constraints should be ignored." ); }
@Test @SpecAssertions({ @SpecAssertion(section = "5.1", id = "b"), @SpecAssertion(section = "5.3", id = "b") }) public void testGetConstraintsForNonExistingProperty() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class ); assertNull( beanDescriptor.getConstraintsForProperty( "foobar" ), "There should be no descriptor" ); }
@Test @SpecAssertion(section = "2.3", id = "h") public void testAttributesDefinedOnComposingConstraints() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor descriptor = validator.getConstraintsForClass( FrenchAddress.class ); Set<ConstraintDescriptor<?>> constraintDescriptors = descriptor.getConstraintsForProperty( "zipCode" ) .getConstraintDescriptors(); boolean findPattern = checkForAppropriateAnnotation( constraintDescriptors ); assertTrue( findPattern, "Could not find @Pattern in composing constraints" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTCOMPOSITION, id = "m") public void testAttributesDefinedOnComposingConstraints() { BeanDescriptor descriptor = getValidator().getConstraintsForClass( FrenchAddress.class ); Set<ConstraintDescriptor<?>> constraintDescriptors = descriptor.getConstraintsForProperty( "zipCode" ) .getConstraintDescriptors(); boolean findPattern = checkForAppropriateAnnotation( constraintDescriptors ); assertTrue( findPattern, "Could not find @Pattern in composing constraints" ); }
@Test @SpecAssertion(section = "5.2", id = "d") public void testUnorderedAndMatchingGroupsWithDefaultGroupOverriding() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( SubClass.class ); assertNotNull( beanDescriptor ); Set<ConstraintDescriptor<?>> descriptors = beanDescriptor.getConstraintsForProperty( "myField" ) .findConstraints() .unorderedAndMatchingGroups( Default.class ) .getConstraintDescriptors(); assertTrue( descriptors.size() == 1 ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_PROPERTYLEVELOVERRIDING, id = "f") }) public void testGroupConversionsAreAdditiveAndExceptionIsThrownForMultipleConversionWithSameSource() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstCreditCard" ); propDescriptor.getGroupConversions(); } }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_FIELDLEVELOVERRIDING, id = "f") }) public void testGroupConversionsAreAdditiveAndExceptionIsThrownForMultipleConversionWithSameSource() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstCreditCard" ); propDescriptor.getGroupConversions(); } }
private boolean isGetterConstrained(Method method, BeanDescriptor beanDescriptor) { String propertyName = ReflectionHelper.getPropertyName( method ); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( propertyName ); return propertyDescriptor != null && propertyDescriptor.findConstraints() .declaredOn( ElementType.METHOD ) .hasConstraints(); }
public boolean isRequired(UIComponent component) { ELContext elContext = DefaultComponentInfo.this.context.getELContext(); ValueExpression expression = component.getValueExpression("value"); Property property = ELUtils.getProperty(expression, elContext); if (property == null) { return false; } BeanDescriptor beanConstraints = this.validator.getConstraintsForClass(property.getObjectType()); PropertyDescriptor propertyConstraints = beanConstraints.getConstraintsForProperty(property.getName()); return isRequired(propertyConstraints.getConstraintDescriptors()); }