use of org.jpox.samples.embedded.DigitalCamera in project tests by datanucleus.
the class EmbeddedPCTest method testNestedEmbeddedPCObjects.
/**
* Test the use of nested embedded PC objects.
* @throws Exception
*/
public void testNestedEmbeddedPCObjects() throws Exception {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC)) {
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// ------------------ Check the persistence of an object with embedded object -----------------
Object cameraId = null;
try {
tx.begin();
Memory memory = new Memory(Memory.COMPACT_FLASH, 64, 3.3);
memory.setChip(new Chip(12));
DigitalCamera camera = new DigitalCamera("Canon", "Powerzoom A40", memory);
pm.makePersistent(camera);
// Access the object containing the embedded object before commit
// This used to try to go to the datastore at this point
camera.getMemory().toString();
tx.commit();
cameraId = pm.getObjectId(camera);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// -------------- Check the retrieval of objects with embedded subobject -----------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object with both embedded subobjects
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
assertTrue("Unable to retrieve object with embedded object(s)", camera != null);
assertTrue("Retrieved object with embedded object(s) has incorrect make field", camera.getMake().equals("Canon"));
assertTrue("Retrieved object with embedded object(s) has no memory", camera.getMemory() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", camera.getMemory().getType() == Memory.COMPACT_FLASH);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while fetching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ------------------- Check update of an embedded object ------------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object and change its memory
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
Memory memory = new Memory(Memory.COMPACT_FLASH, 256, 3.3);
memory.setChip(new Chip(15));
camera.setMemory(memory);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while updating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object that has just been updated
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
assertTrue("Unable to retrieve object with embedded object(s)", camera != null);
assertTrue("Updated object with embedded object(s) has incorrect model field", camera.getModel().equals("Powerzoom A40"));
assertTrue("Updated object with embedded object(s) has no memory", camera.getMemory() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : memory size is wrong", camera.getMemory().getSize() == 256);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while updating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ------------- Check for updates in the embedded object ------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object and update its battery details
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
Memory memory = camera.getMemory();
memory.setVoltage(5.0);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while updating embedded objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object that has just been updated
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
assertTrue("Unable to retrieve object with embedded object(s)", camera != null);
assertTrue("Updated object with embedded object(s) has incorrect model field", camera.getModel().equals("Powerzoom A40"));
assertTrue("Updated object with embedded object(s) has no memory", camera.getMemory() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : memory voltage is wrong", camera.getMemory().getVoltage() == 5.0);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while updating embedded objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ------------- Check for updates in the nested embedded object ------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object and update its battery details
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
Chip chip = camera.getMemory().getChip();
chip.setThickness(6);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while updating nested embedded objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object that has just been updated
DigitalCamera camera = (DigitalCamera) pm.getObjectById(cameraId);
assertTrue("Unable to retrieve object with embedded object(s)", camera != null);
assertTrue("Updated object with embedded object(s) has incorrect model field", camera.getModel().equals("Powerzoom A40"));
assertTrue("Updated object with embedded object(s) has no memory", camera.getMemory() != null);
assertTrue("Updated object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
assertTrue("Updated object with embedded object(s) has incorrect nested embedded object : chip thickness is wrong", camera.getMemory().getChip().getThickness() == 6);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while updating embedded objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// -------------- Check the retrieval of objects with nested embedded subobject with query -----------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery("SELECT FROM org.jpox.samples.embedded.DigitalCamera WHERE memory.chip.thickness == 6");
List results = (List) query.execute();
assertEquals("Number of cameras retrieved by query of nested embedded is incorrect", results.size(), 1);
DigitalCamera camera = (DigitalCamera) results.iterator().next();
assertTrue("Unable to retrieve object with nested embedded object(s)", camera != null);
assertTrue("Retrieved object with nested embedded object(s) has incorrect make field", camera.getMake().equals("Canon"));
assertTrue("Retrieved object with nested embedded object(s) has no memory", camera.getMemory() != null);
assertFalse("Camera memory should not be marked as dirty but is", JDOHelper.isDirty(camera.getMemory()));
assertTrue("Retrieved object with nested embedded object(s) has no memory chip", camera.getMemory().getChip() != null);
assertTrue("Retrieved object with nested embedded object(s) has incorrect memory chip thickness", camera.getMemory().getChip().getThickness() == 6);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying objects with nested embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(DigitalCamera.class);
}
}
Aggregations