Search in sources :

Example 21 with UnitTestClosure

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

the class NestedDataContextWriteIT method testCommitChangesToParent.

@Test
public void testCommitChangesToParent() throws Exception {
    createArtistsDataSet();
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    // make sure we fetch in predictable order
    SelectQuery query = new SelectQuery(Artist.class);
    query.addOrdering(Artist.ARTIST_NAME.asc());
    List<?> objects = childContext.performQuery(query);
    assertEquals(4, objects.size());
    final Artist childNew = childContext.newObject(Artist.class);
    childNew.setArtistName("NNN");
    final Artist childModified = (Artist) objects.get(0);
    childModified.setArtistName("MMM");
    final Artist childCommitted = (Artist) objects.get(1);
    final Artist childHollow = (Artist) objects.get(3);
    childContext.invalidateObjects(childHollow);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.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());
            Artist parentNew = (Artist) context.getGraphManager().getNode(childNew.getObjectId());
            Artist parentModified = (Artist) context.getGraphManager().getNode(childModified.getObjectId());
            Artist parentCommitted = (Artist) context.getGraphManager().getNode(childCommitted.getObjectId());
            Artist parentHollow = (Artist) context.getGraphManager().getNode(childHollow.getObjectId());
            assertNotNull(parentNew);
            assertEquals(PersistenceState.NEW, parentNew.getPersistenceState());
            assertEquals("NNN", parentNew.getArtistName());
            assertNotNull(parentModified);
            assertEquals(PersistenceState.MODIFIED, parentModified.getPersistenceState());
            assertEquals("MMM", parentModified.getArtistName());
            assertNotNull(context.getObjectStore().getChangesByObjectId().get(parentModified.getObjectId()));
            assertNotNull(parentCommitted);
            assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
            assertNotNull(parentHollow);
        // TODO: we can assert that when we figure out how nested "invalidate"
        // should
        // work
        // assertEquals(PersistenceState.HOLLOW,
        // parentHollow.getPersistenceState());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 22 with UnitTestClosure

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

the class NestedDataContextWriteIT method testCommitChangesToParentPropagatedKey.

@Test
public void testCommitChangesToParentPropagatedKey() throws Exception {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Painting childMaster = childContext.newObject(Painting.class);
    childMaster.setPaintingTitle("Master");
    final PaintingInfo childDetail1 = childContext.newObject(PaintingInfo.class);
    childDetail1.setTextReview("Detail1");
    childDetail1.setPainting(childMaster);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childMaster.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childDetail1.getPersistenceState());
            Painting parentMaster = (Painting) context.getGraphManager().getNode(childMaster.getObjectId());
            assertNotNull(parentMaster);
            assertEquals(PersistenceState.NEW, parentMaster.getPersistenceState());
            PaintingInfo parentDetail1 = (PaintingInfo) context.getGraphManager().getNode(childDetail1.getObjectId());
            assertNotNull(parentDetail1);
            assertEquals(PersistenceState.NEW, parentDetail1.getPersistenceState());
            assertSame(parentMaster, parentDetail1.getPainting());
            assertSame(parentDetail1, parentMaster.getToPaintingInfo());
        }
    });
}
Also used : UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 23 with UnitTestClosure

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

the class NestedDataContextWriteIT method testCommitChangesToParentFlattened.

@Test
public void testCommitChangesToParentFlattened() throws Exception {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Artist childO1 = childContext.newObject(Artist.class);
    childO1.setArtistName("Master");
    final ArtGroup childO2 = childContext.newObject(ArtGroup.class);
    childO2.setName("Detail1");
    childO2.addToArtistArray(childO1);
    ObjEntity ent = childContext.getEntityResolver().getObjEntity("ArtGroup");
    Collection<ObjRelationship> rels = ent.getDeclaredRelationships();
    for (ObjRelationship rel : rels) {
        System.out.println(rel.getName());
    }
    assertEquals(1, childO1.getGroupArray().size());
    assertEquals(1, childO2.getArtistArray().size());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
            Artist parentO1 = (Artist) context.getGraphManager().getNode(childO1.getObjectId());
            assertNotNull(parentO1);
            assertEquals(PersistenceState.NEW, parentO1.getPersistenceState());
            ArtGroup parentO2 = (ArtGroup) context.getGraphManager().getNode(childO2.getObjectId());
            assertNotNull(parentO2);
            assertEquals(PersistenceState.NEW, parentO2.getPersistenceState());
            assertEquals(1, parentO1.getGroupArray().size());
            assertEquals(1, parentO2.getArtistArray().size());
            assertTrue(parentO2.getArtistArray().contains(parentO1));
            assertTrue(parentO1.getGroupArray().contains(parentO2));
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 24 with UnitTestClosure

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

the class NestedDataContextWriteIT method testNullifyToOne.

/**
 * A test case for CAY-698 bug.
 */
@Test
public void testNullifyToOne() throws Exception {
    createNullifyToOneDataSet();
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    ObjectContext childContextPeer = runtime.newContext(context);
    final Painting childP1 = Cayenne.objectForPK(childContext, Painting.class, 33001);
    // trigger object creation in the peer nested DC
    Cayenne.objectForPK(childContextPeer, Painting.class, 33001);
    childP1.setToArtist(null);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childP1.getPersistenceState());
            Painting parentP1 = (Painting) context.getGraphManager().getNode(childP1.getObjectId());
            assertNotNull(parentP1);
            assertEquals(PersistenceState.MODIFIED, parentP1.getPersistenceState());
            assertNull(parentP1.getToArtist());
        }
    });
}
Also used : UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 25 with UnitTestClosure

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

the class DataContextRelationshipQuery_PolymorphicIT method testPolymorphicSharedCache.

@Test
public void testPolymorphicSharedCache() throws SQLException {
    // see CAY-2101... we are trying to get a snapshot from a new object in the shared cache, and then read this
    // object via a relationship, so that shared cache is consulted
    Employee e = context1.newObject(Employee.class);
    e.setName("E1");
    e.setSalary(1234.01f);
    PersonNotes n = context1.newObject(PersonNotes.class);
    n.setNotes("N1");
    n.setPerson(e);
    context1.commitChanges();
    // use different context to ensure we hit shared cache for relationship resolving
    final PersonNotes nPeer = Cayenne.objectForPK(context2, PersonNotes.class, Cayenne.intPKForObject(n));
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            assertTrue(nPeer.getPerson() instanceof Employee);
        }
    });
}
Also used : Employee(org.apache.cayenne.testdo.inheritance_people.Employee) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) 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