/**
* Test CRUD and find ops
* @throws Exception
*/
final void daoCRUDAndFind() throws Exception {
IEntity e = getTestEntity();
Assert.assertTrue(e.isNew(), "The created test entity is not new and should be");
Long pk = null;
// create
e = dao.persist(e);
getDbTrans().setComplete();
getDbTrans().endTrans();
pk = e.getId();
Assert.assertNotNull(pk, "The created entities' primary key is null");
Assert.assertTrue(!e.isNew(), "The created entity is new and shouldn't be");
if(e instanceof ITimeStampEntity) {
// verify time stamp
Assert.assertNotNull(((ITimeStampEntity) e).getDateCreated(),
"Created time stamp entity does not have a create date");
Assert.assertNotNull(((ITimeStampEntity) e).getDateModified(),
"Created time stamp entity does not have a modify date");
}
// retrieve
getDbTrans().startTrans();
e = dao.load(e.entityClass(), e.getId());
Assert.assertNotNull(e, "The loaded entity is null");
entityHandler.verifyLoadedEntityState(e);
getDbTrans().setComplete(); // we need to do this for JDO in order to ensure a detached copy is made
getDbTrans().endTrans();
// update
getDbTrans().startTrans();
entityHandler.alterTestEntity(e);
e = dao.persist(e);
getDbTrans().setComplete();
getDbTrans().endTrans();
// find (update verify)
getDbTrans().startTrans();
e = getEntityFromDb(e.entityClass(), e.getId());
Assert.assertNotNull(e, "The retrieved entity for update check is null");
getDbTrans().setComplete();
getDbTrans().endTrans();
entityHandler.verifyEntityAlteration(e);
if(e instanceof ITimeStampEntity) {
// verify modify date
final ITimeStampEntity tse = (ITimeStampEntity) e;
Assert.assertTrue(tse.getDateModified() != null && tse.getDateCreated() != null
&& tse.getDateModified().getTime() >= tse.getDateCreated().getTime(),
"Updated time stamp entity does not an updated modify date");
}
// purge (delete)
getDbTrans().startTrans();
dao.purge(e);
getDbTrans().setComplete();
getDbTrans().endTrans();
// verify purge
getDbTrans().startTrans();
getDbTrans().setComplete();
try {
e = getEntityFromDb(e.entityClass(), e.getId());
Assert.assertNull(e, "The entity was not purged");
}
catch(final EntityNotFoundException ex) {
// expected
}