use of org.apache.cayenne.configuration.ObjectContextFactory in project cayenne by apache.
the class LocalClientServerChannelProvider method get.
public DataChannel get() throws ConfigurationException {
ObjectContextFactory factory = serverInjector.getInstance(ObjectContextFactory.class);
// TODO: ugly cast
DataContext serverContext = (DataContext) factory.createContext();
return new ClientServerChannel(serverContext);
}
use of org.apache.cayenne.configuration.ObjectContextFactory in project cayenne by apache.
the class ServerRuntimeTest method testGetObjectContext_CustomModule.
@Test
public void testGetObjectContext_CustomModule() {
final ObjectContext context = new DataContext();
final ObjectContextFactory factory = new ObjectContextFactory() {
public ObjectContext createContext(DataChannel parent) {
return context;
}
public ObjectContext createContext() {
return context;
}
};
Module module = binder -> binder.bind(ObjectContextFactory.class).toInstance(factory);
ServerRuntime runtime = new ServerRuntime(Collections.singleton(module));
assertSame(context, runtime.newContext());
assertSame(context, runtime.newContext());
}
use of org.apache.cayenne.configuration.ObjectContextFactory in project cayenne by apache.
the class ClientModuleTest method testObjectContextFactory.
@Test
public void testObjectContextFactory() {
Map<String, String> properties = new HashMap<>();
ClientModule module = 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);
}
};
Injector injector = DIBootstrap.createInjector(module);
ObjectContextFactory factory = injector.getInstance(ObjectContextFactory.class);
assertNotNull(factory);
assertSame("ObjectContextFactory must be a singleton", factory, injector.getInstance(ObjectContextFactory.class));
}
use of org.apache.cayenne.configuration.ObjectContextFactory 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());
}
use of org.apache.cayenne.configuration.ObjectContextFactory in project cayenne by apache.
the class ServletContextHandlerTest method testRequestStart_bindContext.
@Test
public void testRequestStart_bindContext() {
Module module = binder -> {
binder.bind(DataChannel.class).to(MockDataChannel.class);
binder.bind(ObjectContextFactory.class).toInstance(new ObjectContextFactory() {
public ObjectContext createContext(DataChannel parent) {
return mock(ObjectContext.class);
}
public ObjectContext createContext() {
return mock(ObjectContext.class);
}
});
};
Injector injector = DIBootstrap.createInjector(module);
SessionContextRequestHandler handler = new SessionContextRequestHandler();
injector.injectMembers(handler);
MockHttpSession session = new MockHttpSession();
BaseContext.bindThreadObjectContext(null);
try {
MockHttpServletRequest request1 = new MockHttpServletRequest();
MockHttpServletResponse response1 = new MockHttpServletResponse();
request1.setSession(session);
handler.requestStart(request1, response1);
ObjectContext c1 = BaseContext.getThreadObjectContext();
assertNotNull(c1);
handler.requestEnd(request1, response1);
try {
BaseContext.getThreadObjectContext();
fail("thread context not null");
} catch (IllegalStateException e) {
// expected
}
MockHttpServletRequest request2 = new MockHttpServletRequest();
MockHttpServletResponse response2 = new MockHttpServletResponse();
request2.setSession(session);
handler.requestStart(request2, response2);
ObjectContext c2 = BaseContext.getThreadObjectContext();
assertSame(c1, c2);
handler.requestEnd(request2, response2);
try {
BaseContext.getThreadObjectContext();
fail("thread context not null");
} catch (IllegalStateException e) {
// expected
}
MockHttpServletRequest request3 = new MockHttpServletRequest();
MockHttpServletResponse response3 = new MockHttpServletResponse();
request3.setSession(new MockHttpSession());
handler.requestStart(request3, response3);
ObjectContext c3 = BaseContext.getThreadObjectContext();
assertNotNull(c3);
assertNotSame(c1, c3);
handler.requestEnd(request3, response3);
try {
BaseContext.getThreadObjectContext();
fail("thread context not null");
} catch (IllegalStateException e) {
// expected
}
} finally {
BaseContext.bindThreadObjectContext(null);
}
}
Aggregations