Search in sources :

Example 21 with LifecycleCallbackRegistry

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

the class DataDomainCallbacksIT method testPostUpdate.

@Test
public void testPostUpdate() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    assertFalse(a1.isPostUpdated());
    a1.setArtistName("YY");
    context.commitChanges();
    assertFalse(a1.isPostUpdated());
    registry.addCallback(LifecycleEvent.POST_UPDATE, Artist.class, "postUpdateCallback");
    a1.setArtistName("ZZ");
    context.commitChanges();
    assertTrue(a1.isPostUpdated());
    a1.resetCallbackFlags();
    assertFalse(a1.isPostUpdated());
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_UPDATE, Artist.class, listener2, "publicCallback");
    a1.setArtistName("AA");
    context.commitChanges();
    assertTrue(a1.isPostUpdated());
    assertSame(a1, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 22 with LifecycleCallbackRegistry

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

the class DataDomainCallbacksIT method testPostRemove_UpdatedDeleted.

@Test
public void testPostRemove_UpdatedDeleted() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    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");
    // change before removing
    a1.setArtistName("YY");
    context.deleteObjects(a1);
    context.commitChanges();
    assertNull(listener2.getPublicCalledbackEntity());
    assertSame(a1, listener1.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 23 with LifecycleCallbackRegistry

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

the class DataDomainCallbacksIT method testPostLoad_Relationship.

@Test
public void testPostLoad_Relationship() 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");
    Painting p1 = context.newObject(Painting.class);
    p1.setToArtist(a1);
    p1.setPaintingTitle("XXX");
    context.commitChanges();
    context.invalidateObjects(a1, p1);
    SelectQuery q = new SelectQuery(Painting.class);
    p1 = (Painting) context1.performQuery(q).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();
    assertEquals(1, a1.getPostLoaded());
    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 24 with LifecycleCallbackRegistry

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

the class DataDomainCallbacksIT method testPreUpdate.

@Test
public void testPreUpdate() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    assertFalse(a1.isPreUpdated());
    a1.setArtistName("YY");
    context.commitChanges();
    assertFalse(a1.isPreUpdated());
    registry.addCallback(LifecycleEvent.PRE_UPDATE, Artist.class, "preUpdateCallback");
    a1.setArtistName("ZZ");
    context.commitChanges();
    assertTrue(a1.isPreUpdated());
    a1.resetCallbackFlags();
    assertFalse(a1.isPreUpdated());
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.PRE_UPDATE, Artist.class, listener2, "publicCallback");
    a1.setArtistName("AA");
    context.commitChanges();
    assertTrue(a1.isPreUpdated());
    assertSame(a1, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 25 with LifecycleCallbackRegistry

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

the class DataContextCallbacksIT method testPrePersistCallbacks.

@Test
public void testPrePersistCallbacks() {
    LifecycleCallbackRegistry registry = runtime.getDataDomain().getEntityResolver().getCallbackRegistry();
    // no callbacks
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("1");
    assertFalse(a1.isPrePersisted());
    context.commitChanges();
    assertFalse(a1.isPrePersisted());
    registry.addCallback(LifecycleEvent.PRE_PERSIST, Artist.class, "prePersistCallback");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("2");
    assertFalse(a2.isPrePersisted());
    context.commitChanges();
    assertTrue(a2.isPrePersisted());
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.PRE_PERSIST, Artist.class, listener2, "publicCallback");
    Artist a3 = context.newObject(Artist.class);
    a3.setArtistName("3");
    assertNull(listener2.getPublicCalledbackEntity());
    context.commitChanges();
    assertSame(a3, 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