[jboss-cvs] JBossAS SVN: r94850 - in projects/jpa/trunk/impl: src/main/java/org/jboss/jpa/impl and 11 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Oct 14 06:37:26 EDT 2009


Author: wolfc
Date: 2009-10-14 06:37:25 -0400 (Wed, 14 Oct 2009)
New Revision: 94850

Added:
   projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/AbstractEntityManagerFactoryDelegator.java
   projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/
   projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/
   projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/MockPersistenceUnit.java
   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/tx/
   projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/tx/test/
   projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/tx/test/TxScopedTestCase.java
   projects/jpa/trunk/impl/src/test/resources/
   projects/jpa/trunk/impl/src/test/resources/jndi.properties
   projects/jpa/trunk/impl/src/test/resources/log4j.xml
Modified:
   projects/jpa/trunk/impl/
   projects/jpa/trunk/impl/.classpath
   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/injection/InjectedEntityManagerFactory.java
   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/tx/TransactionScopedEntityManager.java
Log:
JBJPA-15: implemented JPA 2 interfaces


Property changes on: projects/jpa/trunk/impl
___________________________________________________________________
Name: svn:ignore
   - eclipse-target
target

   + eclipse-target
target
ObjectStore


Modified: projects/jpa/trunk/impl/.classpath
===================================================================
--- projects/jpa/trunk/impl/.classpath	2009-10-14 10:27:10 UTC (rev 94849)
+++ projects/jpa/trunk/impl/.classpath	2009-10-14 10:37:25 UTC (rev 94850)
@@ -2,6 +2,7 @@
 <classpath>
 	<classpathentry kind="src" output="eclipse-target/classes" path="src/main/java"/>
 	<classpathentry kind="src" output="eclipse-target/tests-classes" path="src/test/java"/>
+	<classpathentry excluding="**/*.java" kind="src" output="eclipse-target/tests-classes" path="src/test/resources"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
 	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
 	<classpathentry kind="output" path="eclipse-target/classes"/>

Modified: projects/jpa/trunk/impl/pom.xml
===================================================================
--- projects/jpa/trunk/impl/pom.xml	2009-10-14 10:27:10 UTC (rev 94849)
+++ projects/jpa/trunk/impl/pom.xml	2009-10-14 10:37:25 UTC (rev 94850)
@@ -12,7 +12,7 @@
   <modelVersion>4.0.0</modelVersion>
 
   <artifactId>jboss-jpa-impl</artifactId>
-  <version>1.0.0-SNAPSHOT</version>
+  <version>2.0.0-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>JBoss Container Managed JPA Implementation</name>
   <description>
@@ -20,7 +20,24 @@
     classes for the different JPA interfaces.
   </description>
   
+  <properties>
+    <version.org.jboss.logging>2.1.0.GA</version.org.jboss.logging>
+  </properties>
+  
   <dependencies>
+    <dependency>
+      <groupId>hsqldb</groupId>
+      <artifactId>hsqldb</artifactId>
+      <version>1.8.0.10</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- For 'mocking' a TransactionManager we use JBossTS -->
+    <dependency>
+      <groupId>jboss.jbossts</groupId>
+      <artifactId>jbossjta</artifactId>
+      <version>4.4.0.GA</version>
+      <scope>test</scope>
+    </dependency>
   	<dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
@@ -30,7 +47,7 @@
     <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-entitymanager</artifactId>
-      <version>3.4.0.GA</version>
+      <version>3.5.0.Beta-1</version>
     </dependency>
     <!-- 
   	<dependency>
@@ -57,13 +74,54 @@
     </dependency>
     <dependency>
       <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-log4j</artifactId>
+      <version>${version.org.jboss.logging}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
       <artifactId>jboss-logging-spi</artifactId>
-      <version>2.1.0.GA</version>
+      <version>${version.org.jboss.logging}</version>
     </dependency>
     <dependency>
       <groupId>org.jboss.metadata</groupId>
       <artifactId>jboss-metadata</artifactId>
       <version>1.0.1.GA</version>
     </dependency>
