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);
}
});
}
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());
}
});
}
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());
}
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());
}
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());
}
Aggregations