use of javax.jdo.JDODetachedFieldAccessException in project tests by datanucleus.
the class AttachDetachTest method testAggregatedDetachAttachFieldMap.
/**
* test pc objects aggregating other pcs. associations 1-n with default fetch group
*/
public void testAggregatedDetachAttachFieldMap() {
try {
// Create a Gym with 3 Wardrobes
Gym testGym = new Gym();
Map<String, Wardrobe> wardrobes = new HashMap<>();
Wardrobe wSmall = new Wardrobe();
wSmall.setModel("small");
Wardrobe wMedium = new Wardrobe();
wMedium.setModel("medium");
Wardrobe wLarge = new Wardrobe();
wMedium.setModel("large");
wardrobes.put("small", wSmall);
wardrobes.put("medium", wMedium);
wardrobes.put("large", wLarge);
testGym.setWardrobes(wardrobes);
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
Gym detachedGym;
Gym detachedGym2;
Gym detachedGym3;
Object gymID = null;
try {
// Persist the objects and detach them all
tx.begin();
pm.makePersistent(testGym);
pm.getFetchPlan().clearGroups();
pm.getFetchPlan().setGroup("Gym.wardrobes");
detachedGym = (Gym) pm.detachCopy(testGym);
tx.commit();
assertEquals("1) Gym.wardrobes.size()", 3, detachedGym.getWardrobes().size());
gymID = pm.getObjectId(detachedGym);
// Attach the (unchanged) objects, and detach just the Gym since we're only updating that
tx.begin();
pm.makePersistent(detachedGym);
pm.getFetchPlan().clearGroups();
pm.getFetchPlan().setGroup(FetchPlan.DEFAULT);
detachedGym2 = (Gym) pm.detachCopy(pm.getObjectById(gymID));
tx.commit();
try {
Map testMap = detachedGym2.getWardrobes();
assertEquals("X) Gym.wardrobes.size() == 3", testMap.size(), 3);
} catch (JDODetachedFieldAccessException e) {
fail("Field 'Gym.wardrobes' should have been detached since was loaded at detach!");
}
// Update a field in Gym
detachedGym2.setLocation("Freiburg");
// Attach the objects, and detach them once more
tx.begin();
pm.makePersistent(detachedGym2);
pm.getFetchPlan().clearGroups();
pm.getFetchPlan().setGroup("Gym.wardrobes");
detachedGym3 = (Gym) pm.detachCopy(pm.getObjectById(gymID));
tx.commit();
assertEquals("2) Gym.wardrobes.size()", 3, detachedGym3.getWardrobes().size());
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// again with another pm
pm = newPM();
tx = pm.currentTransaction();
try {
if (gymID != null) {
tx.begin();
pm.getFetchPlan().clearGroups();
pm.getFetchPlan().setGroup(FetchPlan.DEFAULT);
detachedGym = (Gym) pm.detachCopy(pm.getObjectById(gymID));
tx.commit();
detachedGym.setLocation("Basel");
tx.begin();
pm.makePersistent(detachedGym);
tx.commit();
tx.begin();
pm.getFetchPlan().clearGroups();
pm.getFetchPlan().setGroup("Gym.wardrobes");
detachedGym2 = (Gym) pm.detachCopy(pm.getObjectById(gymID));
tx.commit();
assertEquals("3) Gym.wardrobes.size()", 3, detachedGym2.getWardrobes().size());
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
FitnessHelper.cleanFitnessData(pmf);
}
}
use of javax.jdo.JDODetachedFieldAccessException in project tests by datanucleus.
the class AttachDetachTest method testDetachLoadUnloadFields.
/**
* Test of DETACH_LOAD_FIELDS, DETACH_UNLOAD_FIELDS flags.
*/
public void testDetachLoadUnloadFields() {
try {
Manager detachedM1a = null;
Manager detachedM1b = null;
// Persist some data
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee e1 = new Employee(1, "Yogi", "Bear", "yogi@warnerbros.com", 124, "10123");
Employee e2 = new Employee(2, "Fred", "Flintstone", "fred.flintstone@hannabarbara.com", 167, "10019");
Manager m1 = new Manager(3, "Wily", "Coyote", "wily.coyote@warnerbros.com", 202, "10067");
m1.addSubordinate(e1);
m1.addSubordinate(e2);
e1.setManager(m1);
e2.setManager(m1);
Department d1 = new Department("Cartoon");
m1.addDepartment(d1);
d1.setManager(m1);
// This should persist all objects
pm.makePersistent(e1);
// Detach just the FetchPlan fields
pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS | FetchPlan.DETACH_UNLOAD_FIELDS);
detachedM1a = (Manager) pm.detachCopy(m1);
pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS);
detachedM1b = (Manager) pm.detachCopy(m1);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while persisting test data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check what has been detached - when detaching just fetch plan fields
try {
detachedM1a.getEmailAddress();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.emailAddress hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1a.getSerialNo();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.serialNo hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1a.getDepartments();
fail("Field Manager.departments has been detached yet this should not have been since wasn't in fetch-plan");
} catch (JDODetachedFieldAccessException dfae) {
// Expected
}
try {
detachedM1a.getSubordinates();
fail("Field Manager.subordinates has been detached yet this should not have been since wasn't in fetch-plan");
} catch (JDODetachedFieldAccessException dfae) {
// Expected
}
// Check what has been detached - when detaching all loaded fields
try {
detachedM1b.getEmailAddress();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.emailAddress hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1b.getSerialNo();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.serialNo hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1b.getDepartments();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.departments hasn't been detached yet this should have been since was loaded at detach");
}
try {
detachedM1b.getSubordinates();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.subordinates hasn't been detached yet this should have been since was loaded at detach");
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of javax.jdo.JDODetachedFieldAccessException 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);
}
}
use of javax.jdo.JDODetachedFieldAccessException in project tests by datanucleus.
the class AttachDetachTest method testBasicDetachAttach.
/**
* Basic test of detach-attach process.
*/
public void testBasicDetachAttach() {
try {
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
Employee woodyDetached = null;
Employee woody2;
Employee woodyAttached = null;
Object id = null;
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
// test detach and attach
tx.begin();
pm.makePersistent(woody);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
id = pm.getObjectId(woody);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = newPM();
tx = pm.currentTransaction();
try {
woodyDetached.setLastName("Woodpecker0");
tx.begin();
pm.makePersistent(woodyDetached);
tx.commit();
tx.begin();
woody2 = (Employee) pm.getObjectById(id, true);
assertEquals("expected change in attached instance", "Woodpecker0", woody2.getLastName());
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
pm = newPM();
tx = pm.currentTransaction();
try {
// test pc are the same after attach
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
pm = newPM();
tx = pm.currentTransaction();
try {
woodyDetached.setLastName("Woodpecker1");
tx.begin();
woody2 = (Employee) pm.getObjectById(id, true);
woodyAttached = (Employee) pm.makePersistent(woodyDetached);
tx.commit();
assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = newPM();
tx = pm.currentTransaction();
try {
// test pc are the same after attach, now in different order, first attach and later get object
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
pm = newPM();
tx = pm.currentTransaction();
try {
woodyDetached.setLastName("Woodpecker1");
tx.begin();
woodyAttached = (Employee) pm.makePersistent(woodyDetached);
woody2 = (Employee) pm.getObjectById(id, true);
tx.commit();
assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
pm = newPM();
pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS | FetchPlan.DETACH_UNLOAD_FIELDS);
tx = pm.currentTransaction();
try {
// test reading non copied field raises exception - DetachedClean
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
boolean success = false;
try {
woodyDetached.getYearsInCompany();
} catch (JDODetachedFieldAccessException detex) {
success = true;
}
assertTrue("Expected JDODetachedFieldAccessException on reading a non-detached field", success);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
pm = newPM();
pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS | FetchPlan.DETACH_UNLOAD_FIELDS);
tx = pm.currentTransaction();
try {
// test reading non copied field raises exception - DetachedDirty
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
pm = newPM();
tx = pm.currentTransaction();
try {
// makes it dirty
woodyDetached.setFirstName("00");
assertTrue("Detached instance should be dirty", JDOHelper.isDirty(woodyDetached));
boolean success = false;
try {
woodyDetached.getYearsInCompany();
} catch (JDODetachedFieldAccessException detex) {
success = true;
}
assertTrue("Expected JDODetachedFieldAccessException on reading a non-detached field", success);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of javax.jdo.JDODetachedFieldAccessException in project tests by datanucleus.
the class AttachDetachTest method testMaxFetchDepth.
/**
* Test the specification of maximum fetch depth.
*/
public void testMaxFetchDepth() {
try {
Object e1Id = null;
// Persist some data
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee e1 = new Employee(1, "Yogi", "Bear", "yogi@warnerbros.com", 124, "10123");
Employee e2 = new Employee(2, "Fred", "Flintstone", "fred.flintstone@hannabarbara.com", 167, "10019");
Manager m1 = new Manager(3, "Wily", "Coyote", "wily.coyote@warnerbros.com", 202, "10067");
Manager m2 = new Manager(4, "Mickey", "Mouse", "mickey.mouse@hollywood.com", 203, "10066");
Manager m3 = new Manager(5, "Donald", "Duck", "donald.duck@hollywood.com", 204, "10065");
m1.addSubordinate(e1);
m1.addSubordinate(e2);
e1.setManager(m1);
e2.setManager(m1);
m2.addSubordinate(m1);
m1.setManager(m2);
m3.addSubordinate(m2);
m2.setManager(m3);
Department d1 = new Department("Cartoon");
m1.addDepartment(d1);
d1.setManager(m1);
// This should persist all objects
pm.makePersistent(e1);
tx.commit();
e1Id = pm.getObjectId(e1);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while persisting test data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve and detach some objects using default fetch-depth
Employee e1Detached = null;
pm = newPM();
tx = pm.currentTransaction();
try {
tx.begin();
Employee e1 = (Employee) pm.getObjectById(e1Id);
e1Detached = (Employee) pm.detachCopy(e1);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while retrieving/detaching test data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the detached objects (Employee.manager is in the DFG)
try {
// Basic fields of the detached object
e1Detached.getSerialNo();
e1Detached.getManager();
} catch (JDODetachedFieldAccessException dfea) {
fail("Detach of Employee has not detached its DFG fields! Should have been detached");
}
try {
// Second level relation of the detached object
e1Detached.getManager().getManager();
fail("Detach of Employee has also detached Manager of the Manager! Should not have been detached since outside fetch-depth of 1");
} catch (JDODetachedFieldAccessException ndfe) {
// Expected
}
// Retrieve and detach some objects using extra level of fetch-depth
pm = newPM();
pm.getFetchPlan().setMaxFetchDepth(2);
tx = pm.currentTransaction();
try {
tx.begin();
Employee e1 = (Employee) pm.getObjectById(e1Id);
e1Detached = (Employee) pm.detachCopy(e1);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while retrieving/detaching test data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the detached objects
try {
// Basic fields of the detached object
e1Detached.getSerialNo();
e1Detached.getManager();
} catch (JDODetachedFieldAccessException dfea) {
fail("Detach of Employee has not detached its DFG fields! Should have been detached");
}
try {
// Basic fields of the detached object
e1Detached.getManager().getManager();
} catch (JDODetachedFieldAccessException dfea) {
fail("Detach of Employee has not detached Manager of Manager! Should have been detached");
}
try {
// Third level relation of the detached object
e1Detached.getManager().getManager().getManager();
fail("Detach of Employee has also detached Manager of the Manager of the Manager! Should not have been detached since outside fetch-depth of 1");
} catch (JDODetachedFieldAccessException ndfe) {
// Expected
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
Aggregations