+    <dependency>
+      <groupId>org.jboss.naming</groupId>
+      <artifactId>jnpserver</artifactId>
+      <version>5.0.3.GA</version>
+      <scope>test</scope>
+    </dependency>
+    
+    <!-- For Arjuna / JBoss Transaction -->
+    <dependency>
+      <groupId>apache-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.1.0.jboss</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>oswego-concurrent</groupId>
+      <artifactId>concurrent</artifactId>
+      <version>1.3.4-jboss-update1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>jboss.jbossts</groupId>
+      <artifactId>jbossts-common</artifactId>
+      <version>4.4.0.GA</version>
+      <scope>test</scope>
+    </dependency>
+    
+    <!-- Hibernate core logging -->
+    <dependency>
+      <groupId>org.jboss.slf4j</groupId>
+      <artifactId>slf4j-jboss-logging</artifactId>
+      <version>1.0.0.GA</version>
+      <scope>test</scope>
+    </dependency>
+    
   </dependencies>
 </project>

Added: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/AbstractEntityManagerFactoryDelegator.java
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/AbstractEntityManagerFactoryDelegator.java	                        (rev 0)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/AbstractEntityManagerFactoryDelegator.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.impl;
+
+import java.util.Map;
+import java.util.Set;
+
+import javax.persistence.Cache;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnitUtil;
+import javax.persistence.criteria.QueryBuilder;
+import javax.persistence.metamodel.Metamodel;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public abstract class AbstractEntityManagerFactoryDelegator implements EntityManagerFactory
+{
+   private static final long serialVersionUID = 1L;
+   
+   public EntityManager createEntityManager()
+   {
+      return getDelegate().createEntityManager();
+   }
+
+   public EntityManager createEntityManager(@SuppressWarnings("unchecked") Map map)
+   {
+      return getDelegate().createEntityManager(map);
+   }
+
+   public Cache getCache()
+   {
+      return getDelegate().getCache();
+   }
+
+   protected abstract EntityManagerFactory getDelegate();
+   
+   public Metamodel getMetamodel()
+   {
+      return getDelegate().getMetamodel();
+   }
+
+   public PersistenceUnitUtil getPersistenceUnitUtil()
+   {
+      return getDelegate().getPersistenceUnitUtil();
+   }
+
+   public Map<String, Object> getProperties()
+   {
+      return getDelegate().getProperties();
+   }
+
+   public QueryBuilder getQueryBuilder()
+   {
+      return getDelegate().getQueryBuilder();
+   }
+
+   public Set<String> getSupportedProperties()
+   {
+      return getDelegate().getSupportedProperties();
+   }
+
+   public boolean isOpen()
+   {
+      return getDelegate().isOpen();
+   }
+}

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-14 10:27:10 UTC (rev 94849)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -31,6 +31,8 @@
 
 import javax.naming.Context;
 import javax.naming.NamingException;
+import javax.persistence.Caching;
+import javax.persistence.ValidationMode;
 import javax.persistence.spi.ClassTransformer;
 import javax.persistence.spi.PersistenceUnitInfo;
 import javax.persistence.spi.PersistenceUnitTransactionType;
@@ -295,4 +297,21 @@
       this.excludeClasses = excludeClasses;
    }
 
+   public Caching getCaching()
+   {
+      // TODO Auto-generated method stub
+      throw new RuntimeException("NYI");
+   }
+   
+   public ValidationMode getValidationMode()
+   {
+      // TODO Auto-generated method stub
+      throw new RuntimeException("NYI");
+   }
+   
+   public String PersistenceXMLSchemaVersion()
+   {
+      // TODO Auto-generated method stub
+      throw new RuntimeException("NYI");
+   }
 }

Modified: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/InjectedEntityManagerFactory.java
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/InjectedEntityManagerFactory.java	2009-10-14 10:27:10 UTC (rev 94849)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/InjectedEntityManagerFactory.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -25,12 +25,10 @@
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import java.util.Map;
 
-import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 
-import org.jboss.jpa.impl.tx.TransactionScopedEntityManager;
+import org.jboss.jpa.impl.AbstractEntityManagerFactoryDelegator;
 import org.jboss.jpa.spi.PersistenceUnit;
 import org.jboss.jpa.spi.PersistenceUnitRegistry;
 
@@ -40,7 +38,7 @@
  * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @version $Revision$
  */
