Author: hardy.ferentschik
Date: 2009-06-18 12:22:31 -0400 (Thu, 18 Jun 2009)
New Revision: 16832
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/Negative.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/NegativeConstraintValidator.java
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/BoundariesConstraintValidator.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/CustomConstraintValidatorTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/PositiveConstraintValidator.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
Adding TCK test mappings for basic validation routine
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/BoundariesConstraintValidator.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/BoundariesConstraintValidator.java 2009-06-18
14:24:56 UTC (rev 16831)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/BoundariesConstraintValidator.java 2009-06-18
16:22:31 UTC (rev 16832)
@@ -20,20 +20,39 @@
import java.lang.annotation.Annotation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
+import javax.validation.ValidationException;
/**
* @author Emmanuel Bernard
*/
public abstract class BoundariesConstraintValidator<T extends Annotation>
implements ConstraintValidator<T, Integer> {
+ public static boolean initializeCalled = false;
+ public static int isValidCalls = 0;
+ public static boolean throwRuntimeExceptionFromInitalize = false;
+ public static boolean throwRuntimeExceptionFromIsValid = false;
+
private int low;
private int high;
protected void initialize(int low, int high) {
+ initializeCalled = true;
+ if ( throwRuntimeExceptionFromInitalize ) {
+ throwRuntimeExceptionFromInitalize = false;
+ throw new RuntimeException( "Throwing a RuntimeException from
BoundariesConstraintValidator.initialize" );
+ }
this.low = low;
this.high = high;
}
public boolean isValid(Integer value, ConstraintValidatorContext
constraintValidatorContext) {
+ if ( !initializeCalled ) {
+ throw new ValidationException( "initialize() must be called before the usage of
isValid()" );
+ }
+ if ( throwRuntimeExceptionFromIsValid ) {
+ throwRuntimeExceptionFromIsValid = false;
+ throw new RuntimeException( "Throwing a RuntimeException from
BoundariesConstraintValidator.isValid" );
+ }
+ isValidCalls++;
return value >= low && value <= high;
}
}
\ No newline at end of file
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/CustomConstraintValidatorTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/CustomConstraintValidatorTest.java 2009-06-18
14:24:56 UTC (rev 16831)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/CustomConstraintValidatorTest.java 2009-06-18
16:22:31 UTC (rev 16832)
@@ -18,17 +18,22 @@
package org.hibernate.jsr303.tck.tests.constraints.customconstraint;
import java.util.Set;
-import javax.validation.Validation;
+import javax.validation.ConstraintViolation;
+import javax.validation.UnexpectedTypeException;
+import javax.validation.ValidationException;
import javax.validation.Validator;
-import javax.validation.ConstraintViolation;
import javax.validation.metadata.PropertyDescriptor;
-import org.testng.annotations.Test;
-import static org.testng.Assert.*;
+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 org.jboss.testharness.AbstractTest;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.Test;
import org.hibernate.jsr303.tck.util.TestUtil;
@@ -40,18 +45,108 @@
public class CustomConstraintValidatorTest extends AbstractTest {
@Test
- public void testInheritedConstraintValidationImpl() {
- Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
- Phone p = new Phone();
- p.size = -2;
- final PropertyDescriptor propertyDescriptor = validator.getConstraintsForClass(
Phone.class )
+ @SpecAssertions({
+ @SpecAssertion(section = "2.4", id = "a"),
+ @SpecAssertion(section = "2.4", id = "b"),
+ @SpecAssertion(section = "2.4", id = "e")
+ })
+ public void testRightValidatorIsSlectedAndInializedCalled() {
+ Validator validator = TestUtil.getDefaultValidator();
+ Shoe shoe = new Shoe();
+ shoe.size = -2;
+ final PropertyDescriptor propertyDescriptor = validator.getConstraintsForClass(
Shoe.class )
.getConstraintsForProperty( "size" );
assertNotNull( propertyDescriptor );
- final Set<ConstraintViolation<Phone>> constraintViolations =
validator.validate( p );
+
+ BoundariesConstraintValidator.isValidCalls = 0;
+ final Set<ConstraintViolation<Shoe>> constraintViolations =
validator.validate( shoe );
assertEquals( 1, constraintViolations.size() );
+ assertTrue(
+ BoundariesConstraintValidator.isValidCalls == 1,
+ "Ensure the right validator implementation class was picked."
+ );
+ assertTrue(
+ BoundariesConstraintValidator.initializeCalled,
+ "Check initilize was called. Note this is not really ensuring that it was called
before isValid. That is done in the actual implementation of the validator."
+ );
}
- public static class Phone {
- @Positive public int size;
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "2.4", id = "a"),
+ @SpecAssertion(section = "2.4", id = "b"),
+ @SpecAssertion(section = "2.4", id = "f")
+ })
+ public void testIsValidIsCalledForEachValidation() {
+ Validator validator = TestUtil.getDefaultValidator();
+ Shoe shoe = new Shoe();
+ shoe.size = -2;
+
+ BoundariesConstraintValidator.isValidCalls = 0;
+ validator.validate( shoe );
+ assertTrue(
+ BoundariesConstraintValidator.isValidCalls == 1,
+ "Ensure is valid hasbeen called."
+ );
+
+ validator.validate( shoe );
+ assertTrue(
+ BoundariesConstraintValidator.isValidCalls == 2,
+ "Ensure is valid hasbeen called."
+ );
+
+ validator.validateProperty( shoe, "size" );
+ assertTrue(
+ BoundariesConstraintValidator.isValidCalls == 3,
+ "Ensure is valid hasbeen called."
+ );
+
+ validator.validateValue( Shoe.class, "size", 41 );
+ assertTrue(
+ BoundariesConstraintValidator.isValidCalls == 4,
+ "Ensure is valid hasbeen called."
+ );
}
+
+
+ @SpecAssertion(section = "2.4", id = "i")
+ @Test(expectedExceptions = UnexpectedTypeException.class)
+ public void testUnexpectedTypeExceptionIsRaisedForInvalidType() {
+ Validator validator = TestUtil.getDefaultValidator();
+ validator.validate( new OddShoe() );
+ }
+
+ @SpecAssertion(section = "2.4", id = "j")
+ @Test(expectedExceptions = ValidationException.class)
+ public void testRuntimeExceptionFromIsValidIsWrapped() {
+ Validator validator = TestUtil.getDefaultValidator();
+ Shoe shoe = new Shoe();
+ shoe.size = -2;
+ BoundariesConstraintValidator.throwRuntimeExceptionFromIsValid = true;
+ validator.validate( shoe );
+ }
+
+ @SpecAssertion(section = "2.4", id = "j")
+ @Test(expectedExceptions = ValidationException.class)
+ public void testRuntimeExceptionFromInitializeIsWrapped() {
+ Validator validator = TestUtil.getDefaultValidator();
+ validator.validate( new Freezer() );
+ }
+
+ public static class Shoe {
+ @Positive
+ public int size;
+ }
+
+ public static class OddShoe {
+ @Positive
+ public String size;
+ }
+
+ public static class Freezer {
+ @Negative
+ public int temprature;
+ }
+
+
}
\ No newline at end of file
Copied:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/Negative.java
(from rev 16812,
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/Positive.java)
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/Negative.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/Negative.java 2009-06-18
16:22:31 UTC (rev 16832)
@@ -0,0 +1,39 @@
+// $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.customconstraint;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Documented;
+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.ANNOTATION_TYPE;
+import javax.validation.Constraint;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Constraint( validatedBy = { PositiveConstraintValidator.class })
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Documented
+public @interface Negative {
+ public abstract String message() default "{validation.negative}";
+ public abstract Class<?>[] groups() default {};
+}
\ No newline at end of file
Copied:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/NegativeConstraintValidator.java
(from rev 16812,
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/BoundariesConstraintValidator.java)
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/NegativeConstraintValidator.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/NegativeConstraintValidator.java 2009-06-18
16:22:31 UTC (rev 16832)
@@ -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.jsr303.tck.tests.constraints.customconstraint;
+
+import java.lang.annotation.Annotation;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public abstract class NegativeConstraintValidator implements
ConstraintValidator<Negative, Integer> {
+ protected void initialize(int low, int high) {
+ throw new RuntimeException( "Throwing a RuntimeException from
NegativeConstraintValidator.initialize" );
+ }
+
+ public boolean isValid(Integer value, ConstraintValidatorContext
constraintValidatorContext) {
+ return true;
+ }
+}
\ No newline at end of file
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/PositiveConstraintValidator.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/PositiveConstraintValidator.java 2009-06-18
14:24:56 UTC (rev 16831)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/customconstraint/PositiveConstraintValidator.java 2009-06-18
16:22:31 UTC (rev 16832)
@@ -20,7 +20,7 @@
/**
* @author Emmanuel Bernard
*/
-public class PositiveConstraintValidator extends
BoundariesConstraintValidator<Positive> {
+public class PositiveConstraintValidator extends
BoundariesConstraintValidator<Positive> {
public void initialize(Positive constraintAnnotation) {
super.initialize( 0, Integer.MAX_VALUE );
}
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-06-18
14:24:56 UTC (rev 16831)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java 2009-06-18
16:22:31 UTC (rev 16832)
@@ -49,7 +49,8 @@
@Test
@SpecAssertions({
@SpecAssertion(section = "3.5.3", id = "e"),
- @SpecAssertion(section = "2.1", id = "d")
+ @SpecAssertion(section = "2.1", id = "d"),
+ @SpecAssertion(section = "2.4", id = "i")
})
public void testUnexpectedTypeInValidatorResolution() {
Validator validator = TestUtil.getDefaultValidator();
Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-18
14:24:56 UTC (rev 16831)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-18
16:22:31 UTC (rev 16832)
@@ -135,11 +135,11 @@
<text>The implementation classes are specified by the validatedBy
element of the
@Contraint annotation that decorates the constraint
definition</text>
</assertion>
- <assertion id="c">
+ <assertion id="c" testable="false">
<text>The constraint validation implementation implements the
ConstraintValidator
interface</text>
</assertion>
- <assertion id="d">
+ <assertion id="d" testable="false">
<text>T must resolve in a non parameterized type or generic parameters
of T must be
unbounded wildcard types</text>
</assertion>
@@ -151,34 +151,33 @@
<text>The isValid method is evaluated by the Bean Validation provider
each time a given
value is validated</text>
</assertion>
- <assertion id="g">
+ <assertion id="g" testable="false">
<text>It returns false if the value is not valid, true
otherwise</text>
</assertion>
<assertion id="h" testable="false">
<text>isValid implementations must be thread-safe</text>
</assertion>
<assertion id="i">
- <text>If the property is of an unanticipated type, an
UnexpectedTypeException is
- raised</text>
+ <text>If the property is of an unanticipated type, an
UnexpectedTypeException is raised</text>
</assertion>
<assertion id="j">
<text>If an exception occurs either in the initialize or isValid
method, the runtime
- exception is wrapped into a Valid- ationException by the Bean Validation
engine
+ exception is wrapped into a ValidationException by the Bean Validation
engine
</text>
</assertion>
- <assertion id="k">
+ <assertion id="k" testable="false">
<text>The constraint validation implementation is not allowed to change
the state of the
value passed to isValid. </text>
</assertion>
<assertion id="l">
<text>By default, each invalid constraint leads to the generation of
one error object
represented by a ConstraintViolation object. This object is build from
the default
- error message as defined by the constraint de- claration and the context
in which
+ error message as defined by the constraint declaration and the context in
which
the constraint declaration is placed on (bean, property,
attribute)</text>
</assertion>
<assertion id="m">
<text>The ConstraintValidatorContext methods let the constraint
implementation disable
- the default error object gen- eration and create one or more custom
ones</text>
+ the default error object generation and create one or more custom
ones</text>
</assertion>
<assertion id="n">
<text>The non-interpolated message passed as a parameter is used to
build the
@@ -302,7 +301,7 @@
<text>A constraint declaration can be placed on an
interface</text>
</assertion>
<assertion id="b">
- <text>For a given class, constraint declarations held on super- classes
as well as
+ <text>For a given class, constraint declarations held on superclasses
as well as
interfaces are evaluated by the Bean Validation provider</text>
</assertion>
</section>
@@ -379,7 +378,7 @@
</section>
<section id="3.4.3" title="Redefining the Default group for a
class">
<assertion id="a">
- <text>To redefine Default for a class, place a @GroupSequence
annotation on the class.
+ <text>To redefine Default for a class, place a @GroupSequence
annotation on the class.
this sequence expresses the sequence of groups that does substitute
Default for this
class.</text>
</assertion>
@@ -1170,7 +1169,7 @@
</assertion>
</section>
<section id="8.3" title="ConstraintDeclarationException and
UnexpectedTypeException">
- <assertion id="a">
+ <assertion id="a" testable="false">
<text>When a constraint declaration is illegal,
ConstraintDeclarationException is
raised</text>
</assertion>