Search in sources :

Example 1 with Auditable1

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

use of org.apache.cayenne.commitlog.db.Auditable1 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 3 with Auditable1

use of org.apache.cayenne.commitlog.db.Auditable1 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 4 with Auditable1

use of org.apache.cayenne.commitlog.db.Auditable1 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 5 with Auditable1

use of org.apache.cayenne.commitlog.db.Auditable1 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)

Aggregations

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