Search in sources :

Example 1 with AttributeChange

use of org.apache.cayenne.commitlog.model.AttributeChange 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 2 with AttributeChange

use of org.apache.cayenne.commitlog.model.AttributeChange in project cayenne by apache.

the class CommitLogFilter_ListenerInducedChangesIT method testPostCommit_Update.

@Test
public void testPostCommit_Update() throws SQLException {
    auditable1.insert(1, "yy");
    auditableChild1.insert(31, 1, "yyc");
    final UpdateListener listener = new UpdateListener();
    runtime.getDataDomain().addListener(listener);
    final Auditable1 a1 = SelectById.query(Auditable1.class, 1).prefetch(Auditable1.CHILDREN1.joint()).selectFirst(context);
    a1.setCharProperty1("zz");
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertNotNull(listener.toUpdate);
            assertEquals(1, listener.toUpdate.size());
            List<ObjectChange> sortedChanges = sortedChanges(invocation);
            assertEquals(2, sortedChanges.size());
            assertEquals(ObjectChangeType.UPDATE, sortedChanges.get(0).getType());
            assertEquals(a1.getObjectId(), sortedChanges.get(0).getPostCommitId());
            assertEquals(ObjectChangeType.UPDATE, sortedChanges.get(1).getType());
            assertEquals(listener.toUpdate.get(0).getObjectId(), sortedChanges.get(1).getPostCommitId());
            AttributeChange listenerInducedChange = sortedChanges.get(1).getAttributeChanges().get(AuditableChild1.CHAR_PROPERTY1.getName());
            assertNotNull(listenerInducedChange);
            assertEquals("yyc", listenerInducedChange.getOldValue());
            assertEquals("yyc_", listenerInducedChange.getNewValue());
            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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AttributeChange(org.apache.cayenne.commitlog.model.AttributeChange) ArrayList(java.util.ArrayList) List(java.util.List) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 3 with AttributeChange

use of org.apache.cayenne.commitlog.model.AttributeChange in project cayenne by apache.

the class CommitLogFilter_ListenerInducedChangesIT method testPostCommit_Insert.

@Test
public void testPostCommit_Insert() throws SQLException {
    final InsertListener listener = new InsertListener();
    runtime.getDataDomain().addListener(listener);
    final Auditable1 a1 = context.newObject(Auditable1.class);
    a1.setCharProperty1("yy");
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertNotNull(listener.c);
            List<ObjectChange> sortedChanges = sortedChanges(invocation);
            assertEquals(2, sortedChanges.size());
            assertEquals(a1.getObjectId(), sortedChanges.get(0).getPostCommitId());
            assertEquals(ObjectChangeType.INSERT, sortedChanges.get(0).getType());
            assertEquals(listener.c.getObjectId(), sortedChanges.get(1).getPostCommitId());
            assertEquals(ObjectChangeType.INSERT, sortedChanges.get(1).getType());
            AttributeChange listenerInducedChange = sortedChanges.get(1).getAttributeChanges().get(AuditableChild1.CHAR_PROPERTY1.getName());
            assertNotNull(listenerInducedChange);
            assertEquals("c1", listenerInducedChange.getNewValue());
            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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AttributeChange(org.apache.cayenne.commitlog.model.AttributeChange) ArrayList(java.util.ArrayList) List(java.util.List) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 4 with AttributeChange

use of org.apache.cayenne.commitlog.model.AttributeChange in project cayenne by apache.

the class CommitLogFilter_FilteredIT method testPostCommit_Update.

@Test
public void testPostCommit_Update() throws SQLException {
    auditable2.insert(1, "P1_1", "P2_1");
    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("Auditable2", Auditable2.ID_PK_COLUMN, 1));
            assertNotNull(c);
            assertEquals(ObjectChangeType.UPDATE, c.getType());
            assertEquals(1, c.getAttributeChanges().size());
            AttributeChange pc = c.getAttributeChanges().get(Auditable2.CHAR_PROPERTY2.getName());
            assertNotNull(pc);
            assertEquals(Confidential.getInstance(), pc.getOldValue());
            assertEquals(Confidential.getInstance(), pc.getNewValue());
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    Auditable2 a1 = SelectById.query(Auditable2.class, 1).selectOne(context);
    a1.setCharProperty1("P1_2");
    a1.setCharProperty2("P2_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) AttributeChange(org.apache.cayenne.commitlog.model.AttributeChange) Auditable2(org.apache.cayenne.commitlog.db.Auditable2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 5 with AttributeChange

use of org.apache.cayenne.commitlog.model.AttributeChange in project cayenne by apache.

the class CommitLogFilter_ListenerInducedChangesIT method testPostCommit_Delete.

@Test
public void testPostCommit_Delete() throws SQLException {
    auditable1.insert(1, "yy");
    auditableChild1.insert(31, 1, "yyc");
    final DeleteListener listener = new DeleteListener();
    runtime.getDataDomain().addListener(listener);
    final Auditable1 a1 = SelectById.query(Auditable1.class, 1).prefetch(Auditable1.CHILDREN1.joint()).selectFirst(context);
    a1.setCharProperty1("zz");
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertNotNull(listener.toDelete);
            assertEquals(1, listener.toDelete.size());
            List<ObjectChange> sortedChanges = sortedChanges(invocation);
            assertEquals(2, sortedChanges.size());
            assertEquals(ObjectChangeType.UPDATE, sortedChanges.get(0).getType());
            assertEquals(a1.getObjectId(), sortedChanges.get(0).getPostCommitId());
            assertEquals(ObjectChangeType.DELETE, sortedChanges.get(1).getType());
            assertEquals(listener.toDelete.get(0).getObjectId(), sortedChanges.get(1).getPostCommitId());
            AttributeChange listenerInducedChange = sortedChanges.get(1).getAttributeChanges().get(AuditableChild1.CHAR_PROPERTY1.getName());
            assertNotNull(listenerInducedChange);
            assertEquals("yyc", listenerInducedChange.getOldValue());
            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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AttributeChange(org.apache.cayenne.commitlog.model.AttributeChange) ArrayList(java.util.ArrayList) List(java.util.List) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)5 AttributeChange (org.apache.cayenne.commitlog.model.AttributeChange)5 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)5 Test (org.junit.Test)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Auditable1 (org.apache.cayenne.commitlog.db.Auditable1)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ObjectId (org.apache.cayenne.ObjectId)2 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)2 Auditable2 (org.apache.cayenne.commitlog.db.Auditable2)1