Search in sources :

Example 26 with DataNodeDescriptor

use of org.apache.cayenne.configuration.DataNodeDescriptor in project cayenne by apache.

the class DataDomainProviderTest method testGet.

@Test
public void testGet() {
    // create dependencies
    final String testConfigName = "testConfig";
    final DataChannelDescriptor testDescriptor = new DataChannelDescriptor();
    DataMap map1 = new DataMap("map1");
    testDescriptor.getDataMaps().add(map1);
    DataMap map2 = new DataMap("map2");
    testDescriptor.getDataMaps().add(map2);
    DataNodeDescriptor nodeDescriptor1 = new DataNodeDescriptor();
    nodeDescriptor1.setName("node1");
    nodeDescriptor1.getDataMapNames().add("map1");
    nodeDescriptor1.setAdapterType(OracleAdapter.class.getName());
    nodeDescriptor1.setDataSourceFactoryType(MockDataSourceFactory.class.getName());
    nodeDescriptor1.setParameters("jdbc/testDataNode1");
    nodeDescriptor1.setSchemaUpdateStrategyType(ThrowOnPartialOrCreateSchemaStrategy.class.getName());
    testDescriptor.getNodeDescriptors().add(nodeDescriptor1);
    DataNodeDescriptor nodeDescriptor2 = new DataNodeDescriptor();
    nodeDescriptor2.setName("node2");
    nodeDescriptor2.getDataMapNames().add("map2");
    nodeDescriptor2.setParameters("testDataNode2.driver.xml");
    testDescriptor.getNodeDescriptors().add(nodeDescriptor2);
    final DataChannelDescriptorLoader testLoader = new DataChannelDescriptorLoader() {

        @Override
        public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
            return new ConfigurationTree<>(testDescriptor, null);
        }
    };
    final EventManager eventManager = new MockEventManager();
    final TestListener mockListener = new TestListener();
    Module testModule = binder -> {
        final ClassLoaderManager classLoaderManager = new DefaultClassLoaderManager();
        binder.bind(ClassLoaderManager.class).toInstance(classLoaderManager);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        ServerModule.contributeProperties(binder);
        ServerModule.contributeAdapterDetectors(binder).add(FirebirdSniffer.class).add(OpenBaseSniffer.class).add(FrontBaseSniffer.class).add(IngresSniffer.class).add(SQLiteSniffer.class).add(DB2Sniffer.class).add(H2Sniffer.class).add(HSQLDBSniffer.class).add(SybaseSniffer.class).add(DerbySniffer.class).add(SQLServerSniffer.class).add(OracleSniffer.class).add(PostgresSniffer.class).add(MySQLSniffer.class);
        ServerModule.contributeDomainFilters(binder);
        ServerModule.contributeDomainListeners(binder).add(mockListener);
        ServerModule.contributeProjectLocations(binder).add(testConfigName);
        // configure extended types
        ServerModule.contributeDefaultTypes(binder);
        ServerModule.contributeUserTypes(binder);
        ServerModule.contributeTypeFactories(binder);
        binder.bind(EventManager.class).toInstance(eventManager);
        binder.bind(EntitySorter.class).toInstance(new AshwoodEntitySorter());
        binder.bind(SchemaUpdateStrategyFactory.class).to(DefaultSchemaUpdateStrategyFactory.class);
        final ResourceLocator locator = new ClassLoaderResourceLocator(classLoaderManager) {

            public Collection<Resource> findResources(String name) {
                // if this is the request we are getting, just let it go through..
                if (name.endsWith("types.xml")) {
                    return super.findResources(name);
                }
                assertEquals(testConfigName, name);
                return Collections.<Resource>singleton(new MockResource());
            }
        };
        binder.bind(ResourceLocator.class).toInstance(locator);
        binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).toInstance(locator);
        binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
        binder.bind(DataChannelDescriptorMerger.class).to(DefaultDataChannelDescriptorMerger.class);
        binder.bind(DataChannelDescriptorLoader.class).toInstance(testLoader);
        binder.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
        binder.bind(BatchTranslatorFactory.class).to(DefaultBatchTranslatorFactory.class);
        binder.bind(SelectTranslatorFactory.class).to(DefaultSelectTranslatorFactory.class);
        binder.bind(DataSourceFactory.class).toInstance(new MockDataSourceFactory());
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(QueryCache.class).toInstance(mock(QueryCache.class));
        binder.bind(RowReaderFactory.class).toInstance(mock(RowReaderFactory.class));
        binder.bind(DataNodeFactory.class).to(DefaultDataNodeFactory.class);
        binder.bind(SQLTemplateProcessor.class).toInstance(mock(SQLTemplateProcessor.class));
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        ServerModule.contributeValueObjectTypes(binder);
        binder.bind(ValueObjectTypeRegistry.class).to(DefaultValueObjectTypeRegistry.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    // create and initialize provide instance to test
    DataDomainProvider provider = new DataDomainProvider();
    injector.injectMembers(provider);
    DataChannel channel = provider.get();
    assertNotNull(channel);
    assertTrue(channel instanceof DataDomain);
    DataDomain domain = (DataDomain) channel;
    assertSame(eventManager, domain.getEventManager());
    assertEquals(2, domain.getDataMaps().size());
    assertTrue(domain.getDataMaps().contains(map1));
    assertTrue(domain.getDataMaps().contains(map2));
    assertEquals(2, domain.getDataNodes().size());
    DataNode node1 = domain.getDataNode("node1");
    assertNotNull(node1);
    assertEquals(1, node1.getDataMaps().size());
    assertSame(map1, node1.getDataMaps().iterator().next());
    assertSame(node1, domain.lookupDataNode(map1));
    assertEquals(nodeDescriptor1.getDataSourceFactoryType(), node1.getDataSourceFactory());
    assertNotNull(node1.getDataSource());
    assertNotNull(node1.getSchemaUpdateStrategy());
    assertEquals(nodeDescriptor1.getSchemaUpdateStrategyType(), node1.getSchemaUpdateStrategy().getClass().getName());
    assertNotNull(node1.getAdapter());
    assertEquals(OracleAdapter.class, node1.getAdapter().getClass());
    DataNode node2 = domain.getDataNode("node2");
    assertNotNull(node2);
    assertEquals(1, node2.getDataMaps().size());
    assertSame(map2, node2.getDataMaps().iterator().next());
    assertSame(node2, domain.lookupDataNode(map2));
    assertNull(node2.getDataSourceFactory());
    assertNotNull(node2.getDataSource());
    assertNotNull(node2.getSchemaUpdateStrategy());
    assertEquals(SkipSchemaUpdateStrategy.class.getName(), node2.getSchemaUpdateStrategy().getClass().getName());
    assertNotNull(node2.getAdapter());
    // check that we have mock listener passed correctly
    Persistent mockPersistent = mock(Persistent.class);
    ObjectId mockObjectId = mock(ObjectId.class);
    when(mockObjectId.getEntityName()).thenReturn("mock-entity-name");
    when(mockPersistent.getObjectId()).thenReturn(mockObjectId);
    domain.getEntityResolver().getCallbackRegistry().performCallbacks(LifecycleEvent.POST_LOAD, mockPersistent);
    assertEquals("Should call postLoadCallback() method", 1, TestListener.counter.get());
}
Also used : DataNode(org.apache.cayenne.access.DataNode) RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) SchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.SchemaUpdateStrategyFactory) DerbySniffer(org.apache.cayenne.dba.derby.DerbySniffer) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Module(org.apache.cayenne.di.Module) DefaultDataChannelDescriptorMerger(org.apache.cayenne.configuration.DefaultDataChannelDescriptorMerger) MySQLSniffer(org.apache.cayenne.dba.mysql.MySQLSniffer) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) OracleAdapter(org.apache.cayenne.dba.oracle.OracleAdapter) OracleSniffer(org.apache.cayenne.dba.oracle.OracleSniffer) DataDomain(org.apache.cayenne.access.DataDomain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThrowOnPartialOrCreateSchemaStrategy(org.apache.cayenne.access.dbsync.ThrowOnPartialOrCreateSchemaStrategy) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) SQLServerSniffer(org.apache.cayenne.dba.sqlserver.SQLServerSniffer) DIBootstrap(org.apache.cayenne.di.DIBootstrap) EntitySorter(org.apache.cayenne.map.EntitySorter) Key(org.apache.cayenne.di.Key) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) DB2Sniffer(org.apache.cayenne.dba.db2.DB2Sniffer) Collection(java.util.Collection) DefaultBatchTranslatorFactory(org.apache.cayenne.access.translator.batch.DefaultBatchTranslatorFactory) HSQLDBSniffer(org.apache.cayenne.dba.hsqldb.HSQLDBSniffer) FrontBaseSniffer(org.apache.cayenne.dba.frontbase.FrontBaseSniffer) H2Sniffer(org.apache.cayenne.dba.h2.H2Sniffer) Resource(org.apache.cayenne.resource.Resource) SybaseSniffer(org.apache.cayenne.dba.sybase.SybaseSniffer) ConfigurationException(org.apache.cayenne.ConfigurationException) PostLoad(org.apache.cayenne.annotation.PostLoad) SQLiteSniffer(org.apache.cayenne.dba.sqlite.SQLiteSniffer) LifecycleEvent(org.apache.cayenne.map.LifecycleEvent) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) MockResource(org.apache.cayenne.resource.mock.MockResource) Mockito.mock(org.mockito.Mockito.mock) DataChannelDescriptorMerger(org.apache.cayenne.configuration.DataChannelDescriptorMerger) IngresSniffer(org.apache.cayenne.dba.ingres.IngresSniffer) DefaultSelectTranslatorFactory(org.apache.cayenne.access.translator.select.DefaultSelectTranslatorFactory) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) PostgresSniffer(org.apache.cayenne.dba.postgres.PostgresSniffer) QueryCache(org.apache.cayenne.cache.QueryCache) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) Persistent(org.apache.cayenne.Persistent) OpenBaseSniffer(org.apache.cayenne.dba.openbase.OpenBaseSniffer) DefaultSchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.DefaultSchemaUpdateStrategyFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) SQLTemplateProcessor(org.apache.cayenne.access.jdbc.SQLTemplateProcessor) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) MockDataSourceFactory(org.apache.cayenne.configuration.mock.MockDataSourceFactory) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Constants(org.apache.cayenne.configuration.Constants) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) FirebirdSniffer(org.apache.cayenne.dba.firebird.FirebirdSniffer) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) SkipSchemaUpdateStrategy(org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ObjectId(org.apache.cayenne.ObjectId) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Assert(org.junit.Assert) Collections(java.util.Collections) SelectTranslatorFactory(org.apache.cayenne.access.translator.select.SelectTranslatorFactory) DataMap(org.apache.cayenne.map.DataMap) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) SkipSchemaUpdateStrategy(org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy) DataMap(org.apache.cayenne.map.DataMap) DefaultSchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.DefaultSchemaUpdateStrategyFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) DataNode(org.apache.cayenne.access.DataNode) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultBatchTranslatorFactory(org.apache.cayenne.access.translator.batch.DefaultBatchTranslatorFactory) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) OracleAdapter(org.apache.cayenne.dba.oracle.OracleAdapter) DerbySniffer(org.apache.cayenne.dba.derby.DerbySniffer) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) DataDomain(org.apache.cayenne.access.DataDomain) ThrowOnPartialOrCreateSchemaStrategy(org.apache.cayenne.access.dbsync.ThrowOnPartialOrCreateSchemaStrategy) DefaultDataChannelDescriptorMerger(org.apache.cayenne.configuration.DefaultDataChannelDescriptorMerger) Resource(org.apache.cayenne.resource.Resource) MockResource(org.apache.cayenne.resource.mock.MockResource) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) MySQLSniffer(org.apache.cayenne.dba.mysql.MySQLSniffer) DB2Sniffer(org.apache.cayenne.dba.db2.DB2Sniffer) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) Collection(java.util.Collection) Module(org.apache.cayenne.di.Module) IngresSniffer(org.apache.cayenne.dba.ingres.IngresSniffer) MockEventManager(org.apache.cayenne.event.MockEventManager) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) MockResource(org.apache.cayenne.resource.mock.MockResource) MockDataSourceFactory(org.apache.cayenne.configuration.mock.MockDataSourceFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultSelectTranslatorFactory(org.apache.cayenne.access.translator.select.DefaultSelectTranslatorFactory) ObjectId(org.apache.cayenne.ObjectId) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Persistent(org.apache.cayenne.Persistent) DataChannel(org.apache.cayenne.DataChannel) OpenBaseSniffer(org.apache.cayenne.dba.openbase.OpenBaseSniffer) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) OracleSniffer(org.apache.cayenne.dba.oracle.OracleSniffer) HSQLDBSniffer(org.apache.cayenne.dba.hsqldb.HSQLDBSniffer) Test(org.junit.Test)

