use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.
the class LobTest method testAddClobAndBlob.
/**
* Creates ZZ_TEST_PART_REMARKS_PICTURE records having clob and blob elements. It then checks if the data has been added.
*/
public void testAddClobAndBlob() {
try {
// create 3 records
PartRemarksPicture partRemarksPicture = null;
partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
partRemarksPicture.updatePart("Z-TESTPART-01");
partRemarksPicture.updateRemarks(CLOB_FIELD + "01");
partRemarksPicture.updatePicture((BLOB_FIELD + "01").getBytes());
m_uow.add(partRemarksPicture);
partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
partRemarksPicture.updatePart("Z-TESTPART-02");
partRemarksPicture.updateRemarks(CLOB_FIELD + "02");
partRemarksPicture.updatePicture((BLOB_FIELD + "02").getBytes());
m_uow.add(partRemarksPicture);
partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
partRemarksPicture.updatePart("Z-TESTPART-03");
partRemarksPicture.updateRemarks(CLOB_FIELD + "03");
partRemarksPicture.updatePicture((BLOB_FIELD + "03").getBytes());
m_uow.add(partRemarksPicture);
m_uow.commit();
// now check if they have been added
m_uow = new UOW();
Criteria c = new Criteria();
c.setTable(PartRemarksPictureMeta.getName());
c.addCriteria(PartRemarksPictureMeta.PART, Criteria.RELATIONAL_BEGINS_WITH, "Z-");
c.addOrderBy(PartRemarksPictureMeta.PART, Criteria.ORDER_BY_ASC);
Collection col = m_uow.query(c);
// fetch in all the records
for (Iterator i = col.iterator(); i.hasNext(); ) i.next();
assertEquals(3, col.size());
PartRemarksPicture[] partRemarksPictures = (PartRemarksPicture[]) col.toArray(new PartRemarksPicture[0]);
assertEquals("Z-TESTPART-01", partRemarksPictures[0].getPart());
assertEquals(CLOB_FIELD + "01", partRemarksPictures[0].getRemarks());
assertTrue(Arrays.equals((BLOB_FIELD + "01").getBytes(), partRemarksPictures[0].getPicture()));
assertEquals("Z-TESTPART-02", partRemarksPictures[1].getPart());
assertEquals(CLOB_FIELD + "02", partRemarksPictures[1].getRemarks());
assertTrue(Arrays.equals((BLOB_FIELD + "02").getBytes(), partRemarksPictures[1].getPicture()));
assertEquals("Z-TESTPART-03", partRemarksPictures[2].getPart());
assertEquals(CLOB_FIELD + "03", partRemarksPictures[2].getRemarks());
assertTrue(Arrays.equals((BLOB_FIELD + "03").getBytes(), partRemarksPictures[2].getPicture()));
// now delete the records
m_uow.delete(partRemarksPictures[0]);
m_uow.delete(partRemarksPictures[1]);
m_uow.delete(partRemarksPictures[2]);
m_uow.commit();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.
the class ObjectCacheTest method testDisableCaching.
/**
* Tests if caching can be disabled.
*/
public void testDisableCaching() {
UOW uow = null;
final String prefix = "Z-CACHE-";
final int count = 5;
final int pad = ("" + count).length();
try {
// Disable object caching
ContextManagerFactory.instance().setThreadContext(null);
ContextManagerFactory.instance().setProperty("jaffa.persistence.jdbcengine.disableObjectCache", "true");
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
// the 2 sets of objects should again not have the same identity
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.UOW 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.UOW 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.UOW 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