Search in sources :

Example 6 with CompoundDiff

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

the class SyncMessageTest method testConstructor.

@Test
public void testConstructor() {
    ObjectContext source = mock(ObjectContext.class);
    GraphDiff diff = new CompoundDiff();
    SyncMessage message = new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, diff);
    assertSame(source, message.getSource());
    assertEquals(DataChannel.FLUSH_NOCASCADE_SYNC, message.getType());
    assertSame(diff, message.getSenderChanges());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 7 with CompoundDiff

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

the class SyncMessageTest method testConstructorInvalid.

@Test
public void testConstructorInvalid() {
    ObjectContext source = mock(ObjectContext.class);
    new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, new CompoundDiff());
    new SyncMessage(source, DataChannel.FLUSH_CASCADE_SYNC, new CompoundDiff());
    new SyncMessage(null, DataChannel.ROLLBACK_CASCADE_SYNC, new CompoundDiff());
    int bogusType = 45678;
    try {
        new SyncMessage(source, bogusType, new CompoundDiff());
        fail("invalid type was allowed to go unnoticed...");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 8 with CompoundDiff

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

the class ObjectContextChangeLogTest method testGetDiffsSerializable.

@Test
public void testGetDiffsSerializable() throws Exception {
    ObjectContextChangeLog recorder = new ObjectContextChangeLog();
    recorder.addOperation(new NodeCreateOperation(new ObjectId("test")));
    CompoundDiff diff = (CompoundDiff) recorder.getDiffs();
    Object clone = Util.cloneViaSerialization(diff);
    assertNotNull(clone);
    assertTrue(clone instanceof CompoundDiff);
    CompoundDiff d1 = (CompoundDiff) clone;
    assertEquals(1, d1.getDiffs().size());
}
Also used : NodeCreateOperation(org.apache.cayenne.graph.NodeCreateOperation) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 9 with CompoundDiff

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

the class ObjectContextChangeLogTest method testGetDiffs.

@Test
public void testGetDiffs() {
    // assert that after returning, the diffs array won't get modified by
    // operation
    // recorder
    ObjectContextChangeLog recorder = new ObjectContextChangeLog();
    recorder.addOperation(new NodeCreateOperation(new Object()));
    CompoundDiff diff = (CompoundDiff) recorder.getDiffs();
    assertEquals(1, diff.getDiffs().size());
    recorder.addOperation(new NodeCreateOperation(new Object()));
    assertEquals(1, diff.getDiffs().size());
    CompoundDiff diff2 = (CompoundDiff) recorder.getDiffs();
    assertEquals(2, diff2.getDiffs().size());
}
Also used : NodeCreateOperation(org.apache.cayenne.graph.NodeCreateOperation) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 10 with CompoundDiff

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

the class DataContext method flushToParent.

/**
 * Synchronizes with the parent channel, performing a flush or a commit.
 *
 * @since 1.2
 */
GraphDiff flushToParent(boolean cascade) {
    if (this.getChannel() == null) {
        throw new CayenneRuntimeException("Cannot commit changes - channel is not set.");
    }
    int syncType = cascade ? DataChannel.FLUSH_CASCADE_SYNC : DataChannel.FLUSH_NOCASCADE_SYNC;
    ObjectStore objectStore = getObjectStore();
    GraphDiff parentChanges = null;
    // prevent multiple commits occurring simultaneously
    synchronized (objectStore) {
        ObjectStoreGraphDiff changes = objectStore.getChanges();
        boolean noop = isValidatingObjectsOnCommit() ? changes.validateAndCheckNoop() : changes.isNoop();
        if (noop) {
            // need to clear phantom changes
            objectStore.postprocessAfterPhantomCommit();
        } else {
            try {
                parentChanges = getChannel().onSync(this, changes, syncType);
                // Pending better callback design .....
                if (objectStore.hasChanges()) {
                    objectStore.postprocessAfterCommit(parentChanges);
                }
                // this event is caught by peer nested DataContexts to
                // synchronize the
                // state
                fireDataChannelCommitted(this, changes);
            }// "catch" is needed to unwrap OptimisticLockExceptions
             catch (CayenneRuntimeException ex) {
                Throwable unwound = Util.unwindException(ex);
                if (unwound instanceof CayenneRuntimeException) {
                    throw (CayenneRuntimeException) unwound;
                } else {
                    throw new CayenneRuntimeException("Commit Exception", unwound);
                }
            }
        }
        // merge changes from parent as well as changes caused by lifecycle
        // event
        // callbacks/listeners...
        CompoundDiff diff = new CompoundDiff();
        diff.addAll(objectStore.getLifecycleEventInducedChanges());
        if (parentChanges != null) {
            diff.add(parentChanges);
        }
        // ObjectIds with permanent
        if (!diff.isNoop()) {
            fireDataChannelCommitted(getChannel(), diff);
        }
        return diff;
    }
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Aggregations

CompoundDiff (org.apache.cayenne.graph.CompoundDiff)15 Test (org.junit.Test)7 GraphDiff (org.apache.cayenne.graph.GraphDiff)5 NodeCreateOperation (org.apache.cayenne.graph.NodeCreateOperation)4 ArrayList (java.util.ArrayList)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ObjectContext (org.apache.cayenne.ObjectContext)2 NodeIdChangeOperation (org.apache.cayenne.graph.NodeIdChangeOperation)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)1 DataObject (org.apache.cayenne.DataObject)1 DataRow (org.apache.cayenne.DataRow)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 DefaultEventManager (org.apache.cayenne.event.DefaultEventManager)1 EventSubject (org.apache.cayenne.event.EventSubject)1 ChildDiffLoader (org.apache.cayenne.graph.ChildDiffLoader)1