use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextPrefetchIT method testPrefetchToMany_WithQualfier.
@Test
public void testPrefetchToMany_WithQualfier() throws Exception {
createTwoArtistsAndTwoPaintingsDataSet();
Map<String, Object> params = new HashMap<>();
params.put("name1", "artist2");
params.put("name2", "artist3");
Expression e = ExpressionFactory.exp("artistName = $name1 or artistName = $name2");
SelectQuery<Artist> q = new SelectQuery<>("Artist", e.params(params));
q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
final List<Artist> artists = context.select(q);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(2, artists.size());
Artist a1 = artists.get(0);
List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.PAINTING_ARRAY.getName());
assertNotNull(toMany);
assertFalse(((ValueHolder) toMany).isFault());
assertEquals(1, toMany.size());
Painting p1 = (Painting) toMany.get(0);
assertEquals("p_" + a1.getArtistName(), p1.getPaintingTitle());
Artist a2 = artists.get(1);
List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.PAINTING_ARRAY.getName());
assertNotNull(toMany2);
assertFalse(((ValueHolder) toMany2).isFault());
assertEquals(1, toMany2.size());
Painting p2 = (Painting) toMany2.get(0);
assertEquals("p_" + a2.getArtistName(), p2.getPaintingTitle());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextPrefetchIT method testPrefetchToMany_ViaProperty.
@Test
public void testPrefetchToMany_ViaProperty() throws Exception {
createTwoArtistsAndTwoPaintingsDataSet();
SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class);
q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
final List<Artist> artists = context.select(q);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(2, artists.size());
for (int i = 0; i < 2; i++) {
Artist a = artists.get(i);
List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray");
assertNotNull(toMany);
assertFalse(((ValueHolder) toMany).isFault());
assertEquals(1, toMany.size());
Painting p = (Painting) toMany.get(0);
assertEquals("Invalid prefetched painting:" + p, "p_" + a.getArtistName(), p.getPaintingTitle());
}
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextPrefetchIT method testPrefetchToManyOnJoinTableJoinedPrefetch.
/**
* Test that a to-many relationship is initialized when a target entity has
* a compound PK only partially involved in relationship.
*/
@Test
public void testPrefetchToManyOnJoinTableJoinedPrefetch() throws Exception {
createTwoArtistsWithExhibitsDataSet();
SelectQuery q = new SelectQuery(Artist.class);
q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.joint());
q.addOrdering(Artist.ARTIST_NAME.asc());
final List<Artist> artists = context.performQuery(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("artistExhibitArray");
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 testPrefetchingToOneNull.
@Test
public void testPrefetchingToOneNull() throws Exception {
tPainting.insert(6, "p_Xty", null, 1000, null);
SelectQuery q = new SelectQuery(Painting.class);
q.addPrefetch(Painting.TO_ARTIST.disjoint());
final List<Painting> paintings = context.performQuery(q);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(1, paintings.size());
Painting p2 = paintings.get(0);
assertNull(p2.readProperty(Painting.TO_ARTIST.getName()));
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextPrefetchIT method testPrefetchPaintingOverToOneAndToMany.
@Test
public void testPrefetchPaintingOverToOneAndToMany() throws Exception {
createArtistWithTwoPaintingsAndTwoInfosDataSet();
SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class);
query.andQualifier(Painting.PAINTING_TITLE.eq("p_artist2"));
query.addPrefetch(Painting.TO_ARTIST.disjoint());
query.addPrefetch(Painting.TO_ARTIST.dot(Artist.PAINTING_ARRAY).disjoint());
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);
List<?> paintings = (List<?>) a0.readPropertyDirectly(Artist.PAINTING_ARRAY.getName());
assertEquals(2, paintings.size());
}
});
}
Aggregations