Search in sources :

Example 26 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class NestedCayenneContextIT method testCommitChangesToParent.

@Test
public void testCommitChangesToParent() throws Exception {
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    clientContext.commitChanges();
    final ObjectContext child = runtime.newContext(clientContext);
    SelectQuery<ClientMtTable1> query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    List<ClientMtTable1> objects = child.select(query);
    assertEquals(4, objects.size());
    final ClientMtTable1 childNew = child.newObject(ClientMtTable1.class);
    childNew.setGlobalAttribute1("NNN");
    final ClientMtTable1 childModified = objects.get(0);
    childModified.setGlobalAttribute1("MMM");
    final ClientMtTable1 childCommitted = objects.get(1);
    final ClientMtTable1 childHollow = objects.get(3);
    child.invalidateObjects(childHollow);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            child.commitChangesToParent();
            // * all modified child objects must be in committed state now
            // * all modifications should be propagated to the parent
            // * no actual commit should occur.
            assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
            assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
            ClientMtTable1 parentNew = (ClientMtTable1) clientContext.getGraphManager().getNode(childNew.getObjectId());
            final ClientMtTable1 parentModified = (ClientMtTable1) clientContext.getGraphManager().getNode(childModified.getObjectId());
            ClientMtTable1 parentCommitted = (ClientMtTable1) clientContext.getGraphManager().getNode(childCommitted.getObjectId());
            ClientMtTable1 parentHollow = (ClientMtTable1) clientContext.getGraphManager().getNode(childHollow.getObjectId());
            assertNotNull(parentNew);
            assertEquals(PersistenceState.NEW, parentNew.getPersistenceState());
            assertEquals("NNN", parentNew.getGlobalAttribute1());
            assertNotNull(parentModified);
            assertEquals(PersistenceState.MODIFIED, parentModified.getPersistenceState());
            assertEquals("MMM", parentModified.getGlobalAttribute1());
            assertNotNull(parentCommitted);
            assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
            assertNotNull(parentHollow);
            // check that arc changes got recorded in the parent context
            GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
            final int[] modifiedProperties = new int[1];
            diffs.apply(new GraphChangeHandler() {

                @Override
                public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
                }

                @Override
                public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
                }

                @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) {
                    if (nodeId.equals(parentModified.getObjectId())) {
                        modifiedProperties[0]++;
                    }
                }

                @Override
                public void nodeRemoved(Object nodeId) {
                }
            });
            assertEquals(1, modifiedProperties[0]);
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) GraphDiff(org.apache.cayenne.graph.GraphDiff) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Example 27 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class NestedCayenneContextIT method testNullifyToOne.

/**
 * A test case for CAY-698 bug.
 */
@Test
public void testNullifyToOne() throws Exception {
    ClientMtTable1 a = clientContext.newObject(ClientMtTable1.class);
    ClientMtTable2 b = clientContext.newObject(ClientMtTable2.class);
    a.addToTable2Array(b);
    clientContext.commitChanges();
    final ObjectContext child = runtime.newContext(clientContext);
    ObjectContext childPeer = runtime.newContext(clientContext);
    final ClientMtTable2 childP1 = (ClientMtTable2) Cayenne.objectForPK(child, b.getObjectId());
    // trigger object creation in the peer nested DC
    Cayenne.objectForPK(childPeer, b.getObjectId());
    childP1.setTable1(null);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            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() {

                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) {
                }

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

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

                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) GraphDiff(org.apache.cayenne.graph.GraphDiff) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Example 28 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class NestedCayenneContextIT method testAddRemove.

