Search in sources :

Example 1 with LifecycleCallbackRegistry

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

the class CayenneContextReflexiveIT method testCAY830.

@Test
public void testCAY830() throws Exception {
    // an exception was triggered within POST_LOAD callback
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(Reflexive.class, new LifecycleListener() {

            public void postLoad(Object entity) {
            }

            public void postPersist(Object entity) {
            }

            public void postRemove(Object entity) {
            }

            public void postUpdate(Object entity) {
            }

            public void postAdd(Object entity) {
            }

            public void preRemove(Object entity) {
            }

            public void preUpdate(Object entity) {
            }

            public void prePersist(Object entity) {
            }
        });
        ClientReflexive o1 = clientContext.newObject(ClientReflexive.class);
        o1.setName("parent");
        ClientReflexive o2 = clientContext.newObject(ClientReflexive.class);
        o2.setName("child");
        o2.setToParent(o1);
        clientContext.commitChanges();
        clientContext.deleteObjects(o1);
        clientContext.deleteObjects(o2);
        clientContext.commitChanges();
    // per CAY-830 an exception is thrown here
    } finally {
        callbackRegistry.clear();
    }
}
Also used : LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) ClientReflexive(org.apache.cayenne.testdo.reflexive.ClientReflexive) Test(org.junit.Test)

Example 2 with LifecycleCallbackRegistry

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

the class CayenneContextServerDiffsIT method testReturnDiffInPrePersist.

@Test
public void testReturnDiffInPrePersist() {
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
        ClientMtTable1 o = context.newObject(ClientMtTable1.class);
        o.setServerAttribute1("YY");
        context.commitChanges();
        assertFalse(o.getObjectId().isTemporary());
        assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
        assertEquals("XXX", o.getGlobalAttribute1());
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ClientChannelServerDiffsListener1(org.apache.cayenne.remote.ClientChannelServerDiffsListener1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 3 with LifecycleCallbackRegistry

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

the class CayenneContextServerDiffsIT method testReturnDiffServerArcChanges.

@Test
public void testReturnDiffServerArcChanges() {
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersistAddRelationship");
        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(2, o.getTable2Array().size());
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ClientChannelServerDiffsListener1(org.apache.cayenne.remote.ClientChannelServerDiffsListener1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 4 with LifecycleCallbackRegistry

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

the class CayenneContextWithDataContextIT method testPostAddOnObjectCallback.

@Test
public void testPostAddOnObjectCallback() throws Exception {
    final DataContext serverContext = (DataContext) clientServerChannel.getParentChannel();
    LifecycleCallbackRegistry callbackRegistry = serverContext.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addCallback(LifecycleEvent.POST_ADD, MtTable1.class, "prePersistMethod");
        final Persistent clientObject = clientContext.newObject(ClientMtTable1.class);
        clientContext.commitChanges();
        new ParallelTestContainer() {

            @Override
            protected void assertResult() throws Exception {
                // find peer
                MtTable1 peer = (MtTable1) serverContext.getGraphManager().getNode(clientObject.getObjectId());
                assertNotNull(peer);
                assertTrue(peer.isPrePersisted());
            }
        }.runTest(1000);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : DataContext(org.apache.cayenne.access.DataContext) MtTable1(org.apache.cayenne.testdo.mt.MtTable1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 5 with LifecycleCallbackRegistry

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

the class CayenneContextWithDataContextIT method testPreRemoveCallback.

@Test
public void testPreRemoveCallback() throws Exception {
    // an exception was triggered within POST_LOAD callback
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    final boolean[] flag = new boolean[1];
    try {
        callbackRegistry.addListener(MtTable1.class, new LifecycleListener() {

            public void postLoad(Object entity) {
            }

            public void postPersist(Object entity) {
            }

            public void postRemove(Object entity) {
            }

            public void postUpdate(Object entity) {
            }

            public void postAdd(Object entity) {
            }

            public void preRemove(Object entity) {
                flag[0] = true;
            }

            public void preUpdate(Object entity) {
            }

            public void prePersist(Object entity) {
            }
        });
        ClientMtTable1 object = clientContext.newObject(ClientMtTable1.class);
        assertFalse(flag[0]);
        clientContext.commitChanges();
        assertFalse(flag[0]);
        clientContext.deleteObjects(object);
        clientContext.commitChanges();
        assertTrue(flag[0]);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) 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