[hibernate-commits] Hibernate SVN: r16002 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validation/engine/group and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Feb 19 13:26:10 EST 2009


Author: hardy.ferentschik
Date: 2009-02-19 13:26:09 -0500 (Thu, 19 Feb 2009)
New Revision: 16002

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/Billable.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/BuyInOneClick.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/CreditCard.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/User.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChain.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChainGenerator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/Book.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupChainGeneratorTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupTest.java
Log:
HV-92 started to use GroupChainGenerator in ValidatorImpl. Removed expandGroup

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -61,7 +61,7 @@
 	/**
 	 * @return A map mapping defined group sequences to a list of groups.
 	 */
-	List<Class<?>> getDefaultGroupSequence();
+	List<Class<?>> getDefaultGroupList();
 
 	/**
 	 * @return A list of <code>MetaConstraint</code> instances encapsulating the information of all the constraints

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-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -84,7 +84,7 @@
 	/**
 	 * Maps group sequences to the list of group/sequences.
 	 */
-	private List<Class<?>> defaultGroupSequence = new ArrayList<Class<?>>();
+	private List<Class<?>> defaultGroupList = new ArrayList<Class<?>>();
 
 	private final BuiltinConstraints builtinConstraints;
 
@@ -138,10 +138,10 @@
 	private void initDefaultGroupSequence(Class<?> clazz) {
 		GroupSequence groupSequenceAnnotation = clazz.getAnnotation( GroupSequence.class );
 		if ( groupSequenceAnnotation == null ) {
-			defaultGroupSequence.add( Default.class );
+			defaultGroupList.add( Default.class );
 		}
 		else {
-			defaultGroupSequence.addAll( Arrays.asList( groupSequenceAnnotation.value() ) );
+			defaultGroupList.addAll( Arrays.asList( groupSequenceAnnotation.value() ) );
 		}
 	}
 
