Search in sources :

Example 1 with SerialisedObject

use of org.datanucleus.samples.serialised.SerialisedObject in project tests by datanucleus.

the class SerializationTest method testSerialisedPCDetach.

/**
 * Test for attach/detach of serialised PC fields
 */
public void testSerialisedPCDetach() {
    try {
        Object holderId = null;
        SerialisedHolder detachedHolder = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist the object with serialised fields
        try {
            tx.begin();
            SerialisedHolder holder = new SerialisedHolder("Holder(1)", new SerialisedObject("My Description(1)"));
            pm.makePersistent(holder);
            // Update holder and serialised object fields to check that they get to the datastore
            holder.setName("Holder(2)");
            holder.getSerialisedPC().setDescription("My Description(2)");
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while persisted object with serialised PC field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object again to check the most recent update
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SerialisedHolder holder = (SerialisedHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(2)"));
            assertTrue("Retrieved holder has null serialised object!", holder.getSerialisedPC() != null);
            assertEquals("Retrieved serialised object description is incorrect : ", holder.getSerialisedPC().getDescription(), "My Description(2)");
            // Detach the holder
            detachedHolder = (SerialisedHolder) pm.detachCopy(holder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised PC field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update the detached object
        detachedHolder.setName("Holder(3)");
        detachedHolder.getSerialisedPC().setDescription("My Description(3)");
        // Attach the detached object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(detachedHolder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while attaching object with serialised PC field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object again to check the attach
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SerialisedHolder holder = (SerialisedHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(3)"));
            assertTrue("Retrieved holder has null serialised object!", holder.getSerialisedPC() != null);
            assertEquals("Retrieved serialised object description is incorrect : ", holder.getSerialisedPC().getDescription(), "My Description(3)");
            detachedHolder = (SerialisedHolder) pm.detachCopy(holder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised PC field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean up our data
        clean(SerialisedHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SerialisedObject(org.datanucleus.samples.serialised.SerialisedObject) SerialisedObject(org.datanucleus.samples.serialised.SerialisedObject) SerialisedHolder(org.datanucleus.samples.serialised.SerialisedHolder) IOException(java.io.IOException)

Example 2 with SerialisedObject

use of org.datanucleus.samples.serialised.SerialisedObject in project tests by datanucleus.

the class SerializationTest method testSerialisedPC.

/**
 * Test for serialisation of PC fields
 */
public void testSerialisedPC() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_PC)) {
        return;
    }
    try {
        Object holderId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist the object with serialised fields
        try {
            tx.begin();
            SerialisedHolder holder = new SerialisedHolder("Holder(1)", new SerialisedObject("My Description(1)"));
            pm.makePersistent(holder);
            // Update holder and serialised object fields to check that they get to the datastore
            holder.setName("Holder(2)");
            holder.getSerialisedPC().setDescription("My Description(2)");
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while persisted object with serialised PC field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        // Retrieve the object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SerialisedHolder holder = (SerialisedHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(2)"));
            assertTrue("Retrieved holder has null serialised object!", holder.getSerialisedPC() != null);
            assertEquals("Retrieved serialised object description is incorrect : ", holder.getSerialisedPC().getDescription(), "My Description(2)");
            // Update holder and serialised object fields to check that they get to the datastore
            holder.getSerialisedPC().setDescription("My Description(3)");
            holder.setName("Holder(3)");
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised PC field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object again to check the most recent update
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SerialisedHolder holder = (SerialisedHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised PC could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(3)"));
            assertTrue("Retrieved holder has null serialised object!", holder.getSerialisedPC() != null);
            assertEquals("Retrieved serialised object description is incorrect : ", holder.getSerialisedPC().getDescription(), "My Description(3)");
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised PC field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean up our data
        clean(SerialisedHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SerialisedObject(org.datanucleus.samples.serialised.SerialisedObject) SerialisedObject(org.datanucleus.samples.serialised.SerialisedObject) SerialisedHolder(org.datanucleus.samples.serialised.SerialisedHolder) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 SerialisedHolder (org.datanucleus.samples.serialised.SerialisedHolder)2 SerialisedObject (org.datanucleus.samples.serialised.SerialisedObject)2