use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class ReachabilityTest method performOneToOneUniClass.
/**
* Test for reachability using a 1-1 unidirectional relation between 2 classes.
* Tests that when persisting the owner object the related object is also persisted.
* @param optimisticTxn Whether to use optimistic txns
*/
protected void performOneToOneUniClass(boolean optimisticTxn) {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(optimisticTxn);
Object qual1Id = null;
Object org1Id = null;
Object qual2Id = null;
Object org2Id = null;
try {
// A). persist two objects without relation
tx.begin();
Qualification qual1 = new Qualification("ISO 2003 certificate number 34512");
Organisation org1 = new Organisation("JPOX Consulting");
pm.makePersistent(org1);
pm.makePersistent(qual1);
tx.commit();
qual1Id = JDOHelper.getObjectId(qual1);
org1Id = JDOHelper.getObjectId(org1);
tx.begin();
// B). Relate the previous objects
qual1.setOrganisation(org1);
// c). Create and relate two new objects
Qualification qual2 = new Qualification("ISO 2001 certificate number 123045");
Organisation org2 = new Organisation("JPOX Corporation");
qual2.setOrganisation(org2);
// Check that both are transient
assertTrue("Object state of new Qualification is incorrect", !JDOHelper.isPersistent(qual2) && !JDOHelper.isNew(qual2) && !JDOHelper.isDirty(qual2));
assertTrue("Object state of new Organisation is incorrect", !JDOHelper.isPersistent(org2) && !JDOHelper.isNew(org2) && !JDOHelper.isDirty(org2));
// Persist the Qualification (so the Organisation should be persisted too)
pm.makePersistent(qual2);
// Check that both are persistent-new (JDO2 spec 12.6.7)
assertTrue("Object state of newly persisted Qualification is incorrect", JDOHelper.isPersistent(qual2) && JDOHelper.isNew(qual2) && JDOHelper.isDirty(qual2));
assertTrue("Object state of newly persisted (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org2) && JDOHelper.isNew(org2) && JDOHelper.isDirty(org2));
tx.commit();
// Check that both are clean/hollow
assertTrue("Object state of committed Qualification is incorrect", JDOHelper.isPersistent(qual2) && !JDOHelper.isNew(qual2) && !JDOHelper.isDirty(qual2));
assertTrue("Object state of committed (by reachability) Organisation is incorrect", JDOHelper.isPersistent(org2) && !JDOHelper.isNew(org2) && !JDOHelper.isDirty(org2));
qual2Id = pm.getObjectId(qual2);
org2Id = pm.getObjectId(org2);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check that the objects exist in the datastore
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Organisation org1 = (Organisation) pm.getObjectById(org1Id);
assertTrue("Organisation 1 is not in the datastore!", org1 != null);
Qualification qual1 = (Qualification) pm.getObjectById(qual1Id);
assertTrue("Qualification 1 is not in the datastore!", qual1 != null);
Organisation org2 = (Organisation) pm.getObjectById(org2Id);
assertTrue("Organisation 2 is not in the datastore!", org2 != null);
Qualification qual2 = (Qualification) pm.getObjectById(qual2Id);
assertTrue("Qualification 2 is not in the datastore!", qual2 != null);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Qualification.class);
clean(Organisation.class);
}
}
use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class CacheTest method clearEmployeeData.
protected void clearEmployeeData(PersistenceManagerFactory pmf) {
Extent ext = null;
java.util.Iterator it = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// disassociate all Employees and Departments from their Managers
tx.begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
mgr.clearSubordinates();
mgr.clearDepartments();
}
tx.commit();
// delete all Employee objects
tx.begin();
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
pm.deletePersistent(emp);
}
tx.commit();
// delete all Qualification objects
tx.begin();
ext = pm.getExtent(Qualification.class, false);
it = ext.iterator();
while (it.hasNext()) {
Qualification q = (Qualification) it.next();
pm.deletePersistent(q);
}
tx.commit();
// delete all Department objects
tx.begin();
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
while (it.hasNext()) {
Department d = (Department) it.next();
pm.deletePersistent(d);
}
tx.commit();
// delete all Manager objects
tx.begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
pm.deletePersistent(mgr);
}
tx.commit();
// delete all Person objects
tx.begin();
ext = pm.getExtent(Person.class, true);
it = ext.iterator();
while (it.hasNext()) {
Person person = (Person) it.next();
pm.deletePersistent(person);
}
tx.commit();
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class CacheTest method testEvictAll.
/**
* Test DataStoreCache.evictAll(Class, boolean)
*/
public void testEvictAll() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "soft");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
try {
// Create some data we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
// All Employees/Managers get pinned
l2Cache.pinAll(true, Employee.class);
tx.begin();
final Employee woody = new Employee(1, "Woody", null, "woody@woodpecker.com", 13, "serial 1", new Integer(10));
final Employee woodless = new Employee(2, "Woodless", "Woodpecker", "woodless@woodpecker.com", 14, "serial 2", new Integer(11));
Manager bart = new Manager(3, "Bart", "Simpson", "bart@simpson.com", 3, "serial 3");
woody.setManager(bart);
pm.makePersistent(woody);
woody.setLastName("Woodpecker");
pm.makePersistent(woodless);
// Woody, Woodless, and Bart will all be pinned since we have all Employee objects being pinned
// create a few unpinned objects so DefaultLevel2Cache.evictAll() will have something to iterate
// and remove a few times
Qualification quali = new Qualification("patience");
pm.makePersistent(quali);
quali = new Qualification("endurance");
pm.makePersistent(quali);
quali = new Qualification("humour");
pm.makePersistent(quali);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run multithread test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
// cannot assert reliably existence of unpinned objects as they can get GC'ed any time
// just check that the following executes without errors and that there are no unpinned objects
// afterwards
l2Cache.evictAll(Qualification.class, true);
assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", l2Cache.getNumberOfUnpinnedObjects() == 0);
// check whether it was only the Qualification objects that got evicted
assertTrue("Incorrect number of pinned objects : should have been 3 but is " + l2Cache.getNumberOfPinnedObjects(), l2Cache.getNumberOfPinnedObjects() == 3);
assertTrue("Level 2 Cache returned that it is empty yet should have pinned object(s)!", !l2Cache.isEmpty());
// evict all Employee + subclass objects and check if the objects are released
l2Cache.evictAll(Employee.class, true);
assertTrue("Level 2 Cache returned that it is not empty yet we just cleared it!", l2Cache.isEmpty());
assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet we just cleared it!", l2Cache.getNumberOfPinnedObjects() == 0);
assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", l2Cache.getNumberOfUnpinnedObjects() == 0);
} finally {
clearEmployeeData(cachePMF);
cachePMF.close();
}
}
use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class CacheTest method testClassNotCacheable.
/**
* Test for whether a class that is marked as not cacheable is L2 cached.
*/
public void testClassNotCacheable() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
try {
// Create some data
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object qualId = null;
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.pinAll(true, Qualification.class);
tx.begin();
Qualification qual = new Qualification("Certified JPOX Developer");
pm.makePersistent(qual);
qualId = pm.getObjectId(qual);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting data for test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
JDODataStoreCache jdoCache = (JDODataStoreCache) cachePMF.getDataStoreCache();
Level2Cache l2Cache = jdoCache.getLevel2Cache();
assertNull("Qualification object should not have been L2 cached but was!", l2Cache.get(qualId));
// Try to retrieve this object - need a way of detecting if it tried the L2 cache
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
LOG.info(">> getObjectById qualId=" + qualId);
pm.getObjectById(qualId);
LOG.info(">> getObjectById qualId done");
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting data for test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(cachePMF, Qualification.class);
cachePMF.close();
}
}
use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class PersistenceTest method testPersistDate.
/**
* Test the persistence of class with a Date field.
*/
public void testPersistDate() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
Qualification q = new Qualification("Cycling Proficiency");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 15);
cal.set(Calendar.MONTH, 5);
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.HOUR_OF_DAY, 7);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, 0);
q.setDate(cal.getTime());
pm.makePersistent(q);
tx.commit();
id = JDOHelper.getObjectId(q);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Qualification q = (Qualification) pm.getObjectById(id);
assertEquals("Cycling Proficiency", q.getName());
Date d = q.getDate();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
assertEquals("Year is wrong", 2006, cal.get(Calendar.YEAR));
assertEquals("Month is wrong", 5, cal.get(Calendar.MONTH));
assertEquals("Day of month is wrong", 15, cal.get(Calendar.DAY_OF_MONTH));
assertEquals("Hour is wrong", 7, cal.get(Calendar.HOUR_OF_DAY));
assertEquals("Minute is wrong", 30, cal.get(Calendar.MINUTE));
assertEquals("Second is wrong", 0, cal.get(Calendar.SECOND));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Qualification.class);
}
}
Aggregations