Author: drobiazko
Date: Sun May 22 09:17:28 2011
New Revision: 1125889

URL: http://svn.apache.org/viewvc?rev=1125889&view=rev
Log:
Added some tests proving that EntityManager is injectable without 
@PersistenceContext.

Added:
    tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem2.tml
    tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem3.tml
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem2.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem3.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/UserDAO.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/impl/
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/impl/UserDAOImpl.java
   (with props)
Modified:
    
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerObjectProvider.java
    
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/JpaInternalUtils.java
    tapestry/tapestry5/trunk/tapestry-jpa/src/test/   (props changed)
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/JpaTransactionAdvisorImplTest.java
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app2/SinglePersistenceUnitIntegrationTest.java
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/AppModule.java

Modified: 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerObjectProvider.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerObjectProvider.java?rev=1125889&r1=1125888&r2=1125889&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerObjectProvider.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerObjectProvider.java
 Sun May 22 09:17:28 2011
@@ -49,18 +49,16 @@ public class EntityManagerObjectProvider
             final ClassFactory classFactory = 
objectLocator.getService("ClassFactory",
                     ClassFactory.class);
 
+             final PersistenceContext annotation = annotationProvider
+                            .getAnnotation(PersistenceContext.class);
+
             proxy = classFactory.createProxy(EntityManager.class, new 
ObjectCreator()
             {
-
                 public Object createObject()
                 {
-
                     final EntityManagerManager entityManagerManager = 
objectLocator
                             .getService(EntityManagerManager.class);
 
-                    final PersistenceContext annotation = annotationProvider
-                            .getAnnotation(PersistenceContext.class);
-
                     return 
JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
                 }
             }, "<EntityManagerProxy>");

Modified: 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/JpaInternalUtils.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/JpaInternalUtils.java?rev=1125889&r1=1125888&r2=1125889&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/JpaInternalUtils.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/JpaInternalUtils.java
 Sun May 22 09:17:28 2011
@@ -70,7 +70,7 @@ public class JpaInternalUtils
 
         throw new IllegalArgumentException(
                 String.format(
-                        "Failed persisting an entity in the session. The 
entity '%s' does not belong to any of the existing persistence contexts.",
+                        "Failed persisting the entity. The entity '%s' does 
not belong to any of the existing persistence contexts.",
                         entity));
     }
 
@@ -87,6 +87,7 @@ public class JpaInternalUtils
         if (entityManagers.size() == 1)
             return entityManagers.values().iterator().next();
 
-        return null;
+        throw new RuntimeException("Unable to locate a single EntityManager.  
" +
+                "Please provide the persistence unit name as defined in the 
persistence.xml using the @PersistenceContext annotation");
     }
 }

Propchange: tapestry/tapestry5/trunk/tapestry-jpa/src/test/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun May 22 09:17:28 2011
@@ -0,0 +1 @@
+test-output

Added: tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem2.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem2.tml?rev=1125889&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem2.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem2.tml Sun 
May 22 09:17:28 2011
@@ -0,0 +1,13 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd";>
+       <body>
+               <p>
+                       entity name:
+                       <span id="name">
+                               <t:if test="item">${item.name}</t:if>
+                       </span>
+               </p>
+               <p>
+                       <t:eventlink event="createEntity">create 
item</t:eventlink>
+               </p>
+       </body>
+</html>
\ No newline at end of file

Added: tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem3.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem3.tml?rev=1125889&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem3.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-jpa/src/test/app2/PersistItem3.tml Sun 
May 22 09:17:28 2011
@@ -0,0 +1,13 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd";>
+       <body>
+               <p>
+                       entity name:
+                       <span id="name">
+                               <t:if test="item">${item.name}</t:if>
+                       </span>
+               </p>
+               <p>
+                       <t:eventlink event="createEntity">create 
item</t:eventlink>
+               </p>
+       </body>
+</html>
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/JpaTransactionAdvisorImplTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/JpaTransactionAdvisorImplTest.java?rev=1125889&r1=1125888&r2=1125889&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/JpaTransactionAdvisorImplTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/JpaTransactionAdvisorImplTest.java
 Sun May 22 09:17:28 2011
@@ -14,13 +14,6 @@
 
 package org.apache.tapestry5.internal.jpa;
 
-import java.sql.SQLException;
-import java.util.Map;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
-import javax.persistence.PersistenceContext;
-
 import org.apache.tapestry5.ioc.IOCUtilities;
 import org.apache.tapestry5.ioc.Registry;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
@@ -36,6 +29,12 @@ import org.testng.annotations.AfterClass
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.PersistenceContext;
+import java.sql.SQLException;
+import java.util.Map;
+
 public class JpaTransactionAdvisorImplTest extends IOCTestCase
 {
     private static final String UNIT_NAME = "FooUnit";
@@ -101,10 +100,18 @@ public class JpaTransactionAdvisorImplTe
         
         expect(manager.getEntityManagers()).andReturn(managers);
 
-        delegate.persistenceUnitNameMissing();
-
         replay();
-        interceptor.persistenceUnitNameMissing();
+
+        try
+        {
+            interceptor.persistenceUnitNameMissing();
+            TestBase.unreachable();
+        }
+        catch (Exception e)
+        {
+               Assert.assertEquals(e.getMessage(), "Unable to locate a single 
EntityManager.  " +
+                       "Please provide the persistence unit name as defined in 
the persistence.xml using the @PersistenceContext annotation");
+        }
         verify();
     }
     
@@ -154,10 +161,18 @@ public class JpaTransactionAdvisorImplTe
         final VoidService interceptor = builder.build();
 
         expect(manager.getEntityManagers()).andReturn(managers);
-        delegate.persistenceUnitMissing();
 
         replay();
-        interceptor.persistenceUnitMissing();
+        try
+        {
+            interceptor.persistenceUnitMissing();
+            TestBase.unreachable();
+        }
+        catch (Exception e)
+        {
+               Assert.assertEquals(e.getMessage(), "Unable to locate a single 
EntityManager.  " +
+                       "Please provide the persistence unit name as defined in 
the persistence.xml using the @PersistenceContext annotation");
+        }
         verify();
     }
     

Modified: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java?rev=1125889&r1=1125888&r2=1125889&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app1/JpaIntegrationTest.java
 Sun May 22 09:17:28 2011
@@ -62,7 +62,7 @@ public class JpaIntegrationTest extends 
 
         // transient objects cannot be persisted
         clickAndWait("link=set to transient");
-        assertTextPresent("Failed persisting an entity in the session.");
+        assertTextPresent("Failed persisting the entity.");
     }
 
     @Test

