Search in sources :

Example 86 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextCommitIT method testFlushToParent_Commit_Mix.

@Test
public void testFlushToParent_Commit_Mix() {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("Test");
    context.flushToParent(true);
    // commit a mix of new and modified
    Painting p = context.newObject(Painting.class);
    p.setPaintingTitle("PT");
    p.setToArtist(a);
    a.setArtistName("Test_");
    ObjectId beforeId = p.getObjectId();
    GraphDiff diff = context.flushToParent(true);
    ObjectId afterId = p.getObjectId();
    assertNotNull(diff);
    assertFalse(context.hasChanges());
    GraphChangeHandler diffChecker = mock(GraphChangeHandler.class);
    diff.apply(diffChecker);
    verify(diffChecker).nodeIdChanged(beforeId, afterId);
    verifyZeroInteractions(diffChecker);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 87 with Painting

use of org.apache.cayenne.testdo.testmap.Painting 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 88 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextEJBQLDeleteIT method testDeleteSameEntityQualifier.

@Test
public void testDeleteSameEntityQualifier() throws Exception {
    createPaintingsDataSet();
    String ejbql = "delete from Painting AS p WHERE p.paintingTitle = 'P2'";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    QueryResponse result = context.performGenericQuery(query);
    int[] count = result.firstUpdateCount();
    assertNotNull(count);
    assertEquals(1, count.length);
    assertEquals(1, count[0]);
    ObjectContext freshContext = runtime.newContext();
    assertNotNull(Cayenne.objectForPK(freshContext, Painting.class, 33001));
    assertNull(Cayenne.objectForPK(freshContext, Painting.class, 33002));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) QueryResponse(org.apache.cayenne.QueryResponse) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 89 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextEJBQLFetchJoinIT method testSeveralEntitiesAndScalarFetchInnerJoins.

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

        public void execute() {
            assertEquals(2, objects.size());
            Object[] firstRow = (Object[]) objects.get(0);
            Artist a = (Artist) firstRow[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()));
            }
            String artistName = (String) firstRow[1];
            assertEquals("A1", artistName);
            Gallery g1 = (Gallery) firstRow[2];
            assertEquals("gallery1", g1.getGalleryName());
            List<Exhibit> exibits = g1.getExhibitArray();
            assertNotNull(exibits);
            assertFalse(((ValueHolder) exibits).isFault());
            assertEquals(2, exibits.size());
            Object[] secondRow = (Object[]) objects.get(1);
            a = (Artist) secondRow[0];
            assertEquals("A2", a.getArtistName());
            paintings = a.getPaintingArray();
            assertNotNull(paintings);
            assertFalse(((ValueHolder) paintings).isFault());
            assertEquals(1, paintings.size());
            expectedPaintingsNames = new ArrayList<String>();
            expectedPaintingsNames.add("P2");
            paintingsIterator = paintings.iterator();
            while (paintingsIterator.hasNext()) {
                Painting p = paintingsIterator.next();
                assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                assertNotNull(p.getPaintingTitle());
                assertTrue(expectedPaintingsNames.contains(p.getPaintingTitle()));
            }
            artistName = (String) secondRow[1];
            assertEquals("A2", artistName);
            Gallery g2 = (Gallery) secondRow[2];
            assertEquals(g1, g2);
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ArrayList(java.util.ArrayList) ValueHolder(org.apache.cayenne.ValueHolder) Painting(org.apache.cayenne.testdo.testmap.Painting) Gallery(org.apache.cayenne.testdo.testmap.Gallery) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 90 with Painting

use of org.apache.cayenne.testdo.testmap.Painting in project cayenne by apache.

the class DataContextEJBQLFetchJoinIT method testFetchJoinForOneEntity.

@Test
public void testFetchJoinForOneEntity() throws Exception {
    createOneFetchJoinDataSet();
    String ejbql = "SELECT a FROM Artist a JOIN FETCH a.paintingArray ";
    EJBQLQuery query = new EJBQLQuery(ejbql);
    final List<?> objects = context.performQuery(query);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, objects.size());
            Iterator<?> it = objects.iterator();
            while (it.hasNext()) {
                Artist a = (Artist) it.next();
                List<Painting> list = a.getPaintingArray();
                assertNotNull(list);
                assertFalse(((ValueHolder) list).isFault());
                for (Painting p : list) {
                    assertEquals(PersistenceState.COMMITTED, p.getPersistenceState());
                    // make sure properties are not null..
                    assertNotNull(p.getPaintingTitle());
                }
            }
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) 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

Painting (org.apache.cayenne.testdo.testmap.Painting)222 Test (org.junit.Test)218 Artist (org.apache.cayenne.testdo.testmap.Artist)144 SelectQuery (org.apache.cayenne.query.SelectQuery)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)35 List (java.util.List)30 Expression (org.apache.cayenne.exp.Expression)29 ArrayList (java.util.ArrayList)21 BigDecimal (java.math.BigDecimal)19 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)14 ObjectContext (org.apache.cayenne.ObjectContext)13 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)13 Gallery (org.apache.cayenne.testdo.testmap.Gallery)13 ValueHolder (org.apache.cayenne.ValueHolder)12 ROPainting (org.apache.cayenne.testdo.testmap.ROPainting)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 DataRow (org.apache.cayenne.DataRow)7 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)6 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)6 RefreshQuery (org.apache.cayenne.query.RefreshQuery)5