Search in sources :

Example 31 with ValueHolder

use of org.apache.cayenne.ValueHolder 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 32 with ValueHolder

use of org.apache.cayenne.ValueHolder 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 33 with ValueHolder

use of org.apache.cayenne.ValueHolder 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 34 with ValueHolder

use of org.apache.cayenne.ValueHolder 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)

Example 35 with ValueHolder

use of org.apache.cayenne.ValueHolder in project cayenne by apache.

the class MixedPersistenceStrategyIT method testConflictingField2.

/**
 * This test case reproduces CAY-582 bug.
 */
@Test
public void testConflictingField2() throws Exception {
    createConflictingFieldDataSet();
    MixedPersistenceStrategy2 detail1 = Cayenne.objectForPK(context, MixedPersistenceStrategy2.class, 1);
    MixedPersistenceStrategy2 detail2 = Cayenne.objectForPK(context, MixedPersistenceStrategy2.class, 2);
    // resolve master (this is where CAY-582 exception happens)
    assertEquals("n1", detail1.getMaster().getName());
    assertEquals(2, detail2.getMaster().getDetails().size());
    assertTrue(detail2.getMaster().getDetails() instanceof ValueHolder);
}
Also used : MixedPersistenceStrategy2(org.apache.cayenne.testdo.mixed_persistence_strategy.MixedPersistenceStrategy2) ValueHolder(org.apache.cayenne.ValueHolder) Test(org.junit.Test)

Aggregations

ValueHolder (org.apache.cayenne.ValueHolder)35 Test (org.junit.Test)32 List (java.util.List)28 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)23 SelectQuery (org.apache.cayenne.query.SelectQuery)21 Artist (org.apache.cayenne.testdo.testmap.Artist)18 Painting (org.apache.cayenne.testdo.testmap.Painting)12 ArrayList (java.util.ArrayList)10 ArtistExhibit (org.apache.cayenne.testdo.testmap.ArtistExhibit)7 Gallery (org.apache.cayenne.testdo.testmap.Gallery)7 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 Expression (org.apache.cayenne.exp.Expression)4 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)4 ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)4 Exhibit (org.apache.cayenne.testdo.testmap.Exhibit)3 Bag (org.apache.cayenne.testdo.things.Bag)3 Ball (org.apache.cayenne.testdo.things.Ball)3 Box (org.apache.cayenne.testdo.things.Box)3