[hibernate-commits] Hibernate SVN: r16185 - validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Mar 19 09:55:43 EDT 2009


Author: hardy.ferentschik
Date: 2009-03-19 09:55:43 -0400 (Thu, 19 Mar 2009)
New Revision: 16185

Added:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountChecker.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountValidator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorImplTest.java
Modified:
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/Account.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/BeanDescriptorImplTest.java
Log:
HV-116

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/Account.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/Account.java	2009-03-19 11:58:06 UTC (rev 16184)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/Account.java	2009-03-19 13:55:43 UTC (rev 16185)
@@ -18,12 +18,14 @@
 package org.hibernate.validation.engine.metadata;
 
 import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
 
 import org.hibernate.validation.engine.metadata.Customer;
 
 /**
  * Class with no constraints but with a cascade @Valid annotation
  */
+ at AccountChecker
 public class Account {
 	private String accountLogin;
 	private Customer customer;

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountChecker.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountChecker.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountChecker.java	2009-03-19 13:55:43 UTC (rev 16185)
@@ -0,0 +1,43 @@
+// $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.validation.engine.metadata;
+
+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;
+
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+ at Constraint(validatedBy = AccountValidator.class)
+ at Documented
+ at Target({ METHOD, FIELD, TYPE })
+ at Retention(RUNTIME)
+public @interface AccountChecker {
+
+	public abstract String message() default "Account information inconsistent.";
+
+	public abstract Class<?>[] groups() default { };
+}


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountChecker.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountValidator.java	2009-03-19 13:55:43 UTC (rev 16185)
@@ -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.validation.engine.metadata;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class AccountValidator implements ConstraintValidator<AccountChecker, Account> {
+
+	public void initialize(AccountChecker parameters) {
+	}
+
+	public boolean isValid(Account account, ConstraintValidatorContext constraintValidatorContext) {
+		return false;
+	}
+}


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/AccountValidator.java
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/BeanDescriptorImplTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/BeanDescriptorImplTest.java	2009-03-19 11:58:06 UTC (rev 16184)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/BeanDescriptorImplTest.java	2009-03-19 13:55:43 UTC (rev 16185)
@@ -19,8 +19,6 @@
 
 import java.util.Set;
 import javax.validation.BeanDescriptor;
-import javax.validation.ConstraintDescriptor;
-import javax.validation.ElementDescriptor;
 import javax.validation.PropertyDescriptor;
 import javax.validation.Validator;
 
@@ -31,10 +29,7 @@
 import static org.junit.Assert.assertTrue;
 import org.junit.Test;
 
-import org.hibernate.validation.engine.metadata.Account;
-import org.hibernate.validation.engine.metadata.Customer;
 import org.hibernate.validation.engine.Order;
-import org.hibernate.validation.engine.metadata.UnconstraintEntity;
 import org.hibernate.validation.util.TestUtil;
 
 
@@ -44,30 +39,35 @@
 public class BeanDescriptorImplTest {
 
 	@Test
-	public void testHasConstraintsAndIsBeanConstrained() {
+	public void testIsBeanConstrained() {
 		Validator validator = TestUtil.getValidator();
 		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Customer.class );
 
+		// constraint via @Valid
 		assertFalse( "There should be no direct constraints on the specified bean.", beanDescriptor.hasConstraints() );
 		assertTrue( "Bean should be constrainted due to @valid ", beanDescriptor.isBeanConstrained() );
 
+		// constraint hosted on bean itself
 		beanDescriptor = validator.getConstraintsForClass( Account.class );
-		assertTrue(
-				"Bean should be constrainted due to @valid", beanDescriptor.isBeanConstrained()
-		);
+		assertTrue( "There should be direct constraints on the specified bean.", beanDescriptor.hasConstraints() );
+		assertTrue( "Bean should be constrainted due to @valid", beanDescriptor.isBeanConstrained() );
+
+		// constraint on bean property
+		beanDescriptor = validator.getConstraintsForClass( Order.class );
+		assertFalse( "There should be no direct constraints on the specified bean.", beanDescriptor.hasConstraints() );
+		assertTrue( "Bean should be constrainted due to @NotNull", beanDescriptor.isBeanConstrained() );
 	}
 
 	@Test
 	public void testUnconstraintClass() {
 		Validator validator = TestUtil.getValidator();
-		assertFalse(
-				"There should be no constraints",
-				validator.getConstraintsForClass( UnconstraintEntity.class ).hasConstraints()
-		);
+		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( UnconstraintEntity.class );
+		assertFalse( "There should be no direct constraints on the specified bean.", beanDescriptor.hasConstraints() );
+		assertFalse( "Bean should be unconstrainted.", beanDescriptor.isBeanConstrained() );
 	}
 
 	@Test
-	public void testGetConstraintsForProperty() {
+	public void testGetConstraintForExistingConstrainedProperty() {
 		Validator validator = TestUtil.getValidator();
 		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class );
 		PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderNumber" );
@@ -75,11 +75,6 @@
 				"There should be one constraint descriptor", 1, propertyDescriptor.getConstraintDescriptors().size()
 		);
 
-		assertNull( "There should be no descriptor", beanDescriptor.getConstraintsForProperty( "foobar" ) );
-
-		// TODO Is this corect or should we get a IllegalArgumentException
-		assertNull( "There should be no descriptor", beanDescriptor.getConstraintsForProperty( null ) );
-
 		beanDescriptor = validator.getConstraintsForClass( Customer.class );
 		propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderList" );
 		assertEquals(
@@ -88,7 +83,35 @@
 		assertTrue( "The property should be cascaded", propertyDescriptor.isCascaded() );
 	}
 
+	@Test
+	public void testGetConstraintForUnConstrainedProperty() {
+		Validator validator = TestUtil.getValidator();
+		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Customer.class );
+		PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( "orderList" );
+		assertEquals(
+				"There should be no constraint descriptors", 0, propertyDescriptor.getConstraintDescriptors().size()
+		);
+		assertTrue( "The property should be cascaded", propertyDescriptor.isCascaded() );
+	}
+
+	@Test
+	public void testGetConstraintsForNonExistingProperty() {
+		Validator validator = TestUtil.getValidator();
+		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class );
+		assertNull( "There should be no descriptor", beanDescriptor.getConstraintsForProperty( "foobar" ) );
+	}
+
 	/**
+	 * @todo Is this corect or should we get a IllegalArgumentException
+	 */
+	@Test
+	public void testGetConstraintsForNullProperty() {
+		Validator validator = TestUtil.getValidator();
+		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class );
+		assertNull( "There should be no descriptor", beanDescriptor.getConstraintsForProperty( null ) );
+	}
+
+	/**
 	 * HV-95
 	 */
 	@Test
