use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.
the class AttachDetachTest method testCopyOnAttachFalseMultipleDetach.
/**
* Test for use of CopyOnAttach=false with a 1-1 unidir relation and where we detach more than 1 of an object
* and try to attach them in the same txn.
*/
public void testCopyOnAttachFalseMultipleDetach() {
try {
LoginAccount detachedAcct1 = null;
LoginAccount detachedAcct2 = null;
// Persist the owner object
PersistenceManager pm = newPM();
pm.setCopyOnAttach(false);
pm.getFetchPlan().setGroup(FetchPlan.ALL).setMaxFetchDepth(2);
Transaction tx = pm.currentTransaction();
try {
// Persist
tx.begin();
LoginAccount acct = new LoginAccount("George", "Bush", "bush", "iraq");
pm.makePersistent(acct);
// Detach 2 copies
detachedAcct1 = (LoginAccount) pm.detachCopy(acct);
detachedAcct2 = (LoginAccount) pm.detachCopy(acct);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Update the detached objects
assertEquals("Account1 object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedAcct1));
assertEquals("Account2 object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedAcct2));
detachedAcct1.setFirstName("George W");
detachedAcct2.setFirstName("George B");
// Attach the detached objects
pm = newPM();
pm.setCopyOnAttach(false);
tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(detachedAcct1);
pm.flush();
try {
pm.makePersistent(detachedAcct2);
LOG.error("Attach of second version of object succeeded!");
fail("Attach of second version of an object succeeded but should have thrown exception since CopyOnAttach=false");
} catch (JDOUserException ue) {
// Expected so rethrow it
throw ue;
}
tx.commit();
} catch (Exception e) {
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(LoginAccount.class);
clean(Login.class);
}
}
use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.
the class CacheTest method testRelationshipCachingWhenNontransactional.
public void testRelationshipCachingWhenNontransactional() throws Exception {
try {
PersistenceManager pm = pmf.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(e);
fail("Exception thrown while creating 1-1 unidirectional relationship data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Make sure the object isn't in the cache following persistence
pmf.getDataStoreCache().evictAll();
pmf.getDataStoreCache().pinAll(true, LoginAccount.class);
pmf.getDataStoreCache().pinAll(true, Login.class);
// Retrieve the record and check the data
pm = pmf.getPersistenceManager();
try {
LOG.info(">> CacheTest fetching LoginAccount(1)");
LoginAccount acct = (LoginAccount) pm.getObjectById(acctId);
assertTrue("LoginAccount \"firstName\" is incorrect", acct.getFirstName().equals("Barney"));
assertTrue("LoginAccount \"lastName\" is incorrect", acct.getLastName().equals("Rubble"));
LOG.info(">> CacheTest fetching Login(1)");
// Access the login field to make sure it is read. Should get L2 cached now
Login login = acct.getLogin();
assertEquals("Login \"login\" is incorrect", login.getUserName(), "brubble");
assertEquals("Login \"password\" is incorrect", login.getPassword(), "bambam");
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while interrogating 1-1 unidirectional relationship data : " + e.getMessage());
} finally {
pm.close();
}
// login account is in the cache now after the last getObjectById
LOG.info(">> CacheTest fetching LoginAccount(non-tx)");
CachedPC cpc = ((JDOPersistenceManagerFactory) pmf).getNucleusContext().getLevel2Cache().get(acctId);
assertTrue("LoginAccount is not cached", cpc != null);
assertTrue("LoginAccount \"login\" is not cached", cpc.getFieldValue(2) != null);
// Retrieve the record and check the data
pm = pmf.getPersistenceManager();
try {
LoginAccount acct = (LoginAccount) pm.getObjectById(acctId);
String[] loadedFields = NucleusJDOHelper.getLoadedFields(acct, pm);
LOG.info(">> loadedFields=" + StringUtils.objectArrayToString(loadedFields));
LOG.info(">> Accessing field login");
Login login = acct.getLogin();
LOG.info(">> login=" + login);
} finally {
pm.close();
}
} finally {
clean(LoginAccount.class);
}
}
use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testPersistOneToOneUni.
/**
* Test of persist of 1-1 UNIDIR relation (PC field).
*/
public void testPersistOneToOneUni() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object acctId = null;
Object loginId = null;
try {
tx.begin();
LoginAccount acct = new LoginAccount("Mickey", "Mouse", "mickeym", "minnie");
acct.setId(1);
Login login = acct.getLogin();
login.setId(1);
pm.makePersistent(acct);
tx.commit();
acctId = pm.getObjectId(acct);
loginId = pm.getObjectId(login);
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
LoginAccount acct = (LoginAccount) pm.getObjectById(acctId);
Login login = (Login) pm.getObjectById(loginId);
assertEquals("Acct first name retrieved is incorrect", "Mickey", acct.getFirstName());
assertEquals("Acct last name retrieved is incorrect", "Mouse", acct.getLastName());
assertEquals("Login username retrieved is incorrect", "mickeym", login.getUserName());
assertEquals("Login password retrieved is incorrect", "minnie", login.getPassword());
assertEquals("Login of LoginAccount retrieved is incorrect", login.getId(), acct.getLogin().getId());
assertEquals("Login of LoginAccount retrieved is incorrect", login.getUserName(), acct.getLogin().getUserName());
assertEquals("Login of LoginAccount retrieved is incorrect", login.getPassword(), acct.getLogin().getPassword());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown retrieving data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out data
clean(LoginAccount.class);
clean(Login.class);
}
}
use of org.jpox.samples.one_one.unidir.LoginAccount 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();
}
}
use of org.jpox.samples.one_one.unidir.LoginAccount in project tests by datanucleus.
the class ExtentTest method testRequiresExtent.
/**
* Test the use of "requires-extent" being false.
*/
public void testRequiresExtent() throws Exception {
try {
// Create some objects.
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount("Andy", "Jefferson", "andy", "pwd");
pm.makePersistent(acct);
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of objects including one with requires-extent=false", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Try to get extent of "Login"
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getExtent(Login.class, true);
tx.commit();
assertTrue("Should have thrown JDOUserException when getting Extent for class which has no extent defined.", false);
} catch (JDOUserException ue) {
LOG.info(ue);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(LoginAccount.class);
}
}
Aggregations