@@ -150,7 +150,7 @@
 			List<ConstraintDescriptorImpl> fieldMetadata = findFieldLevelConstraints( field );
 			for ( ConstraintDescriptorImpl constraintDescription : fieldMetadata ) {
 				ReflectionHelper.setAccessibility( field );
-				MetaConstraint metaConstraint = new MetaConstraint( field, constraintDescription );
+				MetaConstraint metaConstraint = new MetaConstraint( field, constraintDescription, new ArrayList<Class<?>>(defaultGroupList) );
 				metaConstraintList.add( metaConstraint );
 			}
 			if ( field.isAnnotationPresent( Valid.class ) ) {
@@ -165,7 +165,7 @@
 			List<ConstraintDescriptorImpl> methodMetadata = findMethodLevelConstraints( method );
 			for ( ConstraintDescriptorImpl constraintDescription : methodMetadata ) {
 				ReflectionHelper.setAccessibility( method );
-				MetaConstraint metaConstraint = new MetaConstraint( method, constraintDescription );
+				MetaConstraint metaConstraint = new MetaConstraint( method, constraintDescription, new ArrayList<Class<?>>(defaultGroupList) );
 				metaConstraintList.add( metaConstraint );
 			}
 			if ( method.isAnnotationPresent( Valid.class ) ) {
@@ -178,7 +178,7 @@
 	private void initClassConstraints(Class clazz) {
 		List<ConstraintDescriptorImpl> classMetadata = findClassLevelConstraints( clazz );
 		for ( ConstraintDescriptorImpl constraintDescription : classMetadata ) {
-			MetaConstraint metaConstraint = new MetaConstraint( clazz, constraintDescription );
+			MetaConstraint metaConstraint = new MetaConstraint( clazz, constraintDescription, new ArrayList<Class<?>>(defaultGroupList) );
 			metaConstraintList.add( metaConstraint );
 		}
 	}
@@ -323,8 +323,8 @@
 		return cascadedMembers;
 	}
 
-	public List<Class<?>> getDefaultGroupSequence() {
-		return defaultGroupSequence;
+	public List<Class<?>> getDefaultGroupList() {
+		return defaultGroupList;
 	}
 
 	public List<MetaConstraint> geMetaConstraintList() {

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ExecutionContext.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -18,10 +18,10 @@
 package org.hibernate.validation.engine;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.Stack;
 import javax.validation.ConstraintValidatorFactory;
 import javax.validation.MessageInterpolator;
@@ -207,7 +207,7 @@
 		return propertyPath;
 	}
 
-	public boolean needsValidation(Set<Class<?>> groups) {
+	public boolean needsValidation(Collection<Class<?>> groups) {
 		return groups.contains( currentGroup );
 	}
 

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -21,8 +21,11 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
 import javax.validation.ConstraintDescriptor;
 import javax.validation.ValidationException;
+import javax.validation.groups.Default;
 
 import org.hibernate.validation.util.ReflectionHelper;
 
@@ -68,35 +71,57 @@
 	 */
 	private final ElementType elementType;
 
-	public MetaConstraint(Type t, ConstraintDescriptor constraintDescriptor) {
-		this( t, null, null, ElementType.FIELD, constraintDescriptor, "" );
+	private final List<Class<?>> groupList;
+
+	public MetaConstraint(Type t, ConstraintDescriptor constraintDescriptor, List<Class<?>> defaultGroupList) {
+		this( t, null, null, ElementType.FIELD, constraintDescriptor, "", defaultGroupList );
 	}
 
-	public MetaConstraint(Method m, ConstraintDescriptor constraintDescriptor) {
+	public MetaConstraint(Method m, ConstraintDescriptor constraintDescriptor, List<Class<?>> defaultGroupList) {
 		this(
 				null,
 				m,
 				null,
 				ElementType.METHOD,
 				constraintDescriptor,
-				ReflectionHelper.getPropertyName( m )
+				ReflectionHelper.getPropertyName( m ),
+				defaultGroupList
 		);
 	}
 
-	public MetaConstraint(Field f, ConstraintDescriptor constraintDescriptor) {
-		this( null, null, f, ElementType.FIELD, constraintDescriptor, f.getName() );
+	public MetaConstraint(Field f, ConstraintDescriptor constraintDescriptor, List<Class<?>> defaultGroupList) {
+		this( null, null, f, ElementType.FIELD, constraintDescriptor, f.getName(), defaultGroupList );
 	}
 
-	private MetaConstraint(Type t, Method m, Field f, ElementType elementType, ConstraintDescriptor constraintDescriptor, String property) {
+	private MetaConstraint(Type t, Method m, Field f, ElementType elementType, ConstraintDescriptor constraintDescriptor, String property, List<Class<?>> defaultGroupList) {
 		this.type = t;
 		this.method = m;
 		this.field = f;
 		this.elementType = elementType;
 		this.propertyName = property;
 		constraintTree = new ConstraintTree( constraintDescriptor );
+		this.groupList = new ArrayList<Class<?>>( constraintDescriptor.getGroups() );
+		checkIfPartOfDefaultGroup( defaultGroupList );
 	}
 
+	private void checkIfPartOfDefaultGroup(List<Class<?>> defaultGroupList) {
+		for ( Class<?> clazz : defaultGroupList ) {
+			if ( groupList.contains( clazz ) ) {
+				groupList.add( Default.class );
+				break;
+			}
+		}
+	}
+
 	/**
+	 * @return Returns the list of groups this constraint is part of. This might include the default group even when
+	 * it is not explicitly specified, but part of the redefined default group list of the hosting bean.
+	 */
+	public List<Class<?>> getGroupList() {
+		return groupList;
+	}
+
+	/**
 	 * @param o the object from which to retrieve the value.
 	 *
 	 * @return Returns the value for this constraint from the specified object. Depending on the type either the value itself

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -29,16 +29,17 @@
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import javax.validation.BeanDescriptor;
-import javax.validation.ConstraintDescriptor;
 import javax.validation.ConstraintValidatorFactory;
 import javax.validation.ConstraintViolation;
 import javax.validation.MessageInterpolator;
 import javax.validation.Validator;
 import javax.validation.groups.Default;
 
+import org.hibernate.validation.engine.group.Group;
+import org.hibernate.validation.engine.group.GroupChain;
+import org.hibernate.validation.engine.group.GroupChainGenerator;
 import org.hibernate.validation.util.PropertyIterator;
 import org.hibernate.validation.util.ReflectionHelper;
-import org.hibernate.validation.engine.group.GroupChainGenerator;
 
 /**
  * The main Bean Validation class. This is the core processing class of Hibernate Validator.
@@ -52,6 +53,7 @@
 	 * Set of classes which can be used as index in a map.
 	 */
 	private static final Set<Class<?>> VALID_MAP_INDEX_CLASSES = new HashSet<Class<?>>();
+
 	static {
 		VALID_MAP_INDEX_CLASSES.add( Integer.class );
 		VALID_MAP_INDEX_CLASSES.add( Long.class );
@@ -124,20 +126,17 @@
 			return Collections.emptyList();
 		}
 
-		List<Class<?>> expandedGroups;
-		boolean isGroupSequence;
-		for ( Class<?> group : groups ) {
-			expandedGroups = new ArrayList<Class<?>>();
-			isGroupSequence = expandGroup( context.peekValidatedObjectType(), group, expandedGroups );
+		GroupChain groupChain = groupChainGenerator.getGroupChainFor( groups );
+		while ( groupChain.hasNext() ) {
+			Group group = groupChain.next();
+			Class<?> currentSequence = group.getSequence();
+			context.setCurrentGroup( group.getGroup() );
 
-			for ( Class<?> expandedGroupName : expandedGroups ) {
-				context.setCurrentGroup( expandedGroupName );
+			validateConstraints( context );
+			validateCascadedConstraints( context );
 
-				validateConstraints( context );
-				validateCascadedConstraints( context );
-
-				if ( isGroupSequence && context.getFailingConstraints().size() > 0 ) {
-					break;
+			if ( group.partOfSequence() && context.getFailingConstraints().size() > 0 ) {
+				while ( groupChain.hasNext() && currentSequence == groupChain.next().getSequence() ) {
 				}
 			}
 		}
@@ -155,11 +154,10 @@
 		BeanMetaData<T> beanMetaData =
 				( BeanMetaData<T> ) getBeanMetaData( executionContext.peekValidatedObjectType() );
 		for ( MetaConstraint metaConstraint : beanMetaData.geMetaConstraintList() ) {
-			ConstraintDescriptor mainConstraintDescriptor = metaConstraint.getDescriptor();
 
 			executionContext.pushProperty( metaConstraint.getPropertyName() );
 
-			if ( !executionContext.needsValidation( mainConstraintDescriptor.getGroups() ) ) {
+			if ( !executionContext.needsValidation( metaConstraint.getGroupList() ) ) {
 				executionContext.popProperty();
 				continue;
 			}
@@ -277,28 +275,23 @@
 			groups = DEFAULT_GROUP_ARRAY;
 		}
 
-		List<Class<?>> expandedGroups;
-		boolean isGroupSequence;
-		for ( Class<?> group : groups ) {
-			expandedGroups = new ArrayList<Class<?>>();
-			isGroupSequence = expandGroup( beanType, group, expandedGroups );
-
-			for ( Class<?> expandedGroup : expandedGroups ) {
-
-				for ( MetaConstraint metaConstraint : metaConstraints ) {
-					if ( !metaConstraint.getDescriptor().getGroups().contains( expandedGroup ) ) {
-						continue;
-					}
-
-					ExecutionContext<T> context = new ExecutionContext<T>(
-							object, messageInterpolator, constraintValidatorFactory
-					);
-					metaConstraint.validateConstraint( object.getClass(), context );
-					failingConstraintViolations.addAll( context.getFailingConstraints() );
+		GroupChain groupChain = groupChainGenerator.getGroupChainFor( Arrays.asList( groups ) );
+		while ( groupChain.hasNext() ) {
+			Group group = groupChain.next();
+			Class<?> currentSequence = group.getSequence();
+			for ( MetaConstraint metaConstraint : metaConstraints ) {
+				if ( !metaConstraint.getGroupList().contains( group.getGroup() ) ) {
+					continue;
 				}
+				ExecutionContext<T> context = new ExecutionContext<T>(
+						object, messageInterpolator, constraintValidatorFactory
+				);
+				metaConstraint.validateConstraint( object.getClass(), context );
+				failingConstraintViolations.addAll( context.getFailingConstraints() );
+			}
 
-				if ( isGroupSequence && failingConstraintViolations.size() > 0 ) {
-					break;
+			if ( group.partOfSequence() && failingConstraintViolations.size() > 0 ) {
+				while ( groupChain.hasNext() && currentSequence == groupChain.next().getSequence() ) {
 				}
 			}
 		}
@@ -330,31 +323,27 @@
 			groups = DEFAULT_GROUP_ARRAY;
 		}
 
-		List<Class<?>> expandedGroups;
-		boolean isGroupSequence;
-		for ( Class<?> group : groups ) {
-			expandedGroups = new ArrayList<Class<?>>();
-			isGroupSequence = expandGroup( beanType, group, expandedGroups );
+		GroupChain groupChain = groupChainGenerator.getGroupChainFor( Arrays.asList( groups ) );
+		while ( groupChain.hasNext() ) {
+			Group group = groupChain.next();
+			Class<?> currentSequence = group.getSequence();
 
-			for ( Class<?> expandedGroup : expandedGroups ) {
-
-				for ( MetaConstraint metaConstraint : metaConstraints ) {
-					if ( !metaConstraint.getDescriptor().getGroups().contains( expandedGroup ) ) {
-						continue;
-					}
-
-					ExecutionContext<T> context = new ExecutionContext<T>(
-							( T ) value, messageInterpolator, constraintValidatorFactory
-					);
-					context.pushProperty( propertyIter.getOriginalProperty() );
-					metaConstraint.validateConstraint( beanType, value, context );
-					failingConstraintViolations.addAll( context.getFailingConstraints() );
+			for ( MetaConstraint metaConstraint : metaConstraints ) {
+				if ( !metaConstraint.getGroupList().contains( group.getGroup() ) ) {
+					continue;
 				}
 
-				if ( isGroupSequence && failingConstraintViolations.size() > 0 ) {
-					break;
-				}
+				ExecutionContext<T> context = new ExecutionContext<T>(
+						( T ) value, messageInterpolator, constraintValidatorFactory
+				);
+				context.pushProperty( propertyIter.getOriginalProperty() );
+				metaConstraint.validateConstraint( beanType, value, context );
+				failingConstraintViolations.addAll( context.getFailingConstraints() );
 			}
+
+			if ( group.partOfSequence() && failingConstraintViolations.size() > 0 ) {
+				break;
+			}
 		}
 	}
 
@@ -402,35 +391,6 @@
 	}
 
 	/**
-	 * Checks whether the provided group name is a group sequence and if so expands the group name and add the expanded
-	 * groups names to <code>expandedGroupName</code>.
-	 *
-	 * @param beanType The class for which to expand the group names.
-	 * @param group The group to expand
-	 * @param expandedGroups The exanded group names or just a list with the single provided group name id the name
-	 * was not expandable
-	 *
-	 * @return <code>true</code> if an expansion took place, <code>false</code> otherwise.
-	 */
-	private <T> boolean expandGroup(Class<T> beanType, Class<?> group, List<Class<?>> expandedGroups) {
-		if ( expandedGroups == null ) {
-			throw new IllegalArgumentException( "List cannot be empty" );
-		}
-
-		boolean isGroupSequence;
-		BeanMetaData<T> metaDataProvider = getBeanMetaData( beanType );
-		if ( Default.class.getName().equals( group.getName() ) ) {
-			expandedGroups.addAll( metaDataProvider.getDefaultGroupSequence() );
-			isGroupSequence = true;
-		}
-		else {
-			expandedGroups.add( group );
-			isGroupSequence = false;
-		}
-		return isGroupSequence;
-	}
-
-	/**
 	 * {@inheritDoc}
 	 */
 	private <T> BeanMetaDataImpl<T> getBeanMetaData(Class<T> beanClass) {

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChain.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChain.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChain.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
 /*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -36,7 +36,7 @@
 	 * @return Returns <code>true</code> if there is another group in the chain <code>false</code> otherwise.
 	 */
 	public boolean hasNext() {
-		return nextGroupPointer <= groupList.size();
+		return nextGroupPointer < groupList.size();
 	}
 
 	/**

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChainGenerator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChainGenerator.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/group/GroupChainGenerator.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -18,10 +18,10 @@
 package org.hibernate.validation.engine.group;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import javax.validation.GroupSequence;
 import javax.validation.ValidationException;
 
@@ -34,7 +34,7 @@
 
 	private final Map<Class<?>, List<Group>> resolvedSequences = new HashMap<Class<?>, List<Group>>();
 
-	public GroupChain getGroupChainFor(Set<Class<?>> groups) {
+	public GroupChain getGroupChainFor(Collection<Class<?>> groups) {
 		if ( groups == null || groups.size() == 0 ) {
 			throw new IllegalArgumentException( "At least one group has to be specified." );
 		}

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/Book.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/Book.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/eg/Book.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -19,19 +19,17 @@
 
 import javax.validation.GroupSequence;
 import javax.validation.Valid;
-import javax.validation.groups.Default;
 import javax.validation.constraints.NotNull;
 
 import org.hibernate.validation.constraints.Length;
 import org.hibernate.validation.constraints.NotEmpty;
 import org.hibernate.validation.eg.groups.First;
+import org.hibernate.validation.eg.groups.Last;
 import org.hibernate.validation.eg.groups.Second;
-import org.hibernate.validation.eg.groups.Last;
 
 /**
  * @author Hardy Ferentschik
  */
- at GroupSequence(value = { First.class, Second.class, Last.class })
 public class Book {
 	@NotNull(groups = First.class)
 	@NotEmpty(groups = First.class)
@@ -67,4 +65,8 @@
 	public void setAuthor(Author author) {
 		this.author = author;
 	}
+
+	@GroupSequence(value = { First.class, Second.class, Last.class })
+	public interface All {
+	}
 }

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/Billable.java (from rev 15999, beanvalidation/trunk/validation-api/src/test/java/org/hibernate/validator/spec/s3/s4/Billable.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/Billable.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/Billable.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -0,0 +1,9 @@
+package org.hibernate.validation.engine.group;
+
+/**
+ * Validation group checking whether user is billable.
+ *
+ * @author Emmanuel Bernard
+ */
+public interface Billable {
+}
\ No newline at end of file

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/BuyInOneClick.java (from rev 15999, beanvalidation/trunk/validation-api/src/test/java/org/hibernate/validator/spec/s3/s4/BuyInOneClick.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/BuyInOneClick.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/BuyInOneClick.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -0,0 +1,9 @@
+package org.hibernate.validation.engine.group;
+
+/**
+ * Customer can buy without being harrassed by the checking-out process.
+ *
+ * @author Emmanuel Bernard
+ */
+public interface BuyInOneClick {
+}
\ No newline at end of file

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/CreditCard.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/CreditCard.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/CreditCard.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -0,0 +1,34 @@
+// $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.group;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class CreditCard {
+
+	private String number;
+
+	public String getNumber() {
+		return number;
+	}
+
+	public void setNumber(String number) {
+		this.number = number;
+	}
+}

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupChainGeneratorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupChainGeneratorTest.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupChainGeneratorTest.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
 /*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -31,6 +31,8 @@
 import org.hibernate.validation.eg.groups.Last;
 import org.hibernate.validation.eg.groups.Second;
 
+import static junit.framework.Assert.assertFalse;
+
 /**
  * @author Hardy Ferentschik
  */
@@ -110,6 +112,6 @@
 		assertTrue( "Should have more groups", chain.hasNext() );
 		assertEquals( "Wrong group", Address.HighLevelCoherence.class, chain.next().getGroup() );
 
-		assertTrue( "There should be no more groups", chain.hasNext() );
+		assertFalse( "There should be no more groups", chain.hasNext() );
 	}
 }

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupTest.java	2009-02-19 14:13:19 UTC (rev 16001)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/GroupTest.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -37,7 +37,7 @@
 import org.hibernate.validation.util.TestUtil;
 
 /**
- * Tests for the implementation of <code>Validator</code>.
+ * Tests for the group and group sequence feature.
  *
  * @author Hardy Ferentschik
  */
@@ -54,7 +54,9 @@
 		book.setTitle( "" );
 		book.setAuthor( author );
 
-		Set<ConstraintViolation<Book>> constraintViolations = validator.validate( book, First.class, Second.class, Last.class );
+		Set<ConstraintViolation<Book>> constraintViolations = validator.validate(
+				book, First.class, Second.class, Last.class
+		);
 		assertEquals( "Wrong number of constraints", 3, constraintViolations.size() );
 
 		author.setFirstName( "Gavin" );
@@ -97,7 +99,7 @@
 	}
 
 	@Test
-	public void testDefaultGroupSequence() {
+	public void testGroupSequence() {
 		Validator validator = TestUtil.getValidator();
 
 		Author author = new Author();
@@ -106,13 +108,13 @@
 		Book book = new Book();
 		book.setAuthor( author );
 
-		Set<ConstraintViolation<Book>> constraintViolations = validator.validate( book, Default.class );
+		Set<ConstraintViolation<Book>> constraintViolations = validator.validate( book, Book.All.class );
 		assertEquals( "Wrong number of constraints", 2, constraintViolations.size() );
 
 		author.setFirstName( "Gavin" );
 		author.setLastName( "King" );
 
-		constraintViolations = validator.validate( book, Default.class );
+		constraintViolations = validator.validate( book, Book.All.class );
 		ConstraintViolation constraintViolation = constraintViolations.iterator().next();
 		assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
 		assertEquals( "Wrong message", "may not be null", constraintViolation.getMessage() );
@@ -123,18 +125,18 @@
 		book.setTitle( "Hibernate Persistence with JPA" );
 		book.setSubtitle( "Revised Edition of Hibernate in Action" );
 
-		constraintViolations = validator.validate( book, Default.class );
+		constraintViolations = validator.validate( book, Book.All.class );
 		assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
 
 		book.setSubtitle( "Revised Edition" );
 		author.setCompany( "JBoss a divison of RedHat" );
 
-		constraintViolations = validator.validate( book, Default.class );
+		constraintViolations = validator.validate( book, Book.All.class );
 		assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
 
 		author.setCompany( "JBoss" );
 
-		constraintViolations = validator.validate( book, Default.class );
+		constraintViolations = validator.validate( book, Book.All.class );
 		assertEquals( "Wrong number of constraints", 0, constraintViolations.size() );
 	}
 
@@ -161,7 +163,9 @@
 		elepfant.setName( "" );
 		elepfant.setDomain( Animal.Domain.EUKARYOTA );
 
-		Set<ConstraintViolation<Animal>> constraintViolations = validator.validate( elepfant, First.class, Second.class );
+		Set<ConstraintViolation<Animal>> constraintViolations = validator.validate(
+				elepfant, First.class, Second.class
+		);
 		assertEquals(
 				"The should be two invalid constraints since the same propertyName gets validated in both groups",
 				1,
@@ -178,4 +182,37 @@
 				constraintViolation.getGroups()
 		);
 	}
+
+	@Test
+	public void testValidateAgainstDifferentGroups() {
+		User user = new User();
+
+		// all fields per default null. Depending on the validation group there should be  a different amount
+		// of constraint failures.
+		Validator validator = TestUtil.getValidator();
+
+		Set<ConstraintViolation<User>> constraintViolations = validator.validate( user );
+		assertEquals( "Wrong number of constraints", 2, constraintViolations.size() );
+
+		constraintViolations = validator.validate( user, Default.class );
+		assertEquals( "Wrong number of constraints", 2, constraintViolations.size() );
+
+		constraintViolations = validator.validate( user, Billable.class );
+		assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
+
+		constraintViolations = validator.validate( user, BuyInOneClick.class );
+		assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
+
+		constraintViolations = validator.validate( user, BuyInOneClick.class, Billable.class );
+		assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
+
+		constraintViolations = validator.validate( user, BuyInOneClick.class, Default.class );
+		assertEquals( "Wrong number of constraints", 3, constraintViolations.size() );
+
+		constraintViolations = validator.validate( user, BuyInOneClick.class, Default.class, Billable.class );
+		assertEquals( "Wrong number of constraints", 3, constraintViolations.size() );
+
+		constraintViolations = validator.validate( user, BuyInOneClick.class, BuyInOneClick.class );
+		assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
+	}
 }
\ No newline at end of file

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/User.java (from rev 15999, beanvalidation/trunk/validation-api/src/test/java/org/hibernate/validator/spec/s3/s4/User.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/User.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/group/User.java	2009-02-19 18:26:09 UTC (rev 16002)
@@ -0,0 +1,43 @@
+package org.hibernate.validation.engine.group;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
+
+/**
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class User {
+	@NotNull
+	private String firstname;
+
+	@NotNull(groups = Default.class)
+	private String lastname;
+
+	@NotNull(groups = { Billable.class, BuyInOneClick.class })
+	private CreditCard defaultCreditCard;
+
+	public String getFirstname() {
+		return firstname;
+	}
+
+	public void setFirstname(String firstname) {
+		this.firstname = firstname;
+	}
+
+	public String getLastname() {
+		return lastname;
+	}
+
+	public void setLastname(String lastname) {
+		this.lastname = lastname;
+	}
+
+	public CreditCard getDefaultCreditCard() {
+		return defaultCreditCard;
+	}
+
+	public void setDefaultCreditCard(CreditCard defaultCreditCard) {
+		this.defaultCreditCard = defaultCreditCard;
+	}
+}
\ No newline at end of file




More information about the hibernate-commits mailing list