use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextPrefetchIT method testPrefetchJointAndDisjointByIdTogether.
@Test
public void testPrefetchJointAndDisjointByIdTogether() throws Exception {
createArtistWithTwoPaintingsAndTwoInfosDataSet();
SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class);
query.andQualifier(Painting.PAINTING_TITLE.eq("p_artist2"));
query.addPrefetch(Painting.TO_ARTIST.joint());
query.addPrefetch(Painting.TO_PAINTING_INFO.disjointById());
final List<Painting> results = context.select(query);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(1, results.size());
Painting p0 = results.get(0);
Artist a0 = (Artist) p0.readPropertyDirectly(Painting.TO_ARTIST.getName());
assertNotNull(a0);
PaintingInfo info = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName());
assertNotNull(info);
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextPrefetchIT method testPrefetchToManyOnJoinTableJoinedPrefetch_ViaProperty.
@Test
public void testPrefetchToManyOnJoinTableJoinedPrefetch_ViaProperty() throws Exception {
createTwoArtistsWithExhibitsDataSet();
SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class);
q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.joint());
q.addOrdering(Artist.ARTIST_NAME.asc());
final List<Artist> artists = context.select(q);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(2, artists.size());
Artist a1 = artists.get(0);
assertEquals("artist2", a1.getArtistName());
List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName());
assertNotNull(toMany);
assertFalse(((ValueHolder) toMany).isFault());
assertEquals(2, toMany.size());
ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0);
assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState());
assertSame(a1, artistExhibit.getToArtist());
Artist a2 = artists.get(1);
assertEquals("artist3", a2.getArtistName());
List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName());
assertNotNull(toMany2);
assertFalse(((ValueHolder) toMany2).isFault());
assertEquals(3, toMany2.size());
ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0);
assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState());
assertSame(a2, artistExhibit2.getToArtist());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextPrefetchIT method testPrefetch_OneToOneWithQualifier.
@Test
public void testPrefetch_OneToOneWithQualifier() throws Exception {
createArtistWithTwoPaintingsAndTwoInfosDataSet();
Expression e = ExpressionFactory.likeExp("toArtist.artistName", "a%");
SelectQuery q = new SelectQuery(Painting.class, e);
q.addPrefetch(Painting.TO_PAINTING_INFO.disjoint());
q.addOrdering(Painting.PAINTING_TITLE.asc());
final List<Painting> results = context.performQuery(q);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(2, results.size());
// testing non-null to-one target
Painting p0 = results.get(0);
Object o2 = p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName());
assertTrue(o2 instanceof PaintingInfo);
PaintingInfo pi2 = (PaintingInfo) o2;
assertEquals(PersistenceState.COMMITTED, pi2.getPersistenceState());
assertEquals(Cayenne.intPKForObject(p0), Cayenne.intPKForObject(pi2));
// testing null to-one target
Painting p1 = results.get(1);
assertNull(p1.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()));
// there was a bug marking an object as dirty when clearing the
// relationships
assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure 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.unit.di.UnitTestClosure 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());
}
});
}
Aggregations