@Override public Set<ConstraintDescriptor<?>> getComposingConstraints() { return wrapped.getComposingConstraints(); }
descriptor.getComposingConstraints(), property, propertyDesc, null, canApplyNotNull,
private static boolean applyConstraints(Set<ConstraintDescriptor<?>> constraintDescriptors, Property property, PropertyDescriptor propertyDesc, Set<Class<?>> groups, boolean canApplyNotNull) { boolean hasNotNull = false; for (ConstraintDescriptor<?> descriptor : constraintDescriptors) { if ( groups != null && Collections.disjoint( descriptor.getGroups(), groups) ) continue; if ( canApplyNotNull ) { hasNotNull = hasNotNull || applyNotNull( property, descriptor ); } // apply bean validation specific constraints applyDigits( property, descriptor ); applySize( property, descriptor, propertyDesc ); applyMin( property, descriptor ); applyMax( property, descriptor ); // apply hibernate validator specific constraints - we cannot import any HV specific classes though! applyLength( property, descriptor, propertyDesc ); // pass an empty set as composing constraints inherit the main constraint and thus are matching already hasNotNull = hasNotNull || applyConstraints( descriptor.getComposingConstraints(), property, propertyDesc, null, canApplyNotNull ); } return hasNotNull; }
@Override public Set<ConstraintDescriptor<?>> getComposingConstraints() { return wrapped.getComposingConstraints(); }
@Override public Set<ConstraintDescriptor<?>> getComposingConstraints() { return this.descriptor.getComposingConstraints(); }
@Override protected void recurse(ConstraintDescriptor<?> withDescriptor, ConstraintMetadata metadata) { withDescriptor.getComposingConstraints().add(this.getConstraintDescriptor(metadata)); }
private void findNext() { next = null; while (!stack.isEmpty()) { Iterator<ConstraintDescriptor<?>> top = stack.get(stack.size() - 1); if (top.hasNext()) { next = top.next(); Set<ConstraintDescriptor<?>> composing = next.getComposingConstraints(); if (!composing.isEmpty()) { stack.add(composing.iterator()); } break; } else { stack.remove(stack.size() - 1); } } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "n") public void testEmptyComposingConstraints() { ConstraintDescriptor<?> descriptor = getConstraintDescriptor( Order.class, "orderNumber" ); assertTrue( descriptor.getComposingConstraints().isEmpty() ); }
private void findNext() { next = null; while (!stack.isEmpty()) { Iterator<ConstraintDescriptor<?>> top = stack.get(stack.size() - 1); if (top.hasNext()) { next = top.next(); Set<ConstraintDescriptor<?>> composing = next.getComposingConstraints(); if (!composing.isEmpty()) { stack.add(composing.iterator()); } break; } else { stack.remove(stack.size() - 1); } } }
@Test @SpecAssertion(section = "5.5", id = "l") public void testEmptyComposingConstraints() { ConstraintDescriptor<?> descriptor = getConstraintDescriptor( Order.class, "orderNumber" ); assertTrue( descriptor.getComposingConstraints().isEmpty() ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "l") public void testGetValidationAppliesToFromComposingConstraint() { ConstraintDescriptor<?> descriptor = getConstraintDescriptor( Person.class, "age" ); Set<ConstraintDescriptor<?>> composingDescriptors = descriptor.getComposingConstraints(); assertEquals( composingDescriptors.size(), 1, "Wrong number of composing constraints" ); ConstraintTarget constraintTarget = composingDescriptors.iterator().next().getValidationAppliesTo(); assertNotNull( constraintTarget ); assertEquals( constraintTarget, ConstraintTarget.RETURN_VALUE ); }
private boolean checkForAppropriateAnnotation(Set<ConstraintDescriptor<?>> constraintDescriptors) { boolean findPattern = false; for ( ConstraintDescriptor<?> constraintDescriptor : constraintDescriptors ) { Annotation ann = constraintDescriptor.getAnnotation(); if ( Pattern.class.getName().equals( ann.annotationType().getName() ) ) { String regexp = ( (Pattern) ann ).regexp(); if ( regexp.equals( "bar" ) ) { fail( "The regular expression attributes are defined in the composing constraint." ); } findPattern = true; } findPattern |= checkForAppropriateAnnotation( constraintDescriptor.getComposingConstraints() ); } return findPattern; }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "j") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "k") public void testComposingConstraintsPayload() { ConstraintDescriptor<?> descriptor = getConstraintDescriptor( Person.class, "firstName" ); Set<ConstraintDescriptor<?>> composingDescriptors = descriptor.getComposingConstraints(); assertEquals( composingDescriptors.size(), 2, "Wrong number of composing constraints" ); for ( ConstraintDescriptor<?> desc : composingDescriptors ) { assertTrue( desc.getGroups().size() == 1 ); assertEquals( desc.getPayload().iterator().next(), Severity.Info.class, "Wrong payload" ); } }
private boolean checkForAppropriateAnnotation(Set<ConstraintDescriptor<?>> constraintDescriptors) { boolean findPattern = false; for ( ConstraintDescriptor<?> constraintDescriptor : constraintDescriptors ) { Annotation ann = constraintDescriptor.getAnnotation(); if ( Pattern.class.getName().equals( ann.annotationType().getName() ) ) { String regexp = ( ( Pattern ) ann ).regexp(); if ( regexp.equals( "bar" ) ) { fail( "The regular expression attributes are defined in the composing constraint." ); } findPattern = true; } findPattern |= checkForAppropriateAnnotation( constraintDescriptor.getComposingConstraints() ); } return findPattern; }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "h") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CONSTRAINTDESCRIPTOR, id = "n") public void testComposingConstraints() { ConstraintDescriptor<?> descriptor = getConstraintDescriptor( Person.class, "firstName" ); Set<ConstraintDescriptor<?>> composingDescriptors = descriptor.getComposingConstraints(); assertEquals( composingDescriptors.size(), 2, "Wrong number of composing constraints" ); for ( ConstraintDescriptor<?> desc : composingDescriptors ) { assertTrue( desc.getGroups().size() == 1 ); assertEquals( desc.getGroups().iterator().next(), Person.PersonValidation.class, "Wrong group" ); } }
@Test @SpecAssertions({ @SpecAssertion(section = "5.5", id = "h"), @SpecAssertion(section = "5.5", id = "i") }) public void testComposingConstraintsPayload() { ConstraintDescriptor<?> descriptor = getConstraintDescriptor( Person.class, "firstName" ); Set<ConstraintDescriptor<?>> composingDescriptors = descriptor.getComposingConstraints(); assertEquals( composingDescriptors.size(), 2, "Wrong number of composing constraints" ); for ( ConstraintDescriptor<?> desc : composingDescriptors ) { assertTrue( desc.getGroups().size() == 1 ); assertEquals( desc.getPayload().iterator().next(), Severity.Info.class, "Wrong payload" ); } }
@Test @SpecAssertions({ @SpecAssertion(section = "5.5", id = "f"), @SpecAssertion(section = "5.5", id = "l") }) public void testComposingConstraints() { ConstraintDescriptor<?> descriptor = getConstraintDescriptor( Person.class, "firstName" ); Set<ConstraintDescriptor<?>> composingDescriptors = descriptor.getComposingConstraints(); assertEquals( composingDescriptors.size(), 2, "Wrong number of composing constraints" ); for ( ConstraintDescriptor<?> desc : composingDescriptors ) { assertTrue( desc.getGroups().size() == 1 ); assertEquals( desc.getGroups().iterator().next(), Person.PersonValidation.class, "Wrong group" ); } }
private boolean isRequired(Set<ConstraintDescriptor<?>> descriptors) { for (ConstraintDescriptor<?> descriptor : descriptors) { if (ClassUtils.isAssignableValue(NotNull.class, descriptor.getAnnotation())) { return true; } if (isRequired(descriptor.getComposingConstraints())) { return true; } } return false; } }
void processConstraints(FacesContext context, Set<ConstraintDescriptor<?>> constraints, Collection<ValidatorDescriptor> descriptors) { for (ConstraintDescriptor<?> cd : constraints) { Annotation a = cd.getAnnotation(); Map<String, Object> parameters = cd.getAttributes(); // TODO if cd.isReportedAsSingleConstraint() make sure than only the root constraint raises an error message // if one or several of the composing constraints are invalid) FacesMessage message = validatorFactory.interpolateMessage(context, cd); BeanValidatorDescriptor beanValidatorDescriptor = new BeanValidatorDescriptor(a.getClass(), message); for (Map.Entry<String, Object> entry : parameters.entrySet()) { beanValidatorDescriptor.addParameter(entry.getKey(), entry.getValue()); } beanValidatorDescriptor.makeImmutable(); descriptors.add(beanValidatorDescriptor); processConstraints(context, cd.getComposingConstraints(), descriptors); // process the composing constraints } } }
/** * Create a new ConstraintDescriptorImpl instance. * * @param descriptor */ public ConstraintDescriptorImpl(ConstraintDescriptor<T> descriptor) { this(descriptor.getAnnotation(), descriptor.getGroups(), descriptor.getPayload(), descriptor .getConstraintValidatorClasses(), descriptor.getAttributes(), descriptor.getComposingConstraints(), descriptor.isReportAsSingleViolation()); }