use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class StatelessContextRequestHandler method requestStart.
public void requestStart(ServletRequest request, ServletResponse response) {
CayenneRuntime.bindThreadInjector(injector);
ObjectContext context = injector.getInstance(ObjectContextFactory.class).createContext();
BaseContext.bindThreadObjectContext(context);
}
use of org.apache.cayenne.ObjectContext 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.ObjectContext 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.ObjectContext 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.ObjectContext in project cayenne by apache.
the class ClientLocalRuntimeTest method testGetConnection.
@Test
public void testGetConnection() {
final DataContext serverContext = mock(DataContext.class);
Module serverModule = binder -> binder.bind(ObjectContextFactory.class).toInstance(new ObjectContextFactory() {
public ObjectContext createContext(DataChannel parent) {
return null;
}
public ObjectContext createContext() {
return serverContext;
}
});
ClientRuntime runtime = ClientRuntime.builder().local(DIBootstrap.createInjector(serverModule)).build();
ClientConnection connection = runtime.getConnection();
assertNotNull(connection);
assertTrue(connection instanceof LocalConnection);
LocalConnection localConnection = (LocalConnection) connection;
assertTrue(localConnection.getChannel() instanceof ClientServerChannel);
ClientServerChannel clientServerChannel = (ClientServerChannel) localConnection.getChannel();
assertSame(serverContext, clientServerChannel.getParentChannel());
}
Aggregations