Search in sources :

Example 26 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class DiffProcessor method arcDeleted.

@Override
public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
    ObjectId id = (ObjectId) nodeId;
    String relationshipName = arcId.toString();
    ObjEntity entity = entityResolver.getObjEntity(id.getEntityName());
    ObjRelationship relationship = entity.getRelationship(relationshipName);
    MutableObjectChange c = changeSet.getOrCreate(id, ObjectChangeType.UPDATE);
    ObjectId tid = (ObjectId) targetNodeId;
    if (relationship.isToMany()) {
        c.toManyRelationshipDisconnected(relationshipName, tid);
    } else {
        c.toOneRelationshipDisconnected(relationshipName, tid);
    }
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectId(org.apache.cayenne.ObjectId)

Example 27 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class CommitLogFilter_AllIT method testPostCommit_Insert.

@Test
public void testPostCommit_Insert() throws SQLException {
    final Auditable1 a1 = context.newObject(Auditable1.class);
    a1.setCharProperty1("yy");
    final ObjectId preCommitId = a1.getObjectId();
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertSame(context, invocation.getArguments()[0]);
            ChangeMap changes = (ChangeMap) invocation.getArguments()[1];
            assertNotNull(changes);
            assertEquals(2, changes.getChanges().size());
            assertEquals(1, changes.getUniqueChanges().size());
            ObjectChange c = changes.getUniqueChanges().iterator().next();
            assertNotNull(c);
            assertEquals(ObjectChangeType.INSERT, c.getType());
            assertEquals(1, c.getAttributeChanges().size());
            assertEquals("yy", c.getAttributeChanges().get(Auditable1.CHAR_PROPERTY1.getName()).getNewValue());
            assertNotEquals(preCommitId, a1.getObjectId());
            assertEquals(preCommitId, c.getPreCommitId());
            assertEquals(a1.getObjectId(), c.getPostCommitId());
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    context.commitChanges();
    verify(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
}
Also used : ChangeMap(org.apache.cayenne.commitlog.model.ChangeMap) ObjectId(org.apache.cayenne.ObjectId) ObjectChange(org.apache.cayenne.commitlog.model.ObjectChange) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 28 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class CommitLogFilter_AllIT method testPostCommit_UpdateToOne.

@Test
public void testPostCommit_UpdateToOne() throws SQLException {
    auditable1.insert(1, "xx");
    auditable1.insert(2, "yy");
    auditableChild1.insert(1, 1, "cc1");
    auditableChild1.insert(2, 2, "cc2");
    auditableChild1.insert(3, null, "cc3");
    final AuditableChild1 ac1 = SelectById.query(AuditableChild1.class, 1).selectOne(context);
    final AuditableChild1 ac2 = SelectById.query(AuditableChild1.class, 2).selectOne(context);
    final AuditableChild1 ac3 = SelectById.query(AuditableChild1.class, 3).selectOne(context);
    final Auditable1 a1 = SelectById.query(Auditable1.class, 1).selectOne(context);
    final Auditable1 a2 = SelectById.query(Auditable1.class, 2).selectOne(context);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertSame(context, invocation.getArguments()[0]);
            ChangeMap changes = (ChangeMap) invocation.getArguments()[1];
            assertNotNull(changes);
            assertEquals(4, changes.getUniqueChanges().size());
            ObjectChange ac1c = changes.getChanges().get(new ObjectId("AuditableChild1", AuditableChild1.ID_PK_COLUMN, 1));
            assertNotNull(ac1c);
            assertEquals(ObjectChangeType.UPDATE, ac1c.getType());
            ToOneRelationshipChange ac1c1 = ac1c.getToOneRelationshipChanges().get(AuditableChild1.PARENT.getName());
            assertEquals(a1.getObjectId(), ac1c1.getOldValue());
            assertEquals(null, ac1c1.getNewValue());
            ObjectChange ac2c = changes.getChanges().get(new ObjectId("AuditableChild1", AuditableChild1.ID_PK_COLUMN, 2));
            assertNotNull(ac2c);
            assertEquals(ObjectChangeType.UPDATE, ac2c.getType());
            ToOneRelationshipChange ac2c1 = ac2c.getToOneRelationshipChanges().get(AuditableChild1.PARENT.getName());
            assertEquals(a2.getObjectId(), ac2c1.getOldValue());
            assertEquals(a1.getObjectId(), ac2c1.getNewValue());
            ObjectChange ac3c = changes.getChanges().get(new ObjectId("AuditableChild1", AuditableChild1.ID_PK_COLUMN, 3));
            assertNotNull(ac3c);
            assertEquals(ObjectChangeType.UPDATE, ac3c.getType());
            ToOneRelationshipChange ac3c1 = ac3c.getToOneRelationshipChanges().get(AuditableChild1.PARENT.getName());
            assertEquals(null, ac3c1.getOldValue());
            assertEquals(a1.getObjectId(), ac3c1.getNewValue());
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    a1.removeFromChildren1(ac1);
    a1.addToChildren1(ac2);
    a1.addToChildren1(ac3);
    context.commitChanges();
    verify(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
}
Also used : AuditableChild1(org.apache.cayenne.commitlog.db.AuditableChild1) ChangeMap(org.apache.cayenne.commitlog.model.ChangeMap) ObjectChange(org.apache.cayenne.commitlog.model.ObjectChange) ObjectId(org.apache.cayenne.ObjectId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ToOneRelationshipChange(org.apache.cayenne.commitlog.model.ToOneRelationshipChange) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 29 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class CommitLogFilter_AllIT method testPostCommit_Update.

@Test
public void testPostCommit_Update() throws SQLException {
    auditable1.insert(1, "xx");
    final Auditable1 a1 = SelectById.query(Auditable1.class, 1).selectOne(context);
    a1.setCharProperty1("yy");
    final ObjectId preCommitId = a1.getObjectId();
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertSame(context, invocation.getArguments()[0]);
            ChangeMap changes = (ChangeMap) invocation.getArguments()[1];
            assertNotNull(changes);
            assertEquals(1, changes.getUniqueChanges().size());
            ObjectChange c = changes.getChanges().get(new ObjectId("Auditable1", Auditable1.ID_PK_COLUMN, 1));
            assertNotNull(c);
            assertEquals(ObjectChangeType.UPDATE, c.getType());
            assertEquals(1, c.getAttributeChanges().size());
            AttributeChange pc = c.getAttributeChanges().get(Auditable1.CHAR_PROPERTY1.getName());
            assertNotNull(pc);
            assertEquals("xx", pc.getOldValue());
            assertEquals("yy", pc.getNewValue());
            assertEquals(preCommitId, a1.getObjectId());
            assertEquals(preCommitId, c.getPreCommitId());
            assertEquals(preCommitId, c.getPostCommitId());
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    context.commitChanges();
    verify(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
}
Also used : ChangeMap(org.apache.cayenne.commitlog.model.ChangeMap) ObjectId(org.apache.cayenne.ObjectId) ObjectChange(org.apache.cayenne.commitlog.model.ObjectChange) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AttributeChange(org.apache.cayenne.commitlog.model.AttributeChange) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 30 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class CommitLogFilter_All_FlattenedIT method testPostCommit_UpdateToMany.

@Test
public void testPostCommit_UpdateToMany() throws SQLException {
    e3.insert(1);
    e4.insert(11);
    e4.insert(12);
    e34.insert(1, 11);
    final E3 e3 = SelectById.query(E3.class, 1).selectOne(context);
    final E4 e4_1 = SelectById.query(E4.class, 11).selectOne(context);
    final E4 e4_2 = SelectById.query(E4.class, 12).selectOne(context);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertSame(context, invocation.getArguments()[0]);
            ChangeMap changes = (ChangeMap) invocation.getArguments()[1];
            assertNotNull(changes);
            assertEquals(3, changes.getUniqueChanges().size());
            ObjectChange e3c = changes.getChanges().get(new ObjectId("E3", E3.ID_PK_COLUMN, 1));
            assertNotNull(e3c);
            assertEquals(ObjectChangeType.UPDATE, e3c.getType());
            assertEquals(0, e3c.getAttributeChanges().size());
            assertEquals(1, e3c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange e3c1 = e3c.getToManyRelationshipChanges().get(E3.E4S.getName());
            assertNotNull(e3c1);
            assertEquals(1, e3c1.getAdded().size());
            assertTrue(e3c1.getAdded().contains(e4_2.getObjectId()));
            assertEquals(1, e3c1.getRemoved().size());
            assertTrue(e3c1.getRemoved().contains(e4_1.getObjectId()));
            ObjectChange e41c = changes.getChanges().get(new ObjectId("E4", E4.ID_PK_COLUMN, 11));
            assertNotNull(e41c);
            assertEquals(ObjectChangeType.UPDATE, e41c.getType());
            assertEquals(0, e41c.getAttributeChanges().size());
            assertEquals(1, e41c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange e41c1 = e41c.getToManyRelationshipChanges().get(E4.E3S.getName());
            assertNotNull(e41c);
            assertEquals(0, e41c1.getAdded().size());
            assertEquals(1, e41c1.getRemoved().size());
            assertTrue(e41c1.getRemoved().contains(e3.getObjectId()));
            ObjectChange e42c = changes.getChanges().get(new ObjectId("E4", E4.ID_PK_COLUMN, 12));
            assertNotNull(e42c);
            assertEquals(ObjectChangeType.UPDATE, e42c.getType());
            assertEquals(0, e42c.getAttributeChanges().size());
            assertEquals(1, e42c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange e42c1 = e42c.getToManyRelationshipChanges().get(E4.E3S.getName());
            assertNotNull(e42c);
            assertEquals(0, e42c1.getRemoved().size());
            assertEquals(1, e42c1.getAdded().size());
            assertTrue(e42c1.getAdded().contains(e3.getObjectId()));
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    e3.removeFromE4s(e4_1);
    e3.addToE4s(e4_2);
    context.commitChanges();
    verify(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
}
Also used : ChangeMap(org.apache.cayenne.commitlog.model.ChangeMap) ObjectChange(org.apache.cayenne.commitlog.model.ObjectChange) ObjectId(org.apache.cayenne.ObjectId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ToManyRelationshipChange(org.apache.cayenne.commitlog.model.ToManyRelationshipChange) ObjectContext(org.apache.cayenne.ObjectContext) E3(org.apache.cayenne.commitlog.db.E3) E4(org.apache.cayenne.commitlog.db.E4) Test(org.junit.Test)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)156 Test (org.junit.Test)104 Persistent (org.apache.cayenne.Persistent)38 DataObject (org.apache.cayenne.DataObject)20 DataRow (org.apache.cayenne.DataRow)20 ObjectContext (org.apache.cayenne.ObjectContext)20 ObjEntity (org.apache.cayenne.map.ObjEntity)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)18 HashMap (java.util.HashMap)17 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)16 ArrayList (java.util.ArrayList)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)14 Artist (org.apache.cayenne.testdo.testmap.Artist)14 Map (java.util.Map)13 EntityResolver (org.apache.cayenne.map.EntityResolver)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)10 SelectQuery (org.apache.cayenne.query.SelectQuery)10 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)9