Example 27 with DataNodeDescriptor

use of org.apache.cayenne.configuration.DataNodeDescriptor in project cayenne by apache.

the class DefaultDataSourceFactoryLoaderTest method testGetDataSourceFactory_Explicit.

@Test
public void testGetDataSourceFactory_Explicit() throws Exception {
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setName("node1");
    nodeDescriptor.setDataSourceFactoryType(MockDataSourceFactory1.class.getName());
    DelegatingDataSourceFactory factoryLoader = new DelegatingDataSourceFactory();
    injector.injectMembers(factoryLoader);
    DataSourceFactory factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertTrue(factory instanceof MockDataSourceFactory1);
    assertSame("Injection on the factory hasn't been performed", injector, ((MockDataSourceFactory1) factory).getInjector());
}
Also used : DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) MockDataSourceFactory1(org.apache.cayenne.configuration.mock.MockDataSourceFactory1) Test(org.junit.Test)

Example 28 with DataNodeDescriptor

use of org.apache.cayenne.configuration.DataNodeDescriptor in project cayenne by apache.

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Generic.

@Test
public void testCreatedAdapter_Generic() throws Exception {
    List<DbAdapterDetector> detectors = new ArrayList<DbAdapterDetector>();
    Module testModule = binder -> {
        ServerModule.contributeProperties(binder);
        ServerModule.contributeDefaultTypes(binder);
        ServerModule.contributeUserTypes(binder);
        ServerModule.contributeTypeFactories(binder);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocator.class);
        binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).to(ClassLoaderResourceLocator.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
        binder.bind(BatchTranslatorFactory.class).toInstance(mock(BatchTranslatorFactory.class));
        ServerModule.contributeValueObjectTypes(binder);
        binder.bind(ValueObjectTypeRegistry.class).to(DefaultValueObjectTypeRegistry.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DefaultDbAdapterFactory factory = new DefaultDbAdapterFactory(detectors);
    injector.injectMembers(factory);
    DbAdapter createdAdapter = factory.createAdapter(new DataNodeDescriptor(), new MockDataSource());
    assertNotNull(createdAdapter);
    assertTrue("Unexpected class: " + createdAdapter.getClass().getName(), createdAdapter instanceof AutoAdapter);
    assertEquals("CREATE TABLE Test ()", 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) ArrayList(java.util.ArrayList) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Test(org.junit.Test)

Example 29 with DataNodeDescriptor

use of org.apache.cayenne.configuration.DataNodeDescriptor in project cayenne by apache.

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Custom.

@Test
public void testCreatedAdapter_Custom() throws Exception {
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setAdapterType(SybaseAdapter.class.getName());
    List<DbAdapterDetector> detectors = new ArrayList<DbAdapterDetector>();
    Module testModule = binder -> {
        ServerModule.contributeProperties(binder);
        ServerModule.contributeDefaultTypes(binder);
        ServerModule.contributeUserTypes(binder);
        ServerModule.contributeTypeFactories(binder);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocator.class);
        binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).to(ClassLoaderResourceLocator.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
        binder.bind(BatchTranslatorFactory.class).toInstance(mock(BatchTranslatorFactory.class));
        ServerModule.contributeValueObjectTypes(binder);
        binder.bind(ValueObjectTypeRegistry.class).to(DefaultValueObjectTypeRegistry.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DefaultDbAdapterFactory factory = new DefaultDbAdapterFactory(detectors);
    injector.injectMembers(factory);
    DbAdapter createdAdapter = factory.createAdapter(nodeDescriptor, new MockDataSource());
    assertNotNull(createdAdapter);
    assertTrue("Unexpected class: " + createdAdapter.getClass().getName(), createdAdapter instanceof SybaseAdapter);
}
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) ArrayList(java.util.ArrayList) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) SybaseAdapter(org.apache.cayenne.dba.sybase.SybaseAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Test(org.junit.Test)

Example 30 with DataNodeDescriptor

use of org.apache.cayenne.configuration.DataNodeDescriptor in project cayenne by apache.

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_AutoExplicit.

@Test
public void testCreatedAdapter_AutoExplicit() 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(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.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);
    DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
    nodeDescriptor.setAdapterType(AutoAdapter.class.getName());
    DbAdapter createdAdapter = factory.createAdapter(nodeDescriptor, 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)

Aggregations

DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)58 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)28 DataMap (org.apache.cayenne.map.DataMap)23 Test (org.junit.Test)22 DbEntity (org.apache.cayenne.map.DbEntity)11 ArrayList (java.util.ArrayList)8 Injector (org.apache.cayenne.di.Injector)8 Constants (org.apache.cayenne.configuration.Constants)7 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)7 DbAdapter (org.apache.cayenne.dba.DbAdapter)7 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)7 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)7 DIBootstrap (org.apache.cayenne.di.DIBootstrap)7 Key (org.apache.cayenne.di.Key)7 Module (org.apache.cayenne.di.Module)7 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)7 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)7 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)7 Slf4jJdbcEventLogger (org.apache.cayenne.log.Slf4jJdbcEventLogger)7 ObjEntity (org.apache.cayenne.map.ObjEntity)7