Search in sources :

Example 11 with ClientMtTable2

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

the class CayenneContextWithDataContextIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    final ObjectId prefetchedId = new ObjectId("MtTable1", MtTable1.TABLE1_ID_PK_COLUMN, 1);
    SelectQuery q = new SelectQuery(ClientMtTable2.class);
    q.addOrdering(ClientMtTable2.GLOBAL_ATTRIBUTE_PROPERTY, SortOrder.ASCENDING);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = clientContext.performQuery(q);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, results.size());
            for (ClientMtTable2 o : results) {
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(clientContext, o.getObjectContext());
                ClientMtTable1 o1 = o.getTable1();
                assertNotNull(o1);
                assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
                assertSame(clientContext, o1.getObjectContext());
                assertEquals(prefetchedId, o1.getObjectId());
            }
        }
    });
}
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) Test(org.junit.Test)

Example 12 with ClientMtTable2

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

the class NestedCayenneContextIT method testDeleteNew.

@Test
public void testDeleteNew() throws Exception {
    ObjectContext child = runtime.newContext(clientContext);
    ClientMtTable1 a = clientContext.newObject(ClientMtTable1.class);
    clientContext.commitChanges();
    ClientMtTable2 p = child.newObject(ClientMtTable2.class);
    ClientMtTable1 aChild = (ClientMtTable1) Cayenne.objectForPK(child, a.getObjectId());
    p.setGlobalAttribute("X");
    aChild.addToTable2Array(p);
    child.commitChangesToParent();
    child.deleteObjects(p);
    aChild.removeFromTable2Array(p);
    child.commitChangesToParent();
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Test(org.junit.Test)

Example 13 with ClientMtTable2

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

the class NestedCayenneContextIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    final ClientMtTable1 mt11 = clientContext.newObject(ClientMtTable1.class);
    clientContext.newObject(ClientMtTable1.class);
    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<ClientMtTable2> q = new SelectQuery<ClientMtTable2>(ClientMtTable2.class);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = child.select(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, results.size());
            Iterator<?> it = results.iterator();
            while (it.hasNext()) {
                ClientMtTable2 o = (ClientMtTable2) it.next();
                assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
                assertSame(child, o.getObjectContext());
                ClientMtTable1 o1 = o.getTable1();
                assertNotNull(o1);
                assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
                assertSame(child, o1.getObjectContext());
                assertEquals(mt11.getObjectId(), o1.getObjectId());
            }
        }
    });
}
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) Test(org.junit.Test)

Example 14 with ClientMtTable2

use of org.apache.cayenne.testdo.mt.ClientMtTable2 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 15 with ClientMtTable2

use of org.apache.cayenne.testdo.mt.ClientMtTable2 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)

Aggregations

ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)48 Test (org.junit.Test)48 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)46 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)9 Expression (org.apache.cayenne.exp.Expression)7 SelectQuery (org.apache.cayenne.query.SelectQuery)7 ObjectContext (org.apache.cayenne.ObjectContext)6 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)6 Iterator (java.util.Iterator)3 List (java.util.List)3 GraphDiff (org.apache.cayenne.graph.GraphDiff)3 PersistentObjectHolder (org.apache.cayenne.util.PersistentObjectHolder)3 GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)2 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)2 ClientChannelServerDiffsListener1 (org.apache.cayenne.remote.ClientChannelServerDiffsListener1)2 ROPSerializationService (org.apache.cayenne.rop.ROPSerializationService)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CayenneContext (org.apache.cayenne.CayenneContext)1 ValueHolder (org.apache.cayenne.ValueHolder)1