From e97e42b3eb5a3297f60e4c9a816d8dff8a8f9359 Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Wed, 28 Jul 2010 14:30:05 +0000 Subject: [PATCH] OPENJPA-1749: Throw exception when incompatible configuration options are set git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@980070 13f79535-47bb-0310-9956-ffa450edef68 --- .../conf/TestOverrideNonJtaDataSource.java | 42 ++++++++++++++++++ .../conf/TestSwitchConnection.java | 43 +++++++++++++++++++ .../persistence/EntityManagerFactoryImpl.java | 34 ++++++++++++++- .../openjpa/persistence/localizer.properties | 2 + 4 files changed, 120 insertions(+), 1 deletion(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestOverrideNonJtaDataSource.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestOverrideNonJtaDataSource.java index de8207a4e..c0d43b0bc 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestOverrideNonJtaDataSource.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestOverrideNonJtaDataSource.java @@ -165,4 +165,46 @@ public class TestOverrideNonJtaDataSource extends AbstractPersistenceTestCase { assertTrue(e.getMessage().contains("EntityManager")); // ensure where the JNDI name came from is in message } } + + public void testDataCache() { + EntityManagerFactory emf = null; + + emf = getEmf("openjpa.DataCache", "true"); + try { + getEm(emf, "openjpa.ConnectionFactoryName", "jdbc/NotReal"); + fail("Expected an excepton when creating an EM with a bogus JNDI name"); + } catch (ArgumentException e) { + assertTrue(e.isFatal()); + assertTrue(e.getMessage().contains("jdbc/NotReal")); + assertTrue(e.getMessage().contains("L2 Cache")); + } + } + + public void testQueryCache() { + EntityManagerFactory emf = null; + + emf = getEmf("openjpa.QueryCache", "true"); + try { + getEm(emf, "openjpa.ConnectionFactoryName", "jdbc/NotReal"); + fail("Expected an excepton when creating an EM with a bogus JNDI name"); + } catch (ArgumentException e) { + assertTrue(e.isFatal()); + assertTrue(e.getMessage().contains("jdbc/NotReal")); + assertTrue(e.getMessage().contains("openjpa.QueryCache")); + } + } + + public void testSyncMappings() { + EntityManagerFactory emf = null; + + emf = getEmf("openjpa.jdbc.SynchronizeMappings", "buildSchema"); + try { + getEm(emf, "openjpa.ConnectionFactoryName", "jdbc/NotReal"); + fail("Expected an excepton when creating an EM with a bogus JNDI name"); + } catch (ArgumentException e) { + assertTrue(e.isFatal()); + assertTrue(e.getMessage().contains("jdbc/NotReal")); + assertTrue(e.getMessage().contains("openjpa.jdbc.SynchronizeMappings")); + } + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestSwitchConnection.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestSwitchConnection.java index 1182f6408..ede39d324 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestSwitchConnection.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/conf/TestSwitchConnection.java @@ -28,6 +28,7 @@ import javax.persistence.RollbackException; import org.apache.openjpa.persistence.ArgumentException; import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; +import org.apache.openjpa.util.UserException; public class TestSwitchConnection extends AbstractPersistenceTestCase { private String defaultJndiName = "jdbc/mocked"; @@ -159,4 +160,46 @@ public class TestSwitchConnection extends AbstractPersistenceTestCase { assertTrue(e.getMessage().contains("EntityManager")); // ensure where the JNDI name came from is in message } } + + public void testDataCache() { + EntityManagerFactory emf = null; + + emf = getEmf("openjpa.DataCache", "true"); + try { + getEm(emf, "openjpa.ConnectionFactoryName", "jdbc/NotReal"); + fail("Expected an excepton when creating an EM with a bogus JNDI name"); + } catch (ArgumentException e) { + assertTrue(e.isFatal()); + assertTrue(e.getMessage().contains("jdbc/NotReal")); + assertTrue(e.getMessage().contains("L2 Cache")); + } + } + + public void testQueryCache() { + EntityManagerFactory emf = null; + + emf = getEmf("openjpa.QueryCache", "true"); + try { + getEm(emf, "openjpa.ConnectionFactoryName", "jdbc/NotReal"); + fail("Expected an excepton when creating an EM with a bogus JNDI name"); + } catch (ArgumentException e) { + assertTrue(e.isFatal()); + assertTrue(e.getMessage().contains("jdbc/NotReal")); + assertTrue(e.getMessage().contains("openjpa.QueryCache")); + } + } + + public void testSyncMappings() { + EntityManagerFactory emf = null; + + emf = getEmf("openjpa.jdbc.SynchronizeMappings", "buildSchema"); + try { + getEm(emf, "openjpa.ConnectionFactoryName", "jdbc/NotReal"); + fail("Expected an excepton when creating an EM with a bogus JNDI name"); + } catch (ArgumentException e) { + assertTrue(e.isFatal()); + assertTrue(e.getMessage().contains("jdbc/NotReal")); + assertTrue(e.getMessage().contains("openjpa.jdbc.SynchronizeMappings")); + } + } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java index 648a1c7ef..4c61022a0 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java @@ -48,6 +48,7 @@ import org.apache.openjpa.persistence.criteria.OpenJPACriteriaBuilder; import org.apache.openjpa.persistence.meta.MetamodelImpl; import org.apache.openjpa.persistence.query.OpenJPAQueryBuilder; import org.apache.openjpa.persistence.query.QueryBuilderImpl; +import org.apache.openjpa.util.UserException; /** * Implementation of {@link EntityManagerFactory} that acts as a @@ -214,8 +215,14 @@ public class EntityManagerFactoryImpl } if (log != null && log.isTraceEnabled()) { - log.trace("Found ConnectionFactoryName from props: " + cfName); + if(StringUtils.isNotEmpty(cfName)) { + log.trace("Found ConnectionFactoryName from props: " + cfName); + } + if(StringUtils.isNotEmpty(cf2Name)) { + log.trace("Found ConnectionFactory2Name from props: " + cf2Name); + } } + validateCfNameProps(conf, cfName, cf2Name); Broker broker = _factory.newBroker(user, pass, managed, retainMode, false, cfName, cf2Name); @@ -357,4 +364,29 @@ public class EntityManagerFactoryImpl return (OpenJPAPersistenceUtil.isManagedBy(this, entity) && (OpenJPAPersistenceUtil.isLoaded(entity, attribute) == LoadState.LOADED)); } + + private void validateCfNameProps(OpenJPAConfiguration conf, String cfName, String cf2Name) { + if (StringUtils.isNotEmpty(cfName) || StringUtils.isNotEmpty(cf2Name)) { + if (conf.getDataCache() != "false" && conf.getDataCache() != null) { + throw new ArgumentException(_loc.get("invalid-cfname-prop", new Object[] { + "openjpa.DataCache (L2 Cache)", + cfName, + cf2Name }), null, null, true); + + } + if (conf.getQueryCache() != "false" && conf.getQueryCache() != null) { + throw new ArgumentException(_loc.get("invalid-cfname-prop", new Object[] { + "openjpa.QueryCache", + cfName, + cf2Name }), null, null, true); + } + Object syncMap = conf.toProperties(false).get("openjpa.jdbc.SynchronizeMappings"); + if(syncMap != null) { + throw new ArgumentException(_loc.get("invalid-cfname-prop", new Object[] { + "openjpa.jdbc.SynchronizeMappings", + cfName, + cf2Name }), null, null, true); + } + } + } } diff --git a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties index b01c6604b..c4e12d6c6 100644 --- a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties +++ b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties @@ -240,3 +240,5 @@ override-named-query-lock-mode: Encountered a read lock level less than LockMode NamedQuery {0} "{1}" in class "{2}". Setting query lock level to LockModeType.READ. access-default: Access style for "{0}" can not be determined. The default access of level of "{1}" will be used. invalid-oid: An incorrect object id type was encountered. Expected "{0}" but was passed "{1}". +invalid-cfname-prop: The "{0}" configuration option is not valid when the DataSource JNDI name(s) are provided \ + when you create an EntityManager. Found jtaDataSource: "{1}", nonJtaDataSource: "{2}".