public static Set<ConstraintDescriptor<?>> extractAllConstraintDescriptors(FacesContext context, PrimeApplicationContext applicationContext, ValueExpression ve) { PropertyDescriptor propertyDescriptor = extractPropertyDescriptor(context, applicationContext, ve); if (propertyDescriptor != null) { return propertyDescriptor.getConstraintDescriptors(); } return null; }
if ( property != null ) { hasNotNull = applyConstraints( propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull, dialect ); if ( property.isComposite() && propertyDesc.isCascaded() ) {
@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 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); } }
private static void applyDDL(String prefix, PersistentClass persistentClass, Class<?> clazz, ValidatorFactory factory, Set<Class<?>> groups, boolean activateNotNull) { final BeanDescriptor descriptor = factory.getValidator().getConstraintsForClass( clazz ); //no bean level constraints can be applied, go to the properties for ( PropertyDescriptor propertyDesc : descriptor.getConstrainedProperties() ) { Property property = findPropertyByName( persistentClass, prefix + propertyDesc.getPropertyName() ); boolean hasNotNull = false; if ( property != null ) { hasNotNull = applyConstraints( propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull ); if ( property.isComposite() && propertyDesc.isCascaded() ) { Class<?> componentClass = ( ( Component ) property.getValue() ).getComponentClass(); /* * we can apply not null if the upper component let's us activate not null * and if the property is not null. * Otherwise, all sub columns should be left nullable */ final boolean canSetNotNullOnColumns = activateNotNull && hasNotNull; applyDDL( prefix + propertyDesc.getPropertyName() + ".", persistentClass, componentClass, factory, groups, canSetNotNullOnColumns ); } //FIXME add collection of components } } }
public static Set<ConstraintDescriptor<?>> extractAllConstraintDescriptors(FacesContext context, RequestContext requestContext, ValueExpression ve) { PropertyDescriptor propertyDescriptor = extractPropertyDescriptor(context, requestContext, ve); if (propertyDescriptor != null) { return propertyDescriptor.getConstraintDescriptors(); } return null; }
@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 @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE, 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 @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTDEFINITION_PROPERTIES_GROUPS, id = "d") public void testDefaultGroupAssumedWhenNoGroupsSpecified() { Validator validator = TestUtil.getValidatorUnderTest(); ConstraintDescriptor<?> descriptor = validator.getConstraintsForClass( Person.class ) .getConstraintsForProperty( "firstName" ) .getConstraintDescriptors() .iterator() .next(); Set<Class<?>> groups = descriptor.getGroups(); assertEquals( groups.size(), 1, "The group set should only contain one entry." ); assertEquals( groups.iterator().next(), Default.class, "The Default group should be returned." ); } }
@Test @SpecAssertion(section = "2.1.1", id = "f") public void testDefaultGroupAssumedWhenNoGroupsSpecified() { Validator validator = TestUtil.getValidatorUnderTest(); ConstraintDescriptor<?> descriptor = validator.getConstraintsForClass( Person.class ) .getConstraintsForProperty( "firstName" ) .getConstraintDescriptors() .iterator() .next(); Set<Class<?>> groups = descriptor.getGroups(); assertEquals( groups.size(), 1, "The group set should only contain one entry." ); assertEquals( groups.iterator().next(), Default.class, "The Default group should be returned." ); } }
private ConstraintDescriptor<?> getDescriptorFor(Class<?> clazz, String propertyName) { Validator validator = getValidatorUnderTest(); return validator.getConstraintsForClass( clazz ) .getConstraintsForProperty( propertyName ) .getConstraintDescriptors() .iterator() .next(); }
private ConstraintDescriptor<?> getDescriptorFor(Class<?> clazz, String propertyName) { Validator validator = getValidatorUnderTest(); return validator.getConstraintsForClass( clazz ) .getConstraintsForProperty( propertyName ) .getConstraintDescriptors() .iterator() .next(); }
@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 = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_INHERITANCE, id = "a") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_INHERITANCE, id = "c") public void testConstraintsOnSuperAndSubClassAddUp() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( Bar.class ); String propertyName = "lastName"; assertTrue( beanDescriptor.getConstraintsForProperty( propertyName ) != null ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( propertyName ); List<Class<? extends Annotation>> constraintTypes = getConstraintTypes( propDescriptor.getConstraintDescriptors() ); assertEquals( constraintTypes.size(), 2 ); assertTrue( constraintTypes.contains( DecimalMin.class ) ); assertTrue( constraintTypes.contains( Size.class ) ); }
@Test @SpecAssertion(section = "3.3", id = "b") public void testConstraintsOnSuperClassAreInherited() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Bar.class ); String propertyName = "foo"; assertTrue( beanDescriptor.getConstraintsForProperty( propertyName ) != null ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( propertyName ); // cast is required for JDK 5 - at least on Mac OS X Annotation constraintAnnotation = ( Annotation ) propDescriptor.getConstraintDescriptors() .iterator() .next().getAnnotation(); assertTrue( constraintAnnotation.annotationType() == NotNull.class ); }
@SuppressWarnings({ "rawtypes", "unchecked" }) private <T> void validatePropertyConstraints( final MinijaxConstraintValidatorContext<T> context, final PropertyDescriptor property, final Object value) { for (final ConstraintDescriptor constraint : property.getConstraintDescriptors()) { final ConstraintValidator validator = ((MinijaxConstraintDescriptor) constraint).getValidator(); if (!validator.isValid(value, context)) { context.buildViolation(constraint, value); } } }
@Test @SpecAssertions({ @SpecAssertion(section = "5.1", id = "b"), @SpecAssertion(section = "5.3", id = "b") }) public void testGetConstraintForConstrainedProperty() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class ); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderNumber" ); assertEquals( propertyDescriptor.getConstraintDescriptors().size(), 1, "There should be one constraint 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" ); }
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()); }