Search in sources :

Example 6 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPrefetchingToOneNull.

@Test
public void testPrefetchingToOneNull() throws Exception {
    tMtTable2.insert(15, null, "g3");
    SelectQuery q = new SelectQuery(ClientMtTable2.class);
    q.addPrefetch(ClientMtTable2.TABLE1_PROPERTY);
    final List<ClientMtTable2> results = clientContext.performQuery(q);
    clientServerInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            ClientMtTable2 o = results.get(0);
            assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
            assertSame(clientContext, o.getObjectContext());
            assertNull(o.getTable1());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 7 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure 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 8 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure 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 9 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure 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 10 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure 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)

Aggregations

UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)107 Test (org.junit.Test)107 SelectQuery (org.apache.cayenne.query.SelectQuery)64 List (java.util.List)48 Artist (org.apache.cayenne.testdo.testmap.Artist)47 Painting (org.apache.cayenne.testdo.testmap.Painting)35 ValueHolder (org.apache.cayenne.ValueHolder)23 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)19 ArrayList (java.util.ArrayList)15 ObjectContext (org.apache.cayenne.ObjectContext)14 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)9 ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)8 Iterator (java.util.Iterator)7 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)7 Expression (org.apache.cayenne.exp.Expression)6 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)6 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)6 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)5 RemoteIncrementalFaultList (org.apache.cayenne.remote.RemoteIncrementalFaultList)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)5