[hibernate-commits] Hibernate SVN: r17744 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validator/util and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Oct 14 10:38:57 EDT 2009


Author: hardy.ferentschik
Date: 2009-10-14 10:38:57 -0400 (Wed, 14 Oct 2009)
New Revision: 17744

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ClassValidatorWithTypeVariableTest.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
Log:
HV-250 Added a check for TypeVariable in ReflectionHelper.typeOf

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java	2009-10-14 11:47:08 UTC (rev 17743)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/PathImpl.java	2009-10-14 14:38:57 UTC (rev 17744)
@@ -17,12 +17,12 @@
 */
 package org.hibernate.validator.engine;
 
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.io.Serializable;
 import javax.validation.Path;
 
 /**
@@ -30,12 +30,14 @@
  */
 public class PathImpl implements Path, Serializable {
 
+	private static final long serialVersionUID = 7564511574909882392L;
+
 	/**
 	 * Regular expression used to split a string path into its elements.
 	 *
 	 * @see <a href="http://www.regexplanet.com/simple/index.jsp">Regular expression tester</a>
 	 */
-	private static final Pattern pathPattern = Pattern.compile( "(\\w+)(\\[(\\w+)\\])?(\\.(.*))*" );
+	private static final Pattern pathPattern = Pattern.compile( "(\\w+)(\\[(\\w*)\\])?(\\.(.*))*" );
 
 	private static final String PROPERTY_PATH_SEPERATOR = ".";
 
@@ -192,10 +194,13 @@
 			Matcher matcher = pathPattern.matcher( tmp );
 			if ( matcher.matches() ) {
 				String value = matcher.group( 1 );
+				String indexed = matcher.group( 2 );
 				String index = matcher.group( 3 );
 				NodeImpl node = new NodeImpl( value );
-				if ( index != null ) {
+				if ( indexed != null ) {
 					node.setInIterable( true );
+				}
+				if ( index != null && index.length() > 0 ) {
 					try {
 						Integer i = Integer.parseInt( index );
 						node.setIndex( i );
@@ -213,4 +218,5 @@
 		} while ( tmp != null );
 		return path;
 	}
+
 }

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java	2009-10-14 11:47:08 UTC (rev 17743)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java	2009-10-14 14:38:57 UTC (rev 17744)
@@ -27,6 +27,7 @@
 import java.lang.reflect.Modifier;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
 import java.lang.reflect.WildcardType;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -136,13 +137,20 @@
 	 * @throws IllegalArgumentException in case <code>member</code> is not a <code>Field</code> or <code>Method</code>.
 	 */
 	public static Type typeOf(Member member) {
+		Type type;
 		if ( member instanceof Field ) {
-			return ( ( Field ) member ).getGenericType();
+			type = ( ( Field ) member ).getGenericType();
 		}
-		if ( member instanceof Method ) {
-			return ( ( Method ) member ).getGenericReturnType();
+		else if ( member instanceof Method ) {
+			type = ( ( Method ) member ).getGenericReturnType();
 		}
-		throw new IllegalArgumentException( "Member " + member + " is neither a field nor a method" );
+		else {
+			throw new IllegalArgumentException( "Member " + member + " is neither a field nor a method" );
+		}
+		if ( type instanceof TypeVariable ) {
+			type = TypeUtils.getErasedType( type );
+		}
+		return type;
 	}
 
 

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ClassValidatorWithTypeVariableTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ClassValidatorWithTypeVariableTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ClassValidatorWithTypeVariableTest.java	2009-10-14 14:38:57 UTC (rev 17744)
@@ -0,0 +1,127 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat, Inc. and/or its affiliates, 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.validator.constraints;
+
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Valid;
+import javax.validation.Validator;
+import javax.validation.constraints.NotNull;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.util.TestUtil;
+import static org.hibernate.validator.util.TestUtil.assertCorrectConstraintTypes;
+import static org.hibernate.validator.util.TestUtil.assertCorrectPropertyPaths;
+import static org.hibernate.validator.util.TestUtil.assertNumberOfViolations;
+
+/**
+ * HV-250
+ */
+public class ClassValidatorWithTypeVariableTest {
+
+	private Validator validator;
+
+	@BeforeClass
+	public void setUp() {
+		validator = TestUtil.getValidator();
+	}
+
+	@Test
+	public void offersNull() {
+		Batch batch = new Batch( null );
+
+		Set<ConstraintViolation<Batch>> violations = validator.validate( batch );
+		assertNumberOfViolations( violations, 1 );
+		assertCorrectPropertyPaths( violations, "offers" );
+		assertCorrectConstraintTypes( violations, NotNull.class );
+	}
+
+	@Test
+	public void offerItemNull() {
+		ItemAOffer offer = new ItemAOffer( null );
+		Set<ItemOffer<? extends Item>> offers = new HashSet<ItemOffer<? extends Item>>();
+		offers.add( offer );
+		Batch batch = new Batch( offers );
+
+		Set<ConstraintViolation<Batch>> violations = validator.validate( batch );
+		assertNumberOfViolations( violations, 1 );
+		assertCorrectPropertyPaths( violations, "offers[].item" );
+		assertCorrectConstraintTypes( violations, NotNull.class );
+	}
+
+	@Test
+	public void offerItemDateNull() {
+		ItemA item = new ItemA( null );
+		ItemOffer<? extends Item> offer = new ItemAOffer( item );
+		Set<ItemOffer<? extends Item>> offers = new HashSet<ItemOffer<? extends Item>>();
+		offers.add( offer );
+		Batch batch = new Batch( offers );
+
+		Set<ConstraintViolation<Batch>> violations = validator.validate( batch );
+		assertNumberOfViolations( violations, 1 );
+		assertCorrectPropertyPaths( violations, "offers[].item.date" );
+		assertCorrectConstraintTypes( violations, NotNull.class );
+	}
+
+	private class Batch {
+		@NotNull
+		@Valid
+		private Set<ItemOffer<? extends Item>> offers = new HashSet<ItemOffer<? extends Item>>();
+
+		public Batch(Set<ItemOffer<? extends Item>> offers) {
+			this.offers = offers;
+		}
+	}
+
+	private abstract class Item {
+		@NotNull
+		private Date date;
+
+		public Item(Date date) {
+			this.date = date;
+		}
+	}
+
+	private abstract class ItemOffer<T extends Item> {
+		@NotNull
+		@Valid
+		private T item;
+
+		public ItemOffer(T item) {
+			this.item = item;
+		}
+	}
+
+	private class ItemA extends Item {
+		public ItemA(Date date) {
+			super( date );
+		}
+	}
+
+	private class ItemAOffer extends ItemOffer<ItemA> {
+		public ItemAOffer(ItemA item) {
+			super( item );
+		}
+	}
+}
+
+


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ClassValidatorWithTypeVariableTest.java
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the hibernate-commits mailing list