use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class PersistenceManagerFactoryTest method testObjectForStringProperty.
/**
* Test setting an object for a property value but string was expected
*/
public void testObjectForStringProperty() {
Properties props = TestHelper.getPropertiesForDatastore(1);
Map<Object, Object> map = new HashMap<>();
map.putAll(props);
map.put("javax.jdo.option.Mapping", new Object());
try {
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(map);
try {
fail("Expected JDOFatalInternalException");
} finally {
pmf.close();
}
} catch (JDOFatalUserException ex) {
assertEquals(IllegalArgumentException.class, ex.getNestedExceptions()[0].getCause().getClass());
} catch (Exception e) {
fail("Incorrect exception thrown : expected JDOFatalUserException but got " + e.getClass().getName());
}
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class PersistenceManagerFactoryTest method testJDOHelperInstantiation.
/**
* Test instantiating a <code>PersistenceManagerFactory</code> via
* <code>JDOHelper.getPersistenceManagerFactory(Properties)</code>.
*/
public void testJDOHelperInstantiation() {
/*
* Things to consider:
* - Unknown properties should do nothing.
* - Setting Optimistic or RetainValues to true sets NontransactionalRead
* to true.
* - TransactionIsolation has valid values of Connection.TRANSACTION_*;
* anything else will throw an Exception.
* - A PersistenceManagerFactory obtained via JDOHelper should be
* nonconfigurable.
*/
/*
* 1) Test setting all propers to valid values.
*/
boolean optimistic = true;
boolean retainValues = true;
boolean restoreValues = true;
boolean ignoreCache = true;
boolean nontransactionalRead = true;
boolean nontransactionalWrite = false;
boolean multithreaded = true;
Properties dbProps = TestHelper.getPropertiesForDatastore(1);
String driverName = dbProps.getProperty("datanucleus.ConnectionDriverName");
String url = dbProps.getProperty("datanucleus.ConnectionURL");
String userName = dbProps.getProperty("datanucleus.ConnectionUserName");
String password = dbProps.getProperty("datanucleus.ConnectionPassword");
boolean validateTables = true;
boolean validateConstraints = true;
boolean autoCreateTables = true;
boolean autoCreateConstraints = true;
int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
PMFProperties props = new PMFProperties();
props.setOptimistic(optimistic);
props.setRetainValues(retainValues);
// This throws an exception. Test later.
props.setRestoreValues(restoreValues);
props.setIgnoreCache(ignoreCache);
props.setNontransactionalRead(nontransactionalRead);
props.setNontransactionalWrite(nontransactionalWrite);
props.setMultithreaded(multithreaded);
if (driverName != null) {
props.setDriverName(driverName);
}
if (userName != null) {
props.setUserName(userName);
}
if (password != null) {
props.setPassword(password);
}
props.setURL(url);
// props.setFactoryName(factory1); // Setting this uses jndi which blows up w/out
// props.setFactory2Name(factory2); // a managed environment (ie - no JNDI).
props.setValidateTables(validateTables);
props.setValidateConstraints(validateConstraints);
props.setAutoCreateTables(autoCreateTables);
props.setAutoCreateConstraints(autoCreateConstraints);
props.setTransactionIsolation(transactionIsolation);
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("PMF should be org.datanucleus.api.jdo.JDOPersistenceManagerFactory.", (pmf instanceof JDOPersistenceManagerFactory));
assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
pmf.close();
// Test whether NonTransactionalWrite was correctly handled
props.setNontransactionalWrite(true);
try {
pmf = JDOHelper.getPersistenceManagerFactory(props);
} catch (Exception e) {
fail("Setting of nontransactionalWrite shouldnt have caused an Exception but did : " + e.getMessage());
}
props.setNontransactionalWrite(false);
/*
* Test whether NonTransactionalRead was correctly set
*/
props.setNontransactionalRead(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
pmf.close();
props.setNontransactionalRead(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
pmf.close();
props.setNontransactionalRead(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
pmf.close();
props.setNontransactionalRead(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
pmf.close();
/*
* Test whether RetainValues was correctly set
*/
props.setRetainValues(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("RetainValues should be false", pmf.getRetainValues());
pmf.close();
props.setRetainValues(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("RetainValues should be true", pmf.getRetainValues());
pmf.close();
props.setRetainValues(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("RetainValues should be false", pmf.getRetainValues());
pmf.close();
props.setRetainValues(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("RetainValues should be true", pmf.getRetainValues());
pmf.close();
/*
* Test whether RestoreValues was correctly set
*/
props.setRestoreValues(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("RestoreValues should be false", pmf.getRestoreValues());
pmf.close();
props.setRestoreValues(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("RestoreValues should be true", pmf.getRestoreValues());
pmf.close();
props.setRestoreValues(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("RestoreValues should be false", pmf.getRestoreValues());
pmf.close();
props.setRestoreValues(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("RestoreValues should be true", pmf.getRestoreValues());
pmf.close();
/*
* Test whether Optimistic was correctly set
*/
props.setOptimistic(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("Optimistic should be false", pmf.getOptimistic());
pmf.close();
props.setOptimistic(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("Optimistic should be true", pmf.getOptimistic());
pmf.close();
props.setOptimistic(false);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("Optimistic should be false", pmf.getOptimistic());
pmf.close();
props.setOptimistic(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("Optimistic should be true", pmf.getOptimistic());
pmf.close();
/*
* Test changing one property does not change other property JDO 2 ED
* spec 5.6 NontransactionalRead, NontransactionalWrite, Optimistic, and
* RetainValues are independent options. A JDO implementation must not
* automatically change the values of these properties as a side effect
* of the user changing other properties.
*/
props.setNontransactionalRead(false);
props.setRetainValues(false);
props.setOptimistic(false);
props.setRestoreValues(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
assertFalse("RetainValues should be false", pmf.getRetainValues());
assertFalse("Optimistic should be false", pmf.getOptimistic());
assertTrue("RestoreValues should be true", pmf.getRestoreValues());
pmf.close();
props.setRestoreValues(false);
props.setNontransactionalRead(false);
props.setRetainValues(false);
props.setOptimistic(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("RestoreValues should be false", pmf.getRestoreValues());
assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
assertFalse("RetainValues should be false", pmf.getRetainValues());
assertTrue("Optimistic should be true", pmf.getOptimistic());
pmf.close();
props.setOptimistic(false);
props.setRestoreValues(false);
props.setNontransactionalRead(false);
props.setRetainValues(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("Optimistic should be false", pmf.getOptimistic());
assertFalse("RestoreValues should be false", pmf.getRestoreValues());
assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
assertTrue("RetainValues should be true", pmf.getRetainValues());
pmf.close();
props.setRetainValues(false);
props.setOptimistic(false);
props.setRestoreValues(false);
props.setNontransactionalRead(true);
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertFalse("RetainValues should be false", pmf.getRetainValues());
assertFalse("Optimistic should be false", pmf.getOptimistic());
assertFalse("RestoreValues should be false", pmf.getRestoreValues());
assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
pmf.close();
/*
* 2) Test that an invalid driver name throws an exception
*/
if (vendorID != null) {
props.setDriverName("my.test.driver.should.not.exist");
try {
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("Invalid driver name should have thrown exception.", false);
pmf.close();
} catch (Exception e) {
/* Ignore */
}
}
/*
* 3) Test that setting an invalid transaction isolation throws an exception.
*/
if (vendorID != null) {
props.setDriverName(driverName);
props.setTransactionIsolation(8738);
try {
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("Setting invalid transaction isolation should have thrown exception.", false);
pmf.close();
} catch (Exception e) {
/* Ignore */
}
}
/*
* 4) Test that PMF is not configurable after getting an instance.
*/
try {
if (vendorID != null) {
props.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
}
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
pmf.setConnectionUserName("bobjones");
assertTrue("Setting properties after frozen should throw exception.", false);
pmf.close();
} catch (JDOUserException e) {
/* Ignore */
}
/*
* 5) Test return data from getProperties
*/
Properties pmf_props = pmf.getProperties();
String vendor = pmf_props.getProperty("VendorName");
if (vendor == null) {
assertTrue("PMF doesnt return VendorName property", false);
}
String version = pmf_props.getProperty("VersionNumber");
if (version == null) {
assertTrue("PMF doesnt return VersionNumber property", false);
}
/*
* 6) Test that setting an unknown property does nothing weird.
*/
props.setProperty("datanucleus.MyTestUnknownProperty", "unknown");
pmf = JDOHelper.getPersistenceManagerFactory(props);
assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
pmf.close();
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class PersistenceManagerProxyTest method testLifecycleListenerRegisteredInPMFforAllClasses.
/**
* Test of lifecycle listener registered for all classes
*/
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
BasicListener listener = new BasicListener(true);
PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
pmf.addInstanceLifecycleListener(listener, null);
freezePMF(pmf);
try {
PersistenceManager pm = pmf.getPersistenceManagerProxy();
Transaction tx = pm.currentTransaction();
int i = 0;
try {
tx.begin();
// Persist an object and check the events
Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
pm.makePersistent(person);
// Persist related objects and check the events
// Manager has a 1-N (FK) with Department
Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
Department dept1 = new Department("Invasions");
Department dept2 = new Department("Propaganda");
Department dept3 = new Department("Lies");
manager.addDepartment(dept1);
manager.addDepartment(dept2);
manager.addDepartment(dept3);
dept1.setManager(manager);
dept2.setManager(manager);
dept3.setManager(manager);
pm.makePersistent(manager);
pm.flush();
Integer[] events = listener.getRegisteredEventsAsArray();
assertEquals("Wrong number of lifecycle events", 15, events.length);
if (tx.getOptimistic()) {
// Person
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
int numPreStore = 0;
int numPostStore = 0;
int numEventsToProcess = i + 10;
for (int j = i; j < numEventsToProcess; j++) {
if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
numPreStore++;
} else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
numPostStore++;
}
i++;
}
// 1 for each object
assertEquals("Number of PreStore events is wrong", 5, numPreStore);
// 1 for each object
assertEquals("Number of PostStore events is wrong", 5, numPostStore);
} else {
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
}
tx.rollback();
listener.getRegisteredEvents().clear();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm.currentTransaction();
try {
tx2.begin();
// Persist an object and check the events
pm.makePersistent(person);
pm.flush();
events = listener.getRegisteredEventsAsArray();
i = 0;
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
tx2.rollback();
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
} catch (Exception e) {
LOG.error("Exception while running lifecycle listener simple object test", e);
fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
listener.getRegisteredEvents().clear();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
pmf.close();
}
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class PersistenceManagerTest method testLifecycleListenerRegisteredInPMFforAllClasses.
/**
* Test of lifecycle listener registered for all classes
*/
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
BasicListener listener = new BasicListener(true);
PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
pmf.addInstanceLifecycleListener(listener, null);
freezePMF(pmf);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
int i = 0;
try {
tx.begin();
// Persist an object and check the events
Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
pm.makePersistent(person);
// Persist related objects and check the events
// Manager has a 1-N (FK) with Department
Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
Department dept1 = new Department("Invasions");
Department dept2 = new Department("Propaganda");
Department dept3 = new Department("Lies");
manager.addDepartment(dept1);
manager.addDepartment(dept2);
manager.addDepartment(dept3);
dept1.setManager(manager);
dept2.setManager(manager);
dept3.setManager(manager);
pm.makePersistent(manager);
pm.flush();
Integer[] events = listener.getRegisteredEventsAsArray();
assertEquals("Wrong number of lifecycle events", 15, events.length);
if (tx.getOptimistic()) {
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Expecting 5 PreStore and 5 PostStore; 1 for each of the objects
int numPreStore = 0;
int numPostStore = 0;
int numOther = 0;
for (int j = i; j < 15; j++) {
if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
numPreStore++;
} else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
numPostStore++;
} else {
numOther++;
}
i++;
}
assertEquals("Number of PreStore events was incorrect", 5, numPreStore);
assertEquals("Number of PostStore events was incorrect", 5, numPostStore);
assertEquals("Number of other events was incorrect", 0, numOther);
} else {
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
}
tx.rollback();
listener.getRegisteredEvents().clear();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm.currentTransaction();
try {
tx2.begin();
// Persist an object and check the events
pm.makePersistent(person);
pm.flush();
events = listener.getRegisteredEventsAsArray();
i = 0;
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
tx2.rollback();
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
listener.getRegisteredEvents().clear();
pmf.close();
}
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class ManagedConnectionTest method testNontransactionalReleaseAfterUseFalse.
/**
* Test case for managed connections in nontx usage where not releasing.
*/
public void testNontransactionalReleaseAfterUseFalse() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CONNECTION_NONTX_RELEASE_AFTER_USE, "false");
PersistenceManagerFactory thePMF = getPMF(1, userProps);
try {
PersistenceManager pm = thePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create sample data
Object acctId = null;
try {
tx.begin();
LoginAccount acct = new LoginAccount("Barney", "Rubble", "brubble", "bambam");
pm.makePersistent(acct);
acctId = JDOHelper.getObjectId(acct);
tx.commit();
} catch (Exception e) {
LOG.error("Exception while creating data", e);
fail("Exception thrown while creating data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
try {
for (int i = 0; i < 60; i++) {
pm = thePMF.getPersistenceManager();
pm.getObjectById(acctId);
pm.close();
}
} catch (Exception e) {
LOG.error("Exception while getting connections", e);
fail("Exception thrown while getting connections : " + e.getMessage());
} finally {
if (!pm.isClosed()) {
pm.close();
}
}
} finally {
thePMF.close();
}
}
Aggregations