@Test
public void testAddRemove() throws Exception {
    ObjectContext child = runtime.newContext(clientContext);
    ClientMtTable1 a = child.newObject(ClientMtTable1.class);
    a.setGlobalAttribute1("X");
    child.commitChanges();
    ClientMtTable2 p1 = child.newObject(ClientMtTable2.class);
    p1.setGlobalAttribute("P1");
    a.addToTable2Array(p1);
    ClientMtTable2 p2 = child.newObject(ClientMtTable2.class);
    p2.setGlobalAttribute("P2");
    a.addToTable2Array(p2);
    a.removeFromTable2Array(p2);
    // this causes an error on commit
    child.deleteObjects(p2);
    child.commitChangesToParent();
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Test(org.junit.Test)

Example 29 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class NestedCayenneContextIT method testPrefetchingToMany.

@Test
public void testPrefetchingToMany() throws Exception {
    ClientMtTable1 mt11 = clientContext.newObject(ClientMtTable1.class);
    mt11.setGlobalAttribute1("1");
    ClientMtTable1 mt12 = clientContext.newObject(ClientMtTable1.class);
    mt12.setGlobalAttribute1("2");
    ClientMtTable2 mt21 = clientContext.newObject(ClientMtTable2.class);
    ClientMtTable2 mt22 = clientContext.newObject(ClientMtTable2.class);
    mt21.setTable1(mt11);
    mt22.setTable1(mt11);
    clientContext.commitChanges();
    final ObjectContext child = runtime.newContext(clientContext);
    SelectQuery<ClientMtTable1> q = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    q.addOrdering("globalAttribute1", SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable1.TABLE2ARRAY_PROPERTY);
    final List<ClientMtTable1> results = child.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            ClientMtTable1 o1 = results.get(0);
            assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
            assertSame(child, o1.getObjectContext());
            List<ClientMtTable2> children1 = o1.getTable2Array();
            assertEquals(2, children1.size());
            Iterator<ClientMtTable2> it = children1.iterator();
            while (it.hasNext()) {
                ClientMtTable2 o = it.next();
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(child, o.getObjectContext());
                assertEquals(o1, o.getTable1());
            }
            ClientMtTable1 o2 = results.get(1);
            assertEquals(PersistenceState.COMMITTED, o2.getPersistenceState());
            assertSame(child, o2.getObjectContext());
            List<?> children2 = o2.getTable2Array();
            assertEquals(0, children2.size());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Iterator(java.util.Iterator) List(java.util.List) Test(org.junit.Test)

Example 30 with ClientMtTable1

use of org.apache.cayenne.testdo.mt.ClientMtTable1 in project cayenne by apache.

the class PersistentObjectInContextIT method testResolveToOneReverseResolved.

@Test
public void testResolveToOneReverseResolved() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    ObjectId gid = new ObjectId("MtTable2", MtTable2.TABLE2_ID_PK_COLUMN, new Integer(1));
    ClientMtTable2 mtTable21 = (ClientMtTable2) Cayenne.objectForQuery(context, new ObjectIdQuery(gid));
    assertNotNull(mtTable21);
    ClientMtTable1 mtTable1 = mtTable21.getTable1();
    assertNotNull("To one relationship incorrectly resolved to null", mtTable1);
    List<ClientMtTable2> list = mtTable1.getTable2Array();
    assertNotNull(list);
    assertTrue(list instanceof ValueHolder);
    assertTrue(((ValueHolder) list).isFault());
    // resolve it here...
    assertEquals(2, list.size());
    for (ClientMtTable2 t2 : list) {
        PersistentObjectHolder holder = (PersistentObjectHolder) t2.getTable1Direct();
        assertFalse(holder.isFault());
        assertSame(mtTable1, holder.getValue());
    }
    assertEquals("g1", mtTable1.getGlobalAttribute1());
}
Also used : PersistentObjectHolder(org.apache.cayenne.util.PersistentObjectHolder) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Aggregations

ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)112 Test (org.junit.Test)112 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)46 SelectQuery (org.apache.cayenne.query.SelectQuery)26 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)19 ObjectContext (org.apache.cayenne.ObjectContext)12 Expression (org.apache.cayenne.exp.Expression)12 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)11 List (java.util.List)8 ObjectId (org.apache.cayenne.ObjectId)7 EntityResolver (org.apache.cayenne.map.EntityResolver)7 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)7 RemoteIncrementalFaultList (org.apache.cayenne.remote.RemoteIncrementalFaultList)6 GraphDiff (org.apache.cayenne.graph.GraphDiff)5 MockEventManager (org.apache.cayenne.event.MockEventManager)4 ClientChannelServerDiffsListener1 (org.apache.cayenne.remote.ClientChannelServerDiffsListener1)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)3 MtTable1 (org.apache.cayenne.testdo.mt.MtTable1)3