Search in sources :

Example 1 with CayenneContext

use of org.apache.cayenne.CayenneContext in project cayenne by apache.

the class CayenneContextFactory method createContext.

public ObjectContext createContext(DataChannel parent) {
    boolean changeEvents = properties.getBoolean(ClientConstants.ROP_CONTEXT_CHANGE_EVENTS_PROPERTY, false);
    boolean lifecycleEvents = properties.getBoolean(ClientConstants.ROP_CONTEXT_LIFECYCLE_EVENTS_PROPERTY, false);
    CayenneContext context = newInstance(parent, changeEvents, lifecycleEvents);
    context.setQueryCache(new NestedQueryCache(queryCache));
    return context;
}
Also used : NestedQueryCache(org.apache.cayenne.cache.NestedQueryCache) CayenneContext(org.apache.cayenne.CayenneContext)

Example 2 with CayenneContext

use of org.apache.cayenne.CayenneContext 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 3 with CayenneContext

use of org.apache.cayenne.CayenneContext in project cayenne by apache.

the class ClientChannelTest method testOnQuerySelectOverrideModifiedCached.

@Test
public void testOnQuerySelectOverrideModifiedCached() {
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    EntityResolver resolver = new EntityResolver(entities);
    CayenneContext context = new CayenneContext();
    context.setEntityResolver(resolver);
    ObjectId oid = new ObjectId("test_entity", "x", "y");
    MockPersistentObject o1 = new MockPersistentObject(oid);
    o1.setPersistenceState(PersistenceState.MODIFIED);
    context.getGraphManager().registerNode(oid, o1);
    assertSame(o1, context.getGraphManager().getNode(oid));
    // another object with the same GID ... we must merge it with cached and return
    // cached object instead of the one fetched
    MockPersistentObject o2 = new MockPersistentObject(oid);
    MockClientConnection connection = new MockClientConnection(new GenericResponse(Arrays.asList(o2)));
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    context.setChannel(channel);
    QueryResponse response = channel.onQuery(context, new SelectQuery("test_entity"));
    assertNotNull(response);
    assertEquals(1, response.size());
    List<?> list = response.firstList();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertTrue("Expected cached object, got: " + list, list.contains(o1));
    assertSame(o1, context.getGraphManager().getNode(oid));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) EntityResolver(org.apache.cayenne.map.EntityResolver) CayenneContext(org.apache.cayenne.CayenneContext) DataMap(org.apache.cayenne.map.DataMap) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) MockPersistentObject(org.apache.cayenne.MockPersistentObject) QueryResponse(org.apache.cayenne.QueryResponse) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 4 with CayenneContext

use of org.apache.cayenne.CayenneContext in project cayenne by apache.

the class ClientChannelTest method testOnQuerySelect.

@Test
public void testOnQuerySelect() {
    final MockPersistentObject o1 = new MockPersistentObject();
    ObjectId oid1 = new ObjectId("test_entity");
    o1.setObjectId(oid1);
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
            if (arg instanceof BootstrapMessage) {
                return new EntityResolver();
            } else {
                return new GenericResponse(Arrays.asList(o1));
            }
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    CayenneContext context = new CayenneContext(channel);
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    QueryResponse response = channel.onQuery(context, new SelectQuery("test_entity"));
    assertNotNull(response);
    List<?> list = response.firstList();
    assertNotNull(list);
    assertEquals(1, list.size());
    Persistent o1_1 = (Persistent) list.get(0);
    assertEquals(o1.getObjectId(), o1_1.getObjectId());
    // ObjectContext must be injected
    assertEquals(context, o1_1.getObjectContext());
    assertSame(o1_1, context.getGraphManager().getNode(oid1));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) Persistent(org.apache.cayenne.Persistent) EntityResolver(org.apache.cayenne.map.EntityResolver) CayenneContext(org.apache.cayenne.CayenneContext) DataMap(org.apache.cayenne.map.DataMap) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockPersistentObject(org.apache.cayenne.MockPersistentObject) QueryResponse(org.apache.cayenne.QueryResponse) MockPersistentObject(org.apache.cayenne.MockPersistentObject) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 5 with CayenneContext

use of org.apache.cayenne.CayenneContext in project cayenne by apache.

the class RemoteCayenneCase method createROPContext.

protected CayenneContext createROPContext() {
    ClientServerChannel clientServerChannel = new ClientServerChannel(serverContext);
    UnitLocalConnection connection = new UnitLocalConnection(clientServerChannel, serializationPolicy);
    ClientChannel channel = new ClientChannel(connection, false, // TODO: replace with container managed ClientCase.
    new DefaultEventManager(0), false);
    CayenneContext context = new CayenneContext(channel, true, true);
    context.setQueryCache(new MapQueryCache(10));
    return context;
}
Also used : UnitLocalConnection(org.apache.cayenne.unit.UnitLocalConnection) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) ClientServerChannel(org.apache.cayenne.access.ClientServerChannel) CayenneContext(org.apache.cayenne.CayenneContext)

Aggregations

CayenneContext (org.apache.cayenne.CayenneContext)9 Test (org.junit.Test)7 MockEventManager (org.apache.cayenne.event.MockEventManager)6 ObjectId (org.apache.cayenne.ObjectId)5 ObjectContext (org.apache.cayenne.ObjectContext)4 MockPersistentObject (org.apache.cayenne.MockPersistentObject)3 QueryResponse (org.apache.cayenne.QueryResponse)3 GraphDiff (org.apache.cayenne.graph.GraphDiff)3 DataMap (org.apache.cayenne.map.DataMap)3 EntityResolver (org.apache.cayenne.map.EntityResolver)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 SelectQuery (org.apache.cayenne.query.SelectQuery)3 GenericResponse (org.apache.cayenne.util.GenericResponse)3 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Persistent (org.apache.cayenne.Persistent)1 ClientServerChannel (org.apache.cayenne.access.ClientServerChannel)1 MapQueryCache (org.apache.cayenne.cache.MapQueryCache)1 NestedQueryCache (org.apache.cayenne.cache.NestedQueryCache)1