use of org.datanucleus.samples.models.hashsetcollection.Master in project tests by datanucleus.
the class AttachDetachTest method testRelationManaging.
/**
* Test checks if we can connect detached objects outside pm context
*/
public void testRelationManaging() {
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Master m = new Master();
m.setId("Master");
pm.makePersistent(m);
Detail d = new Detail();
d.setId("Detail");
d.setMaster(m);
Master m1 = new Master();
m1.setId("Master1");
pm.makePersistent(d);
pm.makePersistent(m1);
pm.detachCopy(m);
Detail detachedD = (Detail) pm.detachCopy(d);
detachedD.setMaster(m1);
Master chM = detachedD.getMaster();
assertEquals(chM.getId(), "Master1");
Detail attachedD = (Detail) pm.makePersistent(detachedD);
assertEquals(attachedD.getMaster().getId(), "Master1");
tx.rollback();
} catch (JDOUserException ue) {
LOG.error("Exception in test", ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.samples.models.hashsetcollection.Master in project tests by datanucleus.
the class AttachDetachReplicateTest method testMoveAcrossDatastoresWithRelation.
/**
* Test of detaching from one datastore and persisting the objects to another.
* As we copy a group of related objects JPOX should serialize SQLs in the
* way that do not break foreign key constraints.
*/
public void testMoveAcrossDatastoresWithRelation() {
PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
try {
PersistenceManager pm1 = pmf.getPersistenceManager();
Detail detail = null;
// Persist in first DB
Transaction tx = null;
try {
tx = pm1.currentTransaction();
tx.begin();
// Create some dummy records, so we offset the identity values and make a valid test
pm1.makePersistent(new Master());
pm1.makePersistent(new Master());
pm1.makePersistent(new Master());
pm1.makePersistent(new Detail());
pm1.makePersistent(new Detail());
pm1.makePersistent(new Detail());
pm1.makePersistent(new OtherDetail());
pm1.makePersistent(new OtherDetail());
pm1.makePersistent(new OtherDetail());
// Create our test objects, Master with related Detail and OtherDetail
Master master1 = new Master();
master1.setId("master");
Detail detail1 = new Detail();
detail1.setId("detail");
master1.addDetail(detail1);
detail1.setMaster(master1);
OtherDetail otherDetail1 = new OtherDetail();
otherDetail1.setId("otherDetail1");
master1.addOtherDetail(otherDetail1);
otherDetail1.setMaster(master1);
pm1.makePersistent(detail1);
// Detach it for copying
pm1.getFetchPlan().addGroup("all");
pm1.getFetchPlan().setMaxFetchDepth(2);
detail = (Detail) pm1.detachCopy(detail1);
tx.commit();
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
pm1.close();
}
// Check our detached objects
assertNotNull(detail.getMaster());
assertEquals(detail.getMaster().getOtherDetails().size(), 1);
// Copy to other DB
PersistenceManager pm2 = pmf2.getPersistenceManager();
try {
tx = pm2.currentTransaction();
tx.begin();
// Persist graph of three transient objects to see if this datastore works
Master master2 = new Master();
master2.setId("master2");
Detail detail2 = new Detail();
detail2.setId("detail2");
master2.addDetail(detail2);
detail2.setMaster(master2);
OtherDetail otherDetail2 = new OtherDetail();
otherDetail2.setId("otherDetail2");
master2.addOtherDetail(otherDetail2);
otherDetail2.setMaster(master2);
pm2.makePersistent(detail2);
// Replicate object graph of three detached objects from datastore 1
pm2.makePersistent(detail);
tx.commit();
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while copying object into second datastore : " + ue.getMessage());
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
}
// Check the persistence in the second datastore
try {
tx = pm2.currentTransaction();
tx.begin();
// Use Extent since PM may have just put object in cache.
Extent e = pm2.getExtent(Master.class);
Iterator iter = e.iterator();
int noOfMasters = 0;
while (iter.hasNext()) {
noOfMasters++;
iter.next();
}
assertTrue("Number of masters retrieved from second datastore is incorrect : was " + noOfMasters + " but should have been 2", noOfMasters == 2);
e = pm2.getExtent(OtherDetail.class);
iter = e.iterator();
int noOfOtherDetails = 0;
while (iter.hasNext()) {
noOfOtherDetails++;
iter.next();
}
assertTrue("Number of otherdetails retrieved from second datastore is incorrect : was " + noOfOtherDetails + " but should have been 2", noOfOtherDetails == 2);
tx.commit();
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
pm2.close();
}
} finally {
// Clean up our data in the main DB
clean(Detail.class);
clean(OtherDetail.class);
clean(Master.class);
// Clean up data in the other DB
PersistenceManager pm = pmf2.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// delete all Detail objects
tx.begin();
Extent ext = pm.getExtent(Detail.class, true);
Iterator it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
// delete all OtherDetail objects
tx.begin();
ext = pm.getExtent(OtherDetail.class, true);
it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
// delete all Master objects
tx.begin();
ext = pm.getExtent(Master.class, true);
it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
}
use of org.datanucleus.samples.models.hashsetcollection.Master in project tests by datanucleus.
the class AttachDetachReplicateTest method testSetDetachedObjectOnFieldInPCNewObject.
/**
* Test checks if we can detach a graph of objects where there are
* persistent and/or detached objects inside this graph
*
* Test adding a detached object to a PC_NEW object graph (after it has been made persistent)
* TODO Change the sample to be Company
*/
@TransactionMode(PESSIMISTIC)
public void testSetDetachedObjectOnFieldInPCNewObject() {
PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
try {
PersistenceManager pm = null;
Transaction tx = null;
Master detachedMaster = null;
try {
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
Master master = new Master();
master.setId("Master3");
pm.makePersistent(master);
pm.getFetchPlan().setMaxFetchDepth(2);
detachedMaster = (Master) pm.detachCopy(master);
tx.commit();
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Associate with the detached master in a different datastore
try {
pm = pmf2.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
tx.setRetainValues(true);
Detail detail = new Detail();
detail.setId("Detail3");
pm.makePersistent(detail);
OtherDetail otherDetail = new OtherDetail();
otherDetail.setId("OtherDetail3");
pm.makePersistent(otherDetail);
assertTrue(JDOHelper.isDetached(detachedMaster));
// set a detached object to a field in a PC_NEW instance
detail.setMaster(detachedMaster);
detachedMaster.addDetail(detail);
otherDetail.setMaster(detachedMaster);
detachedMaster.addOtherDetail(otherDetail);
tx.commit();
assertFalse("detail object is still detached, but should have been attached", JDOHelper.isDetached(detail));
assertNotNull("detail object has master which is null!", detail.getMaster());
assertFalse("detached has master that has not been attached", JDOHelper.isDetached(detail.getMaster()));
assertTrue("detail object has master but number of other details is 0!", detail.getMaster().getOtherDetails().size() > 0);
assertFalse("detail object has master that has otherdetail that is detached still!", JDOHelper.isDetached(detail.getMaster().getOtherDetails().iterator().next()));
assertFalse("otherdetail object is still detached, but should have been attached", JDOHelper.isDetached(otherDetail));
assertNotNull("otherdetail object has master which is null!", otherDetail.getMaster());
assertFalse("otherdetail has master that has not been attached!", JDOHelper.isDetached(otherDetail.getMaster()));
// Detach the detail
tx.begin();
pm.getFetchPlan().addGroup("all");
pm.getFetchPlan().setMaxFetchDepth(2);
Detail detachedDetail = (Detail) pm.detachCopy(detail);
tx.commit();
assertTrue(JDOHelper.isDetached(detachedDetail));
assertTrue(JDOHelper.isDetached(detachedDetail.getMaster()));
assertTrue(JDOHelper.isDetached(detachedDetail.getMaster().getOtherDetails().iterator().next()));
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean up our data in the main DB
clean(OtherDetail.class);
clean(Detail.class);
clean(Master.class);
// Clean up data in the other DB
PersistenceManager pm = pmf2.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// delete all Detail objects
tx.begin();
Extent ext = pm.getExtent(Detail.class, true);
Iterator it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
// delete all OtherDetail objects
tx.begin();
ext = pm.getExtent(OtherDetail.class, true);
it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
// delete all Master objects
tx.begin();
ext = pm.getExtent(Master.class, true);
it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
}
use of org.datanucleus.samples.models.hashsetcollection.Master in project tests by datanucleus.
the class AttachDetachTest method testCircularDetach.
/**
* Test of detaching object graph with circular references
*/
public void testCircularDetach() {
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Master m = new Master();
m.setId("tcMaster");
Detail d = new Detail();
d.setId("tcDetail");
Circon c = new Circon();
c.setId("tcConnector");
m.addDetail(d);
m.setCircon(c);
d.setMaster(m);
d.addCircon(c);
c.setDetail(d);
c.addMaster(m);
pm.makePersistent(m);
assertTrue(JDOHelper.isPersistent(m));
assertTrue(JDOHelper.isPersistent(d));
assertTrue(JDOHelper.isPersistent(c));
pm.getFetchPlan().addGroup("all");
pm.getFetchPlan().setMaxFetchDepth(2);
Master dm = (Master) pm.detachCopy(m);
assertTrue(JDOHelper.isDetached(dm));
assertTrue(JDOHelper.isDetached(dm.getCircon()));
assertTrue(JDOHelper.isDetached(dm.getCircon().getDetail()));
tx.rollback();
} catch (JDOUserException ue) {
LOG.error("Exception in test", ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations