@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPINHERITANCE, id = "b") public void testGroupMembership() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor descriptor = validator.getConstraintsForClass( MiniaturePart.class ); // PreManufacturing belongs implicitly to All PropertyDescriptor propertyDescriptor = descriptor.getConstraintsForProperty( "partNumber" ); Set<ConstraintDescriptor<?>> descriptorsForGroup = propertyDescriptor.findConstraints() .unorderedAndMatchingGroups( All.class ) .getConstraintDescriptors(); assertEquals( descriptorsForGroup.size(), 1, "Wrong number of descriptors" ); assertEquals( descriptorsForGroup.iterator().next().getAnnotation().annotationType(), Digits.class ); // PostManufacturing belongs implicitly to All propertyDescriptor = descriptor.getConstraintsForProperty( "qaChecked" ); descriptorsForGroup = propertyDescriptor.findConstraints() .unorderedAndMatchingGroups( All.class ) .getConstraintDescriptors(); assertEquals( descriptorsForGroup.size(), 1, "Wrong number of descriptors" ); assertEquals( descriptorsForGroup.iterator().next().getAnnotation().annotationType(), AssertTrue.class ); propertyDescriptor = descriptor.getConstraintsForProperty( "size" ); descriptorsForGroup = propertyDescriptor.findConstraints() .unorderedAndMatchingGroups( All.class ) .getConstraintDescriptors(); assertEquals( descriptorsForGroup.size(), 1, "Wrong number of descriptors" ); assertEquals( descriptorsForGroup.iterator().next().getAnnotation().annotationType(), Max.class ); }
@Test @SpecAssertion(section = "3.4.1", id = "b") public void testGroupMembership() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor descriptor = validator.getConstraintsForClass( MiniaturePart.class ); // PreManufacturing belongs implicitly to All PropertyDescriptor propertyDescriptor = descriptor.getConstraintsForProperty( "partNumber" ); Set<ConstraintDescriptor<?>> descriptorsForGroup = propertyDescriptor.findConstraints() .unorderedAndMatchingGroups( All.class ) .getConstraintDescriptors(); assertEquals( descriptorsForGroup.size(), 1, "Wrong number of descriptors" ); assertEquals( descriptorsForGroup.iterator().next().getAnnotation().annotationType(), Digits.class ); // PostManufacturing belongs implicitly to All propertyDescriptor = descriptor.getConstraintsForProperty( "qaChecked" ); descriptorsForGroup = propertyDescriptor.findConstraints() .unorderedAndMatchingGroups( All.class ) .getConstraintDescriptors(); assertEquals( descriptorsForGroup.size(), 1, "Wrong number of descriptors" ); assertEquals( descriptorsForGroup.iterator().next().getAnnotation().annotationType(), AssertTrue.class ); propertyDescriptor = descriptor.getConstraintsForProperty( "size" ); descriptorsForGroup = propertyDescriptor.findConstraints() .unorderedAndMatchingGroups( All.class ) .getConstraintDescriptors(); assertEquals( descriptorsForGroup.size(), 1, "Wrong number of descriptors" ); assertEquals( descriptorsForGroup.iterator().next().getAnnotation().annotationType(), Max.class ); }
.lookingAt(scope) .declaredOn(declaredOnTypes.toArray(new ElementType[]{})) .unorderedAndMatchingGroups(matchingGroups.toArray(new Class<?>[]{}));
property.findConstraints().unorderedAndMatchingGroups(groups != null ? groups : new Class[]{Default.class}).getConstraintDescriptors(), orderedAnnotations );
property.findConstraints().unorderedAndMatchingGroups(groups != null ? groups : new Class[]{Default.class}).getConstraintDescriptors(), orderedAnnotations );
property.findConstraints().unorderedAndMatchingGroups(groups != null ? groups : new Class[]{Default.class}).getConstraintDescriptors(), orderedAnnotations );
@SuppressWarnings("unchecked") private <T> Set<ConstraintViolation<T>> validate(Map<Class<?>,Map<String,Map<Object,Set<ConstraintViolation<?>>>>> validationCache, T object, Class<?>... groups) { //get the bean descriptor BeanDescriptor bean = this.getConstraintsForClass(object.getClass()); //create empty constraint violation set Set<ConstraintViolation<T>> violations = new HashSet<ConstraintViolation<T>>(); //validate class level properties for(ConstraintDescriptor<?> descriptor : bean.findConstraints().declaredOn(ElementType.TYPE).unorderedAndMatchingGroups(groups).getConstraintDescriptors()) { violations.addAll(this.validateConstraint((Class<T>)object.getClass(), descriptor, object)); } //validate each constrained property individually for(PropertyDescriptor property : bean.getConstrainedProperties()) { violations.addAll(this.validateProperty(validationCache, object, property.getPropertyName(), groups)); } //update root objects for all violations for(ConstraintViolation<T> violation : violations) { if(violation instanceof ConstraintViolationImpl) { ConstraintViolationImpl<T> impl = (ConstraintViolationImpl<T>)violation; impl.setRootBean(object); impl.setRootBeanClass((Class<T>)object.getClass()); } } return violations; }
Collection<ValidatorDescriptor> processBeanAttribute(FacesContext context, ValueDescriptor descriptor, Class<?>... groups) { PropertyDescriptor constraintsForProperty = getValidator(context).getConstraintsForClass(descriptor.getBeanType()).getConstraintsForProperty( descriptor.getName()); if (null != constraintsForProperty) { ConstraintFinder propertyConstraints = constraintsForProperty.findConstraints(); if (null != groups && groups.length > 0) { // Filter groups, if required propertyConstraints = propertyConstraints.unorderedAndMatchingGroups(groups); } Set<ConstraintDescriptor<?>> constraints = propertyConstraints // or the requested list of groups) .getConstraintDescriptors(); // ContextHolder is an arbitrary object, it will depend on the implementation Set<ValidatorDescriptor> descriptors = new HashSet<ValidatorDescriptor>(constraints.size()); processConstraints(context, constraints, descriptors); return descriptors; } else { return Collections.emptySet(); } }
Collection<ValidatorDescriptor> processBeanAttribute(FacesContext context, ValueDescriptor descriptor, String msg, Class<?>... groups) { PropertyDescriptor constraintsForProperty = getValidator(context).getConstraintsForClass(descriptor.getBeanType()) .getConstraintsForProperty(descriptor.getName()); if (null != constraintsForProperty) { ConstraintFinder propertyConstraints = constraintsForProperty.findConstraints(); if (null != groups && groups.length > 0) { // Filter groups, if required propertyConstraints = propertyConstraints.unorderedAndMatchingGroups(groups); } Set<ConstraintDescriptor<?>> constraints = propertyConstraints // or the requested list of groups) .getConstraintDescriptors(); // ContextHolder is an arbitrary object, it will depend on the implementation FacesMessage message = Strings.isEmpty(msg) ? null : new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg); return processConstraints(context, constraints, message); } else { return Collections.emptySet(); } }
public ConstraintIterator(Validator validator, Property property, Class<?>... groups) { BeanDescriptor beanDesc = validator.getConstraintsForClass(property.getOwner()); if (beanDesc != null) { PropertyDescriptor propDesc = beanDesc.getConstraintsForProperty(property.getName()); if (propDesc != null) { // TODO use hasConstraintDesc to optimize...? Set<ConstraintDescriptor<?>> constraints = propDesc.findConstraints() .unorderedAndMatchingGroups(groups) .getConstraintDescriptors(); if (constraints != null) { stack.add(constraints.iterator()); findNext(); } } } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "d") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "e") public void testUnorderedAndMatchingGroups() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( SubClass.class ); assertNotNull( beanDescriptor ); Set<ConstraintDescriptor<?>> descriptors = beanDescriptor.getConstraintsForProperty( "myField" ) .findConstraints() .unorderedAndMatchingGroups( Default.class, SuperClass.BasicGroup.class ) .getConstraintDescriptors(); assertTrue( descriptors.size() == 2 ); descriptors = beanDescriptor.getConstraintsForProperty( "myField" ) .findConstraints() .unorderedAndMatchingGroups( SuperClass.UnusedGroup.class ) .getConstraintDescriptors(); assertTrue( descriptors.size() == 0 ); }
Collection<ValidatorDescriptor> processBeanAttribute(FacesContext context, ValueDescriptor descriptor, String msg, Class<?>... groups) { PropertyDescriptor constraintsForProperty = getValidator(context).getConstraintsForClass(descriptor.getBeanType()) .getConstraintsForProperty(descriptor.getName()); if (null != constraintsForProperty) { ConstraintFinder propertyConstraints = constraintsForProperty.findConstraints(); if (null != groups && groups.length > 0) { // Filter groups, if required propertyConstraints = propertyConstraints.unorderedAndMatchingGroups(groups); } Set<ConstraintDescriptor<?>> constraints = propertyConstraints // or the requested list of groups) .getConstraintDescriptors(); // ContextHolder is an arbitrary object, it will depend on the implementation FacesMessage message = Strings.isNullOrEmpty(msg) ? null : new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg); return processConstraints(context, constraints, message); } else { return Collections.emptySet(); } }
public ConstraintIterator(Validator validator, Property property, Class<?>... groups) { BeanDescriptor beanDesc = validator.getConstraintsForClass(property.getOwner()); if (beanDesc != null) { PropertyDescriptor propDesc = beanDesc.getConstraintsForProperty(property.getName()); if (propDesc != null) { // TODO use hasConstraintDesc to optimize...? Set<ConstraintDescriptor<?>> constraints = propDesc.findConstraints() .unorderedAndMatchingGroups(groups) .getConstraintDescriptors(); if (constraints != null) { stack.add(constraints.iterator()); findNext(); } } } }
@Test @SpecAssertion(section = "5.2", id = "d") public void testUnorderedAndMatchingGroups() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( SubClass.class ); assertNotNull( beanDescriptor ); Set<ConstraintDescriptor<?>> descriptors = beanDescriptor.getConstraintsForProperty( "myField" ) .findConstraints() .unorderedAndMatchingGroups( Default.class, SuperClass.BasicGroup.class ) .getConstraintDescriptors(); assertTrue( descriptors.size() == 2 ); descriptors = beanDescriptor.getConstraintsForProperty( "myField" ) .findConstraints() .unorderedAndMatchingGroups( SuperClass.UnusedGroup.class ) .getConstraintDescriptors(); assertTrue( descriptors.size() == 0 ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "d") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "e") public void testUnorderedAndMatchingGroupsWithInheritance() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( SubClass.class ); assertNotNull( beanDescriptor ); Set<ConstraintDescriptor<?>> descriptors = beanDescriptor.getConstraintsForProperty( "myField" ) .findConstraints() .unorderedAndMatchingGroups( SuperClass.InheritedGroup.class ) .getConstraintDescriptors(); assertTrue( descriptors.size() == 1 ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "d") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_ELEMENTDESCRIPTOR, id = "e") 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 @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 @SpecAssertion(section = "5.2", id = "d") public void testUnorderedAndMatchingGroupsWithInheritance() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( SubClass.class ); assertNotNull( beanDescriptor ); Set<ConstraintDescriptor<?>> descriptors = beanDescriptor.getConstraintsForProperty( "myField" ) .findConstraints() .unorderedAndMatchingGroups( SuperClass.InheritedGroup.class ) .getConstraintDescriptors(); assertTrue( descriptors.size() == 1 ); }
public static Set<ConstraintDescriptor<?>> extractConstraintDescriptors(FacesContext context, RequestContext requestContext, ValueExpression ve, Class... groups) { PropertyDescriptor propertyDescriptor = extractPropertyDescriptor(context, requestContext, ve); if (propertyDescriptor != null) { return propertyDescriptor.findConstraints().unorderedAndMatchingGroups(groups).getConstraintDescriptors(); } return null; }
public static Set<ConstraintDescriptor<?>> extractConstraintDescriptors(FacesContext context, PrimeApplicationContext applicationContext, ValueExpression ve, Class... groups) { PropertyDescriptor propertyDescriptor = extractPropertyDescriptor(context, applicationContext, ve); if (propertyDescriptor != null) { return propertyDescriptor.findConstraints().unorderedAndMatchingGroups(groups).getConstraintDescriptors(); } return null; }