add test on Bean Validation in HEM

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18206 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-12-11 09:12:10 +00:00
parent edd5bed074
commit 36fddaf4fc
6 changed files with 99 additions and 16 deletions

View File

@ -76,21 +76,6 @@
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<testResources>
<testResource>

View File

@ -125,7 +125,7 @@ public class BeanValidationEventListener implements
StringBuilder builder = new StringBuilder();
builder.append( "validation failed for classes " );
builder.append( classNames );
builder.append( " during" );
builder.append( " during " );
builder.append( operation.getName() );
builder.append( " time for groups " );
builder.append( toString( groups ) );

View File

@ -39,6 +39,16 @@
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!--
Only really needed for the antrun plugin defined below (which in turn is only really needed

View File

@ -0,0 +1,38 @@
package org.hibernate.ejb.test.beanvalidation;
import java.math.BigDecimal;
import javax.persistence.EntityManager;
import javax.validation.ConstraintViolationException;
import org.hibernate.ejb.test.TestCase;
/**
* @author Emmanuel Bernard
*/
public class BeanValidationTest extends TestCase {
public void testBeanValidationIntegration() {
CupHolder ch = new CupHolder();
ch.setRadius( new BigDecimal( "12" ) );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist( ch );
em.flush();
fail("invalid object should not be persisted");
}
catch ( ConstraintViolationException e ) {
assertEquals( 1, e.getConstraintViolations().size() );
}
em.getTransaction().rollback();
em.close();
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {
CupHolder.class
};
}
}

View File

@ -0,0 +1,39 @@
package org.hibernate.ejb.test.beanvalidation;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
/**
* @author Emmanuel Bernard
*/
@Entity
public class CupHolder {
@Id
@GeneratedValue
//@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer id;
private BigDecimal radius;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Max( value = 10, message = "Radius way out")
@NotNull
public BigDecimal getRadius() {
return radius;
}
public void setRadius(BigDecimal radius) {
this.radius = radius;
}
}

View File

@ -472,6 +472,17 @@
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Beta1</version>
</dependency>
<!-- set the optional Bean Validation and Hibernate Validator to be used throughout the project -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
</dependencies>
</dependencyManagement>