use of org.datanucleus.cache.Level2Cache in project tests by datanucleus.
the class CacheTest method testL2CachedObject.
/**
* Test for storage of an object in the L2 cache, and whether it has its fields populated.
*/
public void testL2CachedObject() {
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 we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object woodyId = null;
Object woodlessId = null;
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);
woodyId = pm.getObjectId(woody);
woodlessId = pm.getObjectId(woodless);
// Woody, Woodless, and Bart will all be pinned since we have all Employee objects being pinned
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();
assertEquals("Incorrect number of pinned objects : should have been 3 but is " + l2Cache.getNumberOfPinnedObjects(), 3, l2Cache.getNumberOfPinnedObjects());
assertTrue("Level 2 Cache returned that it is empty yet should have pinned object(s)!", !l2Cache.isEmpty());
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = (Employee) pm.getObjectById(woodyId, false);
assertTrue("Object retrieved from L2 cache was null", woody != null);
assertTrue("Field of object retrieved from L2 cache was null!", woody.getLastName() != null);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error encountered accessing object from L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
l2Cache.evict(woodlessId);
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet should have 2", 2, l2Cache.getNumberOfPinnedObjects());
// Clear the cache and check if the objects are released
l2Cache.evictAll();
assertTrue("Level 2 Cache returned that it is not empty yet we just cleared it!", l2Cache.isEmpty());
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet we just cleared it!", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", 0, l2Cache.getNumberOfUnpinnedObjects());
} finally {
clearEmployeeData(cachePMF);
cachePMF.close();
}
}
Aggregations