Search in sources :

Example 86 with UnitTestClosure

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

the class DataContextDisjointByIdPrefetch_ExtrasIT method testFlattenedRelationship.

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

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

Example 87 with UnitTestClosure

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

the class DataContextDisjointByIdPrefetch_ExtrasIT method testFlattenedMultiColumnRelationship.

@Test
public void testFlattenedMultiColumnRelationship() throws Exception {
    createBagWithTwoBoxesAndPlentyOfBallsDataSet();
    SelectQuery query = new SelectQuery(Box.class);
    query.addPrefetch(Box.THINGS.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<Thing> things = (List<Thing>) box.readPropertyDirectly(Box.THINGS.getName());
                assertNotNull(things);
                assertFalse(((ValueHolder) things).isFault());
                for (Thing t : things) {
                    assertEquals(PersistenceState.COMMITTED, t.getPersistenceState());
                    volumes.add(t.getVolume());
                }
            }
            assertEquals(6, volumes.size());
            assertTrue(volumes.containsAll(Arrays.asList(10, 20, 30, 40)));
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) 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 88 with UnitTestClosure

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

the class DataContextDisjointByIdPrefetch_ExtrasIT method testLongFlattenedRelationship.

@Test
public void testLongFlattenedRelationship() throws Exception {
    createBagWithTwoBoxesAndPlentyOfBallsDataSet();
    SelectQuery query = new SelectQuery(Bag.class);
    query.addPrefetch(Bag.THINGS.disjointById());
    final List<Bag> result = context.performQuery(query);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertFalse(result.isEmpty());
            Bag b1 = result.get(0);
            List<Thing> things = (List<Thing>) b1.readPropertyDirectly(Bag.THINGS.getName());
            assertNotNull(things);
            assertFalse(((ValueHolder) things).isFault());
            assertEquals(6, things.size());
            List<Integer> volumes = new ArrayList<Integer>();
            for (Thing t : things) {
                assertEquals(PersistenceState.COMMITTED, t.getPersistenceState());
                volumes.add(t.getVolume());
            }
            assertTrue(volumes.containsAll(Arrays.asList(10, 20, 20, 30, 40, 40)));
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Bag(org.apache.cayenne.testdo.things.Bag) 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 89 with UnitTestClosure

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

the class DataContextEJBQLFetchJoinIT method testSeveralFetchJoins.

@Test
public void testSeveralFetchJoins() throws Exception {
    createMultipleFetchJoinsDataSet();
    String ejbql = "SELECT a " + "FROM Artist a JOIN FETCH a.paintingArray JOIN FETCH a.artistExhibitArray " + "WHERE a.artistName = 'A1'";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    final List<?> objects = context.performQuery(query);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, objects.size());
            Artist a = (Artist) objects.get(0);
            assertEquals("A1", a.getArtistName());
            List<Painting> paintings = a.getPaintingArray();
            assertNotNull(paintings);
            assertFalse(((ValueHolder) paintings).isFault());
            assertEquals(2, paintings.size());
            List<String> expectedPaintingsNames = new ArrayList<String>();
            expectedPaintingsNames.add("P11");
            expectedPaintingsNames.add("P12");
            Iterator<Painting> paintingsIterator = paintings.iterator();
            while (paintingsIterator.hasNext()) {
                Painting p = paintingsIterator.next();
                assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                assertNotNull(p.getPaintingTitle());
                assertTrue(expectedPaintingsNames.contains(p.getPaintingTitle()));
            }
            List<ArtistExhibit> exibits = a.getArtistExhibitArray();
            assertNotNull(exibits);
            assertFalse(((ValueHolder) exibits).isFault());
            assertEquals(2, exibits.size());
            Iterator<ArtistExhibit> exibitsIterator = exibits.iterator();
            while (exibitsIterator.hasNext()) {
                ArtistExhibit ae = exibitsIterator.next();
                assertEquals(PersistenceState.COMMITTED, ae.getPersistenceState());
                assertNotNull(ae.getObjectId());
            }
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) ArtistExhibit(org.apache.cayenne.testdo.testmap.ArtistExhibit) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Iterator(java.util.Iterator) 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 90 with UnitTestClosure

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

the class DataContextEJBQLFetchJoinIT method testSeveralEntitiesAndScalarFetchOuterJoins.

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

        public void execute() {
            assertEquals(6, objects.size());
            Object[] row = (Object[]) objects.get(0);
            Artist a1 = (Artist) row[0];
            assertEquals("A1", a1.getArtistName());
            List<Painting> paintings = a1.getPaintingArray();
            assertNotNull(paintings);
            assertFalse(((ValueHolder) paintings).isFault());
            assertEquals(2, paintings.size());
            List<String> expectedPaintingsNames = new ArrayList<String>();
            expectedPaintingsNames.add("P11");
            expectedPaintingsNames.add("P12");
            Iterator<Painting> paintingsIterator = paintings.iterator();
            while (paintingsIterator.hasNext()) {
                Painting p = paintingsIterator.next();
                assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                assertNotNull(p.getPaintingTitle());
                assertTrue(expectedPaintingsNames.contains(p.getPaintingTitle()));
            }
            String artistName1 = (String) row[1];
            assertEquals("A1", artistName1);
            Gallery g1 = (Gallery) row[2];
            assertEquals("gallery1", g1.getGalleryName());
            List<?> exibits = g1.getExhibitArray();
            assertNotNull(exibits);
            assertFalse(((ValueHolder) exibits).isFault());
            assertEquals(2, exibits.size());
            row = (Object[]) objects.get(1);
            assertEquals(a1, row[0]);
            assertEquals(artistName1, row[1]);
            Gallery g2 = (Gallery) row[2];
            assertEquals("gallery2", g2.getGalleryName());
            exibits = g2.getExhibitArray();
            assertTrue(exibits.isEmpty());
            row = (Object[]) objects.get(2);
            Artist a2 = (Artist) row[0];
            assertEquals("A2", a2.getArtistName());
            paintings = a2.getPaintingArray();
            assertNotNull(paintings);
            assertEquals(1, paintings.size());
            Painting p = paintings.get(0);
            assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
            assertNotNull(p.getPaintingTitle());
            assertEquals("P2", p.getPaintingTitle());
            String artistName2 = (String) row[1];
            assertEquals("A2", artistName2);
            assertEquals(g1, row[2]);
            row = (Object[]) objects.get(3);
            assertEquals(a2, row[0]);
            assertEquals(artistName2, row[1]);
            assertEquals(g2, row[2]);
            row = (Object[]) objects.get(4);
            Artist a3 = (Artist) row[0];
            assertEquals("A3", a3.getArtistName());
            paintings = a3.getPaintingArray();
            assertTrue(paintings.isEmpty());
            String artistName3 = (String) row[1];
            assertEquals("A3", artistName3);
            assertEquals(g1, row[2]);
            row = (Object[]) objects.get(5);
            assertEquals(a3, row[0]);
            assertEquals(artistName3, row[1]);
            assertEquals(g2, row[2]);
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Gallery(org.apache.cayenne.testdo.testmap.Gallery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) 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