Search in sources :

Example 46 with UnitTestClosure

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

the class DataContextDisjointByIdPrefetchIT method testOneToMany.

@Test
public void testOneToMany() throws Exception {
    createArtistWithTwoPaintingsDataSet();
    SelectQuery query = new SelectQuery(Artist.class);
    query.addPrefetch(Artist.PAINTING_ARRAY.disjointById());
    final List<Artist> result = context.performQuery(query);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertFalse(result.isEmpty());
            Artist b1 = result.get(0);
            List<Painting> toMany = (List<Painting>) b1.readPropertyDirectly(Artist.PAINTING_ARRAY.getName());
            assertNotNull(toMany);
            assertFalse(((ValueHolder) toMany).isFault());
            assertEquals(2, toMany.size());
            List<String> names = new ArrayList<String>();
            for (Painting b : toMany) {
                assertEquals(PersistenceState.COMMITTED, b.getPersistenceState());
                names.add(b.getPaintingTitle());
            }
            assertTrue(names.contains("Y1"));
            assertTrue(names.contains("Y2"));
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ArrayList(java.util.ArrayList) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 47 with UnitTestClosure

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

the class DataContextDisjointByIdPrefetch_ExtrasIT method testJointPrefetchInChild.

@Test
public void testJointPrefetchInChild() throws Exception {
    createBagWithTwoBoxesAndPlentyOfBallsDataSet();
    SelectQuery<Bag> query = new SelectQuery<Bag>(Bag.class);
    query.addPrefetch(Bag.BOXES.disjointById());
    query.addPrefetch(Bag.BOXES.dot(Box.BALLS).joint());
    final List<Bag> result = context.select(query);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            assertFalse(result.isEmpty());
            Bag bag = result.get(0);
            List<Box> boxes = (List<Box>) bag.readPropertyDirectly(Bag.BOXES.getName());
            assertNotNull(boxes);
            assertFalse(((ValueHolder) boxes).isFault());
            assertEquals(2, boxes.size());
            Box big = null;
            List<String> names = new ArrayList<String>();
            for (Box box : boxes) {
                assertEquals(PersistenceState.COMMITTED, box.getPersistenceState());
                names.add(box.getName());
                if (box.getName().equals("big")) {
                    big = box;
                }
            }
            assertTrue(names.contains("big"));
            assertTrue(names.contains("small"));
            List<Ball> balls = (List<Ball>) big.readPropertyDirectly(Box.BALLS.getName());
            assertNotNull(balls);
            assertFalse(((ValueHolder) balls).isFault());
            assertEquals(2, balls.size());
            List<Integer> volumes = new ArrayList<Integer>();
            for (Ball ball : balls) {
                assertEquals(PersistenceState.COMMITTED, ball.getPersistenceState());
                volumes.add(ball.getThingVolume());
            }
            assertTrue(volumes.containsAll(Arrays.asList(10, 20)));
        }
    });
}
Also used : Ball(org.apache.cayenne.testdo.things.Ball) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Bag(org.apache.cayenne.testdo.things.Bag) Box(org.apache.cayenne.testdo.things.Box) ValueHolder(org.apache.cayenne.ValueHolder) SelectQuery(org.apache.cayenne.query.SelectQuery) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 48 with UnitTestClosure

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

the class DataContextDisjointByIdPrefetch_ExtrasIT method testJointPrefetchInParent.

@Test
public void testJointPrefetchInParent() throws Exception {
    createBagWithTwoBoxesAndPlentyOfBallsDataSet();
    SelectQuery query = new SelectQuery(Box.class);
    query.addPrefetch(Box.BALLS.disjointById());
    query.addPrefetch(Box.BALLS.dot(Ball.THING).disjointById());
    final List<Box> result = context.performQuery(query);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertFalse(result.isEmpty());
            List<Integer> volumes = new ArrayList<Integer>();
            for (Box box : result) {
                List<Ball> balls = (List<Ball>) box.readPropertyDirectly(Box.BALLS.getName());
                assertNotNull(balls);
                assertFalse(((ValueHolder) balls).isFault());
                for (Ball ball : balls) {
                    Thing thing = (Thing) ball.readPropertyDirectly(Ball.THING.getName());
                    assertNotNull(thing);
                    assertEquals(PersistenceState.COMMITTED, thing.getPersistenceState());
                    volumes.add(thing.getVolume());
                }
            }
            assertEquals(6, volumes.size());
            assertTrue(volumes.containsAll(Arrays.asList(10, 20, 30, 40)));
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Ball(org.apache.cayenne.testdo.things.Ball) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Box(org.apache.cayenne.testdo.things.Box) ArrayList(java.util.ArrayList) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) Thing(org.apache.cayenne.testdo.things.Thing) Test(org.junit.Test)

Example 49 with UnitTestClosure

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

the class DataContextDisjointByIdPrefetch_ExtrasIT method testMultiColumnRelationship.

@Test
public void testMultiColumnRelationship() throws Exception {
    createBagWithTwoBoxesAndPlentyOfBallsDataSet();
    SelectQuery query = new SelectQuery(Ball.class);
    query.orQualifier(Ball.THING_VOLUME.eq(40).andExp(Ball.THING_WEIGHT.eq(30)));
    query.orQualifier(Ball.THING_VOLUME.eq(20).andExp(Ball.THING_WEIGHT.eq(10)));
    query.addPrefetch(Ball.THING.disjointById());
    final List<Ball> balls = context.performQuery(query);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, balls.size());
            balls.get(0).getThing().getVolume();
            balls.get(1).getThing().getVolume();
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Ball(org.apache.cayenne.testdo.things.Ball) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 50 with UnitTestClosure

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

the class DataContextEJBQLFetchJoinIT method testSeveralEntitiesFetchJoins.

@Test
public void testSeveralEntitiesFetchJoins() throws Exception {
    createMultipleFetchJoinsDataSet();
    String ejbql = "SELECT DISTINCT a , g " + "FROM Artist a JOIN FETCH a.paintingArray , Gallery g JOIN FETCH g.exhibitArray " + "WHERE a.artistName='A1' AND g.galleryName='gallery1'";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    final List<?> objects = context.performQuery(query);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertNotNull(objects);
            assertFalse(objects.isEmpty());
            assertEquals(1, objects.size());
        }
    });
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) 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