propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull, dialect ); if ( property.isComposite() && propertyDesc.isCascaded() ) { Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass();
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 } } }
@Test @SpecAssertion(section = "5.4", id = "a") public void testIsCascaded() { PropertyDescriptor descriptor = getPropertyDescriptor( Customer.class, "orderList" ); assertTrue( descriptor.isCascaded(), "Should be cascaded" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "a") public void testIsNotCascaded() { PropertyDescriptor descriptor = getPropertyDescriptor( Order.class, "orderNumber" ); assertFalse( descriptor.isCascaded(), "Should not be cascaded" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "a") public void testIsCascaded() { PropertyDescriptor descriptor = getPropertyDescriptor( Customer.class, "orderList" ); assertTrue( descriptor.isCascaded(), "Should be cascaded" ); }
@Test @SpecAssertion(section = "5.4", id = "a") public void testIsNotCascaded() { PropertyDescriptor descriptor = getPropertyDescriptor( Order.class, "orderNumber" ); assertFalse( descriptor.isCascaded(), "Should not be cascaded" ); }
@Override public PropertyDescriptor getConstraintsForProperty(String name) { if(name == null) { throw new IllegalArgumentException("Property name cannot be null."); } if(!this.backingReflector.getPropertyNames().contains(name)) { return null; } PropertyDescriptor descriptor = DescriptorFactory.INSTANCE.getPropertyDescriptor(this.backingReflector, name); if(!descriptor.hasConstraints() && !descriptor.isCascaded()) { return null; } return descriptor; }
@Test @SpecAssertions({ @SpecAssertion(section = "7.1.1.3", id = "e") }) public void testCascadedConfiguration() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertNotNull( beanDescriptor ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstCreditCard" ); assertNotNull( propDescriptor ); assertTrue( propDescriptor.isCascaded(), "Cascaded validation is configured via xml." ); propDescriptor = beanDescriptor.getConstraintsForProperty( "secondCreditCard" ); assertNull( propDescriptor, "The @Valid annotation should be ignored." ); } }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_PROPERTYLEVELOVERRIDING, id = "e") }) public void testCascadedConfiguration() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertNotNull( beanDescriptor ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstCreditCard" ); assertNotNull( propDescriptor ); assertTrue( propDescriptor.isCascaded(), "Cascaded validation is configured via xml." ); propDescriptor = beanDescriptor.getConstraintsForProperty( "secondCreditCard" ); assertNull( propDescriptor, "The @Valid annotation should be ignored." ); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_FIELDLEVELOVERRIDING, id = "e") }) public void testCascadedConfiguration() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertNotNull( beanDescriptor ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstCreditCard" ); assertNotNull( propDescriptor ); assertTrue( propDescriptor.isCascaded(), "Cascaded validation is configured via xml." ); propDescriptor = beanDescriptor.getConstraintsForProperty( "secondCreditCard" ); assertNull( propDescriptor, "The @Valid annotation should be ignored." ); }
@Test @SpecAssertions({ @SpecAssertion(section = "7.1.1.2", id = "e") }) public void testCascadedConfiguration() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( User.class ); assertNotNull( beanDescriptor ); PropertyDescriptor propDescriptor = beanDescriptor.getConstraintsForProperty( "firstCreditCard" ); assertNotNull( propDescriptor ); assertTrue( propDescriptor.isCascaded(), "Cascaded validation is configured via xml." ); propDescriptor = beanDescriptor.getConstraintsForProperty( "secondCreditCard" ); assertNull( propDescriptor, "The @Valid annotation should be ignored." ); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_BEANDESCRIPTOR, id = "b") @SpecAssertion(section = Sections.CONSTRAINTMETADATA_CASCADABLEDESCRIPTOR, id = "a") public void testGetConstraintsForUnConstrainedProperty() { BeanDescriptor beanDescriptor = getValidator().getConstraintsForClass( Customer.class ); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderList" ); assertEquals( propertyDescriptor.getConstraintDescriptors().size(), 0, "There should be no constraint descriptors" ); assertTrue( propertyDescriptor.isCascaded(), "The property should be cascaded" ); }
@Override public Set<PropertyDescriptor> getConstrainedProperties() { Set<PropertyDescriptor> propertyDescriptors = new HashSet<PropertyDescriptor>(); //this was added as part of the work-around for the problems found in issue #32. this just gets us passed //one set of exceptions and not passed the entire problem. there was a null pointer exception being thrown //by one of the generated classes at this point because it's backing reflector hadn't been generated. if(this.backingReflector == null || this.backingReflector.getPropertyNames() == null) { return propertyDescriptors; } for(String propertyName : this.backingReflector.getPropertyNames()) { PropertyDescriptor descriptor = DescriptorFactory.INSTANCE.getPropertyDescriptor(this.backingReflector, propertyName); if(descriptor.hasConstraints() || descriptor.isCascaded()) { propertyDescriptors.add(descriptor); } } return propertyDescriptors; }
/** * Build a complete map of object property / list of {@link org.resthub.web.validation.ValidationConstraint} * from a given {@link javax.validation.metadata.BeanDescriptor} <tt>bd</tt> instance and a given {@link java.util.Locale} * <tt>locale</tt> */ private Map<String, List<ValidationConstraint>> getConstraints(BeanDescriptor bd, Locale locale) { Map<String, List<ValidationConstraint>> constraints = new HashMap<String, List<ValidationConstraint>>(); for (PropertyDescriptor pd : bd.getConstrainedProperties()) { // if property has defined constraints directly or delegates validation through cascading option if ((pd.getPropertyName() != null) && (pd.hasConstraints() || pd.isCascaded())) { constraints.put(pd.getPropertyName(), this.getValidationConstraints(pd, locale)); } } return constraints; }
@Test @SpecAssertions({ @SpecAssertion(section = "5.3", id = "b"), @SpecAssertion(section = "5.3", id = "b"), @SpecAssertion(section = "5.4", id = "a") }) public void testGetConstraintForUnConstrainedProperty() { Validator validator = TestUtil.getValidatorUnderTest(); BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Customer.class ); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderList" ); assertEquals( propertyDescriptor.getConstraintDescriptors().size(), 0, "There should be no constraint descriptors" ); assertTrue( propertyDescriptor.isCascaded(), "The property should be cascaded" ); }
/** * Build a list of {@link org.resthub.web.validation.ValidationConstraint} associated to a given * {@link javax.validation.metadata.PropertyDescriptor} <tt>pd</tt> instance and a given {@link java.util.Locale} * <tt>locale</tt> */ private List<ValidationConstraint> getValidationConstraints(PropertyDescriptor pd, Locale locale) { List<ValidationConstraint> validationConstraints = new ArrayList<ValidationConstraint>(); // copy any directly defined constraint into wrapper for (ConstraintDescriptor cd : pd.getConstraintDescriptors()) { ValidationConstraint validationConstraint = new ValidationConstraint(); validationConstraint.setType(this.getType(cd)); validationConstraint.setMessage(this.getMessage(cd, locale)); validationConstraint.setAttributes(this.getAttributes(cd)); validationConstraints.add(validationConstraint); } // manage cascading option by adding a custom "Valid" type and referencing underling model class name if (pd.isCascaded()) { ValidationConstraint validationConstraint = new ValidationConstraint(); validationConstraint.setType("Valid"); validationConstraint.addAttribute("model", pd.getElementClass().getCanonicalName()); validationConstraints.add(validationConstraint); } return validationConstraints; }
private BeanHelper doCreateHelper(final Class<?> clazz, final JClassType beanType, final TreeLogger logger, final GeneratorContext context) throws UnableToCompleteException { BeanHelper helper = getBean(beanType); if (helper == null) { BeanDescriptor bean; try { bean = serverSideValidator.getConstraintsForClass(clazz); } catch (final ValidationException e) { logger.log(TreeLogger.ERROR, "Unable to create a validator for " + clazz.getCanonicalName() + " because " + e.getMessage(), e); throw new UnableToCompleteException(); // NOPMD } helper = new BeanHelper(beanType, clazz, bean); cache.put(helper.getJClass(), helper); writeInterface(context, logger, helper); // now recurse on all Cascaded elements for (final PropertyDescriptor p : bean.getConstrainedProperties()) { // TODO(idol) only bother creating objects for properties that have constrains in the groups // specified in @GwtValidation, but not others if (p.isCascaded()) { doCreateHelperForProp(p, helper, logger, context); } } } return helper; }
private BeanHelper doCreateHelper(Class<?> clazz, JClassType beanType, TreeLogger logger, GeneratorContext context) throws UnableToCompleteException { BeanHelper helper = getBean(beanType); if (helper == null) { BeanDescriptor bean; try { bean = serverSideValidator.getConstraintsForClass(clazz); } catch (ValidationException e) { logger.log(TreeLogger.ERROR, "Unable to create a validator for " + clazz.getCanonicalName() + " because " + e.getMessage(), e); throw new UnableToCompleteException(); } helper = new BeanHelper(beanType, clazz, bean); cache.put(helper.getJClass(), helper); writeInterface(context, logger, helper); // now recurse on all Cascaded elements for (PropertyDescriptor p : bean.getConstrainedProperties()) { // TODO(idol) only bother creating objects for properties that have constrains in the groups // specified in @GwtValidation, but not others if (p.isCascaded()) { doCreateHelperForProp(p, helper, logger, context); } } } return helper; }
private BeanHelper doCreateHelper(Class<?> clazz, JClassType beanType, TreeLogger logger, GeneratorContext context) throws UnableToCompleteException { BeanHelper helper = getBean(beanType); if (helper == null) { BeanDescriptor bean; try { bean = serverSideValidator.getConstraintsForClass(clazz); } catch (ValidationException e) { logger.log(TreeLogger.ERROR, "Unable to create a validator for " + clazz.getCanonicalName() + " because " + e.getMessage(), e); throw new UnableToCompleteException(); } helper = new BeanHelper(beanType, clazz, bean); cache.put(helper.getJClass(), helper); writeInterface(context, logger, helper); // now recurse on all Cascaded elements for (PropertyDescriptor p : bean.getConstrainedProperties()) { // TODO(idol) only bother creating objects for properties that have constrains in the groups // specified in @GwtValidation, but not others if (p.isCascaded()) { doCreateHelperForProp(p, helper, logger, context); } } } return helper; }
propertyDesc.getConstraintDescriptors(), property, propertyDesc, groups, activateNotNull, dialect ); if ( property.isComposite() && propertyDesc.isCascaded() ) { Class<?> componentClass = ( (Component) property.getValue() ).getComponentClass();