openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r905355 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/
Date Mon, 01 Feb 2010 18:28:20 GMT
Author: ppoddar
Date: Mon Feb  1 18:28:18 2010
New Revision: 905355

URL: http://svn.apache.org/viewvc?rev=905355&view=rev
Log:
OPENJPA-960: Block unwrap for Object.class or null argument 

Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestUnwrap.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestUnwrap.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestUnwrap.java?rev=905355&r1=905354&r2=905355&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestUnwrap.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestUnwrap.java
Mon Feb  1 18:28:18 2010
@@ -18,7 +18,10 @@
  */
 package org.apache.openjpa.persistence;
 
+import java.util.Properties;
+
 import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
 import javax.persistence.Query;
 
 import org.apache.openjpa.kernel.QueryLanguages;
@@ -54,15 +57,42 @@
     public void testValidEntityManagerUnwrap() {
         EntityManager em = emf.createEntityManager();
         
-        Class[] validCasts = new Class[] {
+        Class<?>[] validCasts = new Class[] {
             org.apache.openjpa.persistence.OpenJPAEntityManager.class,
             org.apache.openjpa.persistence.OpenJPAEntityManagerSPI.class,
             org.apache.openjpa.kernel.DelegatingBroker.class,
             org.apache.openjpa.kernel.Broker.class
         };
-        for (Class c : validCasts) {
+        for (Class<?> c : validCasts) {
             Object unwrapped = em.unwrap(c);
             assertTrue(c.isInstance(unwrapped));
         }
     }
+    
+    /**
+     * Tests a EntityManager can be unwrapped as an instance of a series of 
+     * class or interface. 
+     */
+    public void testInvalidEntityManagerUnwrap() {
+        EntityManager em = emf.createEntityManager();
+        
+        Class<?>[] validCasts = new Class[] {
+            Object.class,
+            Properties.class,
+            null,
+        };
+        for (Class<?> c : validCasts) {
+            try {
+                em.unwrap(c);
+                fail("Expected to fail to unwarp with " + c);
+            } catch (PersistenceException e) {
+                EntityTransaction txn = em.getTransaction();
+                assertFalse(txn.isActive());
+            } catch (Exception ex) {
+                ex.printStackTrace();
+                fail("Unexpected exception while unwrapping " + c);
+            }
+        }
+    }
+
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java?rev=905355&r1=905354&r2=905355&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
Mon Feb  1 18:28:18 2010
@@ -1605,6 +1605,10 @@
     public <T> T unwrap(Class<T> cls) {
         Object[] delegates = new Object[]{_broker.getInnermostDelegate(),
             _broker.getDelegate(), _broker, this};
+        if (cls == null || cls == Object.class) {
+            throw new PersistenceException(_loc.get("unwrap-em-invalid", cls)
+                    .toString(), null, this, false);
+        }
         for (Object o : delegates) {
             if (cls.isInstance(o))
                 return (T)o;

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=905355&r1=905354&r2=905355&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Mon Feb  1 18:28:18 2010
@@ -473,6 +473,10 @@
     public <T> T unwrap(Class<T> cls) {
         Object[] delegates = new Object[]{_query.getInnermostDelegate(), 
             _query.getDelegate(), _query, this};
+        if (cls == null || cls == Object.class) {
+            throw new PersistenceException(_loc.get("unwrap-em-invalid", cls)
+                    .toString(), null, this, false);
+        }
         for (Object o : delegates) {
             if (cls.isInstance(o))
                 return (T)o;



Mime
View raw message