Author: hardy.ferentschik
Date: 2009-08-31 11:40:40 -0400 (Mon, 31 Aug 2009)
New Revision: 17455
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Coordinate.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/PostCodeList.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Suburb.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ValidatorResolutionTest.java
Removed:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/PostCodeList.java
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Coordinate.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Suburb.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java
Log:
HV-217 Moved the @PostCodeList example into the Hibernate Validator testsuite.
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Coordinate.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Coordinate.java 2009-08-31
15:15:20 UTC (rev 17454)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Coordinate.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -23,10 +23,10 @@
public class Coordinate {
long longitude;
- long latidute;
+ long latitude;
- public Coordinate(long longitude, long latidute) {
+ public Coordinate(long longitude, long latitude) {
this.longitude = longitude;
- this.latidute = latidute;
+ this.latitude = latitude;
}
}
Deleted:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/PostCodeList.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/PostCodeList.java 2009-08-31
15:15:20 UTC (rev 17454)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/PostCodeList.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -1,72 +0,0 @@
-// $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 java.util.Collection;
-import javax.validation.Constraint;
-import javax.validation.ConstraintValidator;
-import javax.validation.ConstraintValidatorContext;
-import javax.validation.Payload;
-
-/**
- * A test constraint which can lead to a error when trying to reslove the validator.
- *
- * @author Hardy Ferentschik
- */
-@Constraint(validatedBy = {
- PostCodeList.PostCodeListValidatorForString.class,
PostCodeList.PostCodeListValidatorForNumber.class
-})
-@Documented
-@Target({ METHOD, FIELD, TYPE })
-@Retention(RUNTIME)
-public @interface PostCodeList {
- public abstract String message() default "foobar";
-
- public abstract Class<?>[] groups() default { };
-
- public abstract Class<? extends Payload>[] payload() default {};
-
- public class PostCodeListValidatorForNumber
- implements ConstraintValidator<PostCodeList, Collection<? extends Number>>
{
- public void initialize(PostCodeList constraintAnnotation) {
- }
-
- public boolean isValid(Collection<? extends Number> value,
ConstraintValidatorContext constraintValidatorContext) {
- return true;
- }
- }
-
- public class PostCodeListValidatorForString implements
ConstraintValidator<PostCodeList, Collection<String>> {
- public void initialize(PostCodeList constraintAnnotation) {
- }
-
- public boolean isValid(Collection<String> value, ConstraintValidatorContext
constraintValidatorContext) {
- if ( value == null ) {
- return true;
- }
- return false;
- }
- }
-}
\ No newline at end of file
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Suburb.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Suburb.java 2009-08-31
15:15:20 UTC (rev 17454)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/Suburb.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -17,7 +17,6 @@
*/
package org.hibernate.jsr303.tck.tests.constraints.validatorresolution;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -44,17 +43,6 @@
@Size(min = 4, max = 1000)
private Coordinate[] boundingBox;
- @PostCodeList
- private Collection<? extends Number> includedPostCodes;
-
- public void setIncludedPostCodes(Collection<? extends Number> includedPostCodes)
{
- this.includedPostCodes = includedPostCodes;
- }
-
- public Collection<? extends Number> getIncludedPostcodes() {
- return includedPostCodes;
- }
-
public void setName(String name) {
this.name = name;
}
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-08-31
15:15:20 UTC (rev 17454)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -137,10 +137,6 @@
Suburb suburb = new Suburb();
- List<Integer> postcodes = new ArrayList<Integer>();
- postcodes.add( 12345 );
- suburb.setIncludedPostCodes( postcodes );
-
// all values are null and should pass
Set<ConstraintViolation<Suburb>> constraintViolations = validator.validate(
suburb );
assertCorrectNumberOfViolations( constraintViolations, 0 );
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Coordinate.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Coordinate.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Coordinate.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -0,0 +1,35 @@
+// $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.validator.constraints;
+
+/**
+ * @author Hardy Ferentschik
+ */
+/**
+ * @author Hardy Ferentschik
+ */
+public class Coordinate {
+
+ long longitude;
+ long latitude;
+
+ public Coordinate(long longitude, long latitude) {
+ this.longitude = longitude;
+ this.latitude = latitude;
+ }
+}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/PostCodeList.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/PostCodeList.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/PostCodeList.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -0,0 +1,72 @@
+// $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.validator.constraints;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Target;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE;
+import java.util.Collection;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+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
+ */
+@Constraint(validatedBy = {
+ PostCodeList.PostCodeListValidatorForString.class,
PostCodeList.PostCodeListValidatorForNumber.class
+})
+@Documented
+@Target({ METHOD, FIELD, TYPE })
+@Retention(RUNTIME)
+public @interface PostCodeList {
+ public abstract String message() default "foobar";
+
+ public abstract Class<?>[] groups() default { };
+
+ public abstract Class<? extends Payload>[] payload() default {};
+
+ public class PostCodeListValidatorForNumber
+ implements ConstraintValidator<PostCodeList, Collection<? extends Number>>
{
+ public void initialize(PostCodeList constraintAnnotation) {
+ }
+
+ public boolean isValid(Collection<? extends Number> value,
ConstraintValidatorContext constraintValidatorContext) {
+ return true;
+ }
+ }
+
+ public class PostCodeListValidatorForString implements
ConstraintValidator<PostCodeList, Collection<String>> {
+ public void initialize(PostCodeList constraintAnnotation) {
+ }
+
+ public boolean isValid(Collection<String> value, ConstraintValidatorContext
constraintValidatorContext) {
+ if ( value == null ) {
+ return true;
+ }
+ return false;
+ }
+ }
+}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Suburb.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Suburb.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Suburb.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -0,0 +1,96 @@
+// $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.validator.constraints;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Suburb {
+ public enum Facility {
+ SHOPPING_MALL, BUS_TERMINAL
+ }
+
+ @Size(min = 5, max = 10, message = "size must be between {min} and {max}")
+ private String name;
+
+ @Size(min = 2, max = 2, message = "size must be between {min} and {max}")
+ private Map<Facility, Boolean> facilities;
+
+ @Size(min = 2, message = "size must be between {min} and {max}")
+ private Set<String> streetNames;
+
+ @Size(min = 4, max = 1000, message = "size must be between {min} and {max}")
+ private Coordinate[] boundingBox;
+
+ @PostCodeList
+ private Collection<? extends Number> includedPostCodes;
+
+ public void setIncludedPostCodes(Collection<? extends Number> includedPostCodes)
{
+ this.includedPostCodes = includedPostCodes;
+ }
+
+ public Collection<? extends Number> getIncludedPostcodes() {
+ return includedPostCodes;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Map<Facility, Boolean> getFacilities() {
+ return facilities;
+ }
+
+ public void addFacility(Facility f, Boolean exist) {
+ if ( facilities == null ) {
+ facilities = new HashMap<Facility, Boolean>();
+ }
+ facilities.put( f, exist );
+ }
+
+ public Set<String> getStreetNames() {
+ return streetNames;
+ }
+
+ public void addStreetName(String streetName) {
+ if ( streetNames == null ) {
+ streetNames = new HashSet<String>();
+ }
+ streetNames.add( streetName );
+ }
+
+ public Coordinate[] getBoundingBox() {
+ return boundingBox;
+ }
+
+ public void setBoundingBox(Coordinate[] boundingBox) {
+ this.boundingBox = boundingBox;
+ }
+}
+
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ValidatorResolutionTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ValidatorResolutionTest.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ValidatorResolutionTest.java 2009-08-31
15:40:40 UTC (rev 17455)
@@ -0,0 +1,116 @@
+// $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.validator.constraints;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.util.TestUtil;
+import static org.hibernate.validator.util.TestUtil.assertConstraintViolation;
+import static org.hibernate.validator.util.TestUtil.assertNumberOfViolations;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ValidatorResolutionTest {
+
+ @Test
+ public void testResolutionOfMultipleSizeValidators() {
+ Validator validator = TestUtil.getValidator();
+
+ Suburb suburb = new Suburb();
+
+ List<Integer> postcodes = new ArrayList<Integer>();
+ postcodes.add( 12345 );
+ suburb.setIncludedPostCodes( postcodes );
+
+ // all values are null and should pass
+ Set<ConstraintViolation<Suburb>> constraintViolations = validator.validate(
suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ suburb.setName( "" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(), "size must be between 5 and 10",
Suburb.class, "", "name"
+ );
+
+ suburb.setName( "Hoegsbo" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ suburb.addFacility( Suburb.Facility.SHOPPING_MALL, false );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ "size must be between 2 and 2",
+ Suburb.class,
+ suburb.getFacilities(),
+ "facilities"
+ );
+
+ suburb.addFacility( Suburb.Facility.BUS_TERMINAL, true );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ suburb.addStreetName( "Sikelsgatan" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ "size must be between 2 and 2147483647",
+ Suburb.class,
+ suburb.getStreetNames(),
+ "streetNames"
+ );
+
+ suburb.addStreetName( "Marklandsgatan" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ Coordinate[] boundingBox = new Coordinate[3];
+ boundingBox[0] = new Coordinate( 0l, 0l );
+ boundingBox[1] = new Coordinate( 0l, 1l );
+ boundingBox[2] = new Coordinate( 1l, 0l );
+ suburb.setBoundingBox( boundingBox );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ "size must be between 4 and 1000",
+ Suburb.class,
+ suburb.getBoundingBox(),
+ "boundingBox"
+ );
+
+ boundingBox = new Coordinate[4];
+ boundingBox[0] = new Coordinate( 0l, 0l );
+ boundingBox[1] = new Coordinate( 0l, 1l );
+ boundingBox[2] = new Coordinate( 1l, 0l );
+ boundingBox[3] = new Coordinate( 1l, 1l );
+ suburb.setBoundingBox( boundingBox );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+ }
+}