Search in sources :

Example 6 with GraphChangeHandler

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

the class DataContextCommitIT method testFlushToParent_Commit_New.

@Test
public void testFlushToParent_Commit_New() {
    // commit new object
    Artist a = context.newObject(Artist.class);
    a.setArtistName("Test");
    assertTrue(context.hasChanges());
    ObjectId beforeId = a.getObjectId();
    GraphDiff diff = context.flushToParent(true);
    ObjectId afterId = a.getObjectId();
    assertNotNull(diff);
    assertFalse(context.hasChanges());
    assertNotEquals(beforeId, afterId);
    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) Test(org.junit.Test)

Example 7 with GraphChangeHandler

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

the class NestedCayenneContextIT method testCommitChangesToParentOneToMany.

@Test
public void testCommitChangesToParentOneToMany() throws Exception {
    ObjectContext child = runtime.newContext(clientContext);
    ClientMtTable1 master = child.newObject(ClientMtTable1.class);
    ClientMtTable2 dep = child.newObject(ClientMtTable2.class);
    master.addToTable2Array(dep);
    child.commitChangesToParent();
    ClientMtTable1 masterParent = (ClientMtTable1) clientContext.getGraphManager().getNode(master.getObjectId());
    ClientMtTable2 depParent = (ClientMtTable2) clientContext.getGraphManager().getNode(dep.getObjectId());
    assertNotNull(masterParent);
    assertNotNull(depParent);
    assertSame(masterParent, depParent.getTable1());
    assertTrue(masterParent.getTable2Array().contains(depParent));
    // check that arc changes got recorded in the parent context
    GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
    final int[] arcDiffs = new int[1];
    final int[] newNodes = new int[1];
    diffs.apply(new GraphChangeHandler() {

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

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

        public void nodeCreated(Object nodeId) {
            newNodes[0]++;
        }

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

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

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

Example 8 with GraphChangeHandler

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

the class NestedCayenneContextTooneIT method testCommitChangesToParentOneToOne.

@Test
public void testCommitChangesToParentOneToOne() throws Exception {
    ObjectContext child = runtime.newContext(clientContext);
    ClientTooneMaster master = child.newObject(ClientTooneMaster.class);
    ClientTooneDep dep = child.newObject(ClientTooneDep.class);
    master.setToDependent(dep);
    child.commitChangesToParent();
    ClientTooneMaster masterParent = (ClientTooneMaster) clientContext.getGraphManager().getNode(master.getObjectId());
    ClientTooneDep depParent = (ClientTooneDep) clientContext.getGraphManager().getNode(dep.getObjectId());
    assertNotNull(masterParent);
    assertNotNull(depParent);
    assertSame(masterParent, depParent.getToMaster());
    assertSame(depParent, masterParent.getToDependent());
    // check that arc changes got recorded in the parent context
    GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
    final int[] arcDiffs = new int[1];
    final int[] newNodes = new int[1];
    diffs.apply(new GraphChangeHandler() {

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

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

        public void nodeCreated(Object nodeId) {
            newNodes[0]++;
        }

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

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

        public void nodeRemoved(Object nodeId) {
            newNodes[0]--;
        }
    });
    assertEquals(2, newNodes[0]);
    assertEquals(2, arcDiffs[0]);
}
Also used : ClientTooneMaster(org.apache.cayenne.testdo.toone.ClientTooneMaster) GraphDiff(org.apache.cayenne.graph.GraphDiff) ClientTooneDep(org.apache.cayenne.testdo.toone.ClientTooneDep) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Example 9 with GraphChangeHandler

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

the class ClientChannelServerDiffsIT method testReturnIdDiff.

@Test
public void testReturnIdDiff() {
    final Object[] ids = new Object[2];
    final GraphChangeHandler diffReader = new NoopGraphChangeHandler() {

        @Override
        public void nodeIdChanged(Object oldId, Object newId) {
            ids[0] = oldId;
            ids[1] = newId;
        }
    };
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {

        @Override
        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
            assertNotNull(serverDiff);
            serverDiff.apply(diffReader);
            return serverDiff;
        }
    };
    CayenneContext context = new CayenneContext(channel);
    context.newObject(ClientMtTable1.class);
    context.commitChanges();
    assertTrue(ids[0] instanceof ObjectId);
    assertTrue(((ObjectId) ids[0]).isTemporary());
    assertTrue(ids[1] instanceof ObjectId);
    assertFalse(((ObjectId) ids[1]).isTemporary());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectId(org.apache.cayenne.ObjectId) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) ObjectContext(org.apache.cayenne.ObjectContext) CayenneContext(org.apache.cayenne.CayenneContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 10 with GraphChangeHandler

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

the class CommitLogFilter method afterCommit.

private void afterCommit(MutableChangeMap changes, DataChannel channel, GraphDiff contextDiff, GraphDiff dbDiff) {
    GraphChangeHandler handler = new DiffFilter(entityFactory, new DiffProcessor(changes, channel.getEntityResolver()));
    contextDiff.apply(handler);
    dbDiff.apply(handler);
}
Also used : GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler)

Aggregations

GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)10 GraphDiff (org.apache.cayenne.graph.GraphDiff)7 Test (org.junit.Test)7 ObjectId (org.apache.cayenne.ObjectId)3 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)3 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)2 Artist (org.apache.cayenne.testdo.testmap.Artist)2 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)2 CayenneContext (org.apache.cayenne.CayenneContext)1 DataObject (org.apache.cayenne.DataObject)1 ObjectContext (org.apache.cayenne.ObjectContext)1 Persistent (org.apache.cayenne.Persistent)1 MockEventManager (org.apache.cayenne.event.MockEventManager)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1 Painting (org.apache.cayenne.testdo.testmap.Painting)1 ClientTooneDep (org.apache.cayenne.testdo.toone.ClientTooneDep)1 ClientTooneMaster (org.apache.cayenne.testdo.toone.ClientTooneMaster)1