Search in sources :

Example 41 with UnitTestClosure

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);
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 42 with UnitTestClosure

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());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 43 with UnitTestClosure

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());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 44 with UnitTestClosure

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);
        }
    });
}
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 45 with UnitTestClosure

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

Aggregations

UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)107 Test (org.junit.Test)107 SelectQuery (org.apache.cayenne.query.SelectQuery)64 List (java.util.List)48 Artist (org.apache.cayenne.testdo.testmap.Artist)47 Painting (org.apache.cayenne.testdo.testmap.Painting)35 ValueHolder (org.apache.cayenne.ValueHolder)23 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)19 ArrayList (java.util.ArrayList)15 ObjectContext (org.apache.cayenne.ObjectContext)14 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)9 ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)8 Iterator (java.util.Iterator)7 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)7 Expression (org.apache.cayenne.exp.Expression)6 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)6 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)6 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)5 RemoteIncrementalFaultList (org.apache.cayenne.remote.RemoteIncrementalFaultList)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)5