[hibernate-commits] Hibernate SVN: r17185 - beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jul 21 10:54:46 EDT 2009


Author: hardy.ferentschik
Date: 2009-07-21 10:54:45 -0400 (Tue, 21 Jul 2009)
New Revision: 17185

Added:
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/BaseClass.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/CustomConstraint.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassA.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassB.java
Modified:
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java
Log:
ConstraintValidator resolution tests

Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/BaseClass.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/BaseClass.java	                        (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/BaseClass.java	2009-07-21 14:54:45 UTC (rev 17185)
@@ -0,0 +1,24 @@
+// $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.jsr303.tck.tests.constraints.validatorresolution;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public abstract class BaseClass {
+}

Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/CustomConstraint.java (from rev 17143, beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Ambigious.java)
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/CustomConstraint.java	                        (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/CustomConstraint.java	2009-07-21 14:54:45 UTC (rev 17185)
@@ -0,0 +1,92 @@
+// $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.jsr303.tck.tests.constraints.validatorresolution;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.ConstraintPayload;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * A test constraint which can lead to a error when trying to reslove the validator.
+ *
+ * @author Hardy Ferentschik
+ */
+ at Constraint(validatedBy = {
+		CustomConstraint.AmbigiousValidatorBaseClass.class,
+		CustomConstraint.AmbigiousValidatorForSubClassA.class,
+		CustomConstraint.AmbigiousValidatorForSubClassB.class
+})
+ at Documented
+ at Target({ METHOD, FIELD, TYPE })
+ at Retention(RUNTIME)
+public @interface CustomConstraint {
+	public abstract String message() default "my custom constraint";
+
+	public abstract Class<?>[] groups() default { };
+
+	public abstract Class<? extends ConstraintPayload>[] payload() default { };
+
+
+	public class AmbigiousValidatorBaseClass implements ConstraintValidator<CustomConstraint, BaseClass> {
+
+		public void initialize(CustomConstraint parameters) {
+		}
+
+		public boolean isValid(BaseClass baseClass, ConstraintValidatorContext constraintValidatorContext) {
+			return false;
+		}
+	}
+
+	public class AmbigiousValidatorForSubClassA implements ConstraintValidator<CustomConstraint, SubClassA> {
+		static int callCounter = 0;
+
+		public void initialize(CustomConstraint parameters) {
+		}
+
+		public boolean isValid(SubClassA subClass, ConstraintValidatorContext constraintValidatorContext) {
+			callCounter++;
+			if ( callCounter > 1 ) {
+				throw new IllegalStateException( "This method should have been only called once during the tests." );
+			}
+			return true;
+		}
+	}
+
+	public class AmbigiousValidatorForSubClassB implements ConstraintValidator<CustomConstraint, SubClassB> {
+		static int callCounter = 0;
+
+		public void initialize(CustomConstraint parameters) {
+		}
+
+		public boolean isValid(SubClassB subClass, ConstraintValidatorContext constraintValidatorContext) {
+			callCounter++;
+			if ( callCounter > 1 ) {
+				throw new IllegalStateException( "This method should have been only called once during the tests." );
+			}
+			return true;
+		}
+	}
+}
\ No newline at end of file

Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassA.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassA.java	                        (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassA.java	2009-07-21 14:54:45 UTC (rev 17185)
@@ -0,0 +1,24 @@
+// $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.jsr303.tck.tests.constraints.validatorresolution;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class SubClassA extends BaseClass {
+}
\ No newline at end of file

Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassB.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassB.java	                        (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/SubClassB.java	2009-07-21 14:54:45 UTC (rev 17185)
@@ -0,0 +1,24 @@
+// $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.jsr303.tck.tests.constraints.validatorresolution;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class SubClassB extends BaseClass {
+}
\ No newline at end of file

Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java	2009-07-21 14:12:43 UTC (rev 17184)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java	2009-07-21 14:54:45 UTC (rev 17185)
@@ -29,6 +29,7 @@
 import org.jboss.testharness.impl.packaging.Artifact;
 import org.jboss.testharness.impl.packaging.ArtifactType;
 import org.jboss.testharness.impl.packaging.Classes;
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
 import org.testng.annotations.Test;
 
@@ -43,44 +44,33 @@
  * @author Hardy Ferentschik
  */
 @Artifact(artifactType = ArtifactType.JSR303)
- at Classes({TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class})
+ at Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
 public class ValidatorResolutionTest {
 
 	@Test
-	@SpecAssertions({
-			@SpecAssertion(section = "3.5.3", id = "e"),
-			@SpecAssertion(section = "2.1", id = "d"),
-			@SpecAssertion(section = "2.4", id = "i")
-	})
-	public void testUnexpectedTypeInValidatorResolution() {
+	@SpecAssertion(section = "3.5.3", id = "b")
+	public void testFieldTypeIsTargetedType() {
 		Validator validator = TestUtil.getDefaultValidator();
+		validator.validate( new SubClassAHolder( new SubClassA() ) );
 
-		Bar bar = new Bar();
-		try {
-			validator.validate( bar );
-			fail();
-		}
-		catch ( UnexpectedTypeException e ) {
-			// success
-		}
+		assertEquals(
+				CustomConstraint.AmbigiousValidatorForSubClassA.callCounter,
+				1,
+				"The validated method of AmbigiousValidatorForSubClassA should have benn called."
+		);
 	}
 
 	@Test
-	@SpecAssertions({
-			@SpecAssertion(section = "3.5.3", id = "f"),
-			@SpecAssertion(section = "8.3", id = "b")
-	})
-	public void testAmbigiousValidatorResolution() {
+	@SpecAssertion(section = "3.5.3", id = "c")
+	public void testGetterTypeIsTargetedType() {
 		Validator validator = TestUtil.getDefaultValidator();
+		validator.validate( new SubClassBHolder( new SubClassB() ) );
 
-		Foo foo = new Foo( new SerializableBarSubclass() );
-		try {
-			validator.validate( foo );
-			fail();
-		}
-		catch ( UnexpectedTypeException e ) {
-			// success
-		}
+		assertEquals(
+				CustomConstraint.AmbigiousValidatorForSubClassB.callCounter,
+				1,
+				"The validated method of AmbigiousValidatorForSubClassA should have benn called."
+		);
 	}
 
 
@@ -88,19 +78,6 @@
 	@SpecAssertions({
 			@SpecAssertion(section = "3.5.3", id = "d")
 	})
-	public void testResolutionOfMinMaxForDifferentTypes() {
-		Validator validator = TestUtil.getDefaultValidator();
-		MinMax minMax = new MinMax( "5", 5 );
-		Set<ConstraintViolation<MinMax>> constraintViolations = validator.validate( minMax );
-		assertCorrectNumberOfViolations( constraintViolations, 2 );
-		assertCorrectPropertyPaths( constraintViolations, new String[] { "number", "numberAsString" } );
-	}
-
-	@Test
-
-	@SpecAssertions({
-			@SpecAssertion(section = "3.5.3", id = "d")
-	})
 	public void testResolutionOfMultipleSizeValidators() {
 		Validator validator = TestUtil.getDefaultValidator();
 
@@ -176,4 +153,79 @@
 		constraintViolations = validator.validate( suburb );
 		assertCorrectNumberOfViolations( constraintViolations, 0 );
 	}
+
+	@Test
+	@SpecAssertions({
+			@SpecAssertion(section = "3.5.3", id = "d")
+	})
+	public void testResolutionOfMinMaxForDifferentTypes() {
+		Validator validator = TestUtil.getDefaultValidator();
+		MinMax minMax = new MinMax( "5", 5 );
+		Set<ConstraintViolation<MinMax>> constraintViolations = validator.validate( minMax );
+		assertCorrectNumberOfViolations( constraintViolations, 2 );
+		assertCorrectPropertyPaths( constraintViolations, "number", "numberAsString" );
+	}
+
+	@Test
+	@SpecAssertions({
+			@SpecAssertion(section = "3.5.3", id = "e"),
+			@SpecAssertion(section = "2.1", id = "d"),
+			@SpecAssertion(section = "2.4", id = "i")
+	})
+	public void testUnexpectedTypeInValidatorResolution() {
+		Validator validator = TestUtil.getDefaultValidator();
+
+		Bar bar = new Bar();
+		try {
+			validator.validate( bar );
+			fail();
+		}
+		catch ( UnexpectedTypeException e ) {
+			// success
+		}
+	}
+
+	@Test
+	@SpecAssertions({
+			@SpecAssertion(section = "3.5.3", id = "f"),
+			@SpecAssertion(section = "8.3", id = "b")
+	})
+	public void testAmbigiousValidatorResolution() {
+		Validator validator = TestUtil.getDefaultValidator();
+
+		Foo foo = new Foo( new SerializableBarSubclass() );
+		try {
+			validator.validate( foo );
+			fail();
+		}
+		catch ( UnexpectedTypeException e ) {
+			// success
+		}
+	}
+
+	public class SubClassAHolder {
+		@CustomConstraint
+		private SubClassA subClass;
+
+		SubClassAHolder(SubClassA subClass) {
+			this.subClass = subClass;
+		}
+
+		public BaseClass getSubClass() {
+			return subClass;
+		}
+	}
+
+	public class SubClassBHolder {
+		private BaseClass baseClass;
+
+		public SubClassBHolder(SubClassB subClass) {
+			this.baseClass = subClass;
+		}
+
+		@CustomConstraint
+		public SubClassB getBaseClass() {
+			return ( SubClassB ) baseClass;
+		}
+	}
 }
\ No newline at end of file



More information about the hibernate-commits mailing list