Search in sources :

Example 1 with AutoAdapter

use of org.apache.cayenne.dba.AutoAdapter in project cayenne by apache.

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Auto.

@Test
public void testCreatedAdapter_Auto() throws Exception {
    final DbAdapter adapter = mock(JdbcAdapter.class);
    when(adapter.createTable(any(DbEntity.class))).thenReturn("XXXXX");
    when(adapter.unwrap()).thenReturn(adapter);
    List<DbAdapterDetector> detectors = new ArrayList<>();
    detectors.add(md -> adapter);
    MockConnection connection = new MockConnection();
    MockDataSource dataSource = new MockDataSource();
    dataSource.setupConnection(connection);
    Module testModule = binder -> {
        ServerModule.contributeProperties(binder);
        ServerModule.contributePkGenerators(binder);
        binder.bind(PkGenerator.class).to(JdbcPkGenerator.class);
        binder.bind(PkGeneratorFactoryProvider.class).to(PkGeneratorFactoryProvider.class);
        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);
        binder.bind(BatchTranslatorFactory.class).toInstance(mock(BatchTranslatorFactory.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) PkGenerator(org.apache.cayenne.dba.PkGenerator) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) Module(org.apache.cayenne.di.Module) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) 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) ValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.ValueComparisonStrategyFactory) 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) DefaultValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.DefaultValueComparisonStrategyFactory) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) 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) 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) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Test(org.junit.Test)

Example 2 with AutoAdapter

use of org.apache.cayenne.dba.AutoAdapter 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 3 with AutoAdapter

use of org.apache.cayenne.dba.AutoAdapter 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);
        ServerModule.contributePkGenerators(binder);
        binder.bind(PkGenerator.class).to(JdbcPkGenerator.class);
        binder.bind(PkGeneratorFactoryProvider.class).to(PkGeneratorFactoryProvider.class);
        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) PkGenerator(org.apache.cayenne.dba.PkGenerator) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) Module(org.apache.cayenne.di.Module) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) 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) ValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.ValueComparisonStrategyFactory) 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) DefaultValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.DefaultValueComparisonStrategyFactory) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) 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) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) 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 4 with AutoAdapter

use of org.apache.cayenne.dba.AutoAdapter in project cayenne by apache.

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_AutoExplicit.

@Test
public void testCreatedAdapter_AutoExplicit() throws Exception {
    final DbAdapter adapter = mock(JdbcAdapter.class);
    when(adapter.createTable(any(DbEntity.class))).thenReturn("XXXXX");
    when(adapter.unwrap()).thenReturn(adapter);
    List<DbAdapterDetector> detectors = new ArrayList<>();
    detectors.add(md -> adapter);
    MockConnection connection = new MockConnection();
    MockDataSource dataSource = new MockDataSource();
    dataSource.setupConnection(connection);
    Module testModule = binder -> {
        ServerModule.contributeProperties(binder);
        ServerModule.contributePkGenerators(binder);
        binder.bind(PkGenerator.class).to(JdbcPkGenerator.class);
        binder.bind(PkGeneratorFactoryProvider.class).to(PkGeneratorFactoryProvider.class);
        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);
        binder.bind(BatchTranslatorFactory.class).toInstance(mock(BatchTranslatorFactory.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) PkGenerator(org.apache.cayenne.dba.PkGenerator) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) Module(org.apache.cayenne.di.Module) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) 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) ValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.ValueComparisonStrategyFactory) 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) DefaultValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.DefaultValueComparisonStrategyFactory) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) 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) 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) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Test(org.junit.Test)

Example 5 with AutoAdapter

use of org.apache.cayenne.dba.AutoAdapter in project cayenne by apache.

the class ToolsModuleTest method testDbApdater.

@Test
public void testDbApdater() throws Exception {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new ToolsModule(log));
    DbAdapterFactory factory = i.getInstance(DbAdapterFactory.class);
    DataNodeDescriptor nodeDescriptor = mock(DataNodeDescriptor.class);
    DataSource dataSource = mock(DataSource.class);
    assertTrue(factory.createAdapter(nodeDescriptor, dataSource) instanceof AutoAdapter);
}
Also used : DefaultDbAdapterFactory(org.apache.cayenne.configuration.server.DefaultDbAdapterFactory) DbAdapterFactory(org.apache.cayenne.configuration.server.DbAdapterFactory) Injector(org.apache.cayenne.di.Injector) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Logger(org.slf4j.Logger) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

AutoAdapter (org.apache.cayenne.dba.AutoAdapter)5 Injector (org.apache.cayenne.di.Injector)5 Test (org.junit.Test)5 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)4 DbAdapter (org.apache.cayenne.dba.DbAdapter)4 MockConnection (com.mockrunner.mock.jdbc.MockConnection)3 MockDataSource (com.mockrunner.mock.jdbc.MockDataSource)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BatchTranslatorFactory (org.apache.cayenne.access.translator.batch.BatchTranslatorFactory)3 DefaultValueObjectTypeRegistry (org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry)3 ValueObjectTypeRegistry (org.apache.cayenne.access.types.ValueObjectTypeRegistry)3 Constants (org.apache.cayenne.configuration.Constants)3 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)3 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)3 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)3 JdbcPkGenerator (org.apache.cayenne.dba.JdbcPkGenerator)3 PkGenerator (org.apache.cayenne.dba.PkGenerator)3 SybaseAdapter (org.apache.cayenne.dba.sybase.SybaseAdapter)3 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)3