Add test for builtin constraints [JSR 303 TCK Result] 138 of 257 (53.70%) Pass with 11 Failures and 7 Errors. Review at http://gwt-code-reviews.appspot.com/1458801 Review by: rchandia@google.com git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10330 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/test-super/org/hibernate/jsr303/tck/super/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java b/user/test-super/org/hibernate/jsr303/tck/super/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java new file mode 100644 index 0000000..fc61fb1 --- /dev/null +++ b/user/test-super/org/hibernate/jsr303/tck/super/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java
@@ -0,0 +1,794 @@ +// $Id: BuiltinConstraintsTest.java 17620 2009-10-04 19:19:28Z hardy.ferentschik +// $ +/* + * JBoss, Home of Professional Open Source Copyright 2009, Red Hat, Inc. and/or + * its affiliates, 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.builtinconstraints; + +import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation; +import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations; +import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths; + +import org.hibernate.jsr303.tck.util.TestUtil; +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.testng.annotations.Test; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Collection; +import java.util.Date; +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.DecimalMax; +import javax.validation.constraints.DecimalMin; +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; + +/** + * Tests for built-in constraints. Basically just checks the availabiltiy of the + * build-in constraints. + * + * Modified by Google: + * <ul> + * <li>Removed Calendar</li> + * </ul> + * + * @author Hardy Ferentschik + */ +@Artifact(artifactType = ArtifactType.JSR303) +@Classes({TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class}) +public class BuiltinConstraintsTest extends AbstractTest { + + class AssertFalseDummyEntity { + @AssertFalse + boolean primitiveBoolean; + + @AssertFalse + Boolean objectBoolean; + + public Boolean isObjectBoolean() { + return objectBoolean; + } + + public boolean isPrimitiveBoolean() { + return primitiveBoolean; + } + + public void setObjectBoolean(Boolean objectBoolean) { + this.objectBoolean = objectBoolean; + } + + public void setPrimitiveBoolean(boolean primitiveBoolean) { + this.primitiveBoolean = primitiveBoolean; + } + } + + class AssertTrueDummyEntity { + @AssertTrue + boolean primitiveBoolean; + + @AssertTrue + Boolean objectBoolean; + + public Boolean isObjectBoolean() { + return objectBoolean; + } + + public boolean isPrimitiveBoolean() { + return primitiveBoolean; + } + + public void setObjectBoolean(Boolean objectBoolean) { + this.objectBoolean = objectBoolean; + } + + public void setPrimitiveBoolean(boolean primitiveBoolean) { + this.primitiveBoolean = primitiveBoolean; + } + } + + class DecimalMaxDummyEntity { + @DecimalMax("101.000000000") + BigDecimal bigDecimal; + + @DecimalMax("1.01E+2") + BigInteger bigInteger; + + @DecimalMax("101") + byte bytePrimitive; + + @DecimalMax("101") + short shortPrimitive; + + @DecimalMax("101") + int intPrimitive; + + @DecimalMax("101") + long longPrimitive; + + @DecimalMax("101") + Byte byteObject; + + @DecimalMax("101") + Short shortObject; + + @DecimalMax("101") + Integer intObject; + + @DecimalMax("101") + Long longObject; + } + + class DecimalMinDummyEntity { + @DecimalMin("101.000000000") + BigDecimal bigDecimal; + + @DecimalMin("1.01E+2") + BigInteger bigInteger; + + @DecimalMin("101") + byte bytePrimitive; + + @DecimalMin("101") + short shortPrimitive; + + @DecimalMin("101") + int intPrimitive; + + @DecimalMin("101") + long longPrimitive; + + @DecimalMin("101") + Byte byteObject; + + @DecimalMin("101") + Short shortObject; + + @DecimalMin("101") + Integer intObject; + + @DecimalMin("101") + Long longObject; + } + + 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 FutureDummyEntity { + // @Future + // Calendar calendar; + + @Future + Date date; + } + + 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 MinDummyEntity { + @Min(101) + BigDecimal bigDecimal; + + @Min(101) + BigInteger bigInteger; + + @Min(101) + byte bytePrimitive; + + @Min(101) + short shortPrimitive; + + @Min(101) + int intPrimitive; + + @Min(101) + long longPrimitive; + + @Min(101) + Byte byteObject; + + @Min(101) + Short shortObject; + + @Min(101) + Integer intObject; + + @Min(101) + Long longObject; + } + + class NotNullDummyEntity { + @NotNull + Object property; + + public Object getProperty() { + return property; + } + + public void setProperty(Object property) { + this.property = property; + } + } + + class NullDummyEntity { + @Null + Object property; + + public Object getProperty() { + return property; + } + + public void setProperty(Object property) { + this.property = property; + } + } + + class PastDummyEntity { + // @Past + // Calendar calendar; + + @Past + Date date; + } + + class PatternDummyEntity { + @Pattern(regexp = "[a-z][a-z] \\d\\d") + String pattern; + } + + 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; + } + + @Test + @SpecAssertions({ + @SpecAssertion(section = "6", id = "a"), + @SpecAssertion(section = "6", id = "f")}) + public void testAssertFalseConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + AssertFalseDummyEntity dummy = new AssertFalseDummyEntity(); + dummy.setPrimitiveBoolean(true); + + Set<ConstraintViolation<AssertFalseDummyEntity>> constraintViolations = validator + .validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + assertConstraintViolation(constraintViolations.iterator().next(), + AssertFalseDummyEntity.class, true, "primitiveBoolean"); + + dummy.setPrimitiveBoolean(false); + dummy.setObjectBoolean(Boolean.TRUE); + + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + assertConstraintViolation(constraintViolations.iterator().next(), + AssertFalseDummyEntity.class, Boolean.TRUE, "objectBoolean"); + + dummy.setObjectBoolean(Boolean.FALSE); + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 0); + } + + @Test + @SpecAssertions({ + @SpecAssertion(section = "6", id = "a"), + @SpecAssertion(section = "6", id = "e")}) + public void testAssertTrueConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + AssertTrueDummyEntity dummy = new AssertTrueDummyEntity(); + + Set<ConstraintViolation<AssertTrueDummyEntity>> constraintViolations = validator + .validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + assertConstraintViolation(constraintViolations.iterator().next(), + AssertTrueDummyEntity.class, false, "primitiveBoolean"); + + dummy.setPrimitiveBoolean(true); + dummy.setObjectBoolean(Boolean.FALSE); + + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + assertConstraintViolation(constraintViolations.iterator().next(), + AssertTrueDummyEntity.class, Boolean.FALSE, "objectBoolean"); + + dummy.setObjectBoolean(Boolean.TRUE); + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 0); + } + + @Test + @SpecAssertions({ + @SpecAssertion(section = "6", id = "a"), + @SpecAssertion(section = "6", id = "j")}) + public void testDecimalMaxConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + DecimalMaxDummyEntity dummy = new DecimalMaxDummyEntity(); + + dummy.intPrimitive = 102; + dummy.longPrimitive = 1234; + dummy.bytePrimitive = 102; + dummy.shortPrimitive = 102; + + Set<ConstraintViolation<DecimalMaxDummyEntity>> 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); + assertCorrectPropertyPaths(constraintViolations, "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); + assertCorrectPropertyPaths(constraintViolations, "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 + @SpecAssertions({ + @SpecAssertion(section = "6", id = "a"), + @SpecAssertion(section = "6", id = "i")}) + public void testDecimalMinConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + DecimalMinDummyEntity dummy = new DecimalMinDummyEntity(); + + Set<ConstraintViolation<DecimalMinDummyEntity>> 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); + assertCorrectPropertyPaths(constraintViolations, "bytePrimitive", + "intPrimitive", "longPrimitive", "shortPrimitive"); + + 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); + assertCorrectPropertyPaths(constraintViolations, "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 = "l")}) + public void testDigitsConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + 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); + assertCorrectPropertyPaths(constraintViolations, "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); + assertCorrectPropertyPaths(constraintViolations, "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 = "n")}) + public void testFutureConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + 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); + // assertCorrectPropertyPaths(constraintViolations, "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 = "h")}) + public void testMaxConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + 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); + assertCorrectPropertyPaths(constraintViolations, "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); + assertCorrectPropertyPaths(constraintViolations, "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 + @SpecAssertions({ + @SpecAssertion(section = "6", id = "a"), + @SpecAssertion(section = "6", id = "g")}) + public void testMinConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + 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); + assertCorrectPropertyPaths(constraintViolations, "bytePrimitive", + "intPrimitive", "longPrimitive", "shortPrimitive"); + + 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); + assertCorrectPropertyPaths(constraintViolations, "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 = "d")}) + public void testNotNullConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + NotNullDummyEntity dummy = new NotNullDummyEntity(); + Set<ConstraintViolation<NotNullDummyEntity>> constraintViolations = validator + .validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + assertConstraintViolation(constraintViolations.iterator().next(), + NotNullDummyEntity.class, null, "property"); + + dummy.setProperty(new Object()); + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 0); + } + + @Test + @SpecAssertions({ + @SpecAssertion(section = "6", id = "a"), + @SpecAssertion(section = "6", id = "c")}) + public void testNullConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + NullDummyEntity dummy = new NullDummyEntity(); + Object foo = new Object(); + dummy.setProperty(foo); + Set<ConstraintViolation<NullDummyEntity>> constraintViolations = validator + .validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + + assertConstraintViolation(constraintViolations.iterator().next(), + NullDummyEntity.class, foo, "property"); + + dummy.setProperty(null); + 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.getValidatorUnderTest(); + 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); + // assertCorrectPropertyPaths(constraintViolations, "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.getValidatorUnderTest(); + 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); + } + + @Test + @SpecAssertions({ + @SpecAssertion(section = "6", id = "a"), + @SpecAssertion(section = "6", id = "k")}) + public void testSizeConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + 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); + assertCorrectPropertyPaths(constraintViolations, "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); + } +}
diff --git a/user/test/com/google/gwt/validation/tck/BuiltinConstraintsGwtSuite.java b/user/test/com/google/gwt/validation/tck/BuiltinConstraintsGwtSuite.java new file mode 100644 index 0000000..ffa3fa9 --- /dev/null +++ b/user/test/com/google/gwt/validation/tck/BuiltinConstraintsGwtSuite.java
@@ -0,0 +1,35 @@ +/* + * Copyright 2010 Google Inc. + * + * 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 com.google.gwt.validation.tck; + +import junit.framework.Test; + +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsGwtTest; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinValidatorOverrideGwtTest; +import org.hibernate.jsr303.tck.util.TckTestSuiteWrapper; + +/** + * Tck Tests for the {@code constraints builtinconstraints} package. + */ +public class BuiltinConstraintsGwtSuite { + public static Test suite() { + TckTestSuiteWrapper suite = new TckTestSuiteWrapper( + "TCK for GWT Validation, constraints builtinconstraints package"); + suite.addTestSuite(BuiltinConstraintsGwtTest.class); + suite.addTestSuite(BuiltinValidatorOverrideGwtTest.class); + return suite; + } +}
diff --git a/user/test/org/hibernate/jsr303/tck/tests/ValidationTck.gwt.xml b/user/test/org/hibernate/jsr303/tck/tests/ValidationTck.gwt.xml index 8a58706..a7a1bf9 100644 --- a/user/test/org/hibernate/jsr303/tck/tests/ValidationTck.gwt.xml +++ b/user/test/org/hibernate/jsr303/tck/tests/ValidationTck.gwt.xml
@@ -23,4 +23,5 @@ <source path=""> <include name="none" /> </source> + <super-source path="super" /> </module> \ No newline at end of file
diff --git a/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsGwtTest.java b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsGwtTest.java new file mode 100644 index 0000000..36ad433 --- /dev/null +++ b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsGwtTest.java
@@ -0,0 +1,152 @@ +/* + * Copyright 2010 Google Inc. + * + * 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.builtinconstraints; + +import com.google.gwt.junit.client.GWTTestCase; + +import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations; +import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths; + +import org.hibernate.jsr303.tck.util.TestUtil; + +import java.util.Date; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validator; +import javax.validation.constraints.Future; +import javax.validation.constraints.Past; + +/** + * Test wrapper for {@link BuiltinConstraintsTest}. + * + * <p> + * NOTE: Test the Future and Past constraints here by hand because Hibernate + * does not use the super source version when creating the beanDescriptor for + * use at compile time. + */ +public class BuiltinConstraintsGwtTest extends GWTTestCase { + + class FutureDummyEntity { + + @Future + Date date; + } + + class PastDummyEntity { + + @Past + Date date; + } + + private static final int DAYS_IN_MILLIS = 24 * 60 * 60 * 1000; + + private final BuiltinConstraintsTest delegate = new BuiltinConstraintsTest(); + + @Override + public String getModuleName() { + return "org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.TckTest"; + } + + public void testAssertFalseConstraint() { + delegate.testAssertFalseConstraint(); + } + + public void testAssertTrueConstraint() { + delegate.testAssertTrueConstraint(); + } + + public void testDecimalMaxConstraint() { + delegate.testDecimalMaxConstraint(); + } + + public void testDecimalMinConstraint() { + delegate.testDecimalMinConstraint(); + } + + public void testDigitsConstraint() { + delegate.testDigitsConstraint(); + } + + public void testFutureConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + FutureDummyEntity dummy = new FutureDummyEntity(); + + Set<ConstraintViolation<FutureDummyEntity>> constraintViolations = validator + .validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 0); + + Date now = new Date(); + dummy.date = new Date(now.getTime() - DAYS_IN_MILLIS); + + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + assertCorrectPropertyPaths(constraintViolations, "date"); + + dummy.date = new Date(now.getTime() + DAYS_IN_MILLIS); + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 0); + } + + public void testMaxConstraint() { + delegate.testMaxConstraint(); + } + + public void testMinConstraint() { + delegate.testMinConstraint(); + } + + public void testNotNullConstraint() { + delegate.testNotNullConstraint(); + } + + public void testNullConstraint() { + delegate.testNullConstraint(); + } + + public void testPastConstraint() { + Validator validator = TestUtil.getValidatorUnderTest(); + PastDummyEntity dummy = new PastDummyEntity(); + + Set<ConstraintViolation<PastDummyEntity>> constraintViolations = validator + .validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 0); + + Date now = new Date(); + dummy.date = new Date(now.getTime() + DAYS_IN_MILLIS); + + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 1); + assertCorrectPropertyPaths(constraintViolations, "date"); + + dummy.date = new Date(now.getTime() - DAYS_IN_MILLIS); + constraintViolations = validator.validate(dummy); + assertCorrectNumberOfViolations(constraintViolations, 0); + } + + public void testPatternConstraint() { + delegate.testPatternConstraint(); + } + + public void testSizeConstraint() { + delegate.testSizeConstraint(); + } + + public String toString() { + return delegate.toString(); + } + +}
diff --git a/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinValidatorOverrideGwtTest.java b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinValidatorOverrideGwtTest.java new file mode 100644 index 0000000..98a0952 --- /dev/null +++ b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinValidatorOverrideGwtTest.java
@@ -0,0 +1,37 @@ +/* + * Copyright 2010 Google Inc. + * + * 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.builtinconstraints; + +import com.google.gwt.junit.client.GWTTestCase; + +import org.hibernate.jsr303.tck.util.client.Failing; + +/** + * Test wrapper for {@link BuiltinValidatorOverrideGwtTest}. + */ +public class BuiltinValidatorOverrideGwtTest extends GWTTestCase { + + + @Override + public String getModuleName() { + return "org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.TckTest"; + } + + @Failing(issue = 6285) + public void testXmlConfiguredValidatorConfigurationHasPrecedence() { + fail("TODO(nchalko): Pass the overrides to hibernate at gwt compile time fromthe GWT module."); + } +}
diff --git a/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/TckTest.gwt.xml b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/TckTest.gwt.xml new file mode 100644 index 0000000..1009a69 --- /dev/null +++ b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/TckTest.gwt.xml
@@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.0.1/distro-source/core/src/gwt-module.dtd"> +<!-- + Copyright 2010 Google Inc. + + 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. +--> +<module> + <inherits name="org.hibernate.jsr303.tck.tests.ValidationTck" /> + <source path=""> + <include name="*.java" /> + </source> + <replace-with class="org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.TckTestValidatorFactory"> + <when-type-is class="javax.validation.ValidatorFactory"/> + </replace-with> +</module> \ No newline at end of file
diff --git a/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/TckTestValidatorFactory.java b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/TckTestValidatorFactory.java new file mode 100644 index 0000000..0365820 --- /dev/null +++ b/user/test/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/TckTestValidatorFactory.java
@@ -0,0 +1,60 @@ +/* + * Copyright 2010 Google Inc. + * + * 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.builtinconstraints; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.validation.client.AbstractGwtValidatorFactory; +import com.google.gwt.validation.client.GwtValidation; +import com.google.gwt.validation.client.impl.AbstractGwtValidator; + +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsGwtTest.FutureDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsGwtTest.PastDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.AssertFalseDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.AssertTrueDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.DecimalMaxDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.DecimalMinDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.DigitsDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.MaxDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.MinDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.NotNullDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.NullDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.PatternDummyEntity; +import org.hibernate.jsr303.tck.tests.constraints.builtinconstraints.BuiltinConstraintsTest.SizeDummyEntity; + +import javax.validation.Validator; + +/** + * {@link AbstractGwtValidatorFactory} implementation that uses + * {@link com.google.gwt.validation.client.GwtValidation GwtValidation}. + */ +public final class TckTestValidatorFactory extends AbstractGwtValidatorFactory { + /** + * Marker Interface for {@link GWT#create(Class)}. + */ + @GwtValidation(value = { + AssertFalseDummyEntity.class, AssertTrueDummyEntity.class, + DecimalMinDummyEntity.class, DecimalMaxDummyEntity.class, + DigitsDummyEntity.class, FutureDummyEntity.class, MinDummyEntity.class, + MaxDummyEntity.class, NullDummyEntity.class, NotNullDummyEntity.class, + PastDummyEntity.class, PatternDummyEntity.class, SizeDummyEntity.class}) + public static interface GwtValidator extends Validator { + } + + @Override + public AbstractGwtValidator createValidator() { + return GWT.create(GwtValidator.class); + } +}