use of org.apache.cayenne.DataObject in project cayenne by apache.
the class SimpleIdIncrementalFaultListIT method testListIterator.
@Test
public void testListIterator() throws Exception {
SimpleIdIncrementalFaultList<?> list = prepareList(6);
ListIterator<?> it = list.listIterator();
int counter = 0;
while (it.hasNext()) {
Object obj = it.next();
assertNotNull(obj);
assertTrue(obj instanceof DataObject);
// iterator must be resolved page by page
int expectedResolved = list.pageIndex(counter) * list.getPageSize() + list.getPageSize();
if (expectedResolved > list.size()) {
expectedResolved = list.size();
}
assertEquals(list.size() - expectedResolved, list.getUnfetchedObjects());
counter++;
}
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class JointPrefetchIT method testJointPrefetchMultiStep.
@Test
public void testJointPrefetchMultiStep() throws Exception {
createJointPrefetchDataSet();
// query with to-many joint prefetches
SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
q.addPrefetch(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).joint());
final DataContext context = this.context;
// make sure phantomly prefetched objects are not deallocated
context.getObjectStore().objectMap = new HashMap<>();
// sanity check...
DataObject g1 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
assertNull(g1);
final List<Artist> objects = q.select(context);
queryInterceptor.runWithQueriesBlocked(() -> {
assertEquals(3, objects.size());
for (Artist a : objects) {
ValueHolder list = (ValueHolder) a.getPaintingArray();
assertNotNull(list);
// intermediate relationship is not fetched...
assertTrue(list.isFault());
}
// however both galleries must be in memory...
DataObject g11 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
assertNotNull(g11);
assertEquals(PersistenceState.COMMITTED, g11.getPersistenceState());
DataObject g2 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33002));
assertNotNull(g2);
assertEquals(PersistenceState.COMMITTED, g2.getPersistenceState());
});
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class JointPrefetchIT method testJointPrefetchSQLSelectNestedJoint.
@Test
public void testJointPrefetchSQLSelectNestedJoint() throws Exception {
createJointPrefetchDataSet();
SQLSelect.query(Artist.class, "SELECT " + "#result('GALLERY_ID' 'int' '' 'paintingArray.toGallery.GALLERY_ID')," + "#result('GALLERY_NAME' 'String' '' 'paintingArray.toGallery.GALLERY_NAME')," + "#result('t0.ARTIST_ID' 'int' '' 'ARTIST_ID') " + "FROM ARTIST t0, GALLERY t2 ").addPrefetch(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).joint()).select(context);
queryInterceptor.runWithQueriesBlocked(() -> {
DataObject g1 = (DataObject) context.getGraphManager().getNode(new ObjectId("Gallery", Gallery.GALLERY_ID_PK_COLUMN, 33001));
assertNotNull(g1);
assertEquals("G1", g1.readProperty("galleryName"));
});
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class NestedDataContextReadIT method testSelect.
@Test
public void testSelect() throws Exception {
createArtistsDataSet();
ObjectContext child = runtime.newContext(context);
// test how different object states appear in the child on select
Persistent _new = context.newObject(Artist.class);
Persistent hollow = Cayenne.objectForPK(context, Artist.class, 33001);
context.invalidateObjects(hollow);
DataObject committed = Cayenne.objectForPK(context, Artist.class, 33002);
Artist modified = Cayenne.objectForPK(context, Artist.class, 33003);
modified.setArtistName("MODDED");
DataObject deleted = Cayenne.objectForPK(context, Artist.class, 33004);
context.deleteObjects(deleted);
assertEquals(PersistenceState.HOLLOW, hollow.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, committed.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
assertEquals(PersistenceState.DELETED, deleted.getPersistenceState());
assertEquals(PersistenceState.NEW, _new.getPersistenceState());
List<Artist> objects = new SelectQuery<>(Artist.class).select(child);
assertEquals("All but NEW object must have been included", 4, objects.size());
for (Artist next : objects) {
assertEquals(PersistenceState.COMMITTED, next.getPersistenceState());
int id = Cayenne.intPKForObject(next);
if (id == 33003) {
assertEquals("MODDED", next.getArtistName());
}
}
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class DataContextQueryCachingIT method testLocalCacheDataObjectsRefresh.
@Test
public void testLocalCacheDataObjectsRefresh() throws Exception {
SelectQuery<Artist> select = new SelectQuery<>(Artist.class);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
try {
// first run, no cache yet
List<?> rows1 = mockupDataRows(2);
engine.reset();
engine.addExpectedResult(select, rows1);
List<?> resultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(2, resultRows.size());
assertTrue(resultRows.get(0) instanceof DataObject);
QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(resultRows, context.getQueryCache().get(cacheKey));
// second run, must refresh the cache
List<?> rows2 = mockupDataRows(4);
engine.reset();
engine.addExpectedResult(select, rows2);
select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
List<?> freshResultRows = context.performQuery(select);
assertEquals(1, engine.getRunCount());
assertEquals(4, freshResultRows.size());
assertTrue(resultRows.get(0) instanceof DataObject);
assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
assertEquals(freshResultRows, context.getQueryCache().get(cacheKey));
} finally {
engine.stopInterceptNode();
}
}
Aggregations