Modified: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app2/SinglePersistenceUnitIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app2/SinglePersistenceUnitIntegrationTest.java?rev=1125889&r1=1125888&r2=1125889&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app2/SinglePersistenceUnitIntegrationTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app2/SinglePersistenceUnitIntegrationTest.java
 Sun May 22 09:17:28 2011
@@ -29,4 +29,24 @@ public class SinglePersistenceUnitIntegr
         clickAndWait("link=create item");
         assertText("//span[@id='name']", "name");
     }
+
+    @Test
+    public void inject_into_page_wihout_jpa_annotationt()
+    {
+        open("/persistitem2");
+        assertEquals(getText("//span[@id='name']").length(), 0);
+
+        clickAndWait("link=create item");
+        assertText("//span[@id='name']", "name");
+    }
+
+    @Test
+    public void inject_into_service_wihout_jpa_annotationt()
+    {
+        open("/persistitem3");
+        assertEquals(getText("//span[@id='name']").length(), 0);
+
+        clickAndWait("link=create item");
+        assertText("//span[@id='name']", "name");
+    }
 }

Added: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem2.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem2.java?rev=1125889&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem2.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem2.java
 Sun May 22 09:17:28 2011
@@ -0,0 +1,45 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.example.app2.pages;
+
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.jpa.annotations.CommitAfter;
+import org.example.app2.entities.Item;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+public class PersistItem2
+{
+    @Inject
+    private EntityManager entityManager;
+
+    @Persist("entity")
+    @Property
+    private Item item;
+
+    @CommitAfter
+    void onCreateEntity()
+    {
+        final Item item = new Item();
+        item.setName("name");
+
+        entityManager.persist(item);
+
+        this.item = item;
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem3.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem3.java?rev=1125889&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem3.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem3.java
 Sun May 22 09:17:28 2011
@@ -0,0 +1,45 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.example.app2.pages;
+
+import org.apache.tapestry5.annotations.Persist;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.jpa.annotations.CommitAfter;
+import org.example.app2.entities.Item;
+import org.example.app2.services.UserDAO;
+
+import javax.persistence.EntityManager;
+
+public class PersistItem3
+{
+    @Inject
+    private UserDAO dao;
+
+    @Persist("entity")
+    @Property
+    private Item item;
+
+    @CommitAfter
+    void onCreateEntity()
+    {
+        final Item item = new Item();
+        item.setName("name");
+
+        dao.persist(item);
+
+        this.item = item;
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/pages/PersistItem3.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/AppModule.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/AppModule.java?rev=1125889&r1=1125888&r2=1125889&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/AppModule.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/AppModule.java
 Sun May 22 09:17:28 2011
@@ -15,16 +15,24 @@
 package org.example.app2.services;
 
 import org.apache.tapestry5.ioc.MappedConfiguration;
+import org.apache.tapestry5.ioc.ServiceBinder;
 import org.apache.tapestry5.ioc.annotations.Contribute;
 import org.apache.tapestry5.ioc.annotations.SubModule;
 import org.apache.tapestry5.ioc.services.ApplicationDefaults;
 import org.apache.tapestry5.ioc.services.SymbolProvider;
 import org.apache.tapestry5.jpa.JpaModule;
 import org.apache.tapestry5.jpa.JpaSymbols;
+import org.example.app2.services.impl.UserDAOImpl;
 
 @SubModule(JpaModule.class)
 public class AppModule
 {
+
+    public static void bind(final ServiceBinder binder)
+    {
+        binder.bind(UserDAO.class, UserDAOImpl.class);
+    }
+
     @Contribute(SymbolProvider.class)
     @ApplicationDefaults
     public static void provideFactoryDefaults(

Added: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/UserDAO.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/UserDAO.java?rev=1125889&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/UserDAO.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/UserDAO.java
 Sun May 22 09:17:28 2011
@@ -0,0 +1,20 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.example.app2.services;
+
+public interface UserDAO
+{
+    void persist(Object entity);
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/UserDAO.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/impl/UserDAOImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/impl/UserDAOImpl.java?rev=1125889&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/impl/UserDAOImpl.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/impl/UserDAOImpl.java
 Sun May 22 09:17:28 2011
@@ -0,0 +1,17 @@
+package org.example.app2.services.impl;
+
+import org.apache.tapestry5.ioc.annotations.Inject;
+import org.example.app2.services.UserDAO;
+
+import javax.persistence.EntityManager;
+
+public class UserDAOImpl implements UserDAO
+{
+    @Inject
+    private EntityManager entityManager;
+
+    public void persist(Object entity)
+    {
+        entityManager.persist(entity);
+    }
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/example/app2/services/impl/UserDAOImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to