Author: hardy.ferentschik
Date: 2009-06-19 07:38:29 -0400 (Fri, 19 Jun 2009)
New Revision: 16840
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java
Removed:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatorImplTest.java
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
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/validatorresolution/ValidatorResolutionTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Address.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Person.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
work on the tests for the different validate methods
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java 2009-06-18
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -51,7 +51,7 @@
import org.hibernate.jsr303.tck.util.TestUtil;
import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
-import static org.hibernate.jsr303.tck.util.TestUtil.assertInvalidPropertyPaths;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
/**
* @author Hardy Ferentschik
@@ -72,7 +72,7 @@
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
person );
assertEquals( constraintViolations.size(), 3, "Wrong number of constraints"
);
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "firstName", "lastName",
"personalNumber" }
);
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-18
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/builtinconstraints/BuiltinConstraintsTest.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -54,7 +54,7 @@
import org.hibernate.jsr303.tck.util.TestUtil;
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.assertInvalidPropertyPaths;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
/**
* Tests for built-in constraints. Basically juyst checks the availabiltiy of the
build-in constraints.
@@ -177,7 +177,7 @@
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(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
);
@@ -197,7 +197,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 6 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
);
@@ -230,7 +230,7 @@
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(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
);
@@ -250,7 +250,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 6 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
);
@@ -278,7 +278,7 @@
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 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
);
@@ -298,7 +298,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 6 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
);
@@ -331,7 +331,7 @@
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 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
);
@@ -351,7 +351,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 6 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
);
@@ -395,7 +395,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 5 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "collection", "map", "string",
"integerArray", "intArray" }
);
@@ -426,7 +426,7 @@
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(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "bytePrimitive", "intPrimitive",
"longPrimitive", "shortPrimitive" }
);
@@ -446,7 +446,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 6 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "byteObject", "intObject", "longObject",
"shortObject", "bigDecimal", "bigInteger" }
);
@@ -482,7 +482,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 2 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "date", "calendar" }
);
@@ -514,7 +514,7 @@
constraintViolations = validator.validate( dummy );
assertCorrectNumberOfViolations( constraintViolations, 2 );
- assertInvalidPropertyPaths(
+ assertCorrectPropertyPaths(
constraintViolations,
new String[] { "date", "calendar" }
);
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
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/constraints/validatorresolution/ValidatorResolutionTest.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -35,7 +35,7 @@
import org.hibernate.jsr303.tck.util.TestUtil;
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.assertInvalidPropertyPaths;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
/**
* Tests for constraint validator resolution.
@@ -93,7 +93,7 @@
MinMax minMax = new MinMax( "5", 5 );
Set<ConstraintViolation<MinMax>> constraintViolations = validator.validate(
minMax );
assertCorrectNumberOfViolations( constraintViolations, 2 );
- assertInvalidPropertyPaths( constraintViolations, new String[] { "number",
"numberAsString" } );
+ assertCorrectPropertyPaths( constraintViolations, new String[] { "number",
"numberAsString" } );
}
@Test
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java 2009-06-18
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Actor.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -34,7 +34,6 @@
private List<Actor> playedWith = new ArrayList<Actor>();
public Actor() {
-
}
public Actor(String firstName, String lastName) {
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Address.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Address.java 2009-06-18
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Address.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -60,7 +60,7 @@
this.zipCode = zipCode;
}
- @Size(max = 30)
+ @Size(max = 30, message = "City name cannot be longer than 30 characters.")
@NotNull
public String getCity() {
return city;
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Person.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Person.java 2009-06-18
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Person.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -28,6 +28,6 @@
String getMiddleName();
- @NotNull
+ @NotNull(message = "Everyone has a last name.")
String getLastName();
}
\ No newline at end of file
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -0,0 +1,178 @@
+// $Id: ValidateTest.java 16824 2009-06-17 20:57:08Z hardy.ferentschik $
+/*
+* 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.validation;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.constraints.NotNull;
+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 static org.testng.Assert.fail;
+import org.testng.annotations.Test;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintType;
+import static
org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintViolationMessage;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectNumberOfViolations;
+
+/**
+ * Tests for the implementation of <code>Validator</code>.
+ *
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+(a)Classes(TestUtil.class)
+public class ValidatePropertyTest extends AbstractTest {
+
+ //TODO Needs verification
+ @Test
+ @SuppressWarnings("NullArgumentToVariableArgMethod")
+ public void testPassingNullAsGroup() {
+ Validator validator = TestUtil.getDefaultValidator();
+ Customer customer = new Customer();
+
+ try {
+ validator.validateProperty( customer, "firstName", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ @Test
+ @SpecAssertion(section = "4.1.1", id = "e")
+ public void testIllegalArgumentExceptionIsThrownForNullValue() {
+ Validator validator = TestUtil.getDefaultValidator();
+ try {
+ validator.validateProperty( null, "firstName" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ @Test
+ @SpecAssertion(section = "4.1.1", id = "e")
+ public void testValidatePropertyWithInvalidPropertyPath() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Customer customer = new Customer();
+ Order order = new Order();
+ customer.addOrder( order );
+
+ try {
+ validator.validateProperty( customer, "orders[].orderNumber" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "foobar" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "orders[].foobar" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "4.1.1", id = "d"),
+ @SpecAssertion(section = "4.1.1", id = "c")
+ })
+ public void testValidateProperty() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Address address = new Address();
+ address.setAddressline1( null );
+ address.setAddressline2( null );
+ String townInNorthWales =
"Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch";
+ address.setCity( townInNorthWales );
+
+ Set<ConstraintViolation<Address>> constraintViolations =
validator.validateProperty( address, "city" );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+
+ ConstraintViolation<Address> violation = constraintViolations.iterator().next();
+ assertCorrectConstraintType( violation, Size.class );
+ assertConstraintViolation( violation, Address.class, townInNorthWales, "city"
);
+ assertCorrectConstraintViolationMessage( violation, "City name cannot be longer
than 30 characters." );
+
+ address.setCity( "London" );
+ constraintViolations = validator.validateProperty( address, "city" );
+ assertCorrectNumberOfViolations( constraintViolations, 0 );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "4.1.1", id = "d"),
+ @SpecAssertion(section = "4.1.1", id = "c")
+ })
+ public void testValidatePropertyWithIndexedPath() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Actor clint = new Actor( "Clint", "Eastwood" );
+ Actor morgan = new Actor( "Morgan", null );
+ Actor charlie = new Actor( "Charlie", "Sheen" );
+
+ clint.addPlayedWith( charlie );
+ charlie.addPlayedWith( clint );
+ charlie.addPlayedWith( morgan );
+ morgan.addPlayedWith( charlie );
+ morgan.addPlayedWith( clint );
+ clint.addPlayedWith( morgan );
+
+ String property = "playedWith[0].playedWith[1].lastName";
+ Set<ConstraintViolation<Actor>> constraintViolations =
validator.validateProperty(
+ clint, property
+ );
+ assertCorrectNumberOfViolations( constraintViolations, 1 );
+
+ ConstraintViolation<Actor> violation = constraintViolations.iterator().next();
+ assertCorrectConstraintType( violation, NotNull.class );
+ assertConstraintViolation( violation, Actor.class, null, property );
+ assertCorrectConstraintViolationMessage( violation, "Everyone has a last
name." );
+ }
+}
\ No newline at end of file
Copied:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
(from rev 16839,
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatorImplTest.java)
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -0,0 +1,444 @@
+// $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.validation;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
+import javax.validation.Validator;
+import javax.validation.groups.Default;
+import javax.validation.metadata.BeanDescriptor;
+import javax.validation.metadata.ConstraintDescriptor;
+import javax.validation.metadata.PropertyDescriptor;
+
+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.test.audit.annotations.SpecAssertion;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+import org.testng.annotations.Test;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+
+/**
+ * Tests for the implementation of <code>Validator</code>.
+ *
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+(a)Classes(TestUtil.class)
+public class ValidateTest extends AbstractTest {
+
+
+ @Test
+ public void testWrongMethodName() {
+ try {
+ Boy boy = new Boy();
+ TestUtil.getDefaultValidator().validate( boy );
+ fail();
+ }
+ catch ( ValidationException e ) {
+ assertEquals(
+ "Annotated methods must follow the JavaBeans naming convention. age() does
not.",
+ e.getMessage(),
+ "Wrong error message"
+ );
+ }
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testNullParamterToValidatorImplConstructor() {
+ TestUtil.getDefaultValidator().getConstraintsForClass( null );
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testValidateWithNull() {
+ Validator validator = TestUtil.getDefaultValidator();
+ validator.validate( null );
+ }
+
+ @Test
+ @SuppressWarnings("NullArgumentToVariableArgMethod")
+ public void testPassingNullAsGroup() {
+ Validator validator = TestUtil.getDefaultValidator();
+ Customer customer = new Customer();
+ try {
+ validator.validate( customer, null );
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "firstName", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateValue( Customer.class, "firstName", "foobar",
null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ @Test
+ public void testValidateWithNullProperty() {
+ Validator validator = TestUtil.getDefaultValidator();
+ try {
+ validator.validate( null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( null, "firstName" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateValue( null, "firstName", "foobar" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ @Test
+ @SpecAssertion(section = "5.1", id="a")
+ public void testBasicValidation() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Customer customer = new Customer();
+ customer.setFirstName( "John" );
+
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+
+ customer.setLastName( "Doe" );
+
+ constraintViolations = validator.validate( customer );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
+ }
+
+ @Test
+ public void testMultipleValidationMethods() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Address address = new Address();
+ address.setAddressline1( null );
+ address.setAddressline2( null );
+ address.setCity(
"Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch" ); //town in
North Wales
+
+ Set<ConstraintViolation<Address>> constraintViolations =
validator.validate( address );
+ assertEquals(
+ constraintViolations.size(),
+ 3,
+ "we should have been 2 not null violation for addresslines and one length
violation for city"
+ );
+
+ constraintViolations = validator.validateProperty( address, "city" );
+ assertEquals(
+ constraintViolations.size(),
+ 1,
+ "only city should be validated"
+ );
+
+ constraintViolations = validator.validateProperty( address, "city" );
+ assertEquals(
+ constraintViolations.size(),
+ 1,
+ "only city should be validated"
+ );
+
+ constraintViolations = validator.validateValue( Address.class, "city",
"Paris" );
+ assertEquals(
+ constraintViolations.size(),
+ 0,
+ "Paris should be a valid city name."
+ );
+ }
+
+ @Test
+ public void testValidateSet() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Customer customer = new Customer();
+ customer.setFirstName( "John" );
+ customer.setLastName( "Doe" );
+
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
+
+ Order order = new Order();
+ customer.addOrder( order );
+
+ constraintViolations = validator.validate( customer );
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
+ org.testng.Assert.assertEquals( constraintViolation.getRootBean(), customer,
"Wrong root entity" );
+ org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(),
order.getOrderNumber(), "Wrong value" );
+ assertEquals( "orders[].orderNumber", constraintViolation.getPropertyPath(),
"Wrong propertyName" );
+ }
+
+ @Test
+ public void testMultiValueConstraint() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Engine engine = new Engine();
+ engine.setSerialNumber( "mail(a)foobar.com" );
+ Set<ConstraintViolation<Engine>> constraintViolations = validator.validate(
engine );
+ assertEquals( constraintViolations.size(), 2, "Wrong number of constraints"
);
+
+ engine.setSerialNumber( "ABCDEFGH1234" );
+ constraintViolations = validator.validate( engine );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+
+ engine.setSerialNumber( "ABCD-EFGH-1234" );
+ constraintViolations = validator.validate( engine );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
+ }
+
+ @Test
+ public void testGraphValidation() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Actor clint = new Actor( "Clint", "Eastwood" );
+ Actor morgan = new Actor( "Morgan", null );
+ Actor charlie = new Actor( "Charlie", "Sheen" );
+
+ clint.addPlayedWith( charlie );
+ charlie.addPlayedWith( clint );
+ charlie.addPlayedWith( morgan );
+ morgan.addPlayedWith( charlie );
+ morgan.addPlayedWith( clint );
+ clint.addPlayedWith( morgan );
+
+
+ Set<ConstraintViolation<Actor>> constraintViolations = validator.validate(
clint );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( "Everyone has a last name.", constraintViolation.getMessage(),
"Wrong message" );
+ org.testng.Assert.assertEquals( constraintViolation.getRootBean(), clint, "Wrong
root entity" );
+ org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(),
morgan.getLastName(), "Wrong value" );
+ assertEquals(
+ constraintViolation.getPropertyPath(),
"playedWith[0].playedWith[1].lastName", "Wrong propertyName"
+ );
+ }
+
+ @Test
+ public void testValidateValue() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validateValue(
+ Customer.class, "orders[0].orderNumber", null
+ );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
+ assertEquals( constraintViolation.getRootBean(), null, "Wrong root entity"
);
+ assertEquals( constraintViolation.getRootBeanClass(), Customer.class, "Wrong root
bean class" );
+ assertEquals( constraintViolation.getInvalidValue(), null, "Wrong value" );
+ assertEquals( "orders[0].orderNumber", constraintViolation.getPropertyPath(),
"Wrong propertyName" );
+
+ constraintViolations = validator.validateValue( Customer.class,
"orders[0].orderNumber", 1234 );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
+ }
+
+ @Test
+ public void testValidateValueWithInvalidPropertyPath() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ try {
+ validator.validateValue( Customer.class, "", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ assertEquals( "Invalid property path.", e.getMessage() );
+ }
+
+ try {
+ validator.validateValue( Customer.class, "foobar", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ assertEquals( "Invalid property path.", e.getMessage() );
+ }
+
+ try {
+ validator.validateValue( Customer.class, "orders[0].foobar", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ assertEquals( "Invalid property path.", e.getMessage() );
+ }
+ }
+
+ @Test
+ public void testValidateProperty() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Customer customer = new Customer();
+ Order order = new Order();
+ customer.addOrder( order );
+
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validateProperty(
+ customer, "orders[0].orderNumber"
+ );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
+ org.testng.Assert.assertEquals( constraintViolation.getRootBean(), customer,
"Wrong root entity" );
+ org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(),
order.getOrderNumber(), "Wrong value" );
+ assertEquals( "orders[0].orderNumber", constraintViolation.getPropertyPath(),
"Wrong propertyName" );
+
+ order.setOrderNumber( 1234 );
+ constraintViolations = validator.validateProperty( customer,
"orderList[0].orderNumber" );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
+ }
+
+ @Test
+ public void testValidatePropertyWithInvalidPropertyPath() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Customer customer = new Customer();
+ Order order = new Order();
+ customer.addOrder( order );
+
+ try {
+ validator.validateProperty( customer, "orders[].orderNumber" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "foobar" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "orders[].foobar" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ /**
+ * HV-108
+ */
+ @Test
+ public void testValidationIsPolymorphic() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Customer customer = new Customer();
+ customer.setFirstName( "Foo" );
+ customer.setLastName( "Bar" );
+
+ Order order = new Order();
+ customer.addOrder( order );
+
+ Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
( Person ) customer );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+
+ TestUtil.assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ Customer.class,
+ null,
+ "orders[].orderNumber"
+ );
+
+ order.setOrderNumber( 123 );
+
+ constraintViolations = validator.validate( ( Person ) customer );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
+ }
+
+ @Test
+ public void testObjectTraversion() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Customer customer = new Customer();
+ customer.setFirstName( "John" );
+ customer.setLastName( "Doe" );
+
+ for ( int i = 0; i < 100; i++ ) {
+ Order order = new Order();
+ customer.addOrder( order );
+ }
+
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate(
+ customer, Default.class, First.class, Second.class, Last.class
+ );
+ assertEquals( constraintViolations.size(), 100, "Wrong number of constraints"
);
+ }
+
+ /**
+ * HV-120
+ */
+ @Test
+ public void testConstraintDescriptorWithoutExplicitGroup() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class );
+ PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(
"orderNumber" );
+ Set<ConstraintDescriptor<?>> descriptors =
propertyDescriptor.getConstraintDescriptors();
+
+ assertEquals( descriptors.size(), 1, "There should be only one constraint
descriptor" );
+ ConstraintDescriptor<?> descriptor = descriptors.iterator().next();
+ Set<Class<?>> groups = descriptor.getGroups();
+ assertTrue( groups.size() == 1, "There should be only one group" );
+ assertEquals(
+ groups.iterator().next(),
+ Default.class,
+ "The declared constraint does not explicitly define a group, hence Default is
expected"
+ );
+ }
+}
\ No newline at end of file
Property changes on:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateValueTest.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -0,0 +1,188 @@
+// $Id: ValidateTest.java 16824 2009-06-17 20:57:08Z hardy.ferentschik $
+/*
+* 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.validation;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+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 static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+import org.testng.annotations.Test;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+
+/**
+ * Tests for the implementation of <code>Validator</code>.
+ *
+ * @author Hardy Ferentschik
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+(a)Classes(TestUtil.class)
+public class ValidateValueTest extends AbstractTest {
+
+
+ @Test
+ @SuppressWarnings("NullArgumentToVariableArgMethod")
+ public void testPassingNullAsGroup() {
+ Validator validator = TestUtil.getDefaultValidator();
+ Customer customer = new Customer();
+ try {
+ validator.validate( customer, null );
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( customer, "firstName", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateValue( Customer.class, "firstName", "foobar",
null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ @Test
+ public void testValidateWithNullProperty() {
+ Validator validator = TestUtil.getDefaultValidator();
+ try {
+ validator.validate( null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateProperty( null, "firstName" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+
+ try {
+ validator.validateValue( null, "firstName", "foobar" );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ // success
+ }
+ }
+
+ @Test
+ public void testMultipleValidationMethods() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Address address = new Address();
+ address.setAddressline1( null );
+ address.setAddressline2( null );
+ address.setCity(
"Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch" ); //town in
North Wales
+
+ Set<ConstraintViolation<Address>> constraintViolations =
validator.validate( address );
+ assertEquals(
+ constraintViolations.size(),
+ 3,
+ "we should have been 2 not null violation for addresslines and one length
violation for city"
+ );
+
+ constraintViolations = validator.validateProperty( address, "city" );
+ assertEquals(
+ constraintViolations.size(),
+ 1,
+ "only city should be validated"
+ );
+
+ constraintViolations = validator.validateProperty( address, "city" );
+ assertEquals(
+ constraintViolations.size(),
+ 1,
+ "only city should be validated"
+ );
+
+ constraintViolations = validator.validateValue( Address.class, "city",
"Paris" );
+ assertEquals(
+ constraintViolations.size(),
+ 0,
+ "Paris should be a valid city name."
+ );
+ }
+
+ @Test
+ public void testValidateValue() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ Set<ConstraintViolation<Customer>> constraintViolations =
validator.validateValue(
+ Customer.class, "orders[0].orderNumber", null
+ );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+
+ ConstraintViolation constraintViolation = constraintViolations.iterator().next();
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
+ assertEquals( constraintViolation.getRootBean(), null, "Wrong root entity"
);
+ assertEquals( constraintViolation.getRootBeanClass(), Customer.class, "Wrong root
bean class" );
+ assertEquals( constraintViolation.getInvalidValue(), null, "Wrong value" );
+ assertEquals( "orders[0].orderNumber", constraintViolation.getPropertyPath(),
"Wrong propertyName" );
+
+ constraintViolations = validator.validateValue( Customer.class,
"orders[0].orderNumber", 1234 );
+ assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
+ }
+
+ @Test
+ public void testValidateValueWithInvalidPropertyPath() {
+ Validator validator = TestUtil.getDefaultValidator();
+
+ try {
+ validator.validateValue( Customer.class, "", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ assertEquals( "Invalid property path.", e.getMessage() );
+ }
+
+ try {
+ validator.validateValue( Customer.class, "foobar", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ assertEquals( "Invalid property path.", e.getMessage() );
+ }
+
+ try {
+ validator.validateValue( Customer.class, "orders[0].foobar", null );
+ fail();
+ }
+ catch ( IllegalArgumentException e ) {
+ assertEquals( "Invalid property path.", e.getMessage() );
+ }
+ }
+}
\ No newline at end of file
Deleted:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatorImplTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatorImplTest.java 2009-06-18
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatorImplTest.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -1,444 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
http://www.apache.org/licenses/LICENSE-2.0
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-package org.hibernate.jsr303.tck.tests.validation;
-
-import java.util.Set;
-import javax.validation.ConstraintViolation;
-import javax.validation.ValidationException;
-import javax.validation.Validator;
-import javax.validation.groups.Default;
-import javax.validation.metadata.BeanDescriptor;
-import javax.validation.metadata.ConstraintDescriptor;
-import javax.validation.metadata.PropertyDescriptor;
-
-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.test.audit.annotations.SpecAssertion;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
-import org.testng.annotations.Test;
-
-import org.hibernate.jsr303.tck.util.TestUtil;
-
-/**
- * Tests for the implementation of <code>Validator</code>.
- *
- * @author Hardy Ferentschik
- */
-@Artifact(artifactType = ArtifactType.JSR303)
-(a)Classes(TestUtil.class)
-public class ValidatorImplTest extends AbstractTest {
-
-
- @Test
- public void testWrongMethodName() {
- try {
- Boy boy = new Boy();
- TestUtil.getDefaultValidator().validate( boy );
- fail();
- }
- catch ( ValidationException e ) {
- assertEquals(
- "Annotated methods must follow the JavaBeans naming convention. age() does
not.",
- e.getMessage(),
- "Wrong error message"
- );
- }
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testNullParamterToValidatorImplConstructor() {
- TestUtil.getDefaultValidator().getConstraintsForClass( null );
- }
-
- @Test(expectedExceptions = IllegalArgumentException.class)
- public void testValidateWithNull() {
- Validator validator = TestUtil.getDefaultValidator();
- validator.validate( null );
- }
-
- @Test
- @SuppressWarnings("NullArgumentToVariableArgMethod")
- public void testPassingNullAsGroup() {
- Validator validator = TestUtil.getDefaultValidator();
- Customer customer = new Customer();
- try {
- validator.validate( customer, null );
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
-
- try {
- validator.validateProperty( customer, "firstName", null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
-
- try {
- validator.validateValue( Customer.class, "firstName", "foobar",
null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
- }
-
- @Test
- public void testValidateWithNullProperty() {
- Validator validator = TestUtil.getDefaultValidator();
- try {
- validator.validate( null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
-
- try {
- validator.validateProperty( null, "firstName" );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
-
- try {
- validator.validateValue( null, "firstName", "foobar" );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
- }
-
- @Test
- @SpecAssertion(section = "5.1", id="a")
- public void testBasicValidation() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Customer customer = new Customer();
- customer.setFirstName( "John" );
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
-
- customer.setLastName( "Doe" );
-
- constraintViolations = validator.validate( customer );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
- }
-
- @Test
- public void testMultipleValidationMethods() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Address address = new Address();
- address.setAddressline1( null );
- address.setAddressline2( null );
- address.setCity(
"Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch" ); //town in
North Wales
-
- Set<ConstraintViolation<Address>> constraintViolations =
validator.validate( address );
- assertEquals(
- constraintViolations.size(),
- 3,
- "we should have been 2 not null violation for addresslines and one length
violation for city"
- );
-
- constraintViolations = validator.validateProperty( address, "city" );
- assertEquals(
- constraintViolations.size(),
- 1,
- "only city should be validated"
- );
-
- constraintViolations = validator.validateProperty( address, "city" );
- assertEquals(
- constraintViolations.size(),
- 1,
- "only city should be validated"
- );
-
- constraintViolations = validator.validateValue( Address.class, "city",
"Paris" );
- assertEquals(
- constraintViolations.size(),
- 0,
- "Paris should be a valid city name."
- );
- }
-
- @Test
- public void testValidateSet() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Customer customer = new Customer();
- customer.setFirstName( "John" );
- customer.setLastName( "Doe" );
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
-
- Order order = new Order();
- customer.addOrder( order );
-
- constraintViolations = validator.validate( customer );
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
- org.testng.Assert.assertEquals( constraintViolation.getRootBean(), customer,
"Wrong root entity" );
- org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(),
order.getOrderNumber(), "Wrong value" );
- assertEquals( "orders[].orderNumber", constraintViolation.getPropertyPath(),
"Wrong propertyName" );
- }
-
- @Test
- public void testMultiValueConstraint() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Engine engine = new Engine();
- engine.setSerialNumber( "mail(a)foobar.com" );
- Set<ConstraintViolation<Engine>> constraintViolations = validator.validate(
engine );
- assertEquals( constraintViolations.size(), 2, "Wrong number of constraints"
);
-
- engine.setSerialNumber( "ABCDEFGH1234" );
- constraintViolations = validator.validate( engine );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
-
- engine.setSerialNumber( "ABCD-EFGH-1234" );
- constraintViolations = validator.validate( engine );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
- }
-
- @Test
- public void testGraphValidation() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Actor clint = new Actor( "Clint", "Eastwood" );
- Actor morgan = new Actor( "Morgan", null );
- Actor charlie = new Actor( "Charlie", "Sheen" );
-
- clint.addPlayedWith( charlie );
- charlie.addPlayedWith( clint );
- charlie.addPlayedWith( morgan );
- morgan.addPlayedWith( charlie );
- morgan.addPlayedWith( clint );
- clint.addPlayedWith( morgan );
-
-
- Set<ConstraintViolation<Actor>> constraintViolations = validator.validate(
clint );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
- org.testng.Assert.assertEquals( constraintViolation.getRootBean(), clint, "Wrong
root entity" );
- org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(),
morgan.getLastName(), "Wrong value" );
- assertEquals(
- constraintViolation.getPropertyPath(),
"playedWith[0].playedWith[1].lastName", "Wrong propertyName"
- );
- }
-
- @Test
- public void testValidateValue() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validateValue(
- Customer.class, "orders[0].orderNumber", null
- );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
-
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
- assertEquals( constraintViolation.getRootBean(), null, "Wrong root entity"
);
- assertEquals( constraintViolation.getRootBeanClass(), Customer.class, "Wrong root
bean class" );
- assertEquals( constraintViolation.getInvalidValue(), null, "Wrong value" );
- assertEquals( "orders[0].orderNumber", constraintViolation.getPropertyPath(),
"Wrong propertyName" );
-
- constraintViolations = validator.validateValue( Customer.class,
"orders[0].orderNumber", 1234 );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
- }
-
- @Test
- public void testValidateValueWithInvalidPropertyPath() {
- Validator validator = TestUtil.getDefaultValidator();
-
- try {
- validator.validateValue( Customer.class, "", null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
- }
-
- try {
- validator.validateValue( Customer.class, "foobar", null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
- }
-
- try {
- validator.validateValue( Customer.class, "orders[0].foobar", null );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- assertEquals( "Invalid property path.", e.getMessage() );
- }
- }
-
- @Test
- public void testValidateProperty() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Customer customer = new Customer();
- Order order = new Order();
- customer.addOrder( order );
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validateProperty(
- customer, "orders[0].orderNumber"
- );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
-
- ConstraintViolation constraintViolation = constraintViolations.iterator().next();
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
- org.testng.Assert.assertEquals( constraintViolation.getRootBean(), customer,
"Wrong root entity" );
- org.testng.Assert.assertEquals( constraintViolation.getInvalidValue(),
order.getOrderNumber(), "Wrong value" );
- assertEquals( "orders[0].orderNumber", constraintViolation.getPropertyPath(),
"Wrong propertyName" );
-
- order.setOrderNumber( 1234 );
- constraintViolations = validator.validateProperty( customer,
"orderList[0].orderNumber" );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
- }
-
- @Test
- public void testValidatePropertyWithInvalidPropertyPath() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Customer customer = new Customer();
- Order order = new Order();
- customer.addOrder( order );
-
- try {
- validator.validateProperty( customer, "orders[].orderNumber" );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
-
- try {
- validator.validateProperty( customer, "" );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
-
- try {
- validator.validateProperty( customer, "foobar" );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
-
- try {
- validator.validateProperty( customer, "orders[].foobar" );
- fail();
- }
- catch ( IllegalArgumentException e ) {
- // success
- }
- }
-
- /**
- * HV-108
- */
- @Test
- public void testValidationIsPolymorphic() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Customer customer = new Customer();
- customer.setFirstName( "Foo" );
- customer.setLastName( "Bar" );
-
- Order order = new Order();
- customer.addOrder( order );
-
- Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
( Person ) customer );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
-
- TestUtil.assertConstraintViolation(
- constraintViolations.iterator().next(),
- Customer.class,
- null,
- "orders[].orderNumber"
- );
-
- order.setOrderNumber( 123 );
-
- constraintViolations = validator.validate( ( Person ) customer );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
- }
-
- @Test
- public void testObjectTraversion() {
- Validator validator = TestUtil.getDefaultValidator();
-
- Customer customer = new Customer();
- customer.setFirstName( "John" );
- customer.setLastName( "Doe" );
-
- for ( int i = 0; i < 100; i++ ) {
- Order order = new Order();
- customer.addOrder( order );
- }
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate(
- customer, Default.class, First.class, Second.class, Last.class
- );
- assertEquals( constraintViolations.size(), 100, "Wrong number of constraints"
);
- }
-
- /**
- * HV-120
- */
- @Test
- public void testConstraintDescriptorWithoutExplicitGroup() {
- Validator validator = TestUtil.getDefaultValidator();
-
- BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class );
- PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(
"orderNumber" );
- Set<ConstraintDescriptor<?>> descriptors =
propertyDescriptor.getConstraintDescriptors();
-
- assertEquals( descriptors.size(), 1, "There should be only one constraint
descriptor" );
- ConstraintDescriptor<?> descriptor = descriptors.iterator().next();
- Set<Class<?>> groups = descriptor.getGroups();
- assertTrue( groups.size() == 1, "There should be only one group" );
- assertEquals(
- groups.iterator().next(),
- Default.class,
- "The declared constraint does not explicitly define a group, hence Default is
expected"
- );
- }
-}
\ No newline at end of file
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-18
17:18:35 UTC (rev 16839)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java 2009-06-19
11:38:29 UTC (rev 16840)
@@ -80,7 +80,7 @@
}
}
- public static <T> void
assertInvalidPropertyPaths(Set<ConstraintViolation<T>> violations, String[]
propertyPaths) {
+ public static <T> void
assertCorrectPropertyPaths(Set<ConstraintViolation<T>> violations, String[]
propertyPaths) {
List<String> propertyPathsOfViolations = new ArrayList<String>();
for ( ConstraintViolation<?> violation : violations ) {
propertyPathsOfViolations.add( violation.getPropertyPath() );
Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-18
17:18:35 UTC (rev 16839)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-19
11:38:29 UTC (rev 16840)
@@ -4,12 +4,12 @@
name="JSR-303: Bean Validation" version="Revised Public Review
Draft">
<section id="2.1" title="Constraint Definition">
- <assertion id="a">
+ <assertion id="a" testable="false">
<text>An annotation is considered a constraint definition if its
retention policy
contains RUNTIME and if the annotation itself is annotated with
javax.validation.Constraint</text>
</assertion>
- <assertion id="b">
+ <assertion id="b" testable="false">
<text>Constraint annotations can target any of the following
ElementTypes: FIELD,
METHOD, TYPE, ANNOTATION_TYPE</text>
</assertion>
@@ -54,10 +54,6 @@
annotations in a special way. Each element in the value array are
processed by the
Bean Validation implementation as regular constraint
annotations.</text>
</assertion>
- <assertion id="b">
- <text>The annotation must have retention RUNTIME and can be applied on
a type, field,
- property or an other annotation</text>
- </assertion>
</section>
<section id="2.3" title="Constraint composition">
<assertion id="a">
@@ -65,8 +61,8 @@
target element and this recursively</text>
</assertion>
<assertion id="b">
- <text>Note that the main annotation and its constraint validation
implementation is
- also applied</text>
+ <text>Note that the main annotation and its constraint validation
implementation is also
+ applied</text>
</assertion>
<assertion id="c">
<text>By default, each failing constraint generates an error
report</text>
@@ -104,8 +100,7 @@
</assertion>
<assertion id="k">
<text>A composing constraint can itself be a composed constraint. In
this case,
- attribute values are overridden recursively according to the described
- rules</text>
+ attribute values are overridden recursively according to the described
rules</text>
</assertion>
<assertion id="l">
<text>If a constraint is used more than once as a composing constraint,
the multi value
@@ -158,7 +153,8 @@
<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
@@ -172,8 +168,8 @@
<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 declaration and the context in
which
- the constraint declaration is placed on (bean, property,
attribute)</text>
+ 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
@@ -378,7 +374,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>
@@ -498,32 +494,44 @@
</section>
<section id="4.1.1" title="Validation methods">
<assertion id="a">
- <text>validate(T object, Class... groups) is used to validate a given
ob- ject</text>
+ <text>validate(T object, Class... groups) is used to validate a given
object</text>
</assertion>
<assertion id="b">
- <text>A Set containing all ConstraintViolation ob- jects representing
the failing
+ <text>An IllegalArgumentException is thrown if object is null when
validate() is
+ called.</text>
+ </assertion>
+ <assertion id="c">
+ <text>A Set containing all ConstraintViolation objects representing the
failing
constraints is returned, an empty Set is returned otherwise</text>
</assertion>
- <assertion id="c">
+ <assertion id="d">
<text>validateProperty(T object, String propertyName, Class... groups)
validates a given
field or property of an object</text>
</assertion>
- <assertion id="d">
+ <assertion id="e">
+ <text>An IllegalArgumentException is thrown if object is null or
propertyName is null
+ empty or invalid when validateProperty() is called.</text>
+ </assertion>
+ <assertion id="f">
<text>The property name is the JavaBeans property name (as defined by
the JavaBeans
Introspector class)</text>
</assertion>
- <assertion id="e">
- <text>@Valid is not honored by this method</text>
+ <assertion id="g">
+ <text>@Valid is not honored by validateProperty()</text>
</assertion>
- <assertion id="f">
+ <assertion id="h">
<text>validateValue(Class beanType, String propertyName, Object value,
Class... groups)
validates the property referenced by propertyName present on beanType or
any of its
superclasses, if the property value were value.</text>
</assertion>
- <assertion id="g">
- <text>@Valid is not honored by this method</text>
+ <assertion id="i">
+ <text>An IllegalArgumentException is thrown if object is null or
propertyName is null
+ empty or invalid when validateValue() is called.</text>
</assertion>
- <assertion id="h">
+ <assertion id="j">
+ <text>@Valid is not honored by validateValue()</text>
+ </assertion>
+ <assertion id="k">
<text>If some unrecoverable failure happens during validation, a
ValidationException is
raised.</text>
</assertion>
@@ -863,7 +871,7 @@
<text>ValidatorFactory is a thread-safe object that should be built
once per deployment
unit</text>
</assertion>
- <assertion id="b" testable="false" >
+ <assertion id="b" testable="false">
<text>Validator is thread-safe too and should be considered a
lightweight object</text>
</assertion>
</section>