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

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Mar 18 14:17:11 EDT 2010


Author: hardy.ferentschik
Date: 2010-03-18 14:17:05 -0400 (Thu, 18 Mar 2010)
New Revision: 19021

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/CustomErrorMessageTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/DummyTestClass.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValid.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValidValidator.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintViolationImpl.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/util/TestUtil.java
Log:
 HV-297 Fixed ConstraintTree imlementation to allow the main ConstraintValidator to generate a custom error message

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java	2010-03-18 16:36:48 UTC (rev 19020)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintTree.java	2010-03-18 18:17:05 UTC (rev 19021)
@@ -68,7 +68,7 @@
 		for ( ConstraintDescriptor<?> composingConstraint : descriptor.getComposingConstraints() ) {
 			composingConstraints.add( ( ConstraintDescriptorImpl<?> ) composingConstraint );
 		}
-		
+
 		children = new ArrayList<ConstraintTree<?>>( composingConstraints.size() );
 
 		for ( ConstraintDescriptorImpl<?> composingDescriptor : composingConstraints ) {
@@ -90,7 +90,7 @@
 	}
 
 	public <T, U, V> void validateConstraints(Type type, GlobalExecutionContext<T> executionContext, LocalExecutionContext<U, V> localExecutionContext, List<ConstraintViolation<T>> constraintViolations) {
-		// first validate composing constraints
+		// first validate composing constraints (recursively)
 		for ( ConstraintTree<?> tree : getChildren() ) {
 			List<ConstraintViolation<T>> tmpViolations = new ArrayList<ConstraintViolation<T>>();
 			tree.validateConstraints( type, executionContext, localExecutionContext, tmpViolations );
@@ -101,6 +101,20 @@
 				localExecutionContext.getPropertyPath(), descriptor
 		);
 
+		// check whether we have constraints violations, but we should only report the single message of the
+		// main constraint. We already have to generate the message here, since the composing constraints might
+		// not have its own ConstraintValidator.
+		// Also we want to leave it open to the final ConstraintValidator to generate a custom message. 
+		if ( constraintViolations.size() > 0 && reportAsSingleViolation() ) {
+			constraintViolations.clear();
+			final String message = ( String ) getDescriptor().getAttributes().get( "message" );
+			MessageAndPath messageAndPath = new MessageAndPath( message, localExecutionContext.getPropertyPath() );
+			ConstraintViolation<T> violation = executionContext.createConstraintViolation(
+					localExecutionContext, messageAndPath, descriptor
+			);
+			constraintViolations.add( violation );
+		}
+
 		// we could have a composing constraint which does not need its own validator.
 		if ( !descriptor.getConstraintValidatorClasses().isEmpty() ) {
 			if ( log.isTraceEnabled() ) {
@@ -123,16 +137,6 @@
 					validator
 			);
 		}
-
-		if ( reportAsSingleViolation() && constraintViolations.size() > 0 ) {
-			constraintViolations.clear();
-			final String message = ( String ) getDescriptor().getAttributes().get( "message" );
-			MessageAndPath messageAndPath = new MessageAndPath( message, localExecutionContext.getPropertyPath() );
-			ConstraintViolation<T> violation = executionContext.createConstraintViolation(
-					localExecutionContext, messageAndPath, descriptor
-			);
-			constraintViolations.add( violation );
-		}
 	}
 
 	private <T, U, V> void validateSingleConstraint(GlobalExecutionContext<T> executionContext, LocalExecutionContext<U, V> localExecutionContext, List<ConstraintViolation<T>> constraintViolations, ConstraintValidatorContextImpl constraintValidatorContext, ConstraintValidator<A, V> validator) {

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintViolationImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintViolationImpl.java	2010-03-18 16:36:48 UTC (rev 19020)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/engine/ConstraintViolationImpl.java	2010-03-18 18:17:05 UTC (rev 19021)
@@ -86,6 +86,7 @@
 
 	@Override
 	@SuppressWarnings("SimplifiableIfStatement")
+	// IMPORTANT - some behaviour of Validator depends on the correct implementation of this equals method!
 	public boolean equals(Object o) {
 		if ( this == o ) {
 			return true;

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/CustomErrorMessageTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/CustomErrorMessageTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/CustomErrorMessageTest.java	2010-03-18 18:17:05 UTC (rev 19021)
@@ -0,0 +1,47 @@
+/*
+ * $Id:$
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.engine.customerror;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import org.testng.annotations.Test;
+
+import static org.hibernate.validator.util.TestUtil.assertCorrectConstraintViolationMessages;
+import static org.hibernate.validator.util.TestUtil.getValidator;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class CustomErrorMessageTest {
+	/**
+	 * HV-297
+	 *
+	 * @throws Exception in case the test fails.
+	 */
+	@Test
+	public void testReportAsSingleViolationDoesNotInfluenceCustomError() throws Exception {
+		Validator validator = getValidator();
+		DummyTestClass dummyTestClass = new DummyTestClass();
+
+		Set<ConstraintViolation<DummyTestClass>> constraintViolations = validator.validate( dummyTestClass );
+		assertCorrectConstraintViolationMessages( constraintViolations, IsValidValidator.message );
+	}
+}
\ No newline at end of file


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

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/DummyTestClass.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/DummyTestClass.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/DummyTestClass.java	2010-03-18 18:17:05 UTC (rev 19021)
@@ -0,0 +1,27 @@
+/*
+ * $Id:$
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.engine.customerror;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at IsValid
+public class DummyTestClass {
+}
\ No newline at end of file


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

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValid.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValid.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValid.java	2010-03-18 18:17:05 UTC (rev 19021)
@@ -0,0 +1,45 @@
+/*
+ * $Id:$
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.engine.customerror;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import javax.validation.ReportAsSingleViolation;
+import javax.validation.constraints.NotNull;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at NotNull
+ at Target(TYPE)
+ at Retention(RUNTIME)
+ at Constraint(validatedBy = IsValidValidator.class)
+ at ReportAsSingleViolation
+public @interface IsValid {
+	Class<?>[] groups() default { };
+
+	String message() default "Default error message";
+
+	Class<? extends Payload>[] payload() default { };
+}
\ No newline at end of file


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

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValidValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValidValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/engine/customerror/IsValidValidator.java	2010-03-18 18:17:05 UTC (rev 19021)
@@ -0,0 +1,41 @@
+/*
+ * $Id:$
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.engine.customerror;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class IsValidValidator implements ConstraintValidator<IsValid, DummyTestClass> {
+
+	public static final String message = "Custom error message";
+
+
+	public void initialize(IsValid isValid) {
+	}
+
+	public boolean isValid(DummyTestClass dummyTestClass, ConstraintValidatorContext constraintValidatorContext) {
+		constraintValidatorContext.disableDefaultConstraintViolation();
+		constraintValidatorContext.buildConstraintViolationWithTemplate( message )
+				.addConstraintViolation();
+		return false;
+	}
+}
\ No newline at end of file


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

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/util/TestUtil.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/util/TestUtil.java	2010-03-18 16:36:48 UTC (rev 19020)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/util/TestUtil.java	2010-03-18 18:17:05 UTC (rev 19021)
@@ -118,7 +118,7 @@
 			actualMessages.add( violation.getMessage() );
 		}
 
-		assertTrue( actualMessages.size() == messages.length, "Wrong number of error messages" );
+		assertEquals( actualMessages.size(), messages.length, "Wrong number of error messages" );
 
 		for ( String expectedMessage : messages ) {
 			assertTrue(



More information about the hibernate-commits mailing list