Search in sources :

Example 21 with Injector

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

the class DefaultDataSourceFactoryLoaderTest method testGetDataSourceFactory_Property.

@Test
public void testGetDataSourceFactory_Property() throws Exception {
    final RuntimeProperties properties = mock(RuntimeProperties.class);
    when(properties.get(Constants.JDBC_DRIVER_PROPERTY)).thenReturn("x");
    when(properties.get(Constants.JDBC_URL_PROPERTY)).thenReturn("y");
    DataChannelDescriptor channelDescriptor = new DataChannelDescriptor();
    channelDescriptor.setName("X");
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setName("node1");
    nodeDescriptor.setDataSourceFactoryType(MockDataSourceFactory1.class.getName());
    nodeDescriptor.setDataChannelDescriptor(channelDescriptor);
    Module testModule = binder -> {
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
        binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).to(MockResourceLocator.class);
        binder.bind(RuntimeProperties.class).toInstance(properties);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DelegatingDataSourceFactory factoryLoader = new DelegatingDataSourceFactory();
    injector.injectMembers(factoryLoader);
    DataSourceFactory factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertTrue(factory instanceof PropertyDataSourceFactory);
    when(properties.get(Constants.JDBC_URL_PROPERTY)).thenReturn(null);
    factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertFalse(factory instanceof PropertyDataSourceFactory);
    when(properties.get(Constants.JDBC_URL_PROPERTY + ".X.node2")).thenReturn("y");
    factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertFalse(factory instanceof PropertyDataSourceFactory);
    when(properties.get(Constants.JDBC_URL_PROPERTY + ".X.node1")).thenReturn("y");
    factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertTrue(factory instanceof PropertyDataSourceFactory);
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) MockDataSourceFactory1(org.apache.cayenne.configuration.mock.MockDataSourceFactory1) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Module(org.apache.cayenne.di.Module) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) Assert.assertSame(org.junit.Assert.assertSame) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Key(org.apache.cayenne.di.Key) Before(org.junit.Before) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) Assert.assertNotNull(org.junit.Assert.assertNotNull) MockResourceLocator(org.apache.cayenne.resource.mock.MockResourceLocator) Constants(org.apache.cayenne.configuration.Constants) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DataSourceInfo(org.apache.cayenne.conn.DataSourceInfo) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.mock(org.mockito.Mockito.mock) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) MockDataSourceFactory1(org.apache.cayenne.configuration.mock.MockDataSourceFactory1) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) MockResourceLocator(org.apache.cayenne.resource.mock.MockResourceLocator) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) MockResourceLocator(org.apache.cayenne.resource.mock.MockResourceLocator) Test(org.junit.Test)

Example 22 with Injector

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

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Auto.

@Test
public void testCreatedAdapter_Auto() throws Exception {
    final DbAdapter adapter = mock(DbAdapter.class);
    when(adapter.createTable(any(DbEntity.class))).thenReturn("XXXXX");
    List<DbAdapterDetector> detectors = new ArrayList<DbAdapterDetector>();
    detectors.add(new DbAdapterDetector() {

        public DbAdapter createAdapter(DatabaseMetaData md) throws SQLException {
            return adapter;
        }
    });
    MockConnection connection = new MockConnection();
    MockDataSource dataSource = new MockDataSource();
    dataSource.setupConnection(connection);
    Module testModule = binder -> {
        ServerModule.contributeProperties(binder);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DefaultDbAdapterFactory factory = new DefaultDbAdapterFactory(detectors);
    injector.injectMembers(factory);
    DbAdapter createdAdapter = factory.createAdapter(new DataNodeDescriptor(), dataSource);
    assertTrue(createdAdapter instanceof AutoAdapter);
    assertEquals("XXXXX", createdAdapter.createTable(new DbEntity("Test")));
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) Module(org.apache.cayenne.di.Module) DatabaseMetaData(java.sql.DatabaseMetaData) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Key(org.apache.cayenne.di.Key) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Constants(org.apache.cayenne.configuration.Constants) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) List(java.util.List) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) SybaseAdapter(org.apache.cayenne.dba.sybase.SybaseAdapter) Assert(org.junit.Assert) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Mockito.mock(org.mockito.Mockito.mock) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DatabaseMetaData(java.sql.DatabaseMetaData) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Test(org.junit.Test)

Example 23 with Injector

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

the class DefaultDataRowStoreFactoryIT method testGetDataRowStoreWithBridge.

@Test
public void testGetDataRowStoreWithBridge() {
    final DataDomain DOMAIN = new DataDomain("test");
    final EventManager EVENT_MANAGER = new DefaultEventManager();
    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(MockEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        ServerModule.contributeProperties(binder);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DataRowStore dataStore = injector.getInstance(DataRowStoreFactory.class).createDataRowStore("test");
    assertEquals(dataStore.getEventBridge().getClass(), MockEventBridge.class);
}
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) MockEventBridgeProvider(org.apache.cayenne.event.MockEventBridgeProvider) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) ServerModule(org.apache.cayenne.configuration.server.ServerModule) Test(org.junit.Test)

Example 24 with Injector

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

the class CayenneGeneratorTask method execute.

/**
 * Executes the task. It will be called by ant framework.
 */
@Override
public void execute() throws BuildException {
    validateAttributes();
    Injector injector = DIBootstrap.createInjector(new ToolsModule(LoggerFactory.getLogger(CayenneGeneratorTask.class)));
    AntLogger logger = new AntLogger(this);
    CayenneGeneratorMapLoaderAction loadAction = new CayenneGeneratorMapLoaderAction(injector);
    loadAction.setMainDataMapFile(map);
    loadAction.setAdditionalDataMapFiles(additionalMaps);
    CayenneGeneratorEntityFilterAction filterAction = new CayenneGeneratorEntityFilterAction();
    filterAction.setClient(client);
    filterAction.setNameFilter(NamePatternMatcher.build(logger, includeEntitiesPattern, excludeEntitiesPattern));
    try {
        DataMap dataMap = loadAction.getMainDataMap();
        ClassGenerationAction generatorAction = createGeneratorAction();
        generatorAction.setLogger(logger);
        generatorAction.setTimestamp(map.lastModified());
        generatorAction.setDataMap(dataMap);
        generatorAction.addEntities(filterAction.getFilteredEntities(dataMap));
        generatorAction.addEmbeddables(filterAction.getFilteredEmbeddables(dataMap));
        generatorAction.addQueries(dataMap.getQueryDescriptors());
        generatorAction.execute();
    } catch (Exception e) {
        throw new BuildException(e);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) BuildException(org.apache.tools.ant.BuildException) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataMap(org.apache.cayenne.map.DataMap) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction)

Example 25 with Injector

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

the class DbGeneratorTask method execute.

@Override
public void execute() {
    Logger logger = new AntLogger(this);
    log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver, url, userName), Project.MSG_VERBOSE);
    log(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK), Project.MSG_VERBOSE);
    validateAttributes();
    ClassLoader loader = null;
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
    try {
        loader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
        // Load the data map and run the db generator.
        DataMap dataMap = loadDataMap(injector);
        // load driver taking custom CLASSPATH into account...
        DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(), url, userName, password);
        DbAdapter adapter = getAdapter(injector, dataSource);
        DbGenerator generator = new DbGenerator(adapter, dataMap, Collections.<DbEntity>emptyList(), null, NoopJdbcEventLogger.getInstance());
        generator.setShouldCreateFKConstraints(createFK);
        generator.setShouldCreatePKSupport(createPK);
        generator.setShouldCreateTables(createTables);
        generator.setShouldDropPKSupport(dropPK);
        generator.setShouldDropTables(dropTables);
        generator.runGenerator(dataSource);
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error generating database";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        log(message, Project.MSG_ERR);
        throw new BuildException(message, th);
    } finally {
        Thread.currentThread().setContextClassLoader(loader);
        injector.shutdown();
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) Logger(org.slf4j.Logger) NoopJdbcEventLogger(org.apache.cayenne.log.NoopJdbcEventLogger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataMap(org.apache.cayenne.map.DataMap) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) BuildException(org.apache.tools.ant.BuildException) DbGenerator(org.apache.cayenne.access.DbGenerator)

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