use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.
the class ObjectCacheTest method testCaching.
/**
* Tests if the key-based retrieval of data brings up cached objects.
*/
public void testCaching() {
UOW uow = null;
final String prefix = "Z-CACHE-";
final int count = 5;
final int pad = ("" + count).length();
try {
uow = new UOW();
// Create CategoryOfInstrument records
createCategoryOfInstrument(uow, prefix, count);
// Read the CategoryOfInstrument records by non-key query
CategoryOfInstrument[] round1 = new CategoryOfInstrument[count];
Criteria c = new Criteria();
c.setTable(CategoryOfInstrumentMeta.getName());
c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
c.addOrderBy(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT);
Iterator itr = uow.query(c).iterator();
for (int i = 0; i < count; i++) {
String expected = prefix + StringHelper.pad(i + 1, pad);
assertTrue("CategoryOfInstrument '" + expected + "' not found", itr.hasNext());
CategoryOfInstrument obj = (CategoryOfInstrument) itr.next();
assertEquals(expected, obj.getCategoryInstrument());
round1[i] = obj;
}
assertTrue("No more CategoryOfInstrument records should have been retrieved", !itr.hasNext());
// Read the CategoryOfInstrument records again by non-key query
// the 2 sets of objects should not have the same identity
CategoryOfInstrument[] round2 = new CategoryOfInstrument[count];
c = new Criteria();
c.setTable(CategoryOfInstrumentMeta.getName());
c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
c.addOrderBy(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT);
itr = uow.query(c).iterator();
for (int i = 0; i < count; i++) {
String expected = prefix + StringHelper.pad(i + 1, pad);
assertTrue("CategoryOfInstrument '" + expected + "' not found", itr.hasNext());
CategoryOfInstrument obj = (CategoryOfInstrument) itr.next();
assertEquals(expected, obj.getCategoryInstrument());
round2[i] = obj;
}
assertTrue("No more CategoryOfInstrument records should have been retrieved", !itr.hasNext());
for (int i = 0; i < count; i++) {
assertTrue(round1[i].equals(round2[i]));
assertTrue(round1[i] != round2[i]);
}
// Now retrieve the next set of objects by their primary-key
// This set of objects should match the previous set exactly
CategoryOfInstrument[] round3 = new CategoryOfInstrument[count];
for (int i = 0; i < count; i++) {
String key = prefix + StringHelper.pad(i + 1, pad);
CategoryOfInstrument obj = CategoryOfInstrument.findByPK(uow, key);
round3[i] = obj;
}
for (int i = 0; i < count; i++) {
assertTrue(round2[i].equals(round3[i]));
assertTrue(round2[i] == round3[i]);
}
// Delete CategoryOfInstrument records
deleteCategoryOfInstrument(uow, prefix);
} catch (Exception e) {
e.printStackTrace();
fail();
} finally {
try {
if (uow != null)
uow.rollback();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}
use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.
the class ObjectCacheTest method deleteCategoryOfInstrument.
private void deleteCategoryOfInstrument(UOW uow, String prefix) throws Exception {
Criteria c = new Criteria();
c.setTable(CategoryOfInstrumentMeta.getName());
c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
for (Iterator i = uow.query(c).iterator(); i.hasNext(); ) uow.delete(i.next());
}
use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.
the class PagingTest method deleteCategoryOfInstrument.
private void deleteCategoryOfInstrument(UOW uow, String prefix) throws Exception {
Criteria c = new Criteria();
c.setTable(CategoryOfInstrumentMeta.getName());
c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
for (Iterator i = uow.query(c).iterator(); i.hasNext(); ) uow.delete(i.next());
}
use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.
the class PagingTest method testPaging.
/**
* Creates 95 CategoryOfInstrument records and then reads them in batces of 25.
*/
public void testPaging() {
// org.apache.log4j.BasicConfigurator.configure();
UOW uow = null;
final String prefix = "Z-PAGING-";
final int count = 95;
final int batch = 25;
final int pad = ("" + count).length();
try {
uow = new UOW();
// Create CategoryOfInstrument records
createCategoryOfInstrument(uow, prefix, count);
// Read CategoryOfInstrument records in batches
int loops = count / batch;
if (count % batch > 0)
++loops;
for (int i = 0; i < loops; i++) {
Criteria c = new Criteria();
c.setTable(CategoryOfInstrumentMeta.getName());
c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
c.addOrderBy(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT);
c.setFirstResult(i * batch);
c.setMaxResults(batch);
Iterator itr = uow.query(c).iterator();
int batchSize = (i * batch) + batch <= count ? batch : count - (i * batch);
for (int j = 0; j < batchSize; j++) {
String expected = prefix + StringHelper.pad((i * batch) + j + 1, pad);
assertTrue("CategoryOfInstrument '" + expected + "' not found", itr.hasNext());
CategoryOfInstrument obj = (CategoryOfInstrument) itr.next();
assertEquals(expected, obj.getCategoryInstrument());
}
assertTrue("No more CategoryOfInstrument records should have been retrieved", !itr.hasNext());
}
// Delete CategoryOfInstrument records
deleteCategoryOfInstrument(uow, prefix);
} catch (Exception e) {
e.printStackTrace();
fail();
} finally {
try {
if (uow != null)
uow.rollback();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}
use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.
the class PerformanceTest method deleteItems.
/**
* Deletes Item records having item_id like 'itemIdBeginsWith%'.
* This will commit the UOW and then acquire a new UOW.
*/
private void deleteItems(String itemIdBeginsWith) throws Exception {
Criteria c = new Criteria();
c.setTable(ItemMeta.getName());
c.addCriteria(ItemMeta.ITEM_ID, Criteria.RELATIONAL_BEGINS_WITH, itemIdBeginsWith);
for (Iterator itr = m_uow.query(c).iterator(); itr.hasNext(); ) {
Item item = (Item) itr.next();
m_uow.delete(item);
}
// commit the exiting UOW and create a new one
m_uow.commit();
m_uow = new UOW();
}
Aggregations