Author: hardy.ferentschik
Date: 2009-04-08 09:21:22 -0400 (Wed, 08 Apr 2009)
New Revision: 16280
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/CreditCard.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
Log:
HV-112
Support for cascaded fields/properties in xml
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-04-08
12:48:10 UTC (rev 16279)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-04-08
13:21:22 UTC (rev 16280)
@@ -124,6 +124,10 @@
metaConstraintList.add( ( MetaConstraint<T, ?> ) metaConstraint );
}
+ public void addCascadedMember(Member member) {
+ cascadedMembers.add( member );
+ }
+
public PropertyDescriptor getPropertyDescriptor(String property) {
return propertyDescriptors.get( property );
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-08
12:48:10 UTC (rev 16279)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-08
13:21:22 UTC (rev 16280)
@@ -83,6 +83,7 @@
private AnnotationIgnores annotationIgnores;
private Map<Class<?>, List<MetaConstraint<?, ?>>>
constraintMap;
+ private List<Member> cascadedMembers = new ArrayList<Member>();
public ValidatorFactoryImpl(ConfigurationState configurationState) {
this.messageInterpolator = configurationState.getMessageInterpolator();
@@ -172,10 +173,19 @@
throw new ValidationException( beanClass.getName() + " does not contain the
fieldType " + fieldName );
}
Field field = ReflectionHelper.getField( beanClass, fieldName );
+
+ // ignore annotations
boolean ignoreFieldAnnotation = fieldType.isIgnoreAnnotations() == null ? false :
fieldType.isIgnoreAnnotations();
if ( ignoreFieldAnnotation ) {
annotationIgnores.setIgnoreAnnotationsOnMember( field );
}
+
+ // valid
+ if ( fieldType.getValid() != null ) {
+ cascadedMembers.add( field );
+ }
+
+ // constraints
for ( ConstraintType constraint : fieldType.getConstraint() ) {
MetaConstraint<?, ?> metaConstraint = createMetaConstraint(
constraint, beanClass, field, defaultPackage
@@ -186,17 +196,26 @@
}
private void parsePropertyLevelOverrides(List<GetterType> getters, Class<?>
beanClass, String defaultPackage) {
- for ( GetterType getter : getters ) {
- String getterName = getter.getName();
+ for ( GetterType getterType : getters ) {
+ String getterName = getterType.getName();
if ( !ReflectionHelper.containsMethod( beanClass, getterName ) ) {
throw new ValidationException( beanClass.getName() + " does not contain the
property " + getterName );
}
Method method = ReflectionHelper.getMethod( beanClass, getterName );
- boolean ignoreGetterAnnotation = getter.isIgnoreAnnotations() == null ? false :
getter.isIgnoreAnnotations();
+
+ // ignore annotations
+ boolean ignoreGetterAnnotation = getterType.isIgnoreAnnotations() == null ? false :
getterType.isIgnoreAnnotations();
if ( ignoreGetterAnnotation ) {
annotationIgnores.setIgnoreAnnotationsOnMember( method );
}
- for ( ConstraintType constraint : getter.getConstraint() ) {
+
+ // valid
+ if ( getterType.getValid() != null ) {
+ cascadedMembers.add( method );
+ }
+
+ // constraints
+ for ( ConstraintType constraint : getterType.getConstraint() ) {
MetaConstraint<?, ?> metaConstraint = createMetaConstraint(
constraint, beanClass, method, defaultPackage
);
@@ -459,6 +478,9 @@
for ( MetaConstraint<?, ?> metaConstraint : entry.getValue() ) {
metaData.addMetaConstraint( metaConstraint );
}
+ for ( Member m : cascadedMembers ) {
+ metaData.addCascadedMember( m );
+ }
BeanMetaDataCache.addBeanMetaData( entry.getKey(), metaData );
}
}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/CreditCard.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/CreditCard.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/CreditCard.java 2009-04-08
13:21:22 UTC (rev 16280)
@@ -0,0 +1,37 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validation.engine.xml;
+
+import javax.validation.constraints.Pattern;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class CreditCard {
+
+ @Pattern(regexp = "[0-9]+", message = "Not a credit casrd number.")
+ private String number;
+
+ public String getNumber() {
+ return number;
+ }
+
+ public void setNumber(String number) {
+ this.number = number;
+ }
+}
Property changes on:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/CreditCard.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java 2009-04-08
12:48:10 UTC (rev 16279)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/User.java 2009-04-08
13:21:22 UTC (rev 16280)
@@ -15,7 +15,9 @@
private String lastname;
- @Pattern(regexp = "[0-9 -]+", message = "A phone nmber can only contain
numbers, whitespaces and dashes.")
+ private CreditCard creditcard;
+
+ @Pattern(regexp = "[0-9 -]+", message = "A phone number can only contain
numbers, whitespaces and dashes.")
private String phoneNumber;
@NotNull(groups = Default.class)
@@ -50,4 +52,12 @@
public boolean isConsistent() {
return isConsistent;
}
+
+ public CreditCard getCreditcard() {
+ return creditcard;
+ }
+
+ public void setCreditcard(CreditCard creditcard) {
+ this.creditcard = creditcard;
+ }
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java 2009-04-08
12:48:10 UTC (rev 16279)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java 2009-04-08
13:21:22 UTC (rev 16280)
@@ -95,11 +95,33 @@
assertNumberOfViolations( constraintViolations, 1 );
TestUtil.assertConstraintViolation(
constraintViolations.iterator().next(),
- "A phone nmber can only contain numbers, whitespaces and dashes."
+ "A phone number can only contain numbers, whitespaces and dashes."
);
user.setPhoneNumber( "112" );
constraintViolations = validator.validate( user );
assertNumberOfViolations( constraintViolations, 0 );
}
+
+ @Test
+ public void testCascadingConfiguredInXml() {
+ Validator validator = getValidator();
+
+ User user = new User();
+ user.setConsistent( true );
+ CreditCard card = new CreditCard();
+ card.setNumber( "not a number" );
+ user.setCreditcard( card );
+
+ Set<ConstraintViolation<User>> constraintViolations = validator.validate(
user );
+ assertNumberOfViolations( constraintViolations, 1 );
+ TestUtil.assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ "Not a credit casrd number."
+ );
+
+ card.setNumber( "1234567890" );
+ constraintViolations = validator.validate( user );
+ assertNumberOfViolations( constraintViolations, 0 );
+ }
}
Modified:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
===================================================================
---
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-08
12:48:10 UTC (rev 16279)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-08
13:21:22 UTC (rev 16280)
@@ -39,8 +39,10 @@
</element-->
</constraint>
</field>
+ <field name="creditcard">
+ <valid/>
+ </field>
<getter name="firstname" ignore-annotations="true">
- <valid/>
<constraint annotation="javax.validation.constraints.Size">
<message>Size is limited!</message>
<groups>