Search in sources :

Example 6 with Chip

use of org.jpox.samples.embedded.Chip 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);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Memory(org.jpox.samples.embedded.Memory) EmbeddedObject(org.jpox.samples.embedded.EmbeddedObject) List(java.util.List) Chip(org.jpox.samples.embedded.Chip) DigitalCamera(org.jpox.samples.embedded.DigitalCamera)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)6 Transaction (javax.jdo.Transaction)6 Chip (org.jpox.samples.embedded.Chip)6 DigitalCamera (org.jpox.samples.embedded.DigitalCamera)6 Memory (org.jpox.samples.embedded.Memory)6 List (java.util.List)3 Query (javax.jdo.Query)3 DataStoreCache (javax.jdo.datastore.DataStoreCache)1 EmbeddedObject (org.jpox.samples.embedded.EmbeddedObject)1