@@ -98,12 +121,20 @@
 		Set<PropertyDescriptor> constraintProperties = beanDescriptor.getConstrainedProperties();
 		assertEquals( "There should be only one property", 1, constraintProperties.size() );
 		boolean hasOrderNumber = false;
-		for(PropertyDescriptor pd : constraintProperties) {
+		for ( PropertyDescriptor pd : constraintProperties ) {
 			hasOrderNumber |= pd.getPropertyName().equals( "orderNumber" );
 		}
 		assertTrue( "Wrong property", hasOrderNumber );
+	}
 
-
+	/**
+	 * HV-95
+	 */
+	@Test
+	public void testGetConstrainedPropertiesImmutable() {
+		Validator validator = TestUtil.getValidator();
+		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Order.class );
+		Set<PropertyDescriptor> constraintProperties = beanDescriptor.getConstrainedProperties();
 		try {
 			constraintProperties.add( null );
 			fail( "Set should be immutable" );
@@ -113,7 +144,7 @@
 		}
 
 		try {
-			constraintProperties.remove( "orderNumber" );
+			constraintProperties.remove( constraintProperties.iterator().next() );
 			fail( "Set should be immutable" );
 		}
 		catch ( UnsupportedOperationException e ) {
@@ -125,28 +156,10 @@
 	 * HV-95
 	 */
 	@Test
-	public void testElementDescriptorImmutable() {
+	public void testGetConstrainedPropertiesForUnconstraintEntity() {
 		Validator validator = TestUtil.getValidator();
-		ElementDescriptor elementDescriptor = validator.getConstraintsForClass( Order.class )
-				.getConstraintsForProperty( "orderNumber" );
-		Set<ConstraintDescriptor<?>> constraintDescriptors = elementDescriptor.getConstraintDescriptors();
-		assertTrue( "There should be a ConstraintDescriptor", constraintDescriptors.size() == 1 );
-		ConstraintDescriptor<?> descriptor = constraintDescriptors.iterator().next();
-
-		try {
-			constraintDescriptors.add( descriptor );
-			fail( "Set should be immutable" );
-		}
-		catch ( UnsupportedOperationException e ) {
-
-		}
-
-		try {
-			constraintDescriptors.remove( descriptor );
-			fail( "Set should be immutable" );
-		}
-		catch ( UnsupportedOperationException e ) {
-
-		}
+		BeanDescriptor beanDescriptor = validator.getConstraintsForClass( UnconstraintEntity.class );
+		Set<PropertyDescriptor> constraintProperties = beanDescriptor.getConstrainedProperties();
+		assertEquals( "We should get the empty set.", 0, constraintProperties.size() );
 	}
 }

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorImplTest.java (from rev 16165, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/BeanDescriptorImplTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorImplTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/metadata/ElementDescriptorImplTest.java	2009-03-19 13:55:43 UTC (rev 16185)
@@ -0,0 +1,65 @@
+// $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.validation.engine.metadata;
+
+import java.util.Set;
+import javax.validation.ConstraintDescriptor;
+import javax.validation.ElementDescriptor;
+import javax.validation.Validator;
+
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+
+import org.hibernate.validation.engine.Order;
+import org.hibernate.validation.util.TestUtil;
+
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ElementDescriptorImplTest {
+	/**
+	 * HV-95
+	 */
+	@Test
+	public void testElementDescriptorImmutable() {
+		Validator validator = TestUtil.getValidator();
+		ElementDescriptor elementDescriptor = validator.getConstraintsForClass( Order.class )
+				.getConstraintsForProperty( "orderNumber" );
+		Set<ConstraintDescriptor<?>> constraintDescriptors = elementDescriptor.getConstraintDescriptors();
+		assertTrue( "There should be a ConstraintDescriptor", constraintDescriptors.size() == 1 );
+		ConstraintDescriptor<?> descriptor = constraintDescriptors.iterator().next();
+
+		try {
+			constraintDescriptors.add( descriptor );
+			fail( "Set should be immutable" );
+		}
+		catch ( UnsupportedOperationException e ) {
+
+		}
+
+		try {
+			constraintDescriptors.remove( descriptor );
+			fail( "Set should be immutable" );
+		}
+		catch ( UnsupportedOperationException e ) {
+
+		}
+	}
+}
\ No newline at end of file




More information about the hibernate-commits mailing list