You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by jg...@apache.org on 2011/01/25 21:21:30 UTC

svn commit: r1063439 - in /openjpa/branches/2.0.x: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/ openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/ openjpa-persistence-jdbc/src/test/resour...

Author: jgrassel
Date: Tue Jan 25 20:21:29 2011
New Revision: 1063439

URL: http://svn.apache.org/viewvc?rev=1063439&view=rev
Log:
OPENJPA-1905: Validate jar-file pu element after confirming OpenJPA is chosen provider

Added:
    openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/
    openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java   (with props)
    openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/
    openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/
    openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml   (with props)
Modified:
    openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
    openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java

Added: openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java?rev=1063439&view=auto
==============================================================================
--- openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java (added)
+++ openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java Tue Jan 25 20:21:29 2011
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.openjpa.persistence.puconf;
+
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.test.PersistenceTestCase;
+
+public class TestPersistenceUnitConfig extends PersistenceTestCase {
+    private String persistenceXmlResource;
+
+    public TestPersistenceUnitConfig() {
+        persistenceXmlResource = getClass().getPackage().getName().replaceAll("\\.", "/") + 
+        "/META-INF/persistence.xml";
+    }
+
+    public EntityManagerFactory createEmf(String unitName) {
+        return OpenJPAPersistence.createEntityManagerFactory(unitName, persistenceXmlResource);
+    }
+
+    public void testCreateEMFWithGoodPU() {
+        EntityManagerFactory emf = null;
+        try {
+            emf = createEmf("PUTest-Good");         
+            assertNotNull("Assert emf was successfully created.", emf);
+        } finally {
+            if (emf != null) {
+                try {
+                    emf.close();
+                } catch (Throwable t) {
+                    // Swallow Exception
+                }
+            }
+        }
+    }
+    
+    public void testCreateEMFWithBadJarFileElement() {
+        EntityManagerFactory emf = null;
+        try {
+            // Create EMF, expecting no problems.
+            emf = createEmf("PUTest-Good");
+        } finally {
+            if (emf != null) {
+                try {
+                    emf.close();
+                } catch (Throwable t) {
+                    // Swallow Exception
+                }
+            }
+        }
+    }
+
+    public void testCreateEMFWithNonOpenJPAProvider() {
+        EntityManagerFactory emf = null;
+        try {
+            emf = createEmf("PUTest-NonOpenJPAProvider");
+
+            // Did not catch the expected MissingResourceException Exception
+            fail("The createEntityManager() operation did not throw any Exception.");
+        } catch (java.util.MissingResourceException mre) {
+            // Caught the expected PersistenceException
+        } finally {
+            if (emf != null) {
+                try {
+                    emf.close();
+                } catch (Throwable t) {
+                    // Swallow Exception
+                }
+            }
+        }
+    }
+
+    public void testCreateEMFWithBadJarFileElementAndNonOpenJPAProvider() {
+        EntityManagerFactory emf = null;
+        try {
+            emf = createEmf("PUTest-BadJarFile-NonOpenJPAProvider");
+
+            // Did not catch the expected MissingResourceException Exception
+            fail("The createEntityManager() operation did not throw any Exception.");
+        } catch (java.util.MissingResourceException mre) {
+            // Caught the expected PersistenceException
+        } finally {
+            if (emf != null) {
+                try {
+                    emf.close();
+                } catch (Throwable t) {
+                    // Swallow Exception
+                }
+            }
+        }
+    }
+    
+}

Propchange: openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml?rev=1063439&view=auto
==============================================================================
--- openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml (added)
+++ openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml Tue Jan 25 20:21:29 2011
@@ -0,0 +1,51 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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.   
+-->
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
+                                 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
+    
+    <persistence-unit name="PUTest-Good" transaction-type="RESOURCE_LOCAL">
+        <class>org.apache.openjpa.persistence.common.apps.Address</class>
+        <class>org.apache.openjpa.persistence.common.apps.CompUser</class>
+    </persistence-unit>
+    
+    <persistence-unit name="PUTest-BadJarFile" transaction-type="RESOURCE_LOCAL">
+        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
+        <jar-file>IDoNotExist.jar</jar-file>
+        
+        <class>org.apache.openjpa.persistence.common.apps.Address</class>
+        <class>org.apache.openjpa.persistence.common.apps.CompUser</class>
+    </persistence-unit>
+    
+    <persistence-unit name="PUTest-NonOpenJPAProvider" transaction-type="RESOURCE_LOCAL">
+        <provider>a.bogus.provider</provider>
+        
+        <class>org.apache.openjpa.persistence.common.apps.Address</class>
+        <class>org.apache.openjpa.persistence.common.apps.CompUser</class>
+    </persistence-unit>
+    
+    <persistence-unit name="PUTest-BadJarFile-NonOpenJPAProvider" transaction-type="RESOURCE_LOCAL">
+        <provider>a.bogus.provider</provider>
+        <jar-file>IDoNotExist.jar</jar-file>
+        
+        <class>org.apache.openjpa.persistence.common.apps.Address</class>
+        <class>org.apache.openjpa.persistence.common.apps.CompUser</class>
+    </persistence-unit>
+</persistence>

Propchange: openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?rev=1063439&r1=1063438&r2=1063439&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java (original)
+++ openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java Tue Jan 25 20:21:29 2011
@@ -533,6 +533,10 @@ public class PersistenceProductDerivatio
                 rsrc, name, pinfo.getPersistenceProviderClassName()).
                 getMessage(), getClass().getName(), rsrc);
         }
+        
+        // Process jar-file references after confirming OpenJPA is the desired JPA provider.
+        pinfo.processJarFileNames();
+        
         cp.addProperties(pinfo.toOpenJPAProperties());
         cp.setSource(pinfo.getPersistenceXmlFileUrl().toString());
         return Boolean.TRUE;

Modified: openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java?rev=1063439&r1=1063438&r2=1063439&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java (original)
+++ openjpa/branches/2.0.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java Tue Jan 25 20:21:29 2011
@@ -81,6 +81,7 @@ public class PersistenceUnitInfoImpl
     private List<String> _mappingFileNames;
     private List<String> _entityClassNames;
     private List<URL> _jarFiles;
+    private List<String> _jarFileNames;
     private String _jtaDataSourceName;
     private DataSource _jtaDataSource;
     private String _nonJtaDataSourceName;
@@ -219,6 +220,30 @@ public class PersistenceUnitInfoImpl
     }
 
     public void addJarFileName(String name) {
+        // Defer searching the classpath for jar files referenced by the jar-file element until after
+        // the XML has been parsed and it has been confirmed that OpenJPA is the desired JPA provider.
+
+        if (_jarFileNames == null) {
+            _jarFileNames = new ArrayList<String>();
+        }
+        _jarFileNames.add(name);
+    }
+
+    /**
+     * Process jar-file elements. An IllegalArgumentException may be thrown if the jar file does not exist in the
+     * classpath.
+     */
+    public void processJarFileNames() {
+        if (_jarFileNames != null) {
+            for (String name : _jarFileNames) {
+                validateJarFileName(name);
+            }
+
+            _jarFileNames.clear();
+        }
+    }
+    
+    public void validateJarFileName(String name) {
         MultiClassLoader loader = AccessController
             .doPrivileged(J2DoPrivHelper.newMultiClassLoaderAction());
         loader.addClassLoader(getClass().getClassLoader());