Search in sources :

Example 16 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class CayenneContextServerDiffsIT method testReturnDiffInPreUpdate.

@Test
public void testReturnDiffInPreUpdate() {
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.PRE_UPDATE, MtTable1.class, new ClientChannelServerDiffsListener1(), "preUpdate");
        ClientMtTable1 o = context.newObject(ClientMtTable1.class);
        o.setServerAttribute1("YY");
        context.commitChanges();
        assertNull(o.getGlobalAttribute1());
        o.setServerAttribute1("XX");
        context.commitChanges();
        assertFalse(o.getObjectId().isTemporary());
        assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
        assertEquals("111", o.getGlobalAttribute1());
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ClientChannelServerDiffsListener1(org.apache.cayenne.remote.ClientChannelServerDiffsListener1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 17 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class CayenneContextServerDiffsIT method testReturnDiffClientArcChanges.

@Test
public void testReturnDiffClientArcChanges() {
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
        ClientMtTable1 o = context.newObject(ClientMtTable1.class);
        ClientMtTable2 o1 = context.newObject(ClientMtTable2.class);
        o.addToTable2Array(o1);
        context.commitChanges();
        assertFalse(o.getObjectId().isTemporary());
        assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
        assertEquals("XXX", o.getGlobalAttribute1());
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ClientChannelServerDiffsListener1(org.apache.cayenne.remote.ClientChannelServerDiffsListener1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 18 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class DataDomainCallbacksIT method testPostLoad_ThatModifiesObject.

@Test
public void testPostLoad_ThatModifiesObject() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    registry.addCallback(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
    MockCallingBackListener listener = new MockCallingBackListener() {

        @Override
        public void publicCallback(Object entity) {
            super.publicCallback(entity);
            Artist a = (Artist) entity;
            // modify object during postLoad callback
            a.setArtistName("Changed");
        }
    };
    registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, listener, "publicCallback");
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    Painting p1 = context.newObject(Painting.class);
    p1.setToArtist(a1);
    p1.setPaintingTitle("XXX");
    context.commitChanges();
    context.invalidateObjects(a1, p1);
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    p1 = q.select(context1).get(0);
    // this should be a hollow object, so no callback just yet
    a1 = p1.getToArtist();
    assertEquals(PersistenceState.HOLLOW, a1.getPersistenceState());
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    a1.getArtistName();
    assertTrue(a1.getPostLoaded() > 0);
    assertSame(a1, listener.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 19 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class DataDomainCallbacksIT method testPostLoad.

@Test
public void testPostLoad() throws Exception {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    registry.addCallback(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
    MockCallingBackListener listener = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, listener, "publicCallback");
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    SelectQuery q = new SelectQuery(Artist.class);
    context.performQuery(q);
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
    a1.resetCallbackFlags();
    listener.reset();
    // post load must be called on rollback...
    a1.resetCallbackFlags();
    listener.reset();
    context.rollbackChanges();
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    // now change and rollback the artist - postLoad must be called
    a1.setArtistName("YY");
    context.rollbackChanges();
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
    // test invalidated
    a1.resetCallbackFlags();
    listener.reset();
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    context.performQuery(new RefreshQuery(a1));
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    a1.getArtistName();
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 20 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class DataDomainCallbacksIT method testPostRemove_InsertedUpdatedDeleted.

@Test
public void testPostRemove_InsertedUpdatedDeleted() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    MockCallingBackListener listener0 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_PERSIST, Artist.class, listener0, "publicCallback");
    MockCallingBackListener listener1 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_REMOVE, Artist.class, listener1, "publicCallback");
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_UPDATE, Artist.class, listener2, "publicCallback");
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.deleteObjects(a1);
    context.commitChanges();
    assertNull(listener0.getPublicCalledbackEntity());
    assertNull(listener1.getPublicCalledbackEntity());
    assertNull(listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Aggregations

LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)27 Test (org.junit.Test)26 Artist (org.apache.cayenne.testdo.testmap.Artist)15 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)7 Painting (org.apache.cayenne.testdo.testmap.Painting)6 SelectQuery (org.apache.cayenne.query.SelectQuery)4 ClientChannelServerDiffsListener1 (org.apache.cayenne.remote.ClientChannelServerDiffsListener1)4 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)2 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)2 ArrayList (java.util.ArrayList)1 CayenneContext (org.apache.cayenne.CayenneContext)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 DataContext (org.apache.cayenne.access.DataContext)1 MockEventManager (org.apache.cayenne.event.MockEventManager)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 RefreshQuery (org.apache.cayenne.query.RefreshQuery)1 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)1 MtTable1 (org.apache.cayenne.testdo.mt.MtTable1)1