-public class InjectedEntityManagerFactory implements EntityManagerFactory, Externalizable
+public class InjectedEntityManagerFactory extends AbstractEntityManagerFactoryDelegator implements EntityManagerFactory, Externalizable
 {
    private static final long serialVersionUID = 1L;
    
@@ -57,7 +55,7 @@
       this.persistenceUnit = persistenceUnit;
    }
 
-
+   @Override
    public EntityManagerFactory getDelegate()
    {
       return delegate;
@@ -77,29 +75,8 @@
       delegate = persistenceUnit.getContainerEntityManagerFactory();
    }
 
-   public EntityManager createEntityManager()
-   {
-      return getDelegate().createEntityManager();
-   }
-
-   @SuppressWarnings("unchecked")
-   public EntityManager createEntityManager(Map map)
-   {
-      return delegate.createEntityManager(map);
-   }
-   
-   public EntityManager getEntityManager()
-   {
-      return new TransactionScopedEntityManager(persistenceUnit);
-   }
-
    public void close()
    {
       throw new IllegalStateException("It is illegal to close an injected EntityManagerFactory");
    }
-
-   public boolean isOpen()
-   {
-      return getDelegate().isOpen();
-   }
 }

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-14 10:27:10 UTC (rev 94849)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -27,20 +27,17 @@
 import java.io.Serializable;
 import java.net.URL;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
-import java.util.Map;
 import java.util.Properties;
-import java.util.Set;
 
 import javax.naming.InitialContext;
-import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.spi.PersistenceProvider;
 import javax.persistence.spi.PersistenceUnitTransactionType;
 
 import org.hibernate.ejb.HibernatePersistence;
+import org.jboss.jpa.impl.AbstractEntityManagerFactoryDelegator;
 import org.jboss.jpa.impl.deployment.PersistenceUnitInfoImpl;
 import org.jboss.logging.Logger;
 import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
@@ -56,7 +53,7 @@
  * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
  * @version $Revision: $
  */
-public class RemotelyInjectEntityManagerFactory implements EntityManagerFactory, Serializable
+public class RemotelyInjectEntityManagerFactory extends AbstractEntityManagerFactoryDelegator implements EntityManagerFactory, Serializable
 {
    private static final Logger log = Logger.getLogger(RemotelyInjectEntityManagerFactory.class);
    
@@ -111,20 +108,17 @@
       }
       */
       
+      // TODO: BeanValidation
       PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
       actualFactory = pp.createContainerEntityManagerFactory(pi, null);      
    }
    
-   public EntityManager createEntityManager()
+   @Override
+   protected EntityManagerFactory getDelegate()
    {
-      return actualFactory.createEntityManager();
+      return actualFactory;
    }
-
-   public EntityManager createEntityManager(@SuppressWarnings("unchecked") Map map)
-   {
-      return actualFactory.createEntityManager(map);
-   }
-
+   
    protected String getJaccContextId()
    {
       //return di.getSimpleName();
@@ -195,11 +189,6 @@
       }
    }
    
-   private static List<String> safeList(Set<String> set)
-   {
-      return (set == null || set.isEmpty()) ? Collections.<String>emptyList() : new ArrayList<String>(set);
-   }
-
    private void writeObject(ObjectOutputStream out) throws IOException
    {
       log.trace("writeObject");

Modified: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/tx/TransactionScopedEntityManager.java
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/tx/TransactionScopedEntityManager.java	2009-10-14 10:27:10 UTC (rev 94849)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/tx/TransactionScopedEntityManager.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -25,12 +25,19 @@
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
 
 import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.FlushModeType;
 import javax.persistence.LockModeType;
 import javax.persistence.Query;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.QueryBuilder;
+import javax.persistence.metamodel.Metamodel;
 
 import org.hibernate.Session;
 import org.hibernate.ejb.HibernateEntityManager;
@@ -84,6 +91,21 @@
       if ( persistenceUnit == null ) throw new IOException( "Unable to find persistence unit in registry: " + kernelName );
    }
 
