[hibernate-commits] Hibernate SVN: r17190 - in beanvalidation/trunk/validation-tck/src/main: java/org/hibernate/jsr303/tck/tests/validation and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jul 22 09:20:08 EDT 2009


Author: hardy.ferentschik
Date: 2009-07-22 09:20:08 -0400 (Wed, 22 Jul 2009)
New Revision: 17190

Added:
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/NotEmpty.java
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateWithGroups.java
Modified:
   beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/metadata/NotEmpty.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/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/util/TestUtil.java
   beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
tests for using groups in the different validation methods

Modified: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/metadata/NotEmpty.java
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/metadata/NotEmpty.java	2009-07-22 12:24:20 UTC (rev 17189)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/metadata/NotEmpty.java	2009-07-22 13:20:08 UTC (rev 17190)
@@ -41,7 +41,7 @@
 @NotNull
 @Size
 public @interface NotEmpty {
-	public abstract String message() default "{org.hibernate.validation.constraints.NotEmpty.message}";
+	public abstract String message() default "cannot be empty";
 
 	public abstract Class<?>[] groups() default { };
 

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-07-22 12:24:20 UTC (rev 17189)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/Address.java	2009-07-22 13:20:08 UTC (rev 17190)
@@ -17,39 +17,40 @@
 */
 package org.hibernate.jsr303.tck.tests.validation;
 
-import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Size;
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
 
 /**
  * @author Hardy Ferentschik
  */
 public class Address {
-	@NotNull
-	@Size(max = 30)
-	private String addressline1;
 
-	@NotNull
-	@Size(max = 30)
-	private String addressline2;
+	@NotEmpty(groups = Minimal.class)
+	private String street;
 
+	@NotNull(message = "You have to specify a city.")
+	@Size(max = 30, message = "City name cannot be longer than {max} characters.")
+	private String city;
+
+	@NotEmpty(groups = { Minimal.class, Default.class })
 	private String zipCode;
 
-	private String city;
 
-	public String getAddressline1() {
-		return addressline1;
+	public String getStreet1() {
+		return street;
 	}
 
-	public void setAddressline1(String addressline1) {
-		this.addressline1 = addressline1;
+	public void setStreet(String street) {
+		this.street = street;
 	}
 
-	public String getAddressline2() {
-		return addressline2;
+	public String getCity() {
+		return city;
 	}
 
-	public void setAddressline2(String addressline2) {
-		this.addressline2 = addressline2;
+	public void setCity(String city) {
+		this.city = city;
 	}
 
 	public String getZipCode() {
@@ -60,13 +61,6 @@
 		this.zipCode = zipCode;
 	}
 
-	@Size(max = 30, message = "City name cannot be longer than 30 characters.")
-	@NotNull(message = "You have to specify a city.")
-	public String getCity() {
-		return city;
+	public interface Minimal {
 	}
-
-	public void setCity(String city) {
-		this.city = city;
-	}
 }
\ No newline at end of file

Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/NotEmpty.java (from rev 17143, beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/metadata/NotEmpty.java)
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/NotEmpty.java	                        (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/NotEmpty.java	2009-07-22 13:20:08 UTC (rev 17190)
@@ -0,0 +1,52 @@
+// $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.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.ConstraintPayload;
+import javax.validation.OverridesAttribute;
+import javax.validation.ReportAsSingleViolation;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Documented
+ at Constraint(validatedBy = { })
+ at Target({ METHOD, FIELD })
+ at Retention(RUNTIME)
+ at ReportAsSingleViolation
+ at NotNull
+ at Size
+public @interface NotEmpty {
+	public abstract String message() default "cannot be empty";
+
+	public abstract Class<?>[] groups() default { };
+
+	public abstract Class<? extends ConstraintPayload>[] payload() default { };
+
+	@OverridesAttribute(constraint = Size.class, name = "min")
+	public abstract int min() default 5;
+}
\ No newline at end of file

Modified: 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	2009-07-22 12:24:20 UTC (rev 17189)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidatePropertyTest.java	2009-07-22 13:20:08 UTC (rev 17190)
@@ -64,7 +64,6 @@
 		}
 	}
 
-
 	@Test
 	@SpecAssertion(section = "4.1.1", id = "e")
 	public void testIllegalArgumentExceptionIsThrownForNullValue() {
@@ -171,8 +170,8 @@
 		Validator validator = TestUtil.getDefaultValidator();
 
 		Address address = new Address();
-		address.setAddressline1( null );
-		address.setAddressline2( null );
+		address.setStreet( null );
+		address.setZipCode( null );
 		String townInNorthWales = "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch";
 		address.setCity( townInNorthWales );
 

Modified: 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/ValidateTest.java	2009-07-22 12:24:20 UTC (rev 17189)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateTest.java	2009-07-22 13:20:08 UTC (rev 17190)
@@ -23,6 +23,7 @@
 import javax.validation.ValidationException;
 import javax.validation.Validator;
 import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
 import javax.validation.groups.Default;
 import javax.validation.metadata.BeanDescriptor;
 import javax.validation.metadata.ConstraintDescriptor;
@@ -44,6 +45,7 @@
 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 static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectConstraintTypes;
 
 /**
  * Tests for the implementation of <code>Validator</code>.
@@ -116,12 +118,13 @@
 		Validator validator = TestUtil.getDefaultValidator();
 
 		Address address = new Address();
-		address.setAddressline1( null );
-		address.setAddressline2( null );
+		address.setStreet( null );
+		address.setZipCode( null );
 		address.setCity( "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch" ); //town in North Wales
 
 		Set<ConstraintViolation<Address>> constraintViolations = validator.validate( address );
-		assertCorrectNumberOfViolations( constraintViolations, 3 );
+		assertCorrectNumberOfViolations( constraintViolations, 2 );
+		assertCorrectConstraintTypes(constraintViolations, Size.class, NotEmpty.class );
 	}
 
 	@Test

Copied: beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateWithGroups.java (from rev 17189, 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/ValidateWithGroups.java	                        (rev 0)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/validation/ValidateWithGroups.java	2009-07-22 13:20:08 UTC (rev 17190)
@@ -0,0 +1,130 @@
+// $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.Validator;
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
+
+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.assertCorrectNumberOfViolations;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertCorrectPropertyPaths;
+
+/**
+ * Tests for the implementation of {@code Validator}.
+ *
+ * @author Hardy Ferentschik
+ */
+ at Artifact(artifactType = ArtifactType.JSR303)
+ at Classes({ TestUtil.class, TestUtil.PathImpl.class, TestUtil.NodeImpl.class })
+public class ValidateWithGroups extends AbstractTest {
+
+	@Test
+	@SpecAssertions({
+			@SpecAssertion(section = "4.1.2", id = "a"),
+			@SpecAssertion(section = "4.1.2", id = "b")
+	})
+	public void testCorrectGroupsAreAppliedForValidate() {
+		Validator validator = TestUtil.getDefaultValidator();
+
+		Set<ConstraintViolation<Address>> constraintViolations = validator.validate( new Address() );
+		assertCorrectNumberOfViolations( constraintViolations, 2 );
+		assertCorrectConstraintTypes( constraintViolations, NotNull.class, NotEmpty.class );
+		assertCorrectPropertyPaths( constraintViolations, "city", "zipCode" );
+
+		constraintViolations = validator.validate( new Address(), Default.class );
+		assertCorrectNumberOfViolations( constraintViolations, 2 );
+		assertCorrectConstraintTypes( constraintViolations, NotNull.class, NotEmpty.class );
+		assertCorrectPropertyPaths( constraintViolations, "city", "zipCode" );
+
+		constraintViolations = validator.validate( new Address(), Address.Minimal.class );
+		assertCorrectNumberOfViolations( constraintViolations, 2 );
+		assertCorrectConstraintTypes( constraintViolations, NotEmpty.class, NotEmpty.class );
+		assertCorrectPropertyPaths( constraintViolations, "street", "zipCode" );
+
+		constraintViolations = validator.validate( new Address(), Default.class, Address.Minimal.class );
+		assertCorrectNumberOfViolations( constraintViolations, 3 );
+		assertCorrectConstraintTypes( constraintViolations, NotNull.class, NotEmpty.class, NotEmpty.class );
+		assertCorrectPropertyPaths( constraintViolations, "city", "street", "zipCode" );
+	}
+
+	@Test
+	@SpecAssertions({
+			@SpecAssertion(section = "4.1.2", id = "a"),
+			@SpecAssertion(section = "4.1.2", id = "b")
+	})
+	public void testCorrectGroupsAreAppliedForValidateProperty() {
+		Validator validator = TestUtil.getDefaultValidator();
+
+		Set<ConstraintViolation<Address>> constraintViolations = validator.validateProperty( new Address(), "city" );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		assertCorrectConstraintTypes( constraintViolations, NotNull.class );
+		assertCorrectPropertyPaths( constraintViolations, "city" );
+
+		constraintViolations = validator.validateProperty( new Address(), "city", Default.class );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		assertCorrectConstraintTypes( constraintViolations, NotNull.class );
+		assertCorrectPropertyPaths( constraintViolations, "city" );
+
+		constraintViolations = validator.validateProperty( new Address(), "city", Address.Minimal.class );
+		assertCorrectNumberOfViolations( constraintViolations, 0 );
+
+		constraintViolations = validator.validateProperty( new Address(), "street", Address.Minimal.class );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		assertCorrectConstraintTypes( constraintViolations, NotEmpty.class );
+		assertCorrectPropertyPaths( constraintViolations, "street" );
+	}
+
+	@Test
+	@SpecAssertions({
+			@SpecAssertion(section = "4.1.2", id = "a"),
+			@SpecAssertion(section = "4.1.2", id = "b")
+	})
+	public void testCorrectGroupsAreAppliedForValidateValue() {
+		Validator validator = TestUtil.getDefaultValidator();
+
+		Set<ConstraintViolation<Address>> constraintViolations = validator.validateValue( Address.class, "city", null );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		assertCorrectConstraintTypes( constraintViolations, NotNull.class );
+		assertCorrectPropertyPaths( constraintViolations, "city" );
+
+		constraintViolations = validator.validateValue( Address.class, "city", null, Default.class );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		assertCorrectConstraintTypes( constraintViolations, NotNull.class );
+		assertCorrectPropertyPaths( constraintViolations, "city" );
+
+		constraintViolations = validator.validateValue( Address.class, "city", null, Address.Minimal.class );
+		assertCorrectNumberOfViolations( constraintViolations, 0 );
+
+		constraintViolations = validator.validateValue( Address.class, "street", null, Address.Minimal.class );
+		assertCorrectNumberOfViolations( constraintViolations, 1 );
+		assertCorrectConstraintTypes( constraintViolations, NotEmpty.class );
+		assertCorrectPropertyPaths( constraintViolations, "street" );
+	}
+}
\ 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-07-22 12:24:20 UTC (rev 17189)
+++ beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/util/TestUtil.java	2009-07-22 13:20:08 UTC (rev 17190)
@@ -102,7 +102,7 @@
 		for ( Class<?> expectedConstraintType : expectedConsraintTypes ) {
 			assertTrue(
 					actualConstraintTypes.contains( expectedConstraintType.getName() ),
-					"The constraint type " + expectedConstraintType.getName() + " should have been violated."
+					"The constraint type " + expectedConstraintType.getName() + " is not in the list of actual violated constraint types: " + actualConstraintTypes
 			);
 		}
 	}

Modified: beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml	2009-07-22 12:24:20 UTC (rev 17189)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml	2009-07-22 13:20:08 UTC (rev 17190)
@@ -564,8 +564,7 @@
     </section>
     <section id="4.1.2" title="groups">
         <assertion id="a">
-            <text>All constraints belonging to the targeted group are applied during the Section
-                3.5</text>
+            <text>All constraints belonging to the targeted group are applied during the validation routine.</text>
         </assertion>
         <assertion id="b">
             <text>If no group is passed, the Default group is assumed</text>
@@ -574,9 +573,6 @@
             <text>When more than one group is evaluated and passed to the various validate methods,
                 order is not constrained</text>
         </assertion>
-        <assertion id="d">
-            <text>If the group definition is invalid, a GroupDefinitionException is raised</text>
-        </assertion>
     </section>
     <section id="4.2" title="ConstraintViolation">
         <assertion id="a">



More information about the hibernate-commits mailing list