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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations