Search in sources :

Example 21 with Artist

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

the class JointPrefetchIT method testJointPrefetchToOne.

@Test
public void testJointPrefetchToOne() throws Exception {
    createJointPrefetchDataSet();
    // query with to-many joint prefetches
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    final List<Painting> objects = q.select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(3, objects.size());
        for (Painting p : objects) {
            Artist target = p.getToArtist();
            assertNotNull(target);
            assertEquals(PersistenceState.COMMITTED, target.getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 22 with Artist

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

the class JointPrefetchIT method testJointPrefetch_ToOne_FetchLimit.

@Test
public void testJointPrefetch_ToOne_FetchLimit() throws Exception {
    createJointPrefetchDataSet();
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    q.setFetchLimit(2);
    q.setFetchOffset(0);
    q.addOrdering("db:PAINTING_ID", SortOrder.ASCENDING);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    final List<Painting> objects = q.select(context);
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(2, objects.size());
        for (Painting p : objects) {
            Artist target = p.getToArtist();
            assertNotNull(target);
            assertEquals(PersistenceState.COMMITTED, target.getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 23 with Artist

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

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedToManyRelationship.

@Test
public void testPeerObjectUpdatedToManyRelationship() throws Exception {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    Painting px = context.newObject(Painting.class);
    px.setToArtist(a);
    px.setPaintingTitle("PX");
    Painting py = context.newObject(Painting.class);
    py.setPaintingTitle("PY");
    context.commitChanges();
    // pause to let the context events propagate
    Thread.sleep(500);
    ObjectContext peer1 = runtime.newContext(context);
    Painting py1 = peer1.localObject(py);
    Artist a1 = peer1.localObject(a);
    final ObjectContext peer2 = runtime.newContext(context);
    final Painting py2 = peer2.localObject(py);
    final Artist a2 = peer2.localObject(a);
    a1.addToPaintingArray(py1);
    assertEquals(1, a2.getPaintingArray().size());
    assertFalse(a2.getPaintingArray().contains(py2));
    peer1.commitChangesToParent();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertEquals(2, a2.getPaintingArray().size());
            assertTrue(a2.getPaintingArray().contains(py2));
            assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
        }
    }.runTest(2000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 24 with Artist

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

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedSimpleProperty.

@Test
public void testPeerObjectUpdatedSimpleProperty() throws Exception {
    Artist a = context.newObject(Artist.class);
    a.setArtistName("X");
    context.commitChanges();
    ObjectContext peer1 = runtime.newContext(context);
    Artist a1 = peer1.localObject(a);
    final ObjectContext peer2 = runtime.newContext(context);
    final Artist a2 = peer2.localObject(a);
    a1.setArtistName("Y");
    assertEquals("X", a2.getArtistName());
    peer1.commitChangesToParent();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertEquals("Y", a2.getArtistName());
            assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
        }
    }.runTest(2000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 25 with Artist

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

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedTempOID.

@Test
public void testPeerObjectUpdatedTempOID() throws Exception {
    ObjectContext peer1 = runtime.newContext(context);
    final Artist a1 = peer1.newObject(Artist.class);
    a1.setArtistName("Y");
    ObjectId a1TempId = a1.getObjectId();
    assertTrue(a1TempId.isTemporary());
    ObjectContext peer2 = runtime.newContext(context);
    final Artist a2 = peer2.localObject(a1);
    assertEquals(a1TempId, a2.getObjectId());
    peer1.commitChanges();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertFalse(a1.getObjectId().isTemporary());
            assertFalse(a2.getObjectId().isTemporary());
            assertEquals(a2.getObjectId(), a1.getObjectId());
        }
    }.runTest(2000);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) ObjectContext(org.apache.cayenne.ObjectContext) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Aggregations

Artist (org.apache.cayenne.testdo.testmap.Artist)490 Test (org.junit.Test)481 Painting (org.apache.cayenne.testdo.testmap.Painting)145 SelectQuery (org.apache.cayenne.query.SelectQuery)126 Expression (org.apache.cayenne.exp.Expression)67 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)47 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)39 List (java.util.List)36 ObjectContext (org.apache.cayenne.ObjectContext)30 SQLTemplate (org.apache.cayenne.query.SQLTemplate)26 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)26 DataRow (org.apache.cayenne.DataRow)21 ArrayList (java.util.ArrayList)20 ValueHolder (org.apache.cayenne.ValueHolder)18 ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)16 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)15 ObjectId (org.apache.cayenne.ObjectId)14 ROPainting (org.apache.cayenne.testdo.testmap.ROPainting)13 Gallery (org.apache.cayenne.testdo.testmap.Gallery)12 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)12