From ef90b00b148e74f985926b1c4110d4eb8aa45318 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Wed, 16 Jun 2010 10:43:40 +0000 Subject: [PATCH] HHH-5281 apply @Length ddl constraints (without loading HV classes) git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19747 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../cfg/beanvalidation/TypeSafeActivator.java | 34 +++++++++++++++---- .../annotations/beanvalidation/DDLTest.java | 17 ++++++++-- .../test/annotations/beanvalidation/Tv.java | 25 ++++++++++---- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java b/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java index d0657298f8..027236e18b 100644 --- a/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java +++ b/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java @@ -39,6 +39,7 @@ import org.hibernate.util.ReflectHelper; /** * @author Emmanuel Bernard + * @author Hardy Ferentschik */ class TypeSafeActivator { @@ -148,12 +149,17 @@ class TypeSafeActivator { if ( canApplyNotNull ) { hasNotNull = hasNotNull || applyNotNull( property, descriptor ); } + + // apply bean validation specific constraints applyDigits( property, descriptor ); applySize( property, descriptor, propertyDesc ); applyMin( property, descriptor ); applyMax( property, descriptor ); - //pass an empty set as composing constraints inherit the main constraint and thus are matching already + // apply hibernate validator specific constraints - we cannot import any HV specific classes though! + applyLength( property, descriptor, propertyDesc ); + + // pass an empty set as composing constraints inherit the main constraint and thus are matching already hasNotNull = hasNotNull || applyConstraints( descriptor.getComposingConstraints(), property, propertyDesc, null, @@ -213,14 +219,28 @@ class TypeSafeActivator { } } - private static void applySize(Property property, ConstraintDescriptor descriptor, PropertyDescriptor propertyDesc) { + private static void applySize(Property property, ConstraintDescriptor descriptor, PropertyDescriptor propertyDescriptor) { if ( Size.class.equals( descriptor.getAnnotation().annotationType() ) - && String.class.equals( propertyDesc.getElementClass() ) ) { - @SuppressWarnings( "unchecked" ) - ConstraintDescriptor sizeConstraint = (ConstraintDescriptor) descriptor; + && String.class.equals( propertyDescriptor.getElementClass() ) ) { + @SuppressWarnings("unchecked") + ConstraintDescriptor sizeConstraint = ( ConstraintDescriptor ) descriptor; int max = sizeConstraint.getAnnotation().max(); - Column col = (Column) property.getColumnIterator().next(); - if ( max < Integer.MAX_VALUE ) col.setLength( max ); + Column col = ( Column ) property.getColumnIterator().next(); + if ( max < Integer.MAX_VALUE ) { + col.setLength( max ); + } + } + } + + private static void applyLength(Property property, ConstraintDescriptor descriptor, PropertyDescriptor propertyDescriptor) { + if ( "org.hibernate.validator.constraints.Length".equals(descriptor.getAnnotation().annotationType().getName()) + && String.class.equals( propertyDescriptor.getElementClass() ) ) { + @SuppressWarnings("unchecked") + int max = (Integer) descriptor.getAttributes().get( "max" ); + Column col = ( Column ) property.getColumnIterator().next(); + if ( max < Integer.MAX_VALUE ) { + col.setLength( max ); + } } } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java b/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java index 22d95b401c..194126b7db 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java @@ -6,13 +6,15 @@ import org.hibernate.mapping.Property; import org.hibernate.test.annotations.TestCase; /** + * Test verifying that DDL constraints get applied when Bean Validation / Hibernate Validator are enabled. + * * @author Emmanuel Bernard + * @author Hardy Ferentschik */ public class DDLTest extends TestCase { public void testBasicDDL() { PersistentClass classMapping = getCfg().getClassMapping( Address.class.getName() ); - //new ClassValidator( Address.class, ResourceBundle.getBundle("messages", Locale.ENGLISH) ).apply( classMapping ); Column stateColumn = (Column) classMapping.getProperty( "state" ).getColumnIterator().next(); assertEquals( stateColumn.getLength(), 3 ); Column zipColumn = (Column) classMapping.getProperty( "zip" ).getColumnIterator().next(); @@ -23,9 +25,20 @@ public class DDLTest extends TestCase { public void testApplyOnIdColumn() throws Exception { PersistentClass classMapping = getCfg().getClassMapping( Tv.class.getName() ); Column serialColumn = (Column) classMapping.getIdentifierProperty().getColumnIterator().next(); - assertEquals( "Vaidator annotation not applied on ids", 2, serialColumn.getLength() ); + assertEquals( "Validator annotation not applied on ids", 2, serialColumn.getLength() ); } + /** + * HHH-5281 + * + * @throws Exception in case the test fails + */ + public void testLengthConstraint() throws Exception { + PersistentClass classMapping = getCfg().getClassMapping( Tv.class.getName() ); + Column modelColumn = (Column) classMapping.getProperty( "model" ).getColumnIterator().next(); + assertEquals( modelColumn.getLength(), 5 ); + } + public void testApplyOnManyToOne() throws Exception { PersistentClass classMapping = getCfg().getClassMapping( TvOwner.class.getName() ); Column serialColumn = (Column) classMapping.getProperty( "tv" ).getColumnIterator().next(); diff --git a/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java b/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java index e92e9cd2c1..73c4502010 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java @@ -1,19 +1,22 @@ package org.hibernate.test.annotations.beanvalidation; -import java.math.BigInteger; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Date; +import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.Id; -import javax.persistence.Embeddable; +import javax.validation.Valid; import javax.validation.constraints.Future; import javax.validation.constraints.Min; -import javax.validation.constraints.Size; import javax.validation.constraints.NotNull; -import javax.validation.Valid; +import javax.validation.constraints.Size; + +import org.hibernate.validator.constraints.Length; /** * @author Emmanuel Bernard + * @author Hardy Ferentschik */ @Entity public class Tv { @@ -21,17 +24,28 @@ public class Tv { @Id @Size(max = 2) public String serial; + + @Length(max=5) + public String model; + public int size; + @Size(max = 2) public String name; + @Future public Date expDate; + @Size(min = 0) public String description; + @Min(1000) public BigInteger lifetime; - @NotNull @Valid + + @NotNull + @Valid public Tuner tuner; + @Valid public Recorder recorder; @@ -46,5 +60,4 @@ public class Tv { @NotNull public BigDecimal time; } - }