Search in sources :

Example 36 with UnitTestClosure

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

the class DataContextPrefetchIT method testPrefetch_ToOneWithQualifierOverlappingPrefetchPath.

@Test
public void testPrefetch_ToOneWithQualifierOverlappingPrefetchPath() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    Expression exp = ExpressionFactory.matchExp("toArtist.artistName", "artist3");
    SelectQuery q = new SelectQuery(Painting.class, exp);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    final List<Painting> results = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            Painting painting = results.get(0);
            // The parent must be fully fetched, not just HOLLOW (a fault)
            assertEquals(PersistenceState.COMMITTED, painting.getToArtist().getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 37 with UnitTestClosure

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

the class DataContextPrefetchIT method testPrefetch_ToOneWith_OuterJoinFlattenedQualifier.

@Test
public void testPrefetch_ToOneWith_OuterJoinFlattenedQualifier() throws Exception {
    tArtGroup.insert(1, "AG");
    tArtist.insert(11, "artist2");
    tArtist.insert(101, "artist3");
    tPainting.insert(6, "p_artist3", 101, 1000, null);
    tPainting.insert(7, "p_artist21", 11, 2000, null);
    tPainting.insert(8, "p_artist22", 11, 3000, null);
    // flattened join matches an object that is NOT the one we are looking
    // for
    tArtistGroup.insert(101, 1);
    // OUTER join part intentionally doesn't match anything
    Expression exp = Property.create("groupArray+.name", String.class).eq("XX").orExp(Artist.ARTIST_NAME.eq("artist2"));
    SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class, exp);
    q.addPrefetch(Artist.PAINTING_ARRAY.disjoint());
    final List<Artist> results = context.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            Artist a = results.get(0);
            assertEquals("artist2", a.getArtistName());
            assertEquals(2, a.getPaintingArray().size());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 38 with UnitTestClosure

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

the class DataContextPrefetchIT method testPrefetch_ToOne.

@Test
public void testPrefetch_ToOne() throws Exception {
    createTwoArtistsAndTwoPaintingsDataSet();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    final List<Painting> result = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertFalse(result.isEmpty());
            Painting p1 = result.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());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 39 with UnitTestClosure

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

the class DataContextPrefetchIT method testPrefetchWithLocalCache.

/**
 * This test and next one is the result of CAY-2349 fix
 */
@Test
public void testPrefetchWithLocalCache() throws Exception {
    tArtist.deleteAll();
    tGallery.deleteAll();
    tPainting.deleteAll();
    tArtist.insert(1, "artist1");
    tGallery.insert(1, "gallery1");
    tPainting.insert(1, "painting1", 1, 100, 1);
    List<Painting> paintings = ObjectSelect.query(Painting.class).localCache("g1").select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault);
    paintings = ObjectSelect.query(Painting.class).prefetch(Painting.TO_ARTIST.joint()).localCache("g1").select(context);
    assertEquals(1, paintings.size());
    assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            List<Painting> paintings = ObjectSelect.query(Painting.class).prefetch(Painting.TO_ARTIST.joint()).localCache("g1").select(context);
            assertEquals(1, paintings.size());
            assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist);
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) 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 40 with UnitTestClosure

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

the class DataContextPrefetchIT method testPrefetch_ReflexiveRelationship.

@Test
public void testPrefetch_ReflexiveRelationship() throws Exception {
    ArtGroup parent = (ArtGroup) context.newObject("ArtGroup");
    parent.setName("parent");
    ArtGroup child = (ArtGroup) context.newObject("ArtGroup");
    child.setName("child");
    child.setToParentGroup(parent);
    context.commitChanges();
    SelectQuery q = new SelectQuery("ArtGroup");
    q.setQualifier(ExpressionFactory.matchExp("name", "child"));
    q.addPrefetch("toParentGroup");
    final List<ArtGroup> results = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            ArtGroup fetchedChild = results.get(0);
            // The parent must be fully fetched, not just HOLLOW (a fault)
            assertEquals(PersistenceState.COMMITTED, fetchedChild.getToParentGroup().getPersistenceState());
        }
    });
    child.setToParentGroup(null);
    context.commitChanges();
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) 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