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