openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgras...@apache.org
Subject svn commit: r1045352 - in /openjpa/branches/2.1.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...
Date Mon, 13 Dec 2010 20:06:19 GMT
Author: jgrassel
Date: Mon Dec 13 20:06:18 2010
New Revision: 1045352

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

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

Added: openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java?rev=1045352&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java
(added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java
Mon Dec 13 20:06:18 2010
@@ -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.1.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/puconf/TestPersistenceUnitConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/2.1.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.1.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml?rev=1045352&view=auto
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml
(added)
+++ openjpa/branches/2.1.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml
Mon Dec 13 20:06:18 2010
@@ -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.1.x/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/puconf/META-INF/persistence.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?rev=1045352&r1=1045351&r2=1045352&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
(original)
+++ openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
Mon Dec 13 20:06:18 2010
@@ -537,6 +537,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.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java?rev=1045352&r1=1045351&r2=1045352&view=diff
==============================================================================
--- openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java
(original)
+++ openjpa/branches/2.1.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceUnitInfoImpl.java
Mon Dec 13 20:06:18 2010
@@ -74,6 +74,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;
@@ -212,6 +213,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());



Mime
View raw message