[hibernate-commits] Hibernate SVN: r16709 - in beanvalidation/trunk/validation-tck/src/main: java/org/hibernate/jsr303/tck/util and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Jun 5 10:17:58 EDT 2009


Author: hardy.ferentschik
Date: 2009-06-05 10:17:58 -0400 (Fri, 05 Jun 2009)
New Revision: 16709

Added:
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/NotEmpty.java
Modified:
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchAddress.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcode.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcodeConstraintValidator.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
   beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
constraint composition tests

Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java	2009-06-05 14:05:44 UTC (rev 16708)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/ConstraintCompositionTest.java	2009-06-05 14:17:58 UTC (rev 16709)
@@ -17,18 +17,30 @@
 */
 package org.hibernate.jsr303.tck.tests.constraints.constraintcomposition;
 
+import java.util.Arrays;
+import java.util.List;
 import java.util.Set;
 import javax.validation.ConstraintViolation;
 import javax.validation.Validator;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
+import javax.validation.groups.Default;
 
+import org.jboss.test.audit.annotations.SpecAssertion;
+import org.jboss.test.audit.annotations.SpecAssertions;
 import org.jboss.testharness.AbstractTest;
 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.assertTrue;
 import org.testng.annotations.Test;
 
 import org.hibernate.jsr303.tck.util.TestUtil;
 import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintType;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
 import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
 
 /**
@@ -41,26 +53,54 @@
 public class ConstraintCompositionTest extends AbstractTest {
 
 	@Test
-	public void testComposition() {
+	@SpecAssertion(section = "2.3", id = "a")
+	public void testComposedConstraintsAreRecursive() {
 		Validator validator = TestUtil.getDefaultValidator();
 
-		FrenchAddress address = new FrenchAddress();
-		address.setAddressline1( "10 rue des Treuils" );
-		address.setAddressline2( "BP 12 " );
-		address.setCity( "Bordeaux" );
+		FrenchAddress address = getFrenchAddressWithoutZipCode();
 		Set<ConstraintViolation<FrenchAddress>> constraintViolations = validator.validate( address );
 		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		ConstraintViolation<FrenchAddress> constraintViolation = constraintViolations.iterator().next();
+		assertCorrectConstraintType( constraintViolation, NotNull.class );
+		assertEquals( constraintViolation.getMessage(), "may not be null", "Wrong error message" );
 		assertConstraintViolation(
-				constraintViolations.iterator().next(),
+				constraintViolation,
 				FrenchAddress.class,
 				null,
 				"zipCode"
 		);
+	}
 
+	@Test
+	@SpecAssertion(section = "2.3", id = "b")
+	public void testValidationOfMainAnnotationIsAlsoApplied() {
 
+		Validator validator = TestUtil.getDefaultValidator();
+		FrenchAddress address = getFrenchAddressWithoutZipCode();
+		address.setZipCode( "00000" );
+		Set<ConstraintViolation<FrenchAddress>> constraintViolations = validator.validate( address );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		assertCorrectConstraintTypes(
+				constraintViolations, new Class<?>[] { FrenchZipcode.class }
+		);
+		ConstraintViolation<FrenchAddress> constraintViolation = constraintViolations.iterator().next();
+		assertEquals( constraintViolation.getMessage(), "00000 is a reserved code", "Wrong error message" );
+	}
+
+	@Test
+	@SpecAssertion(section = "2.3", id = "c")
+	public void testEachFailingConstraintCreatesConstraintViolation() {
+
+		Validator validator = TestUtil.getDefaultValidator();
+
+		FrenchAddress address = getFrenchAddressWithoutZipCode();
+
 		address.setZipCode( "abc" );
-		constraintViolations = validator.validate( address );
+		Set<ConstraintViolation<FrenchAddress>> constraintViolations = validator.validate( address );
 		assertCorrectNumberOfViolations( constraintViolations, 3 );
+		assertCorrectConstraintTypes(
+				constraintViolations, new Class<?>[] { Pattern.class, Pattern.class, Size.class }
+		);
 		for ( ConstraintViolation<FrenchAddress> violation : constraintViolations ) {
 			assertConstraintViolation(
 					violation,
@@ -68,13 +108,14 @@
 					"abc",
 					"zipCode"
 			);
-
 		}
 
-
 		address.setZipCode( "123" );
 		constraintViolations = validator.validate( address );
 		assertCorrectNumberOfViolations( constraintViolations, 2 );
+		assertCorrectConstraintTypes(
+				constraintViolations, new Class<?>[] { Pattern.class, Size.class }
+		);
 		for ( ConstraintViolation<FrenchAddress> violation : constraintViolations ) {
 			assertConstraintViolation(
 					violation,
@@ -91,6 +132,29 @@
 	}
 
 	@Test
+	@SpecAssertions({
+			@SpecAssertion(section = "2.3", id = "d"),
+			@SpecAssertion(section = "2.3", id = "e")
+	})
+	public void testGroupsDefinedOnMainAnnotationAreInherited() {
+		Validator validator = TestUtil.getDefaultValidator();
+
+		FrenchAddress address = getFrenchAddressWithoutZipCode();
+		Set<ConstraintViolation<FrenchAddress>> constraintViolations = validator.validate( address );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		ConstraintViolation<FrenchAddress> constraintViolation = constraintViolations.iterator().next();
+		assertCorrectConstraintType( constraintViolation, NotNull.class );
+		NotNull notNull = ( NotNull ) constraintViolation.getConstraintDescriptor().getAnnotation();
+		List<Class<?>> groups = Arrays.asList( notNull.groups() );
+		assertTrue( groups.size() == 2, "There should be two groups" );
+		assertTrue( groups.contains( Default.class ), "The default group should be in the list." );
+		assertTrue(
+				groups.contains( FrenchAddress.FullAddressCheck.class ),
+				"The FrenchAddress.FullAddressCheck group should be inherited."
+		);
+	}
+
+	@Test
 	public void testNestedComposition() {
 		Validator validator = TestUtil.getDefaultValidator();
 
@@ -127,4 +191,12 @@
 				"zipCode"
 		);
 	}
+
+	private FrenchAddress getFrenchAddressWithoutZipCode() {
+		FrenchAddress address = new FrenchAddress();
+		address.setAddressline1( "10 rue des Treuils" );
+		address.setAddressline2( "BP 12 " );
+		address.setCity( "Bordeaux" );
+		return address;
+	}
 }
\ No newline at end of file

Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchAddress.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchAddress.java	2009-06-05 14:05:44 UTC (rev 16708)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchAddress.java	2009-06-05 14:17:58 UTC (rev 16709)
@@ -17,13 +17,18 @@
 */
 package org.hibernate.jsr303.tck.tests.constraints.constraintcomposition;
 
