From hibernate-commits at lists.jboss.org Wed Oct 14 10:38:57 2009
Content-Type: multipart/mixed; boundary="===============5110008901368208570=="
MIME-Version: 1.0
From: hibernate-commits at lists.jboss.org
To: hibernate-commits at lists.jboss.org
Subject: [hibernate-commits] Hibernate SVN: r17744 - in
validator/trunk/hibernate-validator/src:
main/java/org/hibernate/validator/util and 1 other directories.
Date: Wed, 14 Oct 2009 10:38:57 -0400
Message-ID: <200910141438.n9EEcvZ4027229@svn01.web.mwc.hst.phx2.redhat.com>
--===============5110008901368208570==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
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/validato=
r/constraints/ClassValidatorWithTypeVariableTest.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validato=
r/engine/PathImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validato=
r/util/ReflectionHelper.java
Log:
HV-250 Added a check for TypeVariable in ReflectionHelper.typeOf
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/v=
alidator/engine/PathImpl.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/engine/PathImpl.java 2009-10-14 11:47:08 UTC (rev 17743)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/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 =3D 7564511574909882392L;
+
/**
* Regular expression used to split a string path into its elements.
*
* @see Regular e=
xpression tester
*/
- private static final Pattern pathPattern =3D Pattern.compile( "(\\w+)(\\[=
(\\w+)\\])?(\\.(.*))*" );
+ private static final Pattern pathPattern =3D Pattern.compile( "(\\w+)(\\[=
(\\w*)\\])?(\\.(.*))*" );
=
private static final String PROPERTY_PATH_SEPERATOR =3D ".";
=
@@ -192,10 +194,13 @@
Matcher matcher =3D pathPattern.matcher( tmp );
if ( matcher.matches() ) {
String value =3D matcher.group( 1 );
+ String indexed =3D matcher.group( 2 );
String index =3D matcher.group( 3 );
NodeImpl node =3D new NodeImpl( value );
- if ( index !=3D null ) {
+ if ( indexed !=3D null ) {
node.setInIterable( true );
+ }
+ if ( index !=3D null && index.length() > 0 ) {
try {
Integer i =3D Integer.parseInt( index );
node.setIndex( i );
@@ -213,4 +218,5 @@
} while ( tmp !=3D null );
return path;
}
+
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/v=
alidator/util/ReflectionHelper.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/util/ReflectionHelper.java 2009-10-14 11:47:08 UTC (rev 17743)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validat=
or/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 member
is not a =
Field
or Method
.
*/
public static Type typeOf(Member member) {
+ Type type;
if ( member instanceof Field ) {
- return ( ( Field ) member ).getGenericType();
+ type =3D ( ( Field ) member ).getGenericType();
}
- if ( member instanceof Method ) {
- return ( ( Method ) member ).getGenericReturnType();
+ else if ( member instanceof Method ) {
+ type =3D ( ( 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 =3D TypeUtils.getErasedType( type );
+ }
+ return type;
}
=
=
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/vali=
dator/constraints/ClassValidatorWithTypeVariableTest.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validat=
or/constraints/ClassValidatorWithTypeVariableTest.java =
(rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validat=
or/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 cont=
ributors
+* 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.assertCorrectConstrain=
tTypes;
+import static org.hibernate.validator.util.TestUtil.assertCorrectPropertyP=
aths;
+import static org.hibernate.validator.util.TestUtil.assertNumberOfViolatio=
ns;
+
+/**
+ * HV-250
+ */
+public class ClassValidatorWithTypeVariableTest {
+
+ private Validator validator;
+
+ @BeforeClass
+ public void setUp() {
+ validator =3D TestUtil.getValidator();
+ }
+
+ @Test
+ public void offersNull() {
+ Batch batch =3D new Batch( null );
+
+ Set> violations =3D validator.validate( batch=
);
+ assertNumberOfViolations( violations, 1 );
+ assertCorrectPropertyPaths( violations, "offers" );
+ assertCorrectConstraintTypes( violations, NotNull.class );
+ }
+
+ @Test
+ public void offerItemNull() {
+ ItemAOffer offer =3D new ItemAOffer( null );
+ Set> offers =3D new HashSet>();
+ offers.add( offer );
+ Batch batch =3D new Batch( offers );
+
+ Set> violations =3D validator.validate( batch=
);
+ assertNumberOfViolations( violations, 1 );
+ assertCorrectPropertyPaths( violations, "offers[].item" );
+ assertCorrectConstraintTypes( violations, NotNull.class );
+ }
+
+ @Test
+ public void offerItemDateNull() {
+ ItemA item =3D new ItemA( null );
+ ItemOffer extends Item> offer =3D new ItemAOffer( item );
+ Set> offers =3D new HashSet>();
+ offers.add( offer );
+ Batch batch =3D new Batch( offers );
+
+ Set> violations =3D validator.validate( batch=
);
+ assertNumberOfViolations( violations, 1 );
+ assertCorrectPropertyPaths( violations, "offers[].item.date" );
+ assertCorrectConstraintTypes( violations, NotNull.class );
+ }
+
+ private class Batch {
+ @NotNull
+ @Valid
+ private Set> offers =3D new HashSet>();
+
+ public Batch(Set> offers) {
+ this.offers =3D offers;
+ }
+ }
+
+ private abstract class Item {
+ @NotNull
+ private Date date;
+
+ public Item(Date date) {
+ this.date =3D date;
+ }
+ }
+
+ private abstract class ItemOffer {
+ @NotNull
+ @Valid
+ private T item;
+
+ public ItemOffer(T item) {
+ this.item =3D item;
+ }
+ }
+
+ private class ItemA extends Item {
+ public ItemA(Date date) {
+ super( date );
+ }
+ }
+
+ private class ItemAOffer extends ItemOffer {
+ 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
--===============5110008901368208570==--