Search in sources :

Example 6 with LifecycleCallbackRegistry

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();
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ArrayList(java.util.ArrayList) CayenneContext(org.apache.cayenne.CayenneContext) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectContext(org.apache.cayenne.ObjectContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 7 with LifecycleCallbackRegistry

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());
}
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 8 with LifecycleCallbackRegistry

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 9 with LifecycleCallbackRegistry

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 10 with LifecycleCallbackRegistry

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