use of org.apache.cayenne.di.Injector 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);
}
}
use of org.apache.cayenne.di.Injector in project cayenne by apache.
the class WebModuleTest method testBind_Scopes.
@Test
public void testBind_Scopes() {
Injector injector = new DefaultInjector(new WebModule());
RequestHandler handler = injector.getInstance(RequestHandler.class);
assertTrue(handler instanceof SessionContextRequestHandler);
RequestHandler handler1 = injector.getInstance(RequestHandler.class);
assertNotSame("Incorrect singleton scope for request handler", handler, handler1);
}
use of org.apache.cayenne.di.Injector in project cayenne by apache.
the class XMPPBridgeProviderTest method testUseProperties.
@Test
public void testUseProperties() throws Exception {
Module module = binder -> {
XMPPModule.contributeSecureConnection(binder, SECURE_CONNECTION_TEST);
XMPPModule.contributeHost(binder, HOST_TEST);
XMPPModule.contributePort(binder, PORT_TEST);
XMPPModule.contributeLogin(binder, LOGIN_TEST, PASSWORD_TEST);
XMPPModule.contributeChatService(binder, CHAT_SERVICE_TEST);
};
Injector injector = DIBootstrap.createInjector(new DefaultBindings(), new XMPPModule(), module);
XMPPBridge bridge = (XMPPBridge) injector.getInstance(EventBridge.class);
assertEquals(HOST_TEST, bridge.getXmppHost());
assertEquals(CHAT_SERVICE_TEST, bridge.getChatService());
assertEquals(LOGIN_TEST, bridge.getLoginId());
assertEquals(PASSWORD_TEST, bridge.getPassword());
assertEquals(SECURE_CONNECTION_TEST, bridge.isSecureConnection());
assertEquals(PORT_TEST, bridge.getXmppPort());
}
use of org.apache.cayenne.di.Injector in project cayenne by apache.
the class DbImporterMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
Logger logger = new MavenLogger(this);
// check missing data source parameters
dataSource.validate();
DbImportConfiguration config = createConfig(logger);
Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
DbImportConfigurationValidator validator = new DbImportConfigurationValidator(dbImportConfig, config, injector);
try {
validator.validate();
} catch (Exception ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
}
try {
injector.getInstance(DbImportAction.class).execute(config);
} catch (Exception ex) {
Throwable th = Util.unwindException(ex);
String message = "Error importing database schema";
if (th.getLocalizedMessage() != null) {
message += ": " + th.getLocalizedMessage();
}
getLog().error(message);
throw new MojoExecutionException(message, th);
}
}
Aggregations