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