use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.
the class ClientChannelServerDiffsIT method testReturnDiffInPrePersist.
@Test
public void testReturnDiffInPrePersist() {
final List<GenericDiff> diffs = new ArrayList<GenericDiff>();
final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler() {
@Override
public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
super.nodePropertyChanged(nodeId, property, oldValue, newValue);
diffs.add(new GenericDiff((ObjectId) nodeId, property, oldValue, newValue));
}
};
LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
try {
callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {
@Override
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
assertNotNull(serverDiff);
serverDiff.apply(diffReader);
return serverDiff;
}
};
CayenneContext context = new CayenneContext(channel);
ClientMtTable1 o = context.newObject(ClientMtTable1.class);
ObjectId tempId = o.getObjectId();
o.setServerAttribute1("YY");
context.commitChanges();
assertEquals(2, diffReader.size);
assertEquals(1, diffs.size());
assertEquals(tempId, diffs.get(0).sourceId);
assertEquals(ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, diffs.get(0).property);
assertNull(diffs.get(0).oldValue);
assertEquals("XXX", diffs.get(0).newValue);
} finally {
callbackRegistry.clear();
}
}
use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.
the class DataDomainCallbacksIT method testPostLoad_Prefetch.
@Test
public void testPostLoad_Prefetch() 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();
SelectQuery q = new SelectQuery(Painting.class);
q.addPrefetch(Painting.TO_ARTIST.disjoint());
p1 = (Painting) context1.performQuery(q).get(0);
// artist is prefetched here, and a callback must have been invoked
a1 = p1.getToArtist();
assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
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.
@Test
public void testPostRemove() {
LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
Artist a1 = context.newObject(Artist.class);
a1.setArtistName("XX");
context.commitChanges();
registry.addCallback(LifecycleEvent.POST_REMOVE, Artist.class, "postRemoveCallback");
MockCallingBackListener listener2 = new MockCallingBackListener();
registry.addListener(LifecycleEvent.POST_REMOVE, Artist.class, listener2, "publicCallback");
context.deleteObjects(a1);
context.commitChanges();
assertTrue(a1.isPostRemoved());
assertSame(a1, listener2.getPublicCalledbackEntity());
}
use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.
the class DataDomainCallbacksIT method testPostLoad_MixedResult.
@Test
public void testPostLoad_MixedResult() 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());
EJBQLQuery q = new EJBQLQuery("select a, a.artistName from Artist a");
context.performQuery(q);
assertEquals(1, a1.getPostLoaded());
assertSame(a1, listener.getPublicCalledbackEntity());
}
use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.
the class DataDomainCallbacksIT method testPostLoad_LocalObject.
@Test
public void testPostLoad_LocalObject() throws Exception {
LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
Artist a1 = context.newObject(Artist.class);
a1.setArtistName("XX");
context.commitChanges();
registry.addCallback(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
MockCallingBackListener listener = new MockCallingBackListener();
registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, listener, "publicCallback");
Artist a2 = context1.localObject(a1);
assertEquals(PersistenceState.HOLLOW, a2.getPersistenceState());
assertEquals(0, a2.getPostLoaded());
assertNull(listener.getPublicCalledbackEntity());
a2.getArtistName();
assertEquals(1, a2.getPostLoaded());
assertSame(a2, listener.getPublicCalledbackEntity());
}
Aggregations