Author: hardy.ferentschik
Date: 2010-06-16 06:39:31 -0400 (Wed, 16 Jun 2010)
New Revision: 19746
Modified:
core/branches/Branch_3_5/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java
Log:
HHH-5281 Added ddl constraint generation for @Length without importing HV classes
Modified:
core/branches/Branch_3_5/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java
===================================================================
---
core/branches/Branch_3_5/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java 2010-06-16
09:04:23 UTC (rev 19745)
+++
core/branches/Branch_3_5/annotations/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java 2010-06-16
10:39:31 UTC (rev 19746)
@@ -39,6 +39,7 @@
/**
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
class TypeSafeActivator {
@@ -148,12 +149,17 @@
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,17 +219,31 @@
}
}
- 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<Size> sizeConstraint = (ConstraintDescriptor<Size>)
descriptor;
+ && String.class.equals( propertyDescriptor.getElementClass() ) ) {
+ @SuppressWarnings("unchecked")
+ ConstraintDescriptor<Size> sizeConstraint = ( ConstraintDescriptor<Size> )
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 );
+ }
+ }
+ }
+
/**
* Retrieve the property by path in a recursive way, including IndentifierProperty in
the loop
* If propertyName is null or empty, the IdentifierProperty is returned
Modified:
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java
===================================================================
---
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java 2010-06-16
09:04:23 UTC (rev 19745)
+++
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java 2010-06-16
10:39:31 UTC (rev 19746)
@@ -6,13 +6,15 @@
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 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();
Modified:
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java
===================================================================
---
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java 2010-06-16
09:04:23 UTC (rev 19745)
+++
core/branches/Branch_3_5/annotations/src/test/java/org/hibernate/test/annotations/beanvalidation/Tv.java 2010-06-16
10:39:31 UTC (rev 19746)
@@ -1,19 +1,22 @@
package org.hibernate.test.annotations.beanvalidation;
+import java.math.BigDecimal;
import java.math.BigInteger;
-import java.math.BigDecimal;
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.NotNull;
import javax.validation.constraints.Size;
-import javax.validation.constraints.NotNull;
-import javax.validation.Valid;
+import org.hibernate.validator.constraints.Length;
+
/**
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
@Entity
public class Tv {
@@ -21,17 +24,28 @@
@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 @@
@NotNull
public BigDecimal time;
}
-
}