Author: hardy.ferentschik
Date: 2009-06-09 10:17:25 -0400 (Tue, 09 Jun 2009)
New Revision: 16720
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
Built-in constraint tests
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java 2009-06-08
16:05:31 UTC (rev 16719)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java 2009-06-09
14:17:25 UTC (rev 16720)
@@ -19,21 +19,34 @@
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
+import javax.validation.constraints.Digits;
+import javax.validation.constraints.Future;
+import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
+import javax.validation.constraints.Past;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
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.fail;
import org.testng.annotations.Test;
@@ -43,7 +56,7 @@
import static org.hibernate.jsr303.tck.util.TestUtil.assertInvalidPropertyPaths;
/**
- * Tests for built-in constraints.
+ * Tests for built-in constraints. Basically juyst checks the availabiltiy of the
build-in constraints.
*
* @author Hardy Ferentschik
*/
@@ -161,105 +174,300 @@
MinDummyEntity dummy = new MinDummyEntity();
Set<ConstraintViolation<MinDummyEntity>> constraintViolations =
validator.validate( dummy );
+ // only the min constraints on the primitive values should fail. Object values re still
null and should pass per spec
assertCorrectNumberOfViolations( constraintViolations, 4 );
- assertInvalidPropertyPaths(constraintViolations, new String[]
{"bytePrimitive", "intPrimitive", "longPrimitive",
"shortPrimitive"});
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
+ );
-// dummy.setPrimitiveBoolean( false );
-// dummy.setObjectBoolean( Boolean.TRUE );
-//
-// constraintViolations = validator.validate( dummy );
-// assertCorrectNumberOfViolations( constraintViolations, 1 );
-// assertConstraintViolation(
-// constraintViolations.iterator().next(), MinDummyEntity.class, Boolean.TRUE,
"objectBoolean"
-// );
-//
-// dummy.setObjectBoolean( Boolean.FALSE );
-// constraintViolations = validator.validate( dummy );
-// assertCorrectNumberOfViolations( constraintViolations, 0 );
+ dummy.intPrimitive = 101;
+ dummy.longPrimitive = 1001;
+ dummy.bytePrimitive = 111;
+ dummy.shortPrimitive = 142;
+
+ dummy.intObject = Integer.valueOf( "100" );
+ dummy.longObject = Long.valueOf( "0" );
+ dummy.byteObject = Byte.parseByte( "-1" );
+ dummy.shortObject = Short.parseShort( "3" );
+ dummy.bigDecimal = BigDecimal.valueOf( 100.9 );
+ dummy.bigInteger = BigInteger.valueOf( 100 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 6 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
+ );
+
+ dummy.intObject = Integer.valueOf( "101" );
+ dummy.longObject = Long.valueOf( "12345" );
+ dummy.byteObject = Byte.parseByte( "102" );
+ dummy.shortObject = Short.parseShort( "111" );
+ dummy.bigDecimal = BigDecimal.valueOf( 101.1 );
+ dummy.bigInteger = BigInteger.valueOf( 101 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
}
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "h")
-// })
-// public void testMaxConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
-//
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "i")
-// })
-// public void testDecimalMinConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
-//
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "j")
-// })
-// public void testDecimalMaxConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
-//
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "k")
-// })
-// public void testSizeConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
-//
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "l")
-// })
-// public void testDigitsConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
-//
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "m")
-// })
-// public void testPastConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
-//
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "n")
-// })
-// public void testFutureConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
-//
-// @Test
-// @SpecAssertions({
-// @SpecAssertion(section = "6", id = "a"),
-// @SpecAssertion(section = "6", id = "o")
-// })
-// public void testPatternConstraint() {
-// Validator validator = TestUtil.getDefaultValidator();
-// fail();
-// }
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "h")
+ })
+ public void testMaxConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ MaxDummyEntity dummy = new MaxDummyEntity();
+ dummy.intPrimitive = 102;
+ dummy.longPrimitive = 1234;
+ dummy.bytePrimitive = 102;
+ dummy.shortPrimitive = 102;
+ Set<ConstraintViolation<MaxDummyEntity>> constraintViolations =
validator.validate( dummy );
+ // only the max constraints on the primitive values should fail. Object values re still
null and should pass per spec
+ assertCorrectNumberOfViolations( constraintViolations, 4 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
+ );
+
+
+ dummy.intPrimitive = 101;
+ dummy.longPrimitive = 100;
+ dummy.bytePrimitive = 99;
+ dummy.shortPrimitive = 42;
+
+ dummy.intObject = Integer.valueOf( "102" );
+ dummy.longObject = Long.valueOf( "12345" );
+ dummy.byteObject = Byte.parseByte( "111" );
+ dummy.shortObject = Short.parseShort( "1234" );
+ dummy.bigDecimal = BigDecimal.valueOf( 102 );
+ dummy.bigInteger = BigInteger.valueOf( 102 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 6 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
+ );
+
+ dummy.intObject = Integer.valueOf( "101" );
+ dummy.longObject = Long.valueOf( "100" );
+ dummy.byteObject = Byte.parseByte( "100" );
+ dummy.shortObject = Short.parseShort( "101" );
+ dummy.bigDecimal = BigDecimal.valueOf( 100.9 );
+ dummy.bigInteger = BigInteger.valueOf( 100 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+ }
+
+ @Test(enabled = false)
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "i")
+ })
+ public void testDecimalMinConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ fail();
+ }
+
+ @Test(enabled = false)
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "j")
+ })
+ public void testDecimalMaxConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ fail();
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "k")
+ })
+ public void testSizeConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ SizeDummyEntity dummy = new SizeDummyEntity();
+
+ Set<ConstraintViolation<SizeDummyEntity>> constraintViolations =
validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+
+ dummy.collection = new HashSet<String>();
+ dummy.collection.add( "foo" );
+ dummy.collection.add( "bar" );
+
+ dummy.string = "";
+
+ dummy.map = new HashMap<String, String>();
+ dummy.map.put( "key1", "value1" );
+ dummy.map.put( "key2", "value2" );
+
+ dummy.integerArray = new Integer[0];
+
+ dummy.intArray = new int[0];
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 5 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "collection", "map", "string",
"integerArray", "intArray" }
+ );
+
+ dummy.collection.remove( "bar" );
+ dummy.string = "a";
+ dummy.integerArray = new Integer[1];
+ dummy.intArray = new int[1];
+ dummy.map.remove( "key1" );
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "l")
+ })
+ public void testDigitsConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ DigitsDummyEntity dummy = new DigitsDummyEntity();
+
+ dummy.intPrimitive = 42;
+ dummy.longPrimitive = 42;
+ dummy.bytePrimitive = 42;
+ dummy.shortPrimitive = 42;
+
+ Set<ConstraintViolation<DigitsDummyEntity>> constraintViolations =
validator.validate( dummy );
+ // only the max constraints on the primitive values should fail. Object values re still
null and should pass per spec
+ assertCorrectNumberOfViolations( constraintViolations, 4 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
+ );
+
+
+ dummy.intPrimitive = 1;
+ dummy.longPrimitive = 1;
+ dummy.bytePrimitive = 1;
+ dummy.shortPrimitive = 1;
+
+ dummy.intObject = Integer.valueOf( "102" );
+ dummy.longObject = Long.valueOf( "12345" );
+ dummy.byteObject = Byte.parseByte( "111" );
+ dummy.shortObject = Short.parseShort( "1234" );
+ dummy.bigDecimal = BigDecimal.valueOf( 102 );
+ dummy.bigInteger = BigInteger.valueOf( 102 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 6 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
+ );
+
+ dummy.intObject = Integer.valueOf( "1" );
+ dummy.longObject = Long.valueOf( "1" );
+ dummy.byteObject = Byte.parseByte( "1" );
+ dummy.shortObject = Short.parseShort( "1" );
+ dummy.bigDecimal = BigDecimal.valueOf( 1.93 );
+ dummy.bigInteger = BigInteger.valueOf( 5 );
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "m")
+ })
+ public void testPastConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ PastDummyEntity dummy = new PastDummyEntity();
+
+ Set<ConstraintViolation<PastDummyEntity>> constraintViolations =
validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+
+ Calendar cal = GregorianCalendar.getInstance();
+ cal.add( Calendar.YEAR, 1 );
+
+ dummy.calendar = cal;
+ dummy.date = cal.getTime();
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 2 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "date", "calendar" }
+ );
+
+ cal.add( Calendar.YEAR, -2 );
+ dummy.calendar = cal;
+ dummy.date = cal.getTime();
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "n")
+ })
+ public void testFutureConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ FutureDummyEntity dummy = new FutureDummyEntity();
+
+ Set<ConstraintViolation<FutureDummyEntity>> constraintViolations =
validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+
+ Calendar cal = GregorianCalendar.getInstance();
+ cal.add( Calendar.YEAR, -1 );
+
+ dummy.calendar = cal;
+ dummy.date = cal.getTime();
+
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 2 );
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "date", "calendar" }
+ );
+
+ cal.add( Calendar.YEAR, 2 );
+ dummy.calendar = cal;
+ dummy.date = cal.getTime();
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "6", id = "a"),
+ @SpecAssertion(section = "6", id = "o")
+ })
+ public void testPatternConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+ PatternDummyEntity dummy = new PatternDummyEntity();
+
+ Set<ConstraintViolation<PatternDummyEntity>> constraintViolations =
validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+
+ dummy.pattern = "ab cd";
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(), PatternDummyEntity.class, "ab cd",
"pattern"
+ );
+
+ dummy.pattern = "wc 00";
+ constraintViolations = validator.validate( dummy );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+ }
+
class NullDummyEntity {
@Null
Object property;
@@ -365,4 +573,106 @@
@Min(101)
Long longObject;
}
+
+ class MaxDummyEntity {
+ @Max(101)
+ BigDecimal bigDecimal;
+
+ @Max(101)
+ BigInteger bigInteger;
+
+ @Max(101)
+ byte bytePrimitive;
+
+ @Max(101)
+ short shortPrimitive;
+
+ @Max(101)
+ int intPrimitive;
+
+ @Max(101)
+ long longPrimitive;
+
+ @Max(101)
+ Byte byteObject;
+
+ @Max(101)
+ Short shortObject;
+
+ @Max(101)
+ Integer intObject;
+
+ @Max(101)
+ Long longObject;
+ }
+
+ class SizeDummyEntity {
+ @Size(min = 1, max = 1)
+ String string;
+
+ @Size(min = 1, max = 1)
+ Collection<String> collection;
+
+ @Size(min = 1, max = 1)
+ Map<String, String> map;
+
+ @Size(min = 1, max = 1)
+ Integer[] integerArray;
+
+ @Size(min = 1, max = 1)
+ int[] intArray;
+ }
+
+ class DigitsDummyEntity {
+ @Digits(integer = 1, fraction = 2)
+ BigDecimal bigDecimal;
+
+ @Digits(integer = 1, fraction = 0)
+ BigInteger bigInteger;
+
+ @Digits(integer = 1, fraction = 0)
+ byte bytePrimitive;
+
+ @Digits(integer = 1, fraction = 0)
+ short shortPrimitive;
+
+ @Digits(integer = 1, fraction = 0)
+ int intPrimitive;
+
+ @Digits(integer = 1, fraction = 0)
+ long longPrimitive;
+
+ @Digits(integer = 1, fraction = 0)
+ Byte byteObject;
+
+ @Digits(integer = 1, fraction = 0)
+ Short shortObject;
+
+ @Digits(integer = 1, fraction = 0)
+ Integer intObject;
+
+ @Digits(integer = 1, fraction = 0)
+ Long longObject;
+ }
+
+ class PastDummyEntity {
+ @Past
+ Calendar calendar;
+
+ @Past
+ Date date;
+ }
+
+ class FutureDummyEntity {
+ @Future
+ Calendar calendar;
+
+ @Future
+ Date date;
+ }
+
+ class PatternDummyEntity {
+ @Pattern(regexp = "[a-z][a-z] \\d\\d")
+ String pattern;
+ }
}
\ No newline at end of file
Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-08
16:05:31 UTC (rev 16719)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-09
14:17:25 UTC (rev 16720)
@@ -211,22 +211,14 @@
</section>
<section id="2.5" title="The ConstraintValidatorFactory">
<assertion id="a">
- <text>Constraint validation implementation instances are created by a
- ConstraintValidatorFactory</text>
- </assertion>
- <assertion id="b">
<text>The default ConstraintValidatorFactory provided by the Bean
Validation provider
- implementation uses the pub- lic constraint no-arg
constructor</text>
+ implementation uses the public constraint no-arg
constructor</text>
</assertion>
- <assertion id="c">
- <text>ConstraintValidatorFactory should not cache instances as the
state of each
- instance can be altered in the ini- tialize method</text>
- </assertion>
- <assertion id="d">
+ <assertion id="b">
<text>If an exception occurs in the factory while retrieving the
ConstraintValidator
instance, the runtime exception is wrapped in a
ValidationException</text>
</assertion>
- <assertion id="e">
+ <assertion id="c">
<text>If the instance returned by the factory is null, a
ValidationException is
raised</text>
</assertion>