mirror of https://github.com/apache/openjpa.git
OPENJPA-1905: Validate jar-file pu element after confirming OpenJPA is chosen provider
git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@1063439 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a56b9b26eb
commit
039abeba79
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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>
|
|
@ -533,6 +533,10 @@ public class PersistenceProductDerivation
|
|||
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;
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue