Search in sources :

Example 6 with CayenneContext

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

the class ClientRuntimeTest method testGetObjectContext.

@Test
public void testGetObjectContext() {
    Map<String, String> properties = new HashMap<>();
    ClientModule extraModule = new ClientModule() {

        @Override
        public void configure(Binder binder) {
            super.configure(binder);
            // use a noop connection to prevent startup errors...
            binder.bind(ClientConnection.class).to(MockClientConnection.class);
        }
    };
    ClientRuntime runtime = ClientRuntime.builder().properties(properties).addModule(extraModule).build();
    ObjectContext context = runtime.newContext();
    assertNotNull(context);
    assertTrue(context instanceof CayenneContext);
    assertNotSame("ObjectContext must not be a singleton", context, runtime.newContext());
    CayenneContext clientContext = (CayenneContext) context;
    assertNotNull(clientContext.getChannel());
    assertSame(runtime.getChannel(), clientContext.getChannel());
}
Also used : Binder(org.apache.cayenne.di.Binder) HashMap(java.util.HashMap) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) ObjectContext(org.apache.cayenne.ObjectContext) CayenneContext(org.apache.cayenne.CayenneContext) Test(org.junit.Test)

Example 7 with CayenneContext

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

the class ClientChannelServerDiffsIT method testReturnIdDiff.

@Test
public void testReturnIdDiff() {
    final Object[] ids = new Object[2];
    final GraphChangeHandler diffReader = new NoopGraphChangeHandler() {

        @Override
        public void nodeIdChanged(Object oldId, Object newId) {
            ids[0] = oldId;
            ids[1] = newId;
        }
    };
    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);
    context.newObject(ClientMtTable1.class);
    context.commitChanges();
    assertTrue(ids[0] instanceof ObjectId);
    assertTrue(((ObjectId) ids[0]).isTemporary());
    assertTrue(ids[1] instanceof ObjectId);
    assertFalse(((ObjectId) ids[1]).isTemporary());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectId(org.apache.cayenne.ObjectId) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) ObjectContext(org.apache.cayenne.ObjectContext) CayenneContext(org.apache.cayenne.CayenneContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 8 with CayenneContext

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

the class ClientChannelServerDiffsIT method testReturnDiffClientArcChanges.

@Test
public void testReturnDiffClientArcChanges() {
    final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler();
    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);
    ClientMtTable2 o2 = context.newObject(ClientMtTable2.class);
    o.addToTable2Array(o2);
    context.commitChanges();
    assertEquals(2, diffReader.size);
    diffReader.reset();
    ClientMtTable2 o3 = context.newObject(ClientMtTable2.class);
    o3.setTable1(o);
    context.commitChanges();
    assertEquals(1, diffReader.size);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) CayenneContext(org.apache.cayenne.CayenneContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 9 with CayenneContext

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

the class ClientChannelTest method testOnQuerySelectOverrideCached.

@Test
public void testOnQuerySelectOverrideCached() {
    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);
    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);
    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)

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