+   /**
+    * EJB 3.0 Persistence 5.6.1:
+    * If the entity manager is invoked outside the scope of a transaction, any entities loaded from the database
+    * will immediately become detached at the end of the method call.
+    */
+   private void detachEntitiesIfNoTx(EntityManager em)
+   {
+      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+   }
+   
+   private void verifyInTx()
+   {
+      persistenceUnit.verifyInTx();
+   }
+   
    public Object getDelegate()
    {
       return getEntityManager().getDelegate();
@@ -91,7 +113,7 @@
 
    public void joinTransaction()
    {
-      persistenceUnit.verifyInTx();
+      verifyInTx();
       getEntityManager().joinTransaction();
    }
 
@@ -107,21 +129,21 @@
 
    public void lock(Object entity, LockModeType lockMode)
    {
-      persistenceUnit.verifyInTx();
+      verifyInTx();
       getEntityManager().lock(entity, lockMode);
    }
 
    public <T> T getReference(Class<T> entityClass, Object primaryKey)
    {
       EntityManager em = getEntityManager();
-      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+      detachEntitiesIfNoTx(em);
       try
       {
          return em.getReference(entityClass, primaryKey);
       }
       finally
       {
-         if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+         detachEntitiesIfNoTx(em);
       }
    }
 
@@ -133,73 +155,73 @@
    public Query createQuery(String ejbqlString)
    {
       EntityManager em = getEntityManager();
-      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+      detachEntitiesIfNoTx(em);
       return em.createQuery(ejbqlString);
    }
 
    public Query createNamedQuery(String name)
    {
       EntityManager em = getEntityManager();
-      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+      detachEntitiesIfNoTx(em);
       return em.createNamedQuery(name);
    }
 
    public Query createNativeQuery(String sqlString)
    {
       EntityManager em = getEntityManager();
-      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+      detachEntitiesIfNoTx(em);
       return em.createNativeQuery(sqlString);
    }
 
    public Query createNativeQuery(String sqlString, Class resultClass)
    {
       EntityManager em = getEntityManager();
-      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+      detachEntitiesIfNoTx(em);
       return em.createNativeQuery(sqlString, resultClass);
    }
 
    public Query createNativeQuery(String sqlString, String resultSetMapping)
    {
       EntityManager em = getEntityManager();
-      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+      detachEntitiesIfNoTx(em);
       return em.createNativeQuery(sqlString, resultSetMapping);
    }
 
    public <A> A find(Class<A> entityClass, Object primaryKey)
    {
       EntityManager em = getEntityManager();
-      if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+      detachEntitiesIfNoTx(em);
       try
       {
          return em.find(entityClass, primaryKey);
       }
       finally
       {
-         if (!persistenceUnit.isInTx()) em.clear(); // em will be closed by interceptor
+         detachEntitiesIfNoTx(em);
       }
    }
 
    public void persist(Object entity)
    {
-      persistenceUnit.verifyInTx();
+      verifyInTx();
       getEntityManager().persist(entity);
    }
 
    public <A> A merge(A entity)
    {
-      persistenceUnit.verifyInTx();
+      verifyInTx();
       return (A) getEntityManager().merge(entity);
    }
 
    public void remove(Object entity)
    {
-      persistenceUnit.verifyInTx();
+      verifyInTx();
       getEntityManager().remove(entity);
    }
 
    public void refresh(Object entity)
    {
-      persistenceUnit.verifyInTx();
+      verifyInTx();
       getEntityManager().refresh(entity);
    }
 
@@ -210,7 +232,7 @@
 
    public void flush()
    {
-      persistenceUnit.verifyInTx();
+      verifyInTx();
       getEntityManager().flush();
    }
 
@@ -241,4 +263,136 @@
       return persistenceUnit.getTransactionScopedEntityManager();
    }
 
