use of org.datanucleus.tests.annotations.TransactionMode in project tests by datanucleus.
the class ManagedRelationshipTest method testOneToManyFKBidirSetCollectionMoveElement.
/**
* Test for management of relations with a 1-N FK bidir where an element is being moved from one collection owner to another,
* by setting a collection containing that element on a new owner.
* The implication of the test is that secondary changes will also be handled by the RelationshipManager (i.e if an element is added to
* one collection, and is using FK, then has to also be removed from the old collection (in datastore and in memory).
*/
@TransactionMode(PESSIMISTIC)
public // FIXME Fix text in Optimistic mode
void testOneToManyFKBidirSetCollectionMoveElement() {
PersistenceManager pm = null;
Transaction tx = null;
try {
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
// Create object graph:
// farm1 <-> {animal1, animal2}
// farm2 <-> {animal3, animal4}
// farm5 <-> {animal5}
tx.begin();
Farm farm1 = new Farm("farm1");
Animal animal1 = new Animal("animal1");
Animal animal2 = new Animal("animal2");
farm1.setAnimals(createSet(animal1, animal2));
pm.makePersistent(farm1);
Farm farm2 = new Farm("farm2");
Animal animal3 = new Animal("animal3");
Animal animal4 = new Animal("animal4");
farm2.setAnimals(createSet(animal3, animal4));
pm.makePersistent(farm2);
Farm farm3 = new Farm("farm3");
Animal animal5 = new Animal("animal5");
farm3.setAnimals(createSet(animal5));
pm.makePersistent(farm3);
pm.flush();
// validate objectgraph
assertEquals(createSet(animal1, animal2), farm1.getAnimals());
assertEquals(farm1, animal1.getFarm());
assertEquals(farm1, animal2.getFarm());
assertEquals(createSet(animal3, animal4), farm2.getAnimals());
assertEquals(farm2, animal3.getFarm());
assertEquals(farm2, animal4.getFarm());
assertEquals(createSet(animal5), farm3.getAnimals());
assertEquals(farm3, animal5.getFarm());
pm.flush();
// perform update and validate
LOG.info(">> farm1 " + farm1 + " id=" + JDOHelper.getObjectId(farm1) + " state=" + JDOHelper.getObjectState(farm1));
LOG.info(">> farm2 " + farm2 + " id=" + JDOHelper.getObjectId(farm2) + " state=" + JDOHelper.getObjectState(farm2));
LOG.info(">> farm3 " + farm3 + " id=" + JDOHelper.getObjectId(farm3) + " state=" + JDOHelper.getObjectState(farm3));
LOG.info(">> animal1 " + animal1 + " id=" + JDOHelper.getObjectId(animal1) + " farm=" + JDOHelper.getObjectId(animal1.getFarm()));
LOG.info(">> animal2 " + animal2 + " id=" + JDOHelper.getObjectId(animal2) + " farm=" + JDOHelper.getObjectId(animal2.getFarm()));
LOG.info(">> animal3 " + animal3 + " id=" + JDOHelper.getObjectId(animal3) + " farm=" + JDOHelper.getObjectId(animal3.getFarm()));
LOG.info(">> animal4 " + animal4 + " id=" + JDOHelper.getObjectId(animal4) + " farm=" + JDOHelper.getObjectId(animal4.getFarm()));
LOG.info(">> animal5 " + animal5 + " id=" + JDOHelper.getObjectId(animal5) + " farm=" + JDOHelper.getObjectId(animal5.getFarm()));
farm1.setAnimals(createSet(animal2, animal3, animal5));
LOG.info(">> flush.start for change");
pm.flush();
LOG.info(">> flush.complete");
// will move animal3 from farm2 to farm1
// will move animal5 from farm3 to farm2
// should result in:
// farm1 <-> {animal2, animal3, animal5}
// farm2 <-> {animal4}
// farm3 <-> {}
// i.e. animal3 and animal5 moved from their previous owners to farm1
assertEquals(createSet(animal2, animal3, animal5), farm1.getAnimals());
assertEquals(farm1, animal2.getFarm());
assertEquals(farm1, animal3.getFarm());
assertEquals(farm1, animal5.getFarm());
assertEquals(createSet(animal4), farm2.getAnimals());
assertEquals(farm2, animal4.getFarm());
assertEquals(Collections.EMPTY_SET, farm3.getAnimals());
tx.commit();
} finally {
try {
if (tx != null && tx.isActive()) {
tx.rollback();
}
if (pm != null) {
pm.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
}
use of org.datanucleus.tests.annotations.TransactionMode 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();
}
}
}
Aggregations