Search in sources :

Example 26 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class DbImporterTask method execute.

@Override
public void execute() {
    config.setFiltersConfig(new FiltersConfigBuilder(reverseEngineering).build());
    validateAttributes();
    Logger logger = new AntLogger(this);
    config.setLogger(logger);
    config.setSkipRelationshipsLoading(reverseEngineering.getSkipRelationshipsLoading());
    config.setSkipPrimaryKeyLoading(reverseEngineering.getSkipPrimaryKeyLoading());
    config.setTableTypes(reverseEngineering.getTableTypes());
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
    DbImportConfigurationValidator validator = new DbImportConfigurationValidator(reverseEngineering, config, injector);
    try {
        validator.validate();
    } catch (Exception ex) {
        throw new BuildException(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();
        }
        log(message, Project.MSG_ERR);
        throw new BuildException(message, th);
    } finally {
        injector.shutdown();
    }
}
Also used : 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) BuildException(org.apache.tools.ant.BuildException) FiltersConfigBuilder(org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) BuildException(org.apache.tools.ant.BuildException) DbImportModule(org.apache.cayenne.dbsync.reverse.dbimport.DbImportModule)

Example 27 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class DbGeneratorTaskTest method testSetAdapter.

@Test
public void testSetAdapter() throws Exception {
    DataSource ds = mock(DataSource.class);
    Injector injector = DIBootstrap.createInjector(new ToolsModule(mock(Logger.class)));
    DbGeneratorTask task = new DbGeneratorTask();
    DbAdapter autoAdapter = task.getAdapter(injector, ds);
    assertTrue(autoAdapter instanceof AutoAdapter);
    task.setAdapter(SQLServerAdapter.class.getName());
    DbAdapter sqlServerAdapter = task.getAdapter(injector, ds);
    assertTrue(sqlServerAdapter instanceof SQLServerAdapter);
}
Also used : SQLServerAdapter(org.apache.cayenne.dba.sqlserver.SQLServerAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 28 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class DefaultDataRowStoreFactoryIT method testGetDataRowStoreWithParameters.

@Test
public void testGetDataRowStoreWithParameters() {
    final DataDomain DOMAIN = new DataDomain("test");
    final EventManager EVENT_MANAGER = new DefaultEventManager();
    final int CACHE_SIZE = 500;
    Module testModule = binder -> {
        binder.bind(DataDomain.class).toInstance(DOMAIN);
        binder.bind(EventManager.class).toInstance(EVENT_MANAGER);
        binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
        binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        ServerModule.setSnapshotCacheSize(binder, CACHE_SIZE);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DataRowStore dataStore = injector.getInstance(DataRowStoreFactory.class).createDataRowStore("test");
    assertNotNull(dataStore);
    assertEquals(dataStore.maximumSize(), CACHE_SIZE);
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) TransactionManager(org.apache.cayenne.tx.TransactionManager) ServerCase(org.apache.cayenne.unit.di.server.ServerCase) Module(org.apache.cayenne.di.Module) MockEventBridgeProvider(org.apache.cayenne.event.MockEventBridgeProvider) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) ServerModule(org.apache.cayenne.configuration.server.ServerModule) DIBootstrap(org.apache.cayenne.di.DIBootstrap) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) MockEventBridge(org.apache.cayenne.event.MockEventBridge) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) Assert.assertNotNull(org.junit.Assert.assertNotNull) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Test(org.junit.Test) CayenneProjects(org.apache.cayenne.unit.di.server.CayenneProjects) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) UseServerRuntime(org.apache.cayenne.unit.di.server.UseServerRuntime) Assert.assertEquals(org.junit.Assert.assertEquals) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Module(org.apache.cayenne.di.Module) ServerModule(org.apache.cayenne.configuration.server.ServerModule) Test(org.junit.Test)

Example 29 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class BaseContextTest method testAttachToRuntimeIfNeeded.

@Test
public void testAttachToRuntimeIfNeeded() {
    final DataChannel channel = mock(DataChannel.class);
    final QueryCache cache = mock(QueryCache.class);
    Module testModule = binder -> {
        binder.bind(DataChannel.class).toInstance(channel);
        binder.bind(QueryCache.class).toInstance(cache);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    BaseContext context = new MockBaseContext();
    assertNull(context.channel);
    assertNull(context.queryCache);
    Injector oldInjector = CayenneRuntime.getThreadInjector();
    try {
        CayenneRuntime.bindThreadInjector(injector);
        assertTrue(context.attachToRuntimeIfNeeded());
        assertSame(channel, context.channel);
        assertFalse(context.attachToRuntimeIfNeeded());
        assertFalse(context.attachToRuntimeIfNeeded());
    } finally {
        CayenneRuntime.bindThreadInjector(oldInjector);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) Map(java.util.Map) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) CayenneRuntime(org.apache.cayenne.configuration.CayenneRuntime) Mockito.mock(org.mockito.Mockito.mock) QueryCache(org.apache.cayenne.cache.QueryCache) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 30 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class CayenneRuntimeTest method testBindThreadInjector.

@Test
public void testBindThreadInjector() {
    Injector injector = mock(Injector.class);
    assertNull(CayenneRuntime.getThreadInjector());
    try {
        CayenneRuntime.bindThreadInjector(injector);
        assertSame(injector, CayenneRuntime.getThreadInjector());
    } finally {
        CayenneRuntime.bindThreadInjector(null);
    }
    assertNull(CayenneRuntime.getThreadInjector());
}
Also used : Injector(org.apache.cayenne.di.Injector) Test(org.junit.Test)

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