Search in sources :

Example 6 with ChangeMap

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

the class CommitLogFilter_FilteredIT method testPostCommit_IgnoreAttributes.

@Test
public void testPostCommit_IgnoreAttributes() throws SQLException {
    auditable3.insert(1, "31", "32");
    final Auditable3 a3 = SelectById.query(Auditable3.class, 1).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];
            assertNull(changes.getChanges().get(new ObjectId("Auditable3", Auditable3.ID_PK_COLUMN, 1)));
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    a3.setCharProperty1("33");
    a3.setCharProperty2("34");
    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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Auditable3(org.apache.cayenne.commitlog.db.Auditable3) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 7 with ChangeMap

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

the class CommitLogFilter_FilteredIT method testPostCommit_Delete.

@Test
public void testPostCommit_Delete() 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.DELETE, c.getType());
            assertEquals(1, c.getAttributeChanges().size());
            assertEquals(Confidential.getInstance(), c.getAttributeChanges().get(Auditable2.CHAR_PROPERTY2.getName()).getOldValue());
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    Auditable2 a1 = SelectById.query(Auditable2.class, 1).selectOne(context);
    context.deleteObject(a1);
    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) Auditable2(org.apache.cayenne.commitlog.db.Auditable2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 8 with ChangeMap

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

the class CommitLogFilter_FilteredIT method testPostCommit_IgnoreToOne.

@Test
public void testPostCommit_IgnoreToOne() throws SQLException {
    auditable3.insert(1, "31", "32");
    auditable3.insert(2, "33", "34");
    auditable4.insert(11, "41", "41", 1);
    final Auditable3 a32 = SelectById.query(Auditable3.class, 2).selectOne(context);
    final Auditable4 a4 = SelectById.query(Auditable4.class, 11).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];
            assertNull(changes.getChanges().get(new ObjectId("Auditable4", Auditable4.ID_PK_COLUMN, 11)));
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    a4.setAuditable3(a32);
    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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Auditable4(org.apache.cayenne.commitlog.db.Auditable4) Auditable3(org.apache.cayenne.commitlog.db.Auditable3) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 9 with ChangeMap

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

the class CommitLogFilter_AllIT method testPostCommit_Delete.

@Test
public void testPostCommit_Delete() throws SQLException {
    auditable1.insert(1, "xx");
    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.DELETE, c.getType());
            assertEquals(1, c.getAttributeChanges().size());
            assertEquals("xx", c.getAttributeChanges().get(Auditable1.CHAR_PROPERTY1.getName()).getOldValue());
            assertNull(c.getAttributeChanges().get(Auditable1.CHAR_PROPERTY1.getName()).getNewValue());
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    Auditable1 a1 = SelectById.query(Auditable1.class, 1).selectOne(context);
    context.deleteObject(a1);
    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) ObjectContext(org.apache.cayenne.ObjectContext) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) Test(org.junit.Test)

Example 10 with ChangeMap

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

the class CommitLogFilter_AllIT method testPostCommit_UpdateToMany.

@Test
public void testPostCommit_UpdateToMany() throws SQLException {
    auditable1.insert(1, "xx");
    auditableChild1.insert(1, 1, "cc1");
    auditableChild1.insert(2, null, "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);
    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 a1c = changes.getChanges().get(new ObjectId("Auditable1", Auditable1.ID_PK_COLUMN, 1));
            assertNotNull(a1c);
            assertEquals(ObjectChangeType.UPDATE, a1c.getType());
            assertEquals(0, a1c.getAttributeChanges().size());
            assertEquals(1, a1c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange a1c1 = a1c.getToManyRelationshipChanges().get(Auditable1.CHILDREN1.getName());
            assertNotNull(a1c1);
            assertEquals(2, a1c1.getAdded().size());
            assertTrue(a1c1.getAdded().contains(ac2.getObjectId()));
            assertTrue(a1c1.getAdded().contains(ac3.getObjectId()));
            assertEquals(1, a1c1.getRemoved().size());
            assertTrue(a1c1.getRemoved().contains(ac1.getObjectId()));
            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) ToManyRelationshipChange(org.apache.cayenne.commitlog.model.ToManyRelationshipChange) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)14 ObjectContext (org.apache.cayenne.ObjectContext)13 Test (org.junit.Test)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 ObjectId (org.apache.cayenne.ObjectId)12 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)11 Auditable1 (org.apache.cayenne.commitlog.db.Auditable1)6 Auditable2 (org.apache.cayenne.commitlog.db.Auditable2)3 Auditable3 (org.apache.cayenne.commitlog.db.Auditable3)3 AuditableChild1 (org.apache.cayenne.commitlog.db.AuditableChild1)3 ToManyRelationshipChange (org.apache.cayenne.commitlog.model.ToManyRelationshipChange)3 Auditable4 (org.apache.cayenne.commitlog.db.Auditable4)2 AttributeChange (org.apache.cayenne.commitlog.model.AttributeChange)2 ArrayList (java.util.ArrayList)1 E3 (org.apache.cayenne.commitlog.db.E3)1 E4 (org.apache.cayenne.commitlog.db.E4)1 ToOneRelationshipChange (org.apache.cayenne.commitlog.model.ToOneRelationshipChange)1