+import javax.validation.groups.Default;
+
 /**
  * @author Hardy Ferentschik
  */
 public class FrenchAddress extends Address {
 
-	@FrenchZipcode
+	@FrenchZipcode(groups = { Default.class, FullAddressCheck.class })
 	public String getZipCode() {
 		return super.getZipCode();
 	}
+
+	public interface FullAddressCheck {
+	}
 }
\ No newline at end of file

Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcode.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcode.java	2009-06-05 14:05:44 UTC (rev 16708)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcode.java	2009-06-05 14:17:58 UTC (rev 16709)
@@ -34,7 +34,7 @@
 /**
  * @author Hardy Ferentschik
  */
- at NotNull
+ at NotEmpty
 @Size
 // first pattern just duplicates the length of 5 characters, the second pattern is just to proof that parameters can be overridden.
 @Pattern.List({ @Pattern(regexp = "....."), @Pattern(regexp = "bar") })

Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcodeConstraintValidator.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcodeConstraintValidator.java	2009-06-05 14:05:44 UTC (rev 16708)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/FrenchZipcodeConstraintValidator.java	2009-06-05 14:17:58 UTC (rev 16709)
@@ -28,7 +28,17 @@
 	public void initialize(FrenchZipcode parameters) {
 	}
 
-	public boolean isValid(String object, ConstraintValidatorContext constraintValidatorContext) {
-		return true;
+	public boolean isValid(String zip, ConstraintValidatorContext constraintValidatorContext) {
+		if ( zip == null ) {
+			return true;
+		}
+		if ( "00000".equals( zip ) ) {
+			constraintValidatorContext.disableDefaultError();
+			constraintValidatorContext.addError( "00000 is a reserved code" );
+			return false;
+		}
+		else {
+			return true;
+		}
 	}
 }

Added: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/NotEmpty.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/NotEmpty.java	                        (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/NotEmpty.java	2009-06-05 14:17:58 UTC (rev 16709)
@@ -0,0 +1,60 @@
+// $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.constraintcomposition;
+
+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.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Documented
+ at NotNull(message = "may not be null", groups = NotEmpty.DummyGroup.class)
+ at Size(min = 1)
+ at Constraint(validatedBy = NotEmpty.NotEmptyValidator.class)
+ at Target({ TYPE, METHOD, FIELD })
+ at Retention(RUNTIME)
+public @interface NotEmpty {
+	String message() default "{constraint.notEmpty}";
+
+	Class<?>[] groups() default { };
+
+	public class NotEmptyValidator implements ConstraintValidator<NotEmpty, String> {
+
+		public void initialize(NotEmpty parameters) {
+		}
+
+		public boolean isValid(String object, ConstraintValidatorContext constraintValidatorContext) {
+			return true;
+		}
+	}
+
+	public interface DummyGroup {
+
+	}
+}


Property changes on: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintcomposition/NotEmpty.java
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java	2009-06-05 14:05:44 UTC (rev 16708)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java	2009-06-05 14:17:58 UTC (rev 16709)
@@ -39,10 +39,34 @@
 		return Validation.buildDefaultValidatorFactory().getValidator();
 	}
 
+	public static <T> void assertCorrectConstraintType(ConstraintViolation<T> violation, Class<?> expectedConstraintType) {
+		assertEquals(
+				violation.getConstraintDescriptor().getAnnotation().annotationType().getName(),
+				expectedConstraintType.getName(),
+				"Wrong constraint type"
+		);
+	}
+
 	public static <T> void assertCorrectNumberOfViolations(Set<ConstraintViolation<T>> violations, int expectedViolations) {
 		assertEquals( violations.size(), expectedViolations, "Wrong number of constraint violations" );
 	}
 
+	public static <T> void assertCorrectConstraintTypes(Set<ConstraintViolation<T>> violations, Class<?>[] expectedConsraintTypes) {
+		List<String> constraintTypes = new ArrayList<String>();
+		for ( ConstraintViolation<?> violation : violations ) {
+			constraintTypes.add( violation.getConstraintDescriptor().getAnnotation().annotationType().getName() );
+		}
+
+		assertEquals( expectedConsraintTypes.length, constraintTypes.size(), "Wring number of constraint types." );
+
+		for ( Class<?> expectedConstraintType : expectedConsraintTypes ) {
+			assertTrue(
+					constraintTypes.contains(expectedConstraintType.getName()),
+					"The constraint type " + expectedConstraintType.getName() + " should have been violated."
+			);
+		}
+	}
+
 	public static <T> void assertInvalidPropertyPaths(Set<ConstraintViolation<T>> violations, String[] propertyPaths) {
 		List<String> propertyPathsOfViolations = new ArrayList<String>();
 		for ( ConstraintViolation<?> violation : violations ) {

Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml	2009-06-05 14:05:44 UTC (rev 16708)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml	2009-06-05 14:17:58 UTC (rev 16709)
@@ -88,7 +88,7 @@
                 annotations</text>
         </assertion>
         <assertion id="e">
-            <text>Any groups definition on a composing an- notation is ignored</text>
+            <text>Any groups definition on a composing annotation is ignored</text>
         </assertion>
         <assertion id="f">
             <text>The property type a composed constraint is placed on must be compatible with all




More information about the hibernate-commits mailing list