Search in sources :

Example 6 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class NestedCayenneContextIT method testNullifyToOne.

/**
 * A test case for CAY-698 bug.
 */
@Test
public void testNullifyToOne() {
    ClientMtTable1 a = clientContext.newObject(ClientMtTable1.class);
    ClientMtTable2 b = clientContext.newObject(ClientMtTable2.class);
    a.addToTable2Array(b);
    clientContext.commitChanges();
    ObjectContext child = runtime.newContext(clientContext);
    ObjectContext childPeer = runtime.newContext(clientContext);
    ClientMtTable2 childP1 = SelectById.query(ClientMtTable2.class, b.getObjectId()).selectOne(child);
    // trigger object creation in the peer nested DC
    Cayenne.objectForPK(childPeer, b.getObjectId());
    childP1.setTable1(null);
    queryInterceptor.runWithQueriesBlocked(() -> {
        child.commitChangesToParent();
        assertEquals(PersistenceState.COMMITTED, childP1.getPersistenceState());
        ClientMtTable2 parentP1 = (ClientMtTable2) clientContext.getGraphManager().getNode(childP1.getObjectId());
        assertNotNull(parentP1);
        assertEquals(PersistenceState.MODIFIED, parentP1.getPersistenceState());
        assertNull(parentP1.getTable1());
        // check that arc changes got recorded in the parent context
        GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
        final int[] arcDiffs = new int[1];
        diffs.apply(new GraphChangeHandler() {

            @Override
            public void arcCreated(Object nodeId, Object targetNodeId, ArcId arcId) {
                arcDiffs[0]++;
            }

            @Override
            public void arcDeleted(Object nodeId, Object targetNodeId, ArcId arcId) {
                arcDiffs[0]--;
            }

            @Override
            public void nodeCreated(Object nodeId) {
            }

            @Override
            public void nodeIdChanged(Object nodeId, Object newId) {
            }

            @Override
            public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
            }

            @Override
            public void nodeRemoved(Object nodeId) {
            }
        });
        assertEquals(-2, arcDiffs[0]);
    });
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ArcId(org.apache.cayenne.graph.ArcId) GraphDiff(org.apache.cayenne.graph.GraphDiff) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Example 7 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class DataContext method rollbackChangesLocally.

/**
 * If the parent channel is a DataContext, reverts local changes to make
 * this context look like the parent, if the parent channel is a DataDomain,
 * reverts all changes.
 *
 * @since 1.2
 */
@Override
public void rollbackChangesLocally() {
    if (objectStore.hasChanges()) {
        GraphDiff diff = getObjectStore().getChanges();
        getObjectStore().objectsRolledBack();
        fireDataChannelRolledback(this, diff);
    }
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff)

Example 8 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class CayenneContextGraphManager method graphReverted.

void graphReverted() {
    GraphDiff diff = changeLog.getDiffs();
    diff.undo(new RollbackChangeHandler());
    stateLog.graphReverted();
    reset();
    if (lifecycleEventsEnabled) {
        context.fireDataChannelRolledback(context, diff);
    }
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff)

Example 9 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff in project cayenne by apache.

the class DataContextCommitIT method testFlushToParent_NewNoAttributes.

@Test
public void testFlushToParent_NewNoAttributes() {
    // commit new object with uninitialized attributes
    context.newObject(NullTestEntity.class);
    assertTrue(context.hasChanges());
    GraphDiff diff3 = context.flushToParent(true);
    assertNotNull(diff3);
    assertFalse(context.hasChanges());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) Test(org.junit.Test)

Example 10 with GraphDiff

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

Aggregations

GraphDiff (org.apache.cayenne.graph.GraphDiff)32 Test (org.junit.Test)16 ObjectContext (org.apache.cayenne.ObjectContext)8 GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)7 ObjectId (org.apache.cayenne.ObjectId)5 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)5 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)5 ArcId (org.apache.cayenne.graph.ArcId)4 CayenneContext (org.apache.cayenne.CayenneContext)3 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)3 DataChannel (org.apache.cayenne.DataChannel)3 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)3 MockEventManager (org.apache.cayenne.event.MockEventManager)3 EntityResolver (org.apache.cayenne.map.EntityResolver)3 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)3 Artist (org.apache.cayenne.testdo.testmap.Artist)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 QueryResponse (org.apache.cayenne.QueryResponse)2 QueryCache (org.apache.cayenne.cache.QueryCache)2