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