Author: hardy.ferentschik
Date: 2010-05-25 05:50:12 -0400 (Tue, 25 May 2010)
New Revision: 19598
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
Log:
HV-274 Allow for default group sequences
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-24
19:18:21 UTC (rev 19597)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java 2010-05-25
09:50:12 UTC (rev 19598)
@@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -26,17 +27,21 @@
import java.util.Set;
/**
+ * Top level class for programmatically configured constraints.
+ *
* @author Hardy Ferentschik
*/
public class ConstraintMapping {
private final Map<Class<?>, List<ConstraintDefinition<?>>>
constraintConfig;
private final Map<Class<?>, List<CascadeDefinition>> cascadeConfig;
private final Set<Class<?>> configuredClasses;
+ private final Map<Class<?>, List<Class<?>>>
defaultGroupSequences;
public ConstraintMapping() {
- constraintConfig = new HashMap<Class<?>,
List<ConstraintDefinition<?>>>();
- cascadeConfig = new HashMap<Class<?>, List<CascadeDefinition>>();
- configuredClasses = new HashSet<Class<?>>();
+ this.constraintConfig = new HashMap<Class<?>,
List<ConstraintDefinition<?>>>();
+ this.cascadeConfig = new HashMap<Class<?>,
List<CascadeDefinition>>();
+ this.configuredClasses = new HashSet<Class<?>>();
+ this.defaultGroupSequences = new HashMap<Class<?>,
List<Class<?>>>();
}
public ConstraintsForType type(Class<?> beanClass) {
@@ -69,6 +74,10 @@
}
}
+ protected void addDefaultGroupSequence(Class<?> beanClass,
List<Class<?>> defaultGroupSequence) {
+ defaultGroupSequences.put( beanClass, defaultGroupSequence );
+ }
+
public Map<Class<?>, List<ConstraintDefinition<?>>>
getConstraintConfig() {
return constraintConfig;
}
@@ -81,12 +90,23 @@
return configuredClasses;
}
+ public List<Class<?>> getDefaultSequence(Class<?> beanType) {
+ if ( defaultGroupSequences.containsKey( beanType ) ) {
+ return defaultGroupSequences.get( beanType );
+ }
+ else {
+ return Collections.emptyList();
+ }
+ }
+
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "ConstraintMapping" );
sb.append( "{cascadeConfig=" ).append( cascadeConfig );
sb.append( ", constraintConfig=" ).append( constraintConfig );
+ sb.append( ", configuredClasses=" ).append( configuredClasses );
+ sb.append( ", defaultGroupSequences=" ).append( defaultGroupSequences );
sb.append( '}' );
return sb.toString();
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java 2010-05-24
19:18:21 UTC (rev 19597)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintsForType.java 2010-05-25
09:50:12 UTC (rev 19598)
@@ -20,6 +20,7 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.reflect.Constructor;
+import java.util.Arrays;
import org.hibernate.validator.util.ReflectionHelper;
@@ -64,11 +65,17 @@
}
public ConstraintsForType valid(String property, ElementType type) {
- mapping.addCascadeConfig(new CascadeDefinition( beanClass, property, type));
+ mapping.addCascadeConfig( new CascadeDefinition( beanClass, property, type ) );
return this;
}
- public ConstraintsForType type(Class<?> type) {
+ public ConstraintsForType defaultGroupSequence(Class<?>... defaultGroupSequence)
{
+ mapping.addDefaultGroupSequence( beanClass, Arrays.asList( defaultGroupSequence ) );
+ return this;
+ }
+
+ public ConstraintsForType type(Class<?> type, Class<?>...
defaultGroupSequence) {
+ mapping.addDefaultGroupSequence( type, Arrays.asList( defaultGroupSequence ) );
return new ConstraintsForType( type, mapping );
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-24
19:18:21 UTC (rev 19597)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ValidatorFactoryImpl.java 2010-05-25
09:50:12 UTC (rev 19598)
@@ -154,11 +154,10 @@
}
}
- // TODO handle redefinition of default group
BeanMetaDataImpl<T> metaData = new BeanMetaDataImpl<T>(
beanClass,
constraintHelper,
- new ArrayList<Class<?>>(),
+ mapping.getDefaultSequence( beanClass ),
constraints,
cascadedMembers,
new AnnotationIgnores(),
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-24
19:18:21 UTC (rev 19597)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java 2010-05-25
09:50:12 UTC (rev 19598)
@@ -34,6 +34,7 @@
import org.hibernate.validator.cfg.ConstraintMapping;
import org.hibernate.validator.cfg.FutureDefinition;
import org.hibernate.validator.cfg.MinDefinition;
+import org.hibernate.validator.cfg.NotEmptyDefinition;
import org.hibernate.validator.cfg.NotNullDefinition;
import org.hibernate.validator.test.util.TestUtil;
import org.hibernate.validator.util.LoggerFactory;
@@ -162,6 +163,38 @@
log.debug( e.toString() );
}
}
+
+ @Test
+ public void testDefaultGroupSequence() {
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
+
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( Marathon.class )
+ .defaultGroupSequence( Foo.class, Marathon.class )
+ .property( "name", METHOD )
+ .constraint( NotNullDefinition.class ).groups( Foo.class )
+ .property( "runners", METHOD )
+ .constraint( NotEmptyDefinition.class );
+
+ config.addMapping( mapping );
+
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ Marathon marathon = new Marathon();
+
+ Set<ConstraintViolation<Marathon>> violations = validator.validate(
marathon );
+ assertNumberOfViolations( violations, 1 );
+ assertConstraintViolation( violations.iterator().next(), "may not be null"
);
+
+ marathon.setName( "Stockholm Marathon" );
+ violations = validator.validate( marathon );
+ assertNumberOfViolations( violations, 1 );
+ assertConstraintViolation( violations.iterator().next(), "may not be empty"
);
+ }
+
+ public interface Foo {
+ }
}