Search in sources :

Example 36 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextSharedCacheIT method testSnapshotInsertPropagationToManyRefresh.

/**
 * Test case to prove that inserting an object in one ObjectStore and committing to
 * the database will be reflected in the peer ObjectStore using the same DataRowCache.
 * This would mean refreshing to-many collections.
 */
@Test
public void testSnapshotInsertPropagationToManyRefresh() throws Exception {
    Painting painting1 = (Painting) context.newObject("Painting");
    painting1.setPaintingTitle("p1");
    painting1.setToArtist(artist);
    context.commitChanges();
    // make sure we have a fully resolved copy of an artist and painting
    // objects
    // in the second context
    final Artist altArtist = context1.localObject(artist);
    final Painting altPainting1 = context1.localObject(painting1);
    assertEquals(artist.getArtistName(), altArtist.getArtistName());
    assertEquals(painting1.getPaintingTitle(), altPainting1.getPaintingTitle());
    assertEquals(1, altArtist.getPaintingArray().size());
    assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, altPainting1.getPersistenceState());
    // insert new painting and add to artist
    Painting painting2 = (Painting) context.newObject("Painting");
    painting2.setPaintingTitle("p2");
    painting2.setToArtist(artist);
    context.commitChanges();
    // check peer artist
    // use threaded helper as a barrier, to avoid triggering faults earlier than
    // needed
    ParallelTestContainer helper = new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            Object value = altArtist.readPropertyDirectly("paintingArray");
            assertTrue("Unexpected: " + value, value instanceof ToManyList);
            assertTrue(((ToManyList) value).isFault());
        }
    };
    helper.runTest(2000);
    List<Painting> list = altArtist.getPaintingArray();
    assertEquals(2, list.size());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 37 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataDomainCallbacksIT method testPostLoad_Prefetch.

@Test
public void testPostLoad_Prefetch() throws Exception {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    registry.addCallback(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
    MockCallingBackListener listener = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, listener, "publicCallback");
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    Painting p1 = context.newObject(Painting.class);
    p1.setToArtist(a1);
    p1.setPaintingTitle("XXX");
    context.commitChanges();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    p1 = (Painting) context1.performQuery(q).get(0);
    // artist is prefetched here, and a callback must have been invoked
    a1 = p1.getToArtist();
    assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 38 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataDomainQueryActionIT method testCachedQuery.

@Test
public void testCachedQuery() {
    DataDomain domain = runtime.getDataDomain();
    Painting p = context.newObject(Painting.class);
    p.setPaintingTitle("sample");
    SelectQuery query = new SelectQuery(Painting.class);
    query.addPrefetch(Painting.TO_GALLERY.disjoint());
    query.addPrefetch(Painting.TO_ARTIST.disjoint());
    query.addOrdering(Painting.PAINTING_TITLE.asc());
    query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
    query.setPageSize(5);
    QueryCache cache = domain.queryCache;
    domain.queryCache = new MockQueryCache() {

        @Override
        public List<?> get(QueryMetadata metadata, QueryCacheEntryFactory factory) {
            Object results = factory.createObject();
            assertTrue("Query cache is not serializable.", results instanceof Serializable);
            return null;
        }

        @SuppressWarnings("all")
        @Override
        public void put(QueryMetadata metadata, List results) {
            assertTrue("Query cache is not serializable.", results instanceof Serializable);
        }
    };
    DataDomainQueryAction action = new DataDomainQueryAction(context, domain, query);
    action.execute();
    domain.queryCache = cache;
}
Also used : MockQueryCache(org.apache.cayenne.cache.MockQueryCache) QueryCache(org.apache.cayenne.cache.QueryCache) Serializable(java.io.Serializable) MockQueryCache(org.apache.cayenne.cache.MockQueryCache) QueryMetadata(org.apache.cayenne.query.QueryMetadata) QueryCacheEntryFactory(org.apache.cayenne.cache.QueryCacheEntryFactory) Painting(org.apache.cayenne.testdo.testmap.Painting) SelectQuery(org.apache.cayenne.query.SelectQuery) List(java.util.List) Test(org.junit.Test)

Example 39 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DeleteObjectIT method testDeleteObjectsRelationshipCollection.

@Test
public void testDeleteObjectsRelationshipCollection() throws Exception {
    createObjectsRelationshipCollectionDataSet();
    Artist artist = Cayenne.objectForPK(context, Artist.class, 1);
    List<Painting> paintings = artist.getPaintingArray();
    assertEquals(3, paintings.size());
    // create a clone to be able to inspect paintings after deletion
    List<Painting> paintingsClone = new ArrayList<Painting>(paintings);
    for (Painting object : paintingsClone) {
        assertEquals(PersistenceState.COMMITTED, object.getPersistenceState());
    }
    context.deleteObjects(paintings);
    // as Painting -> Artist has Nullify rule, relationship list has to be
    // cleaned up,
    // and no exceptions thrown on concurrent modification...
    ObjRelationship r = context.getEntityResolver().getObjEntity(Painting.class).getRelationship("toArtist");
    assertEquals(DeleteRule.NULLIFY, r.getDeleteRule());
    assertEquals(0, paintings.size());
    for (Painting object : paintingsClone) {
        assertEquals(PersistenceState.DELETED, object.getPersistenceState());
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ArrayList(java.util.ArrayList) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 40 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class ProcedureCallIT method testSelect.

@Test
public void testSelect() throws Exception {
    if (!accessStackAdapter.supportsStoredProcedures()) {
        return;
    }
    // create an artist with painting in the database
    createArtist(1000.0);
    List<?> artists = runProcedureSelect(ProcedureCall.query(SELECT_STORED_PROCEDURE).param("aName", "An Artist").param("paintingPrice", 3000)).firstList();
    // check the results
    assertNotNull("Null result from StoredProcedure.", artists);
    assertEquals(1, artists.size());
    DataRow artistRow = (DataRow) artists.get(0);
    Artist a = context.objectFromDataRow(Artist.class, uppercaseConverter(artistRow));
    Painting p = a.getPaintingArray().get(0);
    // invalidate painting, it may have been updated in the proc
    context.invalidateObjects(p);
    assertEquals(2000, p.getEstimatedPrice().intValue());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

Painting (org.apache.cayenne.testdo.testmap.Painting)222 Test (org.junit.Test)218 Artist (org.apache.cayenne.testdo.testmap.Artist)144 SelectQuery (org.apache.cayenne.query.SelectQuery)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)35 List (java.util.List)30 Expression (org.apache.cayenne.exp.Expression)29 ArrayList (java.util.ArrayList)21 BigDecimal (java.math.BigDecimal)19 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)14 ObjectContext (org.apache.cayenne.ObjectContext)13 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)13 Gallery (org.apache.cayenne.testdo.testmap.Gallery)13 ValueHolder (org.apache.cayenne.ValueHolder)12 ROPainting (org.apache.cayenne.testdo.testmap.ROPainting)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 DataRow (org.apache.cayenne.DataRow)7 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)6 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)6 RefreshQuery (org.apache.cayenne.query.RefreshQuery)5