use of org.jpox.samples.models.company.Office in project tests by datanucleus.
the class JDOQLBasicTest method testAnalysisRollup.
/**
* Tests the Analsys.rollup() expression
*/
public void testAnalysisRollup() {
RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
if (!srm.getDatastoreAdapter().supportsOption(DatastoreAdapter.ANALYSIS_METHODS)) {
// Not supported so it passed :-)
return;
}
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Persist some simple objects (uses datastore id, or composite application id depending on suite)
tx.begin();
Office o1 = new Office(1, "Green", "Big spacious office");
Calendar cal1 = Calendar.getInstance();
cal1.set(2004, 1, 1);
o1.setDate(cal1.getTime());
Office o2 = new Office(2, "Blue", "Pokey office at the back of the building");
Calendar cal2 = Calendar.getInstance();
cal2.set(2005, 1, 1);
o2.setDate(cal2.getTime());
Office o3 = new Office(1, "Yellow", "Massive open plan office");
Calendar cal3 = Calendar.getInstance();
cal3.set(2005, 1, 1);
o3.setDate(cal3.getTime());
pm.newQuery(Office.class).deletePersistentAll();
pm.makePersistent(o1);
pm.makePersistent(o2);
pm.makePersistent(o3);
tx.commit();
Object[] officeIds = new Object[3];
officeIds[0] = pm.getObjectId(o1);
officeIds[1] = pm.getObjectId(o2);
officeIds[2] = pm.getObjectId(o3);
// TODO This throws an exception about "JDOQL query has result clause PrimaryExpression{roomName} but this is invalid (see JDO spec 14.6.10). When specified with grouping should be aggregate, or grouping expression"
tx.begin();
Query q = pm.newQuery(Office.class);
q.setGrouping("Analysis.rollup({roomName})");
q.setOrdering("roomName ascending");
q.setResult("roomName,count(floor)");
Collection c = (Collection) q.execute();
assertEquals(4, c.size());
Iterator it = c.iterator();
Object[] obj = ((Object[]) it.next());
assertEquals("Blue", obj[0]);
assertEquals(1, ((Long) obj[1]).longValue());
obj = ((Object[]) it.next());
assertEquals(1, ((Long) obj[1]).longValue());
assertEquals("Green", obj[0]);
obj = ((Object[]) it.next());
assertEquals(1, ((Long) obj[1]).longValue());
assertEquals("Yellow", obj[0]);
assertEquals(1, ((Long) obj[1]).longValue());
obj = ((Object[]) it.next());
assertNull(obj[0]);
assertEquals(3, ((Long) obj[1]).longValue());
q = pm.newQuery(Office.class);
q.setGrouping("Analysis.rollup({date})");
q.setOrdering("date ascending");
q.setResult("date,count(floor)");
c = (Collection) q.execute();
assertEquals(3, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Office.class);
}
}
Aggregations