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
This commit is contained in:
Michael Dick 2010-07-28 14:30:05 +00:00
parent d27ca8ef8a
commit e97e42b3eb
4 changed files with 120 additions and 1 deletions

View File

@ -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"));
}
}
}

View File

@ -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"));
}
}
}

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.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()) {
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);
}
}
}
}

View File

@ -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}".