Search in sources :

Example 71 with ObjectContext

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

the class NestedDataContextReadIT method testPrefetchingToMany.

@Test
public void testPrefetchingToMany() throws Exception {
    createPrefetchingDataSet();
    final ObjectContext child = runtime.newContext(context);
    final List<Artist> results = ObjectSelect.query(Artist.class).orderBy(Artist.ARTIST_NAME.asc()).prefetch(Artist.PAINTING_ARRAY.disjoint()).select(child);
    queryInterceptor.runWithQueriesBlocked(() -> {
        Artist o1 = results.get(0);
        assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
        assertSame(child, o1.getObjectContext());
        List<Painting> children1 = o1.getPaintingArray();
        assertEquals(ToManyList.class, children1.getClass());
        assertEquals(2, children1.size());
        for (Painting o : children1) {
            assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
            assertSame(child, o.getObjectContext());
            assertEquals(o1, o.getToArtist());
        }
        Artist o2 = results.get(1);
        assertEquals(PersistenceState.COMMITTED, o2.getPersistenceState());
        assertSame(child, o2.getObjectContext());
        List<Painting> children2 = o2.getPaintingArray();
        assertEquals(0, children2.size());
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 72 with ObjectContext

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

the class NestedDataContextReadIT method testPageableSelect.

@Test
public void testPageableSelect() throws Exception {
    createArtistsDataSet();
    ObjectContext child = runtime.newContext(context);
    @SuppressWarnings("unchecked") IncrementalFaultList<Artist> records = (IncrementalFaultList) ObjectSelect.query(Artist.class).orderBy(Artist.ARTIST_NAME.desc()).pageSize(1).select(child);
    assertEquals(4, records.size());
    assertEquals(1, records.getPageSize());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 73 with ObjectContext

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

the class NestedDataContextParentEventsIT method testParentUpdatedId.

@Test
public void testParentUpdatedId() throws Exception {
    ObjectContext child1 = runtime.newContext(context);
    final Artist ac = child1.newObject(Artist.class);
    ac.setArtistName("X");
    child1.commitChangesToParent();
    final Artist ap = (Artist) context.getGraphManager().getNode(ac.getObjectId());
    assertNotNull(ap);
    assertTrue(ap.getObjectId().isTemporary());
    context.commitChanges();
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertFalse(ap.getObjectId().isTemporary());
            assertEquals(ap.getObjectId(), ac.getObjectId());
        }
    }.runTest(1000);
}
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 74 with ObjectContext

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

the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedToOneRelationship.

@Test
public void testPeerObjectUpdatedToOneRelationship() throws Exception {
    Artist a = context.newObject(Artist.class);
    Artist altA = context.newObject(Artist.class);
    Painting p = context.newObject(Painting.class);
    p.setToArtist(a);
    p.setPaintingTitle("PPP");
    a.setArtistName("X");
    altA.setArtistName("Y");
    context.commitChanges();
    ObjectContext peer1 = runtime.newContext(context);
    Painting p1 = peer1.localObject(p);
    Artist altA1 = peer1.localObject(altA);
    final ObjectContext peer2 = runtime.newContext(context);
    final Painting p2 = peer2.localObject(p);
    final Artist altA2 = peer2.localObject(altA);
    Artist a2 = peer2.localObject(a);
    p1.setToArtist(altA1);
    assertSame(a2, p2.getToArtist());
    peer1.commitChangesToParent();
    // pause to let the context events propagate
    new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            assertEquals(altA2, p2.getToArtist());
            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 75 with ObjectContext

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

the class NestedDataContextRollbackIT method testRollbackChanges.

@Test
public void testRollbackChanges() {
    ObjectContext child1 = runtime.newContext(context);
    assertFalse(context.hasChanges());
    assertFalse(child1.hasChanges());
    context.newObject(Artist.class);
    child1.newObject(Artist.class);
    assertTrue(context.hasChanges());
    assertTrue(child1.hasChanges());
    child1.rollbackChanges();
    assertFalse(context.hasChanges());
    assertFalse(child1.hasChanges());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)127 Test (org.junit.Test)116 Artist (org.apache.cayenne.testdo.testmap.Artist)35 Painting (org.apache.cayenne.testdo.testmap.Painting)14 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 Table1 (org.apache.cayenne.crypto.db.Table1)10 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 ObjectId (org.apache.cayenne.ObjectId)8 GraphDiff (org.apache.cayenne.graph.GraphDiff)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 IvImpl (org.apache.cayenne.testdo.inheritance_vertical.IvImpl)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 QueryResponse (org.apache.cayenne.QueryResponse)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4