From 4a1501ed9dbadbcf047a76fd4c614448768cb253 Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Wed, 28 Jul 2010 20:32:42 +0000 Subject: [PATCH] OPENJPA-1749: Throw exception when incompatible configuration options are set git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@980199 13f79535-47bb-0310-9956-ffa450edef68 --- .../conf/TestOverrideNonJtaDataSource.java | 42 ++++++++++++++++++ .../conf/TestSwitchConnection.java | 43 +++++++++++++++++++ .../jdbc/update/TestTimestampVersion.java | 2 - .../persistence/EntityManagerFactoryImpl.java | 34 ++++++++++++++- .../openjpa/persistence/localizer.properties | 2 + 5 files changed, 120 insertions(+), 3 deletions(-) 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 47609f205..f2c553b1e 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,5 +165,47 @@ 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-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java index ec7995fc1..da16ea344 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/update/TestTimestampVersion.java @@ -89,6 +89,4 @@ public class TestTimestampVersion extends SingleEMFTestCase { int newVersion = pc.getVersion(); assertEquals(newVersion, oldVersion+1); } - - } 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 b1bc10c3e..fc6b1201b 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 @@ -232,3 +232,5 @@ cache-retrieve-override: The setting of CacheRetrieveMode.USE is ignored and set null-detach: Can not detach null entity override-named-query-lock-mode: Encountered a read lock level less than LockModeType.READ when processing the \ NamedQuery {0} "{1}" in class "{2}". Setting query lock level to LockModeType.READ. +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}".