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
This commit is contained in:
Michael Dick 2010-07-28 20:32:42 +00:00
parent dba341a7d9
commit 4a1501ed9d
5 changed files with 120 additions and 3 deletions

View File

@ -165,5 +165,47 @@ public class TestOverrideNonJtaDataSource extends AbstractPersistenceTestCase {
assertTrue(e.getMessage().contains("EntityManager")); // ensure where the JNDI name came from is in message 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"));
}
}
} }

View File

@ -28,6 +28,7 @@ import javax.persistence.RollbackException;
import org.apache.openjpa.persistence.ArgumentException; import org.apache.openjpa.persistence.ArgumentException;
import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
import org.apache.openjpa.util.UserException;
public class TestSwitchConnection extends AbstractPersistenceTestCase { public class TestSwitchConnection extends AbstractPersistenceTestCase {
private String defaultJndiName = "jdbc/mocked"; 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 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"));
}
}
} }

View File

@ -89,6 +89,4 @@ public class TestTimestampVersion extends SingleEMFTestCase {
int newVersion = pc.getVersion(); int newVersion = pc.getVersion();
assertEquals(newVersion, oldVersion+1); assertEquals(newVersion, oldVersion+1);
} }
} }

View File

@ -48,6 +48,7 @@ import org.apache.openjpa.persistence.criteria.OpenJPACriteriaBuilder;
import org.apache.openjpa.persistence.meta.MetamodelImpl; import org.apache.openjpa.persistence.meta.MetamodelImpl;
import org.apache.openjpa.persistence.query.OpenJPAQueryBuilder; import org.apache.openjpa.persistence.query.OpenJPAQueryBuilder;
import org.apache.openjpa.persistence.query.QueryBuilderImpl; import org.apache.openjpa.persistence.query.QueryBuilderImpl;
import org.apache.openjpa.util.UserException;
/** /**
* Implementation of {@link EntityManagerFactory} that acts as a * Implementation of {@link EntityManagerFactory} that acts as a
@ -214,8 +215,14 @@ public class EntityManagerFactoryImpl
} }
if (log != null && log.isTraceEnabled()) { if (log != null && log.isTraceEnabled()) {
if(StringUtils.isNotEmpty(cfName)) {
log.trace("Found ConnectionFactoryName from props: " + 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); Broker broker = _factory.newBroker(user, pass, managed, retainMode, false, cfName, cf2Name);
@ -357,4 +364,29 @@ public class EntityManagerFactoryImpl
return (OpenJPAPersistenceUtil.isManagedBy(this, entity) && return (OpenJPAPersistenceUtil.isManagedBy(this, entity) &&
(OpenJPAPersistenceUtil.isLoaded(entity, attribute) == LoadState.LOADED)); (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);
}
}
}
} }

View File

@ -232,3 +232,5 @@ cache-retrieve-override: The setting of CacheRetrieveMode.USE is ignored and set
null-detach: Can not detach null entity null-detach: Can not detach null entity
override-named-query-lock-mode: Encountered a read lock level less than LockModeType.READ when processing the \ 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. 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}".