[hibernate-commits] Hibernate SVN: r17241 - in validator/trunk/hibernate-validator/src: test/java/org/hibernate/validation/constraints and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Aug 6 08:21:49 EDT 2009


Author: hardy.ferentschik
Date: 2009-08-06 08:21:47 -0400 (Thu, 06 Aug 2009)
New Revision: 17241

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/ConstraintValidatorContextTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Interval.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Item.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEnd.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEndImpl.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
   validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml
Log:
HV-198 - Wront constraint violation path when adding subnode error to subnode


Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java	2009-08-06 12:18:35 UTC (rev 17240)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintValidatorContextImpl.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -97,7 +97,8 @@
 		}
 
 		public NodeBuilderDefinedContext addSubNode(String name) {
-			PathImpl path = PathImpl.createNewPath( name );
+			PathImpl path = PathImpl.createShallowCopy( propertyPath );
+			path.addNode( new NodeImpl( name ) );
 			return new NodeBuilderImpl( messageTemplate, path );
 		}
 

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java	2009-08-06 12:18:35 UTC (rev 17240)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PathImpl.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -68,11 +68,11 @@
 		return path;
 	}
 
-	public static PathImpl createShallowCopy(PathImpl path) {
+	public static PathImpl createShallowCopy(Path path) {
 		return path == null ? null : new PathImpl( path );
 	}
 
-	private PathImpl(PathImpl path) {
+	private PathImpl(Path path) {
 		this.nodeList = new ArrayList<Node>();
 		for ( Object aPath : path ) {
 			nodeList.add( new NodeImpl( ( Node ) aPath ) );

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-08-06 12:18:35 UTC (rev 17240)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -322,7 +322,9 @@
 		}
 		else {
 			newPath = PathImpl.createShallowCopy( path );
-			newPath.addNode( new NodeImpl( metaConstraint.getPropertyName() ) );
+			if ( metaConstraint.getElementType() != ElementType.TYPE ) {
+				newPath.addNode( new NodeImpl( metaConstraint.getPropertyName() ) );
+			}
 		}
 
 		localExecutionContext.setPropertyPath( newPath );
@@ -678,7 +680,7 @@
 
 		final BeanMetaData<T> metaData = getBeanMetaData( clazz );
 		//use precomputed method list as ReflectionHelper#containsMember is slow
-		if ( ! metaData.isPropertyPresent( elem.getName() ) ) {
+		if ( !metaData.isPropertyPresent( elem.getName() ) ) {
 			throw new IllegalArgumentException(
 					"Invalid property path. There is no property " + elem.getName() + " in entity " + metaData.getBeanClass()
 							.getName()

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/ConstraintValidatorContextTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/ConstraintValidatorContextTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/ConstraintValidatorContextTest.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -0,0 +1,51 @@
+// $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.constraints;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.validation.util.TestUtil;
+import static org.hibernate.validation.util.TestUtil.assertCorrectPropertyPaths;
+import static org.hibernate.validation.util.TestUtil.assertNumberOfViolations;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintValidatorContextTest {
+
+	/**
+	 * HV-198
+	 */
+	@Test
+	public void testCorrectAnnotationTypeForWithReportAsSingleViolation() {
+		Validator validator = TestUtil.getValidator();
+
+		Item item = new Item();
+		item.interval = new Interval();
+		item.interval.start = 10;
+		item.interval.end = 5;
+
+		Set<ConstraintViolation<Item>> constraintViolations = validator.validate( item );
+		assertNumberOfViolations( constraintViolations, 1 );
+		assertCorrectPropertyPaths( constraintViolations, "interval.start" );
+	}
+}

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Interval.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Interval.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Interval.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -0,0 +1,28 @@
+// $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.constraints;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at StartLessThanEnd 
+class Interval
+{
+  int start;
+  int end;
+} 

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Item.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Item.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/Item.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -0,0 +1,28 @@
+// $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.constraints;
+
+import javax.validation.Valid;
+
+/**
+ * @author Hardy Ferentschik
+ */
+class Item {
+	@Valid
+	Interval interval;
+} 

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEnd.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEnd.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEnd.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -0,0 +1,41 @@
+// $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.constraints;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.ConstraintPayload;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Target(ElementType.TYPE)
+ at Retention(RetentionPolicy.RUNTIME)
+ at Constraint(validatedBy = StartLessThanEndImpl.class)
+public @interface StartLessThanEnd {
+	String message() default "x";
+
+	Class<?>[] groups() default { };
+
+	Class<? extends ConstraintPayload>[] payload() default { };
+}
+

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEndImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEndImpl.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/StartLessThanEndImpl.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -0,0 +1,39 @@
+// $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.constraints;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class StartLessThanEndImpl implements ConstraintValidator<StartLessThanEnd, Interval> {
+
+	public void initialize(StartLessThanEnd constraintAnnotation) {
+	}
+
+	public boolean isValid(Interval value, ConstraintValidatorContext c) {
+		if ( value.start > value.end ) {
+			c.disableDefaultError();
+			c.buildErrorWithMessageTemplate( c.getDefaultErrorMessageTemplate() ).addSubNode( "start" ).addError();
+			return false;
+		}
+		return true;
+	}
+}

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java	2009-08-06 12:18:35 UTC (rev 17240)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java	2009-08-06 12:21:47 UTC (rev 17241)
@@ -165,7 +165,7 @@
 				}
 			}
 			if ( !containsPath ) {
-				fail( expectedPath + " is not in the list of path instances contained in the actual constraint violations" );
+				fail( expectedPath + " is not in the list of path instances contained in the actual constraint violations: " + propertyPathsOfViolations );
 			}
 		}
 	}

Modified: validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml
===================================================================
--- validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml	2009-08-06 12:18:35 UTC (rev 17240)
+++ validator/trunk/hibernate-validator/src/test/suite/unit-tests.xml	2009-08-06 12:21:47 UTC (rev 17241)
@@ -4,6 +4,7 @@
     <test name="Unit tests">
         <packages>
             <package name="org.hibernate.validation.bootstrap"/>
+            <package name="org.hibernate.validation.constraints"/>
             <package name="org.hibernate.validation.constraints.impl"/>
             <package name="org.hibernate.validation.constraints.composition"/>
             <package name="org.hibernate.validation.engine"/>



More information about the hibernate-commits mailing list