use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class ReachabilityTest method testOneToOneUniClassNewDeleted.
/**
* Tests if reachability on commit does not try to persist an object that was reachable then deleted.
* Uses 1-1 unidir relation.
* Requires a JPOX extension so is outside the JDO2 spec.
*/
public void testOneToOneUniClassNewDeleted() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_FLUSH_MODE, "manual");
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Organisation org = new Organisation("JPOX Corporation");
assertTrue("Object state of Organisation is incorrect", !JDOHelper.isPersistent(org) && !JDOHelper.isNew(org) && !JDOHelper.isDirty(org));
Qualification qual = new Qualification("ISO 4002 Certificate 17001");
// persist Qualification
pm.makePersistent(qual);
// Relate the objects so persisting Organisation by reachability
qual.setOrganisation(org);
pm.flush();
// Remove the reference to the organisation and delete the organisation
qual.setOrganisation(null);
// Adding pm.flush(); here will make it work
pm.deletePersistent(org);
// Clear references and wait so that the references are GCed
org = null;
qual = null;
Thread.sleep(2000);
// now commit, should not fail but can if it gets the updates in the wrong order (since is UNIDIRECTIONAL relation)
tx.commit();
} catch (Exception e) {
// This can happen if the commit processes the delete of the object before the update of the owner
e.printStackTrace();
fail("Exception thrown when commiting deleted object which should not have affected commit");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clear out our data
clean(Qualification.class);
clean(Organisation.class);
}
}
use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testInsertSerialised.
public void testInsertSerialised() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
Qualification q = new Qualification("MSc");
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.YEAR, 2010);
cal.set(Calendar.MONTH, 5);
cal.set(Calendar.DAY_OF_MONTH, 10);
q.setDate(cal.getTime());
pm.makePersistent(q);
tx.commit();
id = pm.getObjectId(q);
} catch (Exception e) {
LOG.error("Exception during persist", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
// Retrieve and check it
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Qualification q = (Qualification) pm.getObjectById(id);
Date d = q.getDate();
assertNotNull("Date is null!", d);
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(d);
assertEquals("Year is wrong", 2010, cal.get(Calendar.YEAR));
assertEquals("Month is wrong", 5, cal.get(Calendar.MONTH));
assertEquals("Day is wrong", 10, cal.get(Calendar.DAY_OF_MONTH));
tx.commit();
} catch (Exception e) {
LOG.error("Exception during retrieve", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Qualification.class);
}
}
use of org.jpox.samples.models.company.Qualification in project tests by datanucleus.
the class CacheTest method testSCOAndPCReuse.
/**
* Demonstrate refreshing SCO and PC field values for a PC object returned from the L2 cache.
*/
public void testSCOAndPCReuse() {
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);
l2Cache.pinAll(true, Organisation.class);
l2Cache.pinAll(true, Person.class);
tx.begin();
Person tweety = new Person(1, "Tweety", "Pie", "tweety.pie@warnerbros.com");
pm.makePersistent(tweety);
Organisation org = new Organisation("The Training Company");
pm.makePersistent(org);
Qualification qual = new Qualification("Certified JPOX Developer");
qual.setPerson(tweety);
qual.setOrganisation(org);
qual.setDate(new GregorianCalendar(2005, 8, 02).getTime());
pm.makePersistent(qual);
qualId = pm.getObjectId(qual);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run SCO/PC field reuse test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
/*
* OK, now we have a Person, an Organisation and a Qualification object in 2L cache.
* Previously getting the Qualification object from 2L cache cleared SCO
* and PC fields, so accessing the room/guest or any of the date
* fields resulted in SQL query, even though they are in the cache.
* Let's see if that changed. The following should cause no SQL
* execution at all.
*/
PersistenceManager pm2 = cachePMF.getPersistenceManager();
tx = pm2.currentTransaction();
try {
tx.begin();
Qualification qual = (Qualification) pm2.getObjectById(qualId);
// these are PC fields, their primary keys are kept even in cache
assertEquals("Person's first name is not what expected", qual.getPerson().getFirstName(), "Tweety");
assertEquals("Person's last name is not what expected", qual.getPerson().getLastName(), "Pie");
// this is a SCO field, value is kept even in cache
assertEquals("From date is not what expected", qual.getDate(), new GregorianCalendar(2005, 8, 02).getTime());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error using objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm2.close();
}
/*
* Now we unpin all objects and clean out the cache. Then we make
* sure, that in spite of the changes, it is still reloading fields,
* when they are not available
*/
PersistenceManager pm3 = cachePMF.getPersistenceManager();
tx = pm3.currentTransaction();
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.unpinAll(true, Qualification.class);
l2Cache.unpinAll(true, Person.class);
l2Cache.unpinAll(true, Organisation.class);
l2Cache.evictAll();
tx.begin();
Qualification qual = (Qualification) pm3.getObjectById(qualId);
assertEquals("Person's first name is not what expected", qual.getPerson().getFirstName(), "Tweety");
assertEquals("Person's last name is not what expected", qual.getPerson().getLastName(), "Pie");
assertEquals("From date is not what expected", qual.getDate(), new GregorianCalendar(2005, 8, 02).getTime());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error using objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm3.close();
}
} finally {
clean(cachePMF, Qualification.class);
clean(cachePMF, Organisation.class);
clean(cachePMF, Person.class);
cachePMF.close();
}
}
Aggregations