Search in sources :

Example 6 with DataObject

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++;
    }
}
Also used : DataObject(org.apache.cayenne.DataObject) DataObject(org.apache.cayenne.DataObject) Test(org.junit.Test)

Example 7 with DataObject

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());
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 8 with DataObject

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"));
    });
}
Also used : DataObject(org.apache.cayenne.DataObject) ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 9 with DataObject

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());
        }
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 10 with DataObject

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();
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) QueryMetadata(org.apache.cayenne.query.QueryMetadata) Test(org.junit.Test)

Aggregations

DataObject (org.apache.cayenne.DataObject)38 Test (org.junit.Test)27 ObjectId (org.apache.cayenne.ObjectId)18 DataRow (org.apache.cayenne.DataRow)15 Persistent (org.apache.cayenne.Persistent)8 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)7 Artist (org.apache.cayenne.testdo.testmap.Artist)7 CayenneDataObject (org.apache.cayenne.CayenneDataObject)6 SelectQuery (org.apache.cayenne.query.SelectQuery)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)4 Expression (org.apache.cayenne.exp.Expression)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)3 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)3 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 MockDataObject (org.apache.cayenne.MockDataObject)2 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)2