Author: hardy.ferentschik
Date: 2009-06-26 08:20:03 -0400 (Fri, 26 Jun 2009)
New Revision: 16957
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java
Log:
Tests for validation requirements. Includes test for HV-165 (tests for class level
constraints using groups)
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Building.java 2009-06-26
12:20:03 UTC (rev 16957)
@@ -0,0 +1,37 @@
+// $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.application;
+
+import javax.validation.constraints.Max;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Building {
+ @Max(value = 5000000, message = "Building costs are max {max} dollars.")
+ long buildingCosts;
+
+ @Max(value = 5000000, message = "Building costs are max {max} dollars.")
+ public long getBuildingCosts() {
+ return buildingCosts;
+ }
+
+ public Building(long buildingCosts) {
+ this.buildingCosts = buildingCosts;
+ }
+}
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Citizen.java 2009-06-26
12:20:03 UTC (rev 16957)
@@ -0,0 +1,34 @@
+// $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.application;
+
+import javax.validation.constraints.Pattern;
+import javax.validation.groups.Default;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@SecurityCheck(groups = { Default.class, TightSecurity.class })
+public interface Citizen {
+ @Pattern(regexp = "^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$",
+ message = "Personal number must be 10 digits with the last 4 seperated by a
dash.")
+ public String getPersonalNumber();
+}
+
+interface TightSecurity {
+}
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Person.java 2009-06-26
12:20:03 UTC (rev 16957)
@@ -0,0 +1,64 @@
+// $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.application;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@SecurityCheck(groups = Default.class)
+public abstract class Person implements Citizen {
+ @NotNull
+ String firstName;
+ String lastName;
+ String personalNumber;
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ @NotNull
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public String getPersonalNumber() {
+ return personalNumber;
+ }
+
+ public void setPersonalNumber(String personalNumber) {
+ this.personalNumber = personalNumber;
+ }
+
+ public abstract Gender getGender();
+
+ enum Gender {
+ MALE,
+ FEMALE
+ }
+}
Copied:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java
(from rev 16948,
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/constraintdefinition/AlwaysValid.java)
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SecurityCheck.java 2009-06-26
12:20:03 UTC (rev 16957)
@@ -0,0 +1,63 @@
+// $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.application;
+
+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 javax.validation.Constraint;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Documented
+@Constraint(validatedBy = SecurityCheck.SecurityCheckValidator.class)
+@Target({ TYPE, METHOD, FIELD })
+@Retention(RUNTIME)
+public @interface SecurityCheck {
+ public abstract String message() default "Security check failed.";
+
+ public abstract Class<?>[] groups() default { };
+
+ public class SecurityCheckValidator implements ConstraintValidator<SecurityCheck,
Object> {
+
+
+ public void initialize(SecurityCheck parameters) {
+
+ }
+
+ public boolean isValid(Object object, ConstraintValidatorContext
constraintValidatorContext) {
+ if ( object == null ) {
+ return true;
+ }
+
+ if ( !( object instanceof Person ) && !( object instanceof Citizen ) ) {
+ return false;
+ }
+
+ Citizen citizen = ( Citizen ) object;
+ return !"000000-0000".equals( citizen.getPersonalNumber() );
+ }
+ }
+}
\ No newline at end of file
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/SuperWoman.java 2009-06-26
12:20:03 UTC (rev 16957)
@@ -0,0 +1,36 @@
+// $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.application;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class SuperWoman extends Woman {
+ public SuperWoman() {
+ firstName = "Lois";
+ lastName = null;
+ }
+
+ public String getFirstName() {
+ return null;
+ }
+
+ public String getLastName() {
+ return "Lane";
+ }
+}
\ No newline at end of file
Copied:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java
(from rev 16948,
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinValidatorOverrideTest.java)
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/ValidationRequirementTest.java 2009-06-26
12:20:03 UTC (rev 16957)
@@ -0,0 +1,128 @@
+// $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.application;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+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 org.hibernate.jsr303.tck.util.TestUtil;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
+import static
org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessages;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
+
+/**
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+@Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
+public class ValidationRequirementTest extends AbstractTest {
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "c"),
+ @SpecAssertion(section = "3.1", id = "d"),
+ @SpecAssertion(section = "3.1.1", id = "a")
+ })
+ public void testClassLevelConstraints() {
+ Woman sarah = new Woman();
+ sarah.setFirstName( "Sarah" );
+ sarah.setLastName( "Jones" );
+ sarah.setPersonalNumber( "000000-0000" );
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<Woman>> violations = validator.validate( sarah );
+
+ assertCorrectNumberOfViolations(
+ violations, 1
+ ); // SecurityCheck for Default in Person
+ assertCorrectConstraintTypes( violations, SecurityCheck.class );
+
+ violations = validator.validate( sarah, TightSecurity.class );
+ assertCorrectNumberOfViolations(
+ violations, 1
+ ); // SecurityCheck for TightSecurity in Citizen
+ assertCorrectConstraintTypes( violations, SecurityCheck.class );
+
+ // just to make sure - validating against a group which does not have any constraints
assigned to it
+ violations = validator.validate( sarah, DummyGroup.class );
+ assertCorrectNumberOfViolations( violations, 0 );
+
+ sarah.setPersonalNumber( "740523-1234" );
+ violations = validator.validate( sarah );
+ assertCorrectNumberOfViolations( violations, 0 );
+
+ violations = validator.validate( sarah, TightSecurity.class );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "d"),
+ @SpecAssertion(section = "3.1.2", id = "a"),
+ @SpecAssertion(section = "3.1.2", id = "c")
+ })
+ public void testFieldAccess() {
+ SuperWoman superwoman = new SuperWoman();
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<SuperWoman>> violations =
validator.validateProperty( superwoman, "firstName" );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1", id = "d"),
+ @SpecAssertion(section = "3.1.2", id = "a"),
+ @SpecAssertion(section = "3.1.2", id = "d")
+ })
+ public void testPropertyAccess() {
+ SuperWoman superwoman = new SuperWoman();
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<SuperWoman>> violations =
validator.validateProperty( superwoman, "firstName" );
+ assertCorrectNumberOfViolations( violations, 0 );
+ }
+
+ @Test(enabled = false)
+ @SpecAssertions({
+ @SpecAssertion(section = "3.1.2", id = "a"),
+ @SpecAssertion(section = "3.1.2", id = "b")
+ })
+ public void testConstraintAppliedOnFieldAndProperty() {
+ Building building = new Building( 10000000 );
+
+ Validator validator = TestUtil.getDefaultValidator();
+ Set<ConstraintViolation<Building>> violations = validator.validate(
building );
+ assertCorrectNumberOfViolations( violations, 2 );
+ String expectedMessage = "Building costs are max {max} dollars.";
+ assertCorrectConstraintViolationMessages( violations, expectedMessage, expectedMessage
);
+ }
+}
+
+interface DummyGroup {
+}
\ No newline at end of file
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/application/Woman.java 2009-06-26
12:20:03 UTC (rev 16957)
@@ -0,0 +1,27 @@
+// $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.application;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Woman extends Person {
+ public Gender getGender() {
+ return Gender.FEMALE;
+ }
+}