Search in sources :

Example 81 with Painting

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

the class DataContextPrefetchIT method testPrefetchToOneSharedCache.

@Test
public void testPrefetchToOneSharedCache() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    final SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    q.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
    context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            // per CAY-499 second run of a cached query with prefetches
            // (i.e. when the
            // result is served from cache) used to throw an exception...
            List<Painting> cachedResult = context.performQuery(q);
            assertFalse(cachedResult.isEmpty());
            Painting p1 = cachedResult.get(0);
            Object toOnePrefetch = p1.readNestedProperty("toArtist");
            assertNotNull(toOnePrefetch);
            assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), toOnePrefetch instanceof Artist);
            Artist a1 = (Artist) toOnePrefetch;
            assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
            // and just in case - run one more time...
            context.performQuery(q);
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 82 with Painting

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

the class DataContextPrefetchIT method testPrefetch9.

@Test
public void testPrefetch9() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    Expression artistExp = ExpressionFactory.matchExp("artistName", "artist3");
    SelectQuery artistQuery = new SelectQuery(Artist.class, artistExp);
    Artist artist1 = (Artist) context.performQuery(artistQuery).get(0);
    // find the painting not matching the artist (this is the case where
    // such prefetch
    // at least makes sense)
    Expression exp = ExpressionFactory.noMatchExp("toArtist", artist1);
    SelectQuery q = new SelectQuery(Painting.class, exp);
    q.addPrefetch("toArtist");
    final List<Painting> results = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            // see that artists are resolved...
            Painting px = results.get(0);
            Artist ax = (Artist) px.readProperty(Painting.TO_ARTIST.getName());
            assertEquals(PersistenceState.COMMITTED, ax.getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 83 with Painting

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

the class DataContextPrefetchIT method testPrefetch_ToOne_DbPath.

@Test
public void testPrefetch_ToOne_DbPath() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    q.andQualifier(ExpressionFactory.matchDbExp("toArtist.ARTIST_NAME", "artist2"));
    List<Painting> results = context.performQuery(q);
    assertEquals(1, results.size());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 84 with Painting

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

the class DataContextCallbacksIT method testPostAddCallbacks.

@Test
public void testPostAddCallbacks() {
    LifecycleCallbackRegistry registry = runtime.getDataDomain().getEntityResolver().getCallbackRegistry();
    // no callbacks
    Artist a1 = context.newObject(Artist.class);
    assertNotNull(a1);
    assertFalse(a1.isPostAdded());
    registry.addCallback(LifecycleEvent.POST_ADD, Artist.class, "postAddCallback");
    Artist a2 = context.newObject(Artist.class);
    assertNotNull(a2);
    assertTrue(a2.isPostAdded());
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_ADD, Artist.class, listener2, "publicCallback");
    Artist a3 = context.newObject(Artist.class);
    assertNotNull(a3);
    assertTrue(a3.isPostAdded());
    assertSame(a3, listener2.getPublicCalledbackEntity());
    Painting p3 = context.newObject(Painting.class);
    assertNotNull(p3);
    assertFalse(p3.isPostAdded());
    assertSame(a3, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 85 with Painting

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

the class DataContextCallbacksIT method testPreRemoveCallbacks.

@Test
public void testPreRemoveCallbacks() {
    LifecycleCallbackRegistry registry = runtime.getDataDomain().getEntityResolver().getCallbackRegistry();
    // no callbacks
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    context.deleteObjects(a1);
    assertFalse(a1.isPostAdded());
    assertFalse(a1.isPreRemoved());
    registry.addCallback(LifecycleEvent.PRE_REMOVE, Artist.class, "preRemoveCallback");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("XX");
    context.commitChanges();
    context.deleteObjects(a2);
    assertFalse(a2.isPostAdded());
    assertTrue(a2.isPreRemoved());
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.PRE_REMOVE, Artist.class, listener2, "publicCallback");
    Artist a3 = context.newObject(Artist.class);
    a3.setArtistName("XX");
    context.commitChanges();
    context.deleteObjects(a3);
    assertFalse(a3.isPostAdded());
    assertTrue(a3.isPreRemoved());
    assertSame(a3, listener2.getPublicCalledbackEntity());
    Painting p3 = context.newObject(Painting.class);
    p3.setPaintingTitle("XX");
    context.commitChanges();
    context.deleteObjects(p3);
    assertFalse(p3.isPostAdded());
    assertFalse(p3.isPreRemoved());
    assertSame(a3, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) 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