use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class DefaultDbAdapterFactory method detectAdapter.
protected DbAdapter detectAdapter(DatabaseMetaData metaData) throws SQLException {
// default ones configured in constructor
for (int i = detectors.size() - 1; i >= 0; i--) {
DbAdapterDetector detector = detectors.get(i);
DbAdapter adapter = detector.createAdapter(metaData);
if (adapter != null) {
jdbcEventLogger.log("Detected and installed adapter: " + adapter.getClass().getName());
// TODO: should detector do this??
injector.injectMembers(adapter);
return adapter;
}
}
return defaultAdapter();
}
use of org.apache.cayenne.dba.DbAdapter 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")));
}
use of org.apache.cayenne.dba.DbAdapter 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);
}
use of org.apache.cayenne.dba.DbAdapter 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")));
}
use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class DefaultBatchTranslatorIT method testConstructor.
@Test
public void testConstructor() throws Exception {
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, null) {
@Override
protected String createSql() {
return null;
}
@Override
protected DbAttributeBinding[] createBindings() {
return new DbAttributeBinding[0];
}
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
return new DbAttributeBinding[0];
}
};
assertSame(adapter, builder.adapter);
}
Aggregations