Search in sources :

Example 71 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class DataContextPrefetchIT method testPrefetch_ToManyNoReverse.

/**
 * Test that a to-many relationship is initialized when there is no inverse
 * relationship
 */
@Test
public void testPrefetch_ToManyNoReverse() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    ObjEntity paintingEntity = context.getEntityResolver().getObjEntity(Painting.class);
    ObjRelationship relationship = paintingEntity.getRelationship("toArtist");
    paintingEntity.removeRelationship("toArtist");
    try {
        SelectQuery<Artist> q = new SelectQuery<>(Artist.class);
        q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
        final List<Artist> result = context.performQuery(q);
        queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

            public void execute() {
                assertFalse(result.isEmpty());
                Artist a1 = result.get(0);
                List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray");
                assertNotNull(toMany);
                assertFalse(((ValueHolder) toMany).isFault());
            }
        });
    } finally {
        paintingEntity.addRelationship(relationship);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Example 72 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class DataContextPrefetchIT method testPrefetchWithSharedCache.

@Test
public void testPrefetchWithSharedCache() throws Exception {
    tArtist.deleteAll();
    tGallery.deleteAll();
    tPainting.deleteAll();
    tArtist.insert(1, "artist1");
    tGallery.insert(1, "gallery1");
    tPainting.insert(1, "painting1", 1, 100, 1);
    final ObjectSelect<Painting> s1 = ObjectSelect.query(Painting.class).sharedCache("g1");
    final ObjectSelect<Painting> s2 = ObjectSelect.query(Painting.class).prefetch(Painting.TO_ARTIST.disjoint()).sharedCache("g1");
    final ObjectSelect<Painting> s3 = ObjectSelect.query(Painting.class).prefetch(Painting.TO_GALLERY.joint()).sharedCache("g1");
    final ObjectSelect<Painting> s4 = ObjectSelect.query(Painting.class).prefetch(Painting.TO_ARTIST.disjoint()).prefetch(Painting.TO_GALLERY.joint()).sharedCache("g1");
    // first iteration select from DB and cache
    List<Painting> paintings = s1.select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault);
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Fault);
    paintings = s2.select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Fault);
    paintings = s3.select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault);
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery);
    paintings = s4.select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            // select from cache
            List<Painting> paintings = s2.select(context);
            assertEquals(1, paintings.size());
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Fault);
            paintings = s3.select(context);
            assertEquals(1, paintings.size());
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault);
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery);
            paintings = s4.select(context);
            assertEquals(1, paintings.size());
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery);
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Gallery(org.apache.cayenne.testdo.testmap.Gallery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Fault(org.apache.cayenne.Fault) List(java.util.List) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 73 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class DataContextPrefetchIT method testPrefetchToManyNoQualifier.

@Test
public void testPrefetchToManyNoQualifier() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
    final List<Artist> artists = context.performQuery(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());
            }
        }
    });
}
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) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 74 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class DataContextPrefetchIT method testPrefetchToMany_OnJoinTableDisjoinedPrefetch.

/**
 * Test that a to-many relationship is initialized when a target entity has
 * a compound PK only partially involved in relationship.
 */
@Test
public void testPrefetchToMany_OnJoinTableDisjoinedPrefetch() throws Exception {
    createTwoArtistsWithExhibitsDataSet();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.disjoint());
    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(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 : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) 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 75 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class DataContextPrefetchIT method testPrefetchToOneLocalCache.

@Test
public void testPrefetchToOneLocalCache() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    final SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    q.setCacheStrategy(QueryCacheStrategy.LOCAL_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)

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