Search in sources :

Example 1 with DigitalCamera

use of org.jpox.samples.embedded.DigitalCamera in project tests by datanucleus.

the class EmbeddedTest method testNestedEmbeddedPCObjectDetachAttach.

/**
 * Test for detaching/attaching an nested embedded PC object.
 * @throws Exception
 */
public void testNestedEmbeddedPCObjectDetachAttach() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist some objects
        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);
            camera.setId(245);
            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) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object(s) and detach them
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        DigitalCamera detachedCamera = null;
        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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 12, camera.getMemory().getChip().getThickness());
            detachedCamera = (DigitalCamera) pm.detachCopy(camera);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while retrieving/detaching objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check the detached object(s) and check them
        assertNotNull("Detached Computer is null!", detachedCamera);
        assertTrue("Unable to retrieve object with embedded object(s)", detachedCamera != null);
        assertTrue("Retrieved object with embedded object(s) has incorrect make field", detachedCamera.getMake().equals("Canon"));
        assertTrue("Retrieved object with embedded object(s) has no memory", detachedCamera.getMemory() != null);
        assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", detachedCamera.getMemory().getType() == Memory.COMPACT_FLASH);
        assertTrue("Retrieved object with embedded object(s) has no chip", detachedCamera.getMemory().getChip() != null);
        assertEquals("Retrieved chip has wrong thickness", 12, detachedCamera.getMemory().getChip().getThickness());
        // Update some objects
        detachedCamera.getMemory().getChip().setThickness(15);
        // Attach the objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        try {
            tx.begin();
            DigitalCamera camera = (DigitalCamera) pm.makePersistent(detachedCamera);
            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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while attaching objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object(s) and re-check updated objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        try {
            tx.begin();
            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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while retrieving/detaching objects with 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) PersistenceManager(javax.jdo.PersistenceManager) Memory(org.jpox.samples.embedded.Memory) Chip(org.jpox.samples.embedded.Chip) DigitalCamera(org.jpox.samples.embedded.DigitalCamera)

Example 2 with DigitalCamera

use of org.jpox.samples.embedded.DigitalCamera in project tests by datanucleus.

the class EmbeddedTest method testNestedEmbeddedPCObjects.

/**
 * Test the use of nested embedded PC objects.
 * @throws Exception
 */