+   public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass)
+   {
+      EntityManager em = getEntityManager();
+      detachEntitiesIfNoTx(em);
+      return em.createNamedQuery(name, resultClass);
+   }
+
+   public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery)
+   {
+      EntityManager em = getEntityManager();
+      detachEntitiesIfNoTx(em);
+      return em.createQuery(criteriaQuery);
+   }
+
+   public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass)
+   {
+      EntityManager em = getEntityManager();
+      detachEntitiesIfNoTx(em);
+      return em.createQuery(qlString, resultClass);
+   }
+
+   public void detach(Object entity)
+   {
+      getEntityManager().detach(entity);
+   }
+
+   public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode)
+   {
+      EntityManager em = getEntityManager();
+      detachEntitiesIfNoTx(em);
+      try
+      {
+         return em.find(entityClass, primaryKey, lockMode);
+      }
+      finally
+      {
+         detachEntitiesIfNoTx(em);
+      }
+   }
+
+   public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode, Map<String, Object> properties)
+   {
+      EntityManager em = getEntityManager();
+      detachEntitiesIfNoTx(em);
+      try
+      {
+         return em.find(entityClass, primaryKey, lockMode, properties);
+      }
+      finally
+      {
+         detachEntitiesIfNoTx(em);
+      }
+   }
+
+   public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties)
+   {
+      EntityManager em = getEntityManager();
+      detachEntitiesIfNoTx(em);
+      try
+      {
+         return em.find(entityClass, primaryKey, properties);
+      }
+      finally
+      {
+         detachEntitiesIfNoTx(em);
+      }
+   }
+
+   public EntityManagerFactory getEntityManagerFactory()
+   {
+      return getEntityManager().getEntityManagerFactory();
+   }
+
+   public LockModeType getLockMode(Object entity)
+   {
+      verifyInTx();
+      return getEntityManager().getLockMode(entity);
+   }
+
+   public Metamodel getMetamodel()
+   {
+      return getEntityManager().getMetamodel();
+   }
+
+   public Map<String, Object> getProperties()
+   {
+      return getEntityManager().getProperties();
+   }
+
+   public QueryBuilder getQueryBuilder()
+   {
+      return getEntityManager().getQueryBuilder();
+   }
+
+   public Set<String> getSupportedProperties()
+   {
+      return getEntityManager().getSupportedProperties();
+   }
+
+   public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties)
+   {
+      verifyInTx();
+      getEntityManager().lock(entity, lockMode, properties);
+   }
+
+   public void refresh(Object entity, Map<String, Object> properties)
+   {
+      verifyInTx();
+      getEntityManager().refresh(entity, properties);
+   }
+
+   public void refresh(Object entity, LockModeType lockMode)
+   {
+      verifyInTx();
+      getEntityManager().refresh(entity, lockMode);
+   }
+
+   public void refresh(Object entity, LockModeType lockMode, Map<String, Object> properties)
+   {
+      verifyInTx();
+      getEntityManager().refresh(entity, lockMode, properties);
+   }
+
+   public void setProperty(String propertyName, Object value)
+   {
+      getEntityManager().setProperty(propertyName, value);
+   }
+
+   public <T> T unwrap(Class<T> cls)
+   {
+      return getEntityManager().unwrap(cls);
+   }
 }

Added: projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/MockPersistenceUnit.java
===================================================================
--- projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/MockPersistenceUnit.java	                        (rev 0)
+++ projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/MockPersistenceUnit.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -0,0 +1,106 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.impl.test.common;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.TransactionRequiredException;
+import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceUnitInfo;
+
+import org.jboss.jpa.spi.PersistenceUnit;
+import org.jboss.jpa.spi.XPCResolver;
+import org.jboss.tm.TxUtils;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MockPersistenceUnit implements PersistenceUnit
+{
+   private PersistenceUnitInfo persistenceUnitInfo;
+   private EntityManagerFactory entityManagerFactory;
+   private XPCResolver xpcResolver;
+   
+   public MockPersistenceUnit(PersistenceUnitInfo pui)
+   {
+      this.persistenceUnitInfo = pui;
+      this.xpcResolver = new XPCResolver()
+      {
+         public EntityManager getExtendedPersistenceContext(String kernelName)
+         {
+            // TODO
+            return null;
+         }
+      };
+   }
+   
+   public EntityManagerFactory getContainerEntityManagerFactory()
+   {
+      return entityManagerFactory;
+   }
+
+   public String getName()
+   {
+      return persistenceUnitInfo.getPersistenceUnitName();
+   }
+
+   public EntityManager getTransactionScopedEntityManager()
+   {
+      // TODO
+      EntityManager em = entityManagerFactory.createEntityManager();
+      return em;
+   }
+
+   public XPCResolver getXPCResolver()
+   {
+      return xpcResolver;
+   }
+
+   public boolean isInTx()
+   {
+      return TxUtils.isActive();
+   }
+
+   public void start() throws Exception
+   {
+      Class<?> providerClass = Thread.currentThread().getContextClassLoader().loadClass(persistenceUnitInfo.getPersistenceProviderClassName());
+      PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
+      Map<?, ?> properties = new HashMap<Object, Object>();
+      entityManagerFactory = pp.createContainerEntityManagerFactory(persistenceUnitInfo, properties);
+   }
+   
+   public void stop()
+   {
+      
+   }
+   
+   public void verifyInTx()
+   {
+      if (!TxUtils.isActive())
+         throw new TransactionRequiredException("Transaction must be active to access EntityManager");
+   }
+
+}

Copied: projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/Person.java (from rev 94507, projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/Person.java)
===================================================================
--- projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/Person.java	                        (rev 0)
+++ projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/common/Person.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.impl.test.common;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * A generic entity with an id and a string for testing.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Entity
+public class Person
+{
+   @Id
+   private int id;
+   
+   private String name;
+
+   @Override
+   public boolean equals(Object obj)
+   {
+      if(!(obj instanceof Person))
+         return false;
+      Person other = (Person) obj;
+      if(other.id != this.id)
+         return false;
+      // TODO: name
+      return true;
+   }
+   
+   @Override
+   public int hashCode()
+   {
+      return id;
+   }
+   
+   /**
+    * @return the id
+    */
+   public int getId()
+   {
+      return id;
+   }
+   
+   /**
+    * @return the name
+    */
+   public String getName()
+   {
+      return name;
+   }
+   
+   /**
+    * @param id the id to set
+    */
+   public void setId(int id)
+   {
+      this.id = id;
+   }
+   
+   /**
+    * @param name the name to set
+    */
+   public void setName(String name)
+   {
+      this.name = name;
+   }
+}

Added: projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/tx/test/TxScopedTestCase.java
===================================================================
--- projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/tx/test/TxScopedTestCase.java	                        (rev 0)
+++ projects/jpa/trunk/impl/src/test/java/org/jboss/jpa/impl/test/tx/test/TxScopedTestCase.java	2009-10-14 10:37:25 UTC (rev 94850)
@@ -0,0 +1,165 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.impl.test.tx.test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+import java.net.URL;
+import java.sql.DriverManager;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+import javax.transaction.TransactionManager;
+
+import org.hibernate.ejb.HibernatePersistence;
+import org.hibernate.transaction.JBossTransactionManagerLookup;
+import org.hsqldb.jdbcDriver;
+import org.jboss.jpa.impl.deployment.PersistenceUnitInfoImpl;
+import org.jboss.jpa.impl.test.common.MockPersistenceUnit;
+import org.jboss.jpa.impl.test.common.Person;
+import org.jboss.jpa.impl.tx.TransactionScopedEntityManager;
+import org.jboss.jpa.spi.PersistenceUnitRegistry;
+import org.jnp.server.SingletonNamingServer;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import com.arjuna.ats.jta.utils.JNDIManager;
+
+/**
+ * EJB 3.0 Persistence 5.6.1:
+ * If the entity manager is invoked outside the scope of a transaction, any entities loaded from the database
+ * will immediately become detached at the end of the method call.
+ *
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class TxScopedTestCase
+{
+   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);
+      
+      EntityManager em = persistenceUnit.getContainerEntityManagerFactory().createEntityManager();
+      Person person = new Person();
+      person.setId(1);
+      person.setName("Test");
+      em.persist(person);
+      transactionManager.begin();
+      em.joinTransaction();
+      em.flush();
+      transactionManager.commit();      
+   }
+   
+   @Test
+   public void test1() throws Exception
+   {
+      EntityManager em = new TransactionScopedEntityManager(persistenceUnit);
+      Person result = em.find(Person.class, 1);
+      assertNotNull(result);
+      assertFalse("entity should have been detached", em.contains(result));
+   }
+
+   @Test
+   @Ignore("JPA-2.0 functions are not yet implemented")
+   public void test2() throws Exception
+   {
+      EntityManager em = new TransactionScopedEntityManager(persistenceUnit);
+      Map<String, Object> props = new HashMap<String, Object>();
+      Person result = em.find(Person.class, 1, props);
+      assertNotNull("failed to find person 1", result);
+      assertFalse("entity should have been detached", em.contains(result));
+   }
+}

Copied: projects/jpa/trunk/impl/src/test/resources/jndi.properties (from rev 94507, projects/jpa/trunk/deployers/src/test/resources/jndi.properties)
===================================================================
--- projects/jpa/trunk/impl/src/test/resources/jndi.properties	                        (rev 0)
+++ projects/jpa/trunk/impl/src/test/resources/jndi.properties	2009-10-14 10:37:25 UTC (rev 94850)
@@ -0,0 +1,2 @@
+java.naming.factory.initial=org.jnp.interfaces.LocalOnlyContextFactory
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
\ No newline at end of file

Copied: projects/jpa/trunk/impl/src/test/resources/log4j.xml (from rev 94507, projects/jpa/trunk/deployers/src/test/resources/log4j.xml)
===================================================================
--- projects/jpa/trunk/impl/src/test/resources/log4j.xml	                        (rev 0)
+++ projects/jpa/trunk/impl/src/test/resources/log4j.xml	2009-10-14 10:37:25 UTC (rev 94850)
@@ -0,0 +1,123 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<!-- ===================================================================== -->
+<!--                                                                       -->
+<!--  Log4j Configuration                                                  -->
+<!--                                                                       -->
+<!-- ===================================================================== -->
+
+<!-- $Id: log4j.xml 68671 2008-01-08 10:04:25Z wolfc $ -->
+
+<!--
+   | For more configuration infromation and examples see the Jakarta Log4j
+   | owebsite: http://jakarta.apache.org/log4j
+ -->
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
+
+  <!-- ================================= -->
+  <!-- Preserve messages in a local file -->
+  <!-- ================================= -->
+
+  <!-- A time/date based rolling appender -->
+  <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
+    <param name="File" value="target/test.log"/>
+    <param name="Append" value="false"/>
+
+    <!-- Rollover at midnight each day -->
+    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
+
+    <!-- Rollover at the top of each hour
+    <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
+    -->
+
+    <layout class="org.apache.log4j.PatternLayout">
+      <!-- The default pattern: Date Priority [Category] Message\n -->
+      <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
+
+      <!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
+      <param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
+       -->
+    </layout>	    
+  </appender>
+
+  <!-- A size based file rolling appender
+  <appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
+    <param name="File" value="${jboss.server.home.dir}/log/server.log"/>
+    <param name="Append" value="false"/>
+    <param name="MaxFileSize" value="500KB"/>
+    <param name="MaxBackupIndex" value="1"/>
+
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
+    </layout>	    
+  </appender>
+  -->
+
+  <!-- ============================== -->
+  <!-- Append messages to the console -->
+  <!-- ============================== -->
+
+  <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
+    <param name="Target" value="System.out"/>
+    <param name="Threshold" value="DEBUG"/>
+
+    <layout class="org.apache.log4j.PatternLayout">
+      <!-- The default pattern: Date Priority [Category] Message\n -->
+      <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
+    </layout>
+  </appender>
+
+
+  <!-- ================ -->
+  <!-- Limit categories -->
+  <!-- ================ -->
+
+  <!-- Limit JBoss categories to INFO
+  <category name="org.jboss">
+    <priority value="INFO" class="org.jboss.logging.XLevel"/>
+  </category>
+  -->
+
+  <!-- Increase the priority threshold for the DefaultDS category
+  <category name="DefaultDS">
+    <priority value="FATAL"/>
+  </category>
+  -->
+
+  <!-- Decrease the priority threshold for the org.jboss.varia category
+  <category name="org.jboss.varia">
+    <priority value="DEBUG"/>
+  </category>
+  -->
+
+  <!--
+     | An example of enabling the custom TRACE level priority that is used
+     | by the JBoss internals to diagnose low level details. This example
+     | turns on TRACE level msgs for the org.jboss.ejb.plugins package and its
+     | subpackages. This will produce A LOT of logging output.
+  <category name="org.jboss.system">
+    <priority value="TRACE" class="org.jboss.logging.XLevel"/>
+  </category>
+  -->
+
+  <category name="org.jboss">
+    <priority value="INFO"/>
+  </category>
+  
+  <category name="org.jboss.jpa">
+    <priority value="DEBUG"/>
+  </category>
+  
+  <!-- ======================= -->
+  <!-- Setup the Root category -->
+  <!-- ======================= -->
+
+  <root>
+    <priority value="ALL" />
+    <appender-ref ref="CONSOLE"/>
+    <appender-ref ref="FILE"/>
+  </root>
+  
+</log4j:configuration>




More information about the jboss-cvs-commits mailing list