OPENJPA-2875 make sure to avoid doing things when another persistence provider is specified.
diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/TestPersistenceProviderFilteringTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/TestPersistenceProviderFilteringTestCase.java
new file mode 100644
index 0000000..9ee4946
--- /dev/null
+++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/TestPersistenceProviderFilteringTestCase.java
@@ -0,0 +1,114 @@
+/*
+ * 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.test;
+
+import org.apache.openjpa.persistence.PersistenceProviderImpl;
+import org.apache.openjpa.persistence.entity.EntityA;
+import org.apache.openjpa.persistence.entity.EntityB;
+import org.apache.openjpa.persistence.entity.EntityC;
+import org.apache.openjpa.util.UserException;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.ProviderUtil;
+import java.util.HashMap;
+import java.util.Map;
+
+/*
+ * This tests that OpenJPA should not be doing anything if another provider is specified.
+ */
+public class TestPersistenceProviderFilteringTestCase extends SQLListenerTestCase {
+
+ private final String persistenceUnitName = "test";
+
+ @Override
+ public void setUp() {
+ setUp(DROP_TABLES, EntityA.class, EntityB.class, EntityC.class);
+
+ }
+
+ public void testGenerateSchemaNoProvider() {
+ final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
+ final Map<Object, Object> map = new HashMap<>();
+ map.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
+ map.put("javax.persistence.jdbc.url", "jdbc:derby:target/database/openjpa-test-provider-database;create=true");
+ try {
+ ppi.generateSchema(persistenceUnitName, map);
+ fail("Should have fail because there is no pool or so to generate the schema");
+
+ } catch (final UserException e) {
+ // great
+ }
+ }
+
+ public void testGenerateSchemaOpenJPAProvider() {
+ final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
+ final Map<Object, Object> map = new HashMap<>();
+ map.put("javax.persistence.provider", PersistenceProviderImpl.class.getName());
+ map.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
+ map.put("javax.persistence.jdbc.url", "jdbc:derby:target/database/openjpa-test-provider-database;create=true");
+ try {
+ ppi.generateSchema(persistenceUnitName, map);
+ fail("Should have fail because there is no pool or so to generate the schema");
+
+ } catch (final UserException e) {
+ // great
+ }
+ }
+
+ public void testGenerateSchemaEclipseProvider() {
+ final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
+ final Map<Object, Object> map = new HashMap<>();
+ map.put("javax.persistence.provider", "org.eclipse.persistence.jpa.PersistenceProvider");
+ assertFalse(ppi.generateSchema(persistenceUnitName, map));
+ }
+
+ public void testGenerateSchemaFakeProviderClass() {
+ final PersistenceProviderImpl ppi = new PersistenceProviderImpl();
+ final Map<Object, Object> map = new HashMap<>();
+ map.put("javax.persistence.provider", FakeProvider.class);
+ assertFalse(ppi.generateSchema(persistenceUnitName, map));
+ }
+
+
+ public static final class FakeProvider implements PersistenceProvider {
+
+ @Override public EntityManagerFactory createEntityManagerFactory(final String s, final Map map) {
+ return null;
+ }
+
+ @Override public EntityManagerFactory createContainerEntityManagerFactory(
+ final PersistenceUnitInfo persistenceUnitInfo, final Map map) {
+ return null;
+ }
+
+ @Override public void generateSchema(final PersistenceUnitInfo persistenceUnitInfo, final Map map) {
+
+ }
+
+ @Override public boolean generateSchema(final String s, final Map map) {
+ return false;
+ }
+
+ @Override public ProviderUtil getProviderUtil() {
+ return null;
+ }
+ }
+}
diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
index bbe358d..031c4bb 100644
--- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
+++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
@@ -18,21 +18,6 @@
*/
package org.apache.openjpa.persistence;
-import java.lang.instrument.ClassFileTransformer;
-import java.lang.instrument.IllegalClassFormatException;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.security.ProtectionDomain;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.persistence.EntityManager;
-import javax.persistence.spi.ClassTransformer;
-import javax.persistence.spi.LoadState;
-import javax.persistence.spi.PersistenceProvider;
-import javax.persistence.spi.PersistenceUnitInfo;
-import javax.persistence.spi.ProviderUtil;
-
import org.apache.openjpa.conf.BrokerValue;
import org.apache.openjpa.conf.OpenJPAConfiguration;
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
@@ -55,6 +40,20 @@
import org.apache.openjpa.persistence.validation.ValidationUtils;
import org.apache.openjpa.util.ClassResolver;
+import javax.persistence.EntityManager;
+import javax.persistence.spi.ClassTransformer;
+import javax.persistence.spi.LoadState;
+import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.ProviderUtil;
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.IllegalClassFormatException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.security.ProtectionDomain;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* Bootstrapping class that allows the creation of a stand-alone
@@ -228,6 +227,11 @@
@Override
public void generateSchema(final PersistenceUnitInfo info, final Map map) {
final Map runMap = map == null ? new HashMap<>() : new HashMap<>(map);
+
+ if (!acceptProvider(runMap)) {
+ return;
+ }
+
runMap.put("javax.persistence.schema-generation.database.action", "create");
final OpenJPAEntityManagerFactory factory = createContainerEntityManagerFactory(info, runMap);
try {
@@ -240,6 +244,11 @@
@Override
public boolean generateSchema(final String persistenceUnitName, final Map map) {
final Map runMap = map == null ? new HashMap<>() : new HashMap<>(map);
+
+ if (!acceptProvider(runMap)) {
+ return false;
+ }
+
runMap.put("javax.persistence.schema-generation.database.action", "create");
final OpenJPAEntityManagerFactory factory = createEntityManagerFactory(persistenceUnitName, runMap);
try {
@@ -250,6 +259,32 @@
}
}
+ // if persistence provider is specific, don't do anything
+ // only allowed to process if persistence provider matches or if not provider is specified
+ public boolean acceptProvider(final Map properties) {
+ Object provider = properties.get("javax.persistence.provider");
+
+ // provider is specified, so it has to match
+ if (provider != null) {
+ if (provider instanceof Class) {
+ provider = ((Class) provider).getName();
+ }
+ try {
+ if (!((String) provider).equals(org.apache.openjpa.persistence.PersistenceProviderImpl.class.getName())) {
+ return false;
+ }
+
+ } catch (final ClassCastException e) {
+ return false;
+ // not a recognized provider property value so must be another provider.
+ }
+ }
+
+ // no provider specified
+ return true;
+
+ }
+
private Object synchronizeMappings(final OpenJPAEntityManagerFactory factory) {
if (EntityManagerFactoryImpl.class.isInstance(factory)) {
final EntityManagerFactoryImpl entityManagerFactory = EntityManagerFactoryImpl.class.cast(factory);