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);
}
}
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);
}
}
Aggregations