use of org.datanucleus.samples.detach.fetchdepth.Directory in project tests by datanucleus.
the class AttachDetachTest method testFetchRecurse.
/**
* Test for recursive relations in fetching (use of recursion-depth)
*/
public void testFetchRecurse() {
try {
Object dir1id = null;
Object dir5id = null;
Directory detachedDir = null;
// Persist sample data
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Directory /usr
Directory dir1 = new Directory("/usr");
// Directory /usr/local
Directory dir2 = new Directory("/local");
dir2.setParent(dir1);
dir1.addChild(dir2);
// Directory /usr/local/audio
Directory dir3 = new Directory("/audio");
dir3.setParent(dir2);
dir2.addChild(dir3);
// Directory /usr/local/audio/mp3
Directory dir4 = new Directory("/mp3");
dir4.setParent(dir3);
dir3.addChild(dir4);
// Directory /usr/local/audio/flac
Directory dir5 = new Directory("/flac");
dir5.setParent(dir3);
dir3.addChild(dir5);
pm.makePersistent(dir1);
tx.commit();
dir1id = pm.getObjectId(dir1);
dir5id = pm.getObjectId(dir5);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating sample data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Test 1 - detach and check the detach from the bottom directory ... checking the parents (unlimited recursion)
pm = newPM();
tx = pm.currentTransaction();
try {
tx.begin();
Directory dir = (Directory) pm.getObjectById(dir5id);
pm.getFetchPlan().addGroup("groupA");
// Max big enough to not cause any limit
pm.getFetchPlan().setMaxFetchDepth(5);
detachedDir = (Directory) pm.detachCopy(dir);
tx.commit();
} catch (Exception e) {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
fail("Exception thrown while detaching top-down recursive tree : " + e.getMessage());
}
try {
// Check that all parents are detached
Directory parent = detachedDir;
while (parent != null) {
parent = parent.getParent();
}
} catch (JDODetachedFieldAccessException dfae) {
fail("Exception thrown while inspecting detached bottom-up recursive tree : " + dfae.getMessage());
}
// Test 2 - detach and check the detach from the main parent ... checking the children (limited recursion)
pm = newPM();
tx = pm.currentTransaction();
try {
tx.begin();
Directory dir = (Directory) pm.getObjectById(dir1id);
pm.getFetchPlan().addGroup("groupA");
// Max big enough to not cause any limit
pm.getFetchPlan().setMaxFetchDepth(5);
detachedDir = (Directory) pm.detachCopy(dir);
tx.commit();
} catch (Exception e) {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
fail("Exception thrown while detaching top-down recursive tree : " + e.getMessage());
}
try {
// Check that we have the 1st level of children
Set level1children = detachedDir.getChildren();
Iterator level1childIter = level1children.iterator();
while (level1childIter.hasNext()) {
Directory child = (Directory) level1childIter.next();
child.getParent();
try {
// Check that we have the 2nd level of children
Set level2children = child.getChildren();
Iterator level2childIter = level2children.iterator();
while (level2childIter.hasNext()) {
Directory grandchild = (Directory) level2childIter.next();
try {
// Check that we dont have the 3rd level of children
Set level3children = grandchild.getChildren();
Iterator level3childIter = level3children.iterator();
while (level3childIter.hasNext()) {
Directory greatgrandchild = (Directory) level3childIter.next();
// Should throw exception
LOG.info(">> Detached 3rd level child " + greatgrandchild.getName());
fail("Managed to detach 3rd level of directories children - recursion should have stopped at 2 levels");
}
} catch (JDODetachedFieldAccessException e) {
// To be expected
}
}
} catch (JDODetachedFieldAccessException e) {
LOG.error("Exception in test", e);
fail("One of the objects in a recursive relation that should have been detached hasn't! : " + e.getMessage());
}
}
} catch (JDODetachedFieldAccessException e) {
LOG.error("Exception in test", e);
fail("One of the objects in a recursive relation that should have been detached hasn't! : " + e.getMessage());
}
} finally {
// Clean out our data
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(Directory.class, false);
Iterator it = ext.iterator();
while (it.hasNext()) {
Directory dir = (Directory) it.next();
dir.setParent(null);
dir.clearChildren();
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
clean(Directory.class);
}
}
Aggregations