public void testCacheQueryResults() {
ds.put(null, newFlightEntity("1", "yar", "bam", 1, 2));
ds.put(null, newFlightEntity("1", "yam", null, 1, 2));
QueryResultsCache cache = null;
try {
String query = "SELECT FROM " + Flight.class.getName();
Query q = pm.newQuery(query);
q.addExtension("datanucleus.query.results.cached", "true");
try {
List<Flight> results = (List<Flight>) q.execute();
Assert.assertEquals("Number of results was wrong", 2, results.size());
} catch (JDOException jdoe) {
fail("Threw exception when evaluating query and caching results : " + jdoe.getMessage());
}
q.closeAll();
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
cache =
((JDOPersistenceManagerFactory)pmf).getNucleusContext().getStoreManager().getQueryManager().getQueryResultsCache();
assertEquals("Number of entries in the query results cache is wrong", 1, cache.size());
pm = pmf.getPersistenceManager();
Query q2 = pm.newQuery(query);
try {
List<Flight> results = (List<Flight>) q2.execute();
Assert.assertEquals("Number of results was wrong", 2, results.size());
} catch (JDOException jdoe) {
fail("Threw exception when evaluating query with cached results : " + jdoe.getMessage());
}
q2.closeAll();
} finally {
// Evict the cached results
cache.evictAll();
}
}