Search in sources :

Example 61 with Injector

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);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) ObjectContext(org.apache.cayenne.ObjectContext) MockDataChannel(org.apache.cayenne.MockDataChannel) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) BaseContext(org.apache.cayenne.BaseContext) DataChannel(org.apache.cayenne.DataChannel) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) MockDataChannel(org.apache.cayenne.MockDataChannel) DataChannel(org.apache.cayenne.DataChannel) Injector(org.apache.cayenne.di.Injector) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) MockDataChannel(org.apache.cayenne.MockDataChannel) Test(org.junit.Test)

Example 62 with Injector

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);
}
Also used : Injector(org.apache.cayenne.di.Injector) DefaultInjector(org.apache.cayenne.di.spi.DefaultInjector) DefaultInjector(org.apache.cayenne.di.spi.DefaultInjector) Test(org.junit.Test)

Example 63 with Injector

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());
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) Binder(org.apache.cayenne.di.Binder) TransactionManager(org.apache.cayenne.tx.TransactionManager) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) Assert.assertNotNull(org.junit.Assert.assertNotNull) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Constants(org.apache.cayenne.configuration.Constants) Module(org.apache.cayenne.di.Module) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataDomain(org.apache.cayenne.access.DataDomain) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert.assertEquals(org.junit.Assert.assertEquals) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 64 with Injector

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DbImportConfigurationValidator(org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator) Logger(org.slf4j.Logger) DbImportAction(org.apache.cayenne.dbsync.reverse.dbimport.DbImportAction) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DbImportConfiguration(org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfiguration) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) DbImportModule(org.apache.cayenne.dbsync.reverse.dbimport.DbImportModule)

Aggregations

Injector (org.apache.cayenne.di.Injector)64 Test (org.junit.Test)52 Module (org.apache.cayenne.di.Module)27 DIBootstrap (org.apache.cayenne.di.DIBootstrap)23 URL (java.net.URL)15 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)15 DataMap (org.apache.cayenne.map.DataMap)15 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)14 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)14 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)14 Slf4jJdbcEventLogger (org.apache.cayenne.log.Slf4jJdbcEventLogger)14 URLResource (org.apache.cayenne.resource.URLResource)14 Assert (org.junit.Assert)14 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)13 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)12 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)12 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)12 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)12 Logger (org.slf4j.Logger)11 Constants (org.apache.cayenne.configuration.Constants)10