public void testNestedEmbeddedPCObjects() throws Exception {
    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);
            camera.setId(245);
            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) {
            e.printStackTrace();
            LOG.error(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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Retrieved object with embedded object(s) has incorrect make field", "Canon", camera.getMake());
            assertNotNull("Retrieved object with embedded object(s) has no memory", camera.getMemory());
            assertEquals("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", Memory.COMPACT_FLASH, camera.getMemory().getType());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(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) {
            e.printStackTrace();
            LOG.error(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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Updated object with embedded object(s) has incorrect model field", "Powerzoom A40", camera.getModel());
            assertNotNull("Updated object with embedded object(s) has no memory", camera.getMemory());
            assertEquals("Updated object with embedded object(s) has incorrect embedded object : memory size is wrong", 256, camera.getMemory().getSize());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(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) {
            e.printStackTrace();
            LOG.error(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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Updated object with embedded object(s) has incorrect model field", "Powerzoom A40", camera.getModel());
            assertNotNull("Updated object with embedded object(s) has no memory", camera.getMemory());
            assertEquals("Updated object with embedded object(s) has incorrect embedded object : memory voltage is wrong", 5.0, camera.getMemory().getVoltage());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(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) {
            e.printStackTrace();
            LOG.error(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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Updated object with embedded object(s) has incorrect model field", "Powerzoom A40", camera.getModel());
            assertNotNull("Updated object with embedded object(s) has no memory", camera.getMemory());
            assertNotNull("Updated object with embedded object(s) has no chip", camera.getMemory().getChip());
            assertEquals("Updated object with embedded object(s) has incorrect nested embedded object : chip thickness is wrong", 6, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(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();
            assertNotNull("Unable to retrieve object with nested embedded object(s)", camera != null);
            assertEquals("Retrieved object with nested embedded object(s) has incorrect make field", "Canon", camera.getMake());
            assertNotNull("Retrieved object with nested embedded object(s) has no memory", camera.getMemory());
            assertNotNull("Retrieved object with nested embedded object(s) has no memory chip", camera.getMemory().getChip());
            assertEquals("Retrieved object with nested embedded object(s) has incorrect memory chip thickness", 6, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(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) List(java.util.List) Chip(org.jpox.samples.embedded.Chip) DigitalCamera(org.jpox.samples.embedded.DigitalCamera)

Example 3 with DigitalCamera

use of org.jpox.samples.embedded.DigitalCamera in project tests by datanucleus.

the class EmbeddedMixedTest method testNestedEmbeddedPCObjectDetachAttach.

/**
 * Test for detaching/attaching an nested embedded PC object.
 * @throws Exception
 */
public void testNestedEmbeddedPCObjectDetachAttach() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist some objects
        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);
            tx.commit();
            cameraId = pm.getObjectId(camera);
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object(s) and detach them
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        DigitalCamera detachedCamera = null;
        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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 12, camera.getMemory().getChip().getThickness());
            detachedCamera = (DigitalCamera) pm.detachCopy(camera);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while retrieving/detaching objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check the detached object(s) and check them
        assertNotNull("Detached Computer is null!", detachedCamera);
        assertTrue("Unable to retrieve object with embedded object(s)", detachedCamera != null);
        assertTrue("Retrieved object with embedded object(s) has incorrect make field", detachedCamera.getMake().equals("Canon"));
        assertTrue("Retrieved object with embedded object(s) has no memory", detachedCamera.getMemory() != null);
        assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", detachedCamera.getMemory().getType() == Memory.COMPACT_FLASH);
        assertTrue("Retrieved object with embedded object(s) has no chip", detachedCamera.getMemory().getChip() != null);
        assertEquals("Retrieved chip has wrong thickness", 12, detachedCamera.getMemory().getChip().getThickness());
        // Update some objects
        detachedCamera.getMemory().getChip().setThickness(15);
        // Attach the objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        try {
            tx.begin();
            DigitalCamera camera = (DigitalCamera) pm.makePersistent(detachedCamera);
            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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while attaching objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object(s) and re-check updated objects
        DataStoreCache cache = pmf.getDataStoreCache();
        cache.evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        try {
            tx.begin();
            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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e);
            fail("Exception thrown while retrieving/detaching objects with 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) PersistenceManager(javax.jdo.PersistenceManager) Memory(org.jpox.samples.embedded.Memory) Chip(org.jpox.samples.embedded.Chip) DigitalCamera(org.jpox.samples.embedded.DigitalCamera) DataStoreCache(javax.jdo.datastore.DataStoreCache)

Example 4 with DigitalCamera

use of org.jpox.samples.embedded.DigitalCamera in project tests by datanucleus.

the class EmbeddedTest method testNestedEmbeddedPCObjects.

/**
 * Test the use of nested embedded PC objects.
 * @throws Exception
 */
public void testNestedEmbeddedPCObjects() throws Exception {
    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);
            camera.setId(245);
            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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Retrieved object with embedded object(s) has incorrect make field", "Canon", camera.getMake());
            assertNotNull("Retrieved object with embedded object(s) has no memory", camera.getMemory());
            assertEquals("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", Memory.COMPACT_FLASH, camera.getMemory().getType());
            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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Updated object with embedded object(s) has incorrect model field", "Powerzoom A40", camera.getModel());
            assertNotNull("Updated object with embedded object(s) has no memory", camera.getMemory());
            assertEquals("Updated object with embedded object(s) has incorrect embedded object : memory size is wrong", 256, camera.getMemory().getSize());
            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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Updated object with embedded object(s) has incorrect model field", "Powerzoom A40", camera.getModel());
            assertNotNull("Updated object with embedded object(s) has no memory", camera.getMemory());
            assertEquals("Updated object with embedded object(s) has incorrect embedded object : memory voltage is wrong", 5.0, camera.getMemory().getVoltage());
            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);
            assertNotNull("Unable to retrieve object with embedded object(s)", camera);
            assertEquals("Updated object with embedded object(s) has incorrect model field", "Powerzoom A40", camera.getModel());
            assertNotNull("Updated object with embedded object(s) has no memory", camera.getMemory());
            assertNotNull("Updated object with embedded object(s) has no chip", camera.getMemory().getChip());
            assertEquals("Updated object with embedded object(s) has incorrect nested embedded object : chip thickness is wrong", 6, camera.getMemory().getChip().getThickness());
            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();
            LOG.info(">> query of nested object value");
            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();
            assertNotNull("Unable to retrieve object with nested embedded object(s)", camera != null);
            assertEquals("Retrieved object with nested embedded object(s) has incorrect make field", "Canon", camera.getMake());
            assertNotNull("Retrieved object with nested embedded object(s) has no memory", camera.getMemory());
            assertNotNull("Retrieved object with nested embedded object(s) has no memory chip", camera.getMemory().getChip());
            assertEquals("Retrieved object with nested embedded object(s) has incorrect memory chip thickness", 6, camera.getMemory().getChip().getThickness());
            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) List(java.util.List) Chip(org.jpox.samples.embedded.Chip) DigitalCamera(org.jpox.samples.embedded.DigitalCamera)

Example 5 with DigitalCamera

use of org.jpox.samples.embedded.DigitalCamera in project tests by datanucleus.

the class EmbeddedTest method testNestedEmbeddedPCObjectDetachAttach.

/**
 * Test for detaching/attaching an nested embedded PC object.
 * @throws Exception
 */
public void testNestedEmbeddedPCObjectDetachAttach() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist some objects
        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);
            camera.setId(245);
            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();
        }
        // Retrieve the object(s) and detach them
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        DigitalCamera detachedCamera = null;
        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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 12, camera.getMemory().getChip().getThickness());
            detachedCamera = (DigitalCamera) pm.detachCopy(camera);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving/detaching objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check the detached object(s) and check them
        assertNotNull("Detached Computer is null!", detachedCamera);
        assertTrue("Unable to retrieve object with embedded object(s)", detachedCamera != null);
        assertTrue("Retrieved object with embedded object(s) has incorrect make field", detachedCamera.getMake().equals("Canon"));
        assertTrue("Retrieved object with embedded object(s) has no memory", detachedCamera.getMemory() != null);
        assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : memory type is wrong", detachedCamera.getMemory().getType() == Memory.COMPACT_FLASH);
        assertTrue("Retrieved object with embedded object(s) has no chip", detachedCamera.getMemory().getChip() != null);
        assertEquals("Retrieved chip has wrong thickness", 12, detachedCamera.getMemory().getChip().getThickness());
        // Update some objects
        detachedCamera.getMemory().getChip().setThickness(15);
        // Attach the objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        try {
            tx.begin();
            DigitalCamera camera = (DigitalCamera) pm.makePersistent(detachedCamera);
            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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while attaching objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object(s) and re-check updated objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(2);
        try {
            tx.begin();
            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);
            assertTrue("Retrieved object with embedded object(s) has no chip", camera.getMemory().getChip() != null);
            assertEquals("Retrieved chip has wrong thickness", 15, camera.getMemory().getChip().getThickness());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving/detaching objects with 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) PersistenceManager(javax.jdo.PersistenceManager) Memory(org.jpox.samples.embedded.Memory) 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