[jboss-cvs] JBossAS SVN: r95510 - in projects/jpa/trunk/impl: src/main/java/org/jboss/jpa/impl and 5 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Oct 23 10:27:11 EDT 2009
Author: epbernard
Date: 2009-10-23 10:27:11 -0400 (Fri, 23 Oct 2009)
New Revision: 95510
Added:
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/JPAConstants.java
projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/beanvalidation/
projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/beanvalidation/MinimalBeanValidationTestCase.java
Modified:
projects/jpa/trunk/impl/pom.xml
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java
projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/Person.java
Log:
JBJPA-15 add more support for Bean Validation
Modified: projects/jpa/trunk/impl/pom.xml
===================================================================
--- projects/jpa/trunk/impl/pom.xml 2009-10-23 14:20:02 UTC (rev 95509)
+++ projects/jpa/trunk/impl/pom.xml 2009-10-23 14:27:11 UTC (rev 95510)
@@ -46,6 +46,19 @@
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
+ <artifactId>hibernate-validator</artifactId>
+ <version>4.0.0.GA</version>
+ <scope>test</scope>
+ </dependency>
+
+
+ <dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ <version>1.0.0.GA</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.0.Beta-1</version>
</dependency>
@@ -86,7 +99,13 @@
<dependency>
<groupId>org.jboss.metadata</groupId>
<artifactId>jboss-metadata</artifactId>
- <version>1.0.1.GA</version>
+ <version>1.0.2.Beta2</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.hibernate</groupId>
+ <artifactId>ejb3-persistence</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.jboss.naming</groupId>
Added: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/JPAConstants.java
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/JPAConstants.java (rev 0)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/JPAConstants.java 2009-10-23 14:27:11 UTC (rev 95510)
@@ -0,0 +1,15 @@
+package org.jboss.jpa.impl;
+
+/**
+ * JPA constants
+ *
+ * @author Emmanuel Bernard
+ */
+public interface JPAConstants
+{
+ /**
+ * key the Bean Validation Factory should be placed in the property Map when
+ * initializing the EntityManagerFactory
+ */
+ public static final String BEAN_VALIDATION_FACTORY = "javax.persistence.validation.factory";
+}
\ No newline at end of file
Modified: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java 2009-10-23 14:20:02 UTC (rev 95509)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java 2009-10-23 14:27:11 UTC (rev 95510)
@@ -42,11 +42,13 @@
import org.jboss.logging.Logger;
import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
import org.jboss.metadata.jpa.spec.TransactionType;
+import org.jboss.metadata.jpa.spec.SharedCacheMode;
/**
* Comment
*
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author Emmanuel Bernard
* @version $Revision$
*/
public class PersistenceUnitInfoImpl implements PersistenceUnitInfo
@@ -65,6 +67,9 @@
private PersistenceUnitTransactionType transactionType;
private URL persistenceUnitRootUrl;
private boolean excludeClasses;
+ private ValidationMode validationMode;
+ private Caching caching;
+ private String persistenceXMLSchemaVersion;
public PersistenceUnitInfoImpl()
{
@@ -97,6 +102,10 @@
this.setPersistenceUnitRootUrl(persistenceUnitRootUrl);
PersistenceUnitTransactionType transactionType = getJPATransactionType(metaData);
this.setTransactionType(transactionType);
+ this.setCaching( convertToCaching( metaData.getSharedCacheMode() ) );
+ this.setValidationMode( convertToValidationMode( metaData.getValidationMode() ) );
+ //FIXME set appropriate version when accessible from metadata
+ this.setPersistenceXMLSchemaVersion( null);
if (metaData.getProvider() != null) this.setPersistenceProviderClassName(metaData.getProvider());
/*
@@ -143,7 +152,37 @@
}
*/
}
-
+
+ private ValidationMode convertToValidationMode(org.jboss.metadata.jpa.spec.ValidationMode validationMode)
+ {
+ switch (validationMode) {
+ case AUTO:
+ return ValidationMode.AUTO;
+ case CALLBACK:
+ return ValidationMode.CALLBACK;
+ case NONE:
+ return ValidationMode.NONE;
+ default:
+ return null;
+ }
+ }
+
+ private Caching convertToCaching(SharedCacheMode sharedCacheMode)
+ {
+ switch (sharedCacheMode) {
+ case ALL:
+ return Caching.ALL;
+ case DISABLE_SELECTIVE:
+ return Caching.DISABLE_SELECTIVE;
+ case ENABLE_SELECTIVE:
+ return Caching.ENABLE_SELECTIVE;
+ case NONE:
+ return Caching.NONE;
+ default:
+ return null;
+ }
+ }
+
public void addTransformer(ClassTransformer transformer)
{
//throw new RuntimeException("NOT IMPLEMENTED");
@@ -297,21 +336,39 @@
this.excludeClasses = excludeClasses;
}
+ public ValidationMode getValidationMode()
+ {
+ return validationMode;
+ }
+
+ public void setValidationMode(ValidationMode validationMode)
+ {
+ this.validationMode = validationMode;
+ }
+
public Caching getCaching()
{
- // TODO Auto-generated method stub
- throw new RuntimeException("NYI");
+ return caching;
}
-
- public ValidationMode getValidationMode()
+
+ public void setCaching(Caching caching)
{
- // TODO Auto-generated method stub
- throw new RuntimeException("NYI");
+ this.caching = caching;
}
-
+
+ public String getPersistenceXMLSchemaVersion()
+ {
+ return persistenceXMLSchemaVersion;
+ }
+
+ //TODO remove once typo is fixed in JPA API
public String PersistenceXMLSchemaVersion()
{
- // TODO Auto-generated method stub
- throw new RuntimeException("NYI");
+ return persistenceXMLSchemaVersion;
}
+
+ public void setPersistenceXMLSchemaVersion(String persistenceXMLSchemaVersion)
+ {
+ this.persistenceXMLSchemaVersion = persistenceXMLSchemaVersion;
+ }
}
Modified: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java 2009-10-23 14:20:02 UTC (rev 95509)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java 2009-10-23 14:27:11 UTC (rev 95510)
@@ -30,14 +30,19 @@
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
+import java.util.HashMap;
+import java.util.Map;
import javax.naming.InitialContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.validation.ValidatorFactory;
+import javax.validation.Validation;
import org.hibernate.ejb.HibernatePersistence;
import org.jboss.jpa.impl.AbstractEntityManagerFactoryDelegator;
+import org.jboss.jpa.impl.JPAConstants;
import org.jboss.jpa.impl.deployment.PersistenceUnitInfoImpl;
import org.jboss.logging.Logger;
import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
@@ -51,6 +56,7 @@
* up JPA locally.
*
* @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @author Emmanuel Bernard
* @version $Revision: $
*/
public class RemotelyInjectEntityManagerFactory extends AbstractEntityManagerFactoryDelegator implements EntityManagerFactory, Serializable
@@ -107,11 +113,17 @@
pi.getProperties().put("hibernate.session_factory_name", kernelName);
}
*/
-
- // TODO: BeanValidation
+
PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
+ Map<Object, Object> properties = new HashMap<Object, Object>(1);
+ properties.put( JPAConstants.BEAN_VALIDATION_FACTORY, getValidatorFactory() );
actualFactory = pp.createContainerEntityManagerFactory(pi, null);
}
+
+ private ValidatorFactory getValidatorFactory() {
+ //FIXME get it from JNDI or the deployer
+ return Validation.buildDefaultValidatorFactory();
+ }
@Override
protected EntityManagerFactory getDelegate()
Added: projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/beanvalidation/MinimalBeanValidationTestCase.java
===================================================================
--- projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/beanvalidation/MinimalBeanValidationTestCase.java (rev 0)
+++ projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/beanvalidation/MinimalBeanValidationTestCase.java 2009-10-23 14:27:11 UTC (rev 95510)
@@ -0,0 +1,138 @@
+package org.jboss.jpa.impl.test.beanvalidation;
+
+import java.sql.DriverManager;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Properties;
+import java.net.URL;
+import javax.transaction.TransactionManager;
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
+import javax.validation.ConstraintViolationException;
+
+import org.hsqldb.jdbcDriver;
+import org.jnp.server.SingletonNamingServer;
+import org.jboss.jpa.impl.test.common.MockPersistenceUnit;
+import org.jboss.jpa.impl.test.common.Person;
+import org.jboss.jpa.impl.deployment.PersistenceUnitInfoImpl;
+import org.jboss.jpa.spi.PersistenceUnitRegistry;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import org.hibernate.ejb.HibernatePersistence;
+import org.hibernate.transaction.JBossTransactionManagerLookup;
+import com.arjuna.ats.jta.utils.JNDIManager;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class MinimalBeanValidationTestCase
+{
+ private static jdbcDriver driver;
+ private static SingletonNamingServer namingServer;
+ private static TransactionManager transactionManager;
+
+ private MockPersistenceUnit persistenceUnit;
+
+ @AfterClass
+ public static void afterClass() throws Exception
+ {
+ new InitialContext().unbind("java:/TransactionManager");
+
+ namingServer.destroy();
+
+ DriverManager.deregisterDriver(driver);
+ }
+
+ @BeforeClass
+ public static void beforeClass() throws Exception
+ {
+ driver = new jdbcDriver();
+
+ DriverManager.registerDriver(driver);
+
+ namingServer = new SingletonNamingServer();
+
+ JNDIManager.bindJTAImplementation();
+
+ transactionManager = (TransactionManager) new InitialContext().lookup("java:/TransactionManager");
+ }
+
+ @After
+ public void after() throws Exception
+ {
+ PersistenceUnitRegistry.unregister(persistenceUnit);
+
+ persistenceUnit.stop();
+ }
+
+ @Before
+ public void before() throws Exception
+ {
+ List<String> entityClassNames = new ArrayList<String>();
+ entityClassNames.add(Person.class.getName());
+
+ PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl();
+ pui.setClassLoader(Thread.currentThread().getContextClassLoader());
+ List<URL> jarFiles = new ArrayList<URL>();
+ pui.setJarFiles(jarFiles);
+ pui.setManagedClassnames(entityClassNames);
+ List<String> mappingFileNames = new ArrayList<String>();
+ pui.setMappingFileNames(mappingFileNames);
+ pui.setPersistenceProviderClassName(HibernatePersistence.class.getName());
+ pui.setPersistenceUnitName("TestPersistenceUnit");
+ Properties properties = new Properties();
+ properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:testdb");
+ properties.put("hibernate.connection.user", "sa");
+ properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
+ properties.put("hibernate.hbm2ddl.auto", "create");
+ //properties.put("hibernate.transaction.manager_lookup_class", MockTransactionManagerLookup.class.getName());
+ properties.put("hibernate.transaction.manager_lookup_class", JBossTransactionManagerLookup.class.getName());
+ pui.setProperties(properties);
+
+ MockPersistenceUnit persistenceUnit = new MockPersistenceUnit(pui);
+ this.persistenceUnit = persistenceUnit;
+ persistenceUnit.start();
+
+ PersistenceUnitRegistry.register(persistenceUnit);
+ }
+
+ @Test
+ //This does *not* test the proper creation and injection of BV as we use a mock persistenceunit
+ //in fact BV is initialized by Hibernate in this case
+ public void testBeanValidation() throws Exception
+ {
+ transactionManager.begin();
+ EntityManager em = persistenceUnit.getContainerEntityManagerFactory().createEntityManager();
+ try
+ {
+ Person p = new Person();
+ p.setId(2);
+ p.setName("Caroline");
+ p.setNickname("C"); //invalid nickname
+ em.persist(p);
+ em.joinTransaction();
+ em.flush();
+ fail("Bean validation should have been triggered");
+ }
+ catch (ConstraintViolationException e)
+ {
+ //success
+ }
+ catch (PersistenceException e)
+ {
+ assertEquals("Should be a validation exception", e.getCause().getClass(), ConstraintViolationException.class);
+ }
+ finally
+ {
+ transactionManager.rollback();
+ em.close();
+ }
+ }
+
+}
Modified: projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/Person.java
===================================================================
--- projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/Person.java 2009-10-23 14:20:02 UTC (rev 95509)
+++ projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/Person.java 2009-10-23 14:27:11 UTC (rev 95510)
@@ -23,6 +23,7 @@
import javax.persistence.Entity;
import javax.persistence.Id;
+import javax.validation.constraints.Size;
/**
* A generic entity with an id and a string for testing.
@@ -37,6 +38,7 @@
private int id;
private String name;
+ private String nickname;
@Override
public boolean equals(Object obj)
@@ -87,4 +89,15 @@
{
this.name = name;
}
+
+ @Size(min=2, max=5)
+ public String getNickname()
+ {
+ return nickname;
+ }
+
+ public void setNickname(String nickname)
+ {
+ this.nickname = nickname;
+ }
}
More information about the jboss-cvs-commits
mailing list