Author: hardy.ferentschik
Date: 2009-10-15 08:37:13 -0400 (Thu, 15 Oct 2009)
New Revision: 17764
Modified:
beanvalidation/tck/trunk/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintValidatorContextTest.java
Log:
BVTCK-2
Modified:
beanvalidation/tck/trunk/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java
===================================================================
---
beanvalidation/tck/trunk/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java 2009-10-15
12:07:45 UTC (rev 17763)
+++
beanvalidation/tck/trunk/src/main/java/org/hibernate/jsr303/tck/tests/validation/validatorcontext/ConstraintValidatorContextTest.java 2009-10-15
12:37:13 UTC (rev 17764)
@@ -17,10 +17,21 @@
*/
package org.hibernate.jsr303.tck.tests.validation.validatorcontext;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Set;
+import javax.validation.Constraint;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
+import javax.validation.Payload;
import javax.validation.ValidationException;
import javax.validation.Validator;
@@ -33,6 +44,7 @@
import org.testng.annotations.Test;
import org.hibernate.jsr303.tck.util.TestUtil;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
import static
org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
@@ -112,4 +124,80 @@
assertCorrectConstraintViolationMessages( constraintViolations, "subnode
message" );
assertCorrectPropertyPaths( constraintViolations, "value.subnode" );
}
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "2.4", id = "m"),
+ @SpecAssertion(section = "2.4", id = "q")
+ })
+ public void propertyPathInIterable() {
+ Validator validator = TestUtil.getValidatorUnderTest();
+ Group group = new Group( Gender.MALE, new Person( Gender.FEMALE ) );
+
+ Set<ConstraintViolation<Group>> constraintViolations = validator.validate(
group );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertCorrectPropertyPaths( constraintViolations, "persons[0]" );
+ assertCorrectConstraintTypes( constraintViolations, CompatiblePersons.class );
+ }
+
+ private enum Gender {
+ MALE, FEMALE
+ }
+
+ @CompatiblePersons
+ private class Group {
+ Gender gender;
+ List<Person> persons = new ArrayList<Person>();
+
+ public Group(Gender gender, Person... persons) {
+ this.gender = gender;
+ this.persons.addAll( Arrays.asList( persons ) );
+ }
+ }
+
+ private class Person {
+ Gender gender;
+
+ public Person(Gender gender) {
+ this.gender = gender;
+ }
+ }
+
+ @Target({ java.lang.annotation.ElementType.TYPE,
java.lang.annotation.ElementType.ANNOTATION_TYPE })
+ @Retention(RetentionPolicy.RUNTIME)
+ @Constraint(validatedBy = { CompatiblePersonsValidator.class })
+ @Documented
+ public @interface CompatiblePersons {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+ }
+
+ public static class CompatiblePersonsValidator implements
ConstraintValidator<CompatiblePersons, Group> {
+ public void initialize(CompatiblePersons constraintAnnotation) {
+ }
+
+ public boolean isValid(Group group, ConstraintValidatorContext
constraintValidatorContext) {
+ if ( group == null ) {
+ return true;
+ }
+
+ constraintValidatorContext.disableDefaultConstraintViolation();
+
+ for ( int index = 0; index < group.persons.size(); index++ ) {
+ Person person = group.persons.get( index );
+ if ( !group.gender.equals( person.gender ) ) {
+ constraintValidatorContext
+ .buildConstraintViolationWithTemplate(
"constraints.CompatiblePersons.gender.message" )
+ .addNode( "persons" )
+ .addNode( null ).inIterable().atIndex( index )
+ .addConstraintViolation();
+ return false;
+ }
+ }
+ return true;
+ }
+ }
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintValidatorContextTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintValidatorContextTest.java 2009-10-15
12:07:45 UTC (rev 17763)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintValidatorContextTest.java 2009-10-15
12:37:13 UTC (rev 17764)
@@ -17,19 +17,9 @@
*/
package org.hibernate.validator.constraints;
-import java.lang.annotation.Documented;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.Set;
-import javax.validation.Constraint;
-import javax.validation.ConstraintValidator;
-import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
-import javax.validation.Payload;
import javax.validation.Validator;
import static org.testng.Assert.assertEquals;
@@ -40,7 +30,6 @@
import org.hibernate.validator.engine.MessageAndPath;
import org.hibernate.validator.engine.PathImpl;
import org.hibernate.validator.util.TestUtil;
-import static org.hibernate.validator.util.TestUtil.assertCorrectConstraintTypes;
import static org.hibernate.validator.util.TestUtil.assertCorrectPropertyPaths;
import static org.hibernate.validator.util.TestUtil.assertNumberOfViolations;
@@ -134,7 +123,7 @@
.addConstraintViolation();
messageAndPathList = context.getMessageAndPathList();
- assertMessageAndPath( messageAndPathList.get( 0 ), message,
"foo[test].bar[].fubar" );
+ assertMessageAndPath( messageAndPathList.get( 0 ), message,
"foo[test].bar[].fubar" );
}
@Test
@@ -155,20 +144,6 @@
assertMessageAndPath( messageAndPathList.get( 1 ), message2, "" );
}
- /**
- * HV-253
- */
- @Test
- public void propertyPathInIterable() {
- Validator validator = TestUtil.getValidator();
- Group group = new Group( Gender.MALE, new Person( Gender.FEMALE ) );
-
- Set<ConstraintViolation<Group>> constraintViolations = validator.validate(
group );
- assertNumberOfViolations( constraintViolations, 1 );
- assertCorrectPropertyPaths( constraintViolations, "persons[0]" );
- assertCorrectConstraintTypes( constraintViolations, CompatiblePersons.class );
- }
-
private ConstraintValidatorContextImpl createEmptyConstraintValidatorContextImpl() {
ConstraintValidatorContextImpl context = new ConstraintValidatorContextImpl(
PathImpl.createNewPath( null ), null
@@ -181,65 +156,4 @@
assertEquals( messageAndPath.getPath(), PathImpl.createPathFromString( expectedPath ),
"Wrong path" );
assertEquals( messageAndPath.getMessage(), expectedMessage, "Wrong message"
);
}
-
- private enum Gender {
- MALE, FEMALE
- }
-
- @CompatiblePersons
- private class Group {
- Gender gender;
- List<Person> persons = new ArrayList<Person>();
-
- public Group(Gender gender, Person... persons) {
- this.gender = gender;
- this.persons.addAll( Arrays.asList( persons ) );
- }
- }
-
- private class Person {
- Gender gender;
-
- public Person(Gender gender) {
- this.gender = gender;
- }
- }
-
- @Target({ java.lang.annotation.ElementType.TYPE,
java.lang.annotation.ElementType.ANNOTATION_TYPE })
- @Retention(RetentionPolicy.RUNTIME)
- @Constraint(validatedBy = { CompatiblePersonsValidator.class })
- @Documented
- public @interface CompatiblePersons {
- String message() default "";
-
- Class<?>[] groups() default { };
-
- Class<? extends Payload>[] payload() default { };
- }
-
- public static class CompatiblePersonsValidator implements
ConstraintValidator<CompatiblePersons, Group> {
- public void initialize(CompatiblePersons constraintAnnotation) {
- }
-
- public boolean isValid(Group group, ConstraintValidatorContext
constraintValidatorContext) {
- if ( group == null ) {
- return true;
- }
-
- constraintValidatorContext.disableDefaultConstraintViolation();
-
- for ( int index = 0; index < group.persons.size(); index++ ) {
- Person person = group.persons.get( index );
- if ( !group.gender.equals( person.gender ) ) {
- constraintValidatorContext
- .buildConstraintViolationWithTemplate(
"constraints.CompatiblePersons.gender.message" )
- .addNode( "persons" )
- .addNode( null ).inIterable().atIndex( index )
- .addConstraintViolation();
- return false;
- }
- }
- return true;
- }
- }
}