use of com.mockrunner.mock.jdbc.MockDataSource 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")));
}
use of com.mockrunner.mock.jdbc.MockDataSource 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")));
}
use of com.mockrunner.mock.jdbc.MockDataSource 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")));
}
use of com.mockrunner.mock.jdbc.MockDataSource 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<>();
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);
binder.bind(ValueComparisonStrategyFactory.class).to(DefaultValueComparisonStrategyFactory.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 com.mockrunner.mock.jdbc.MockDataSource in project cayenne by apache.
the class JNDIDataSourceFactoryIT method testGetDataSource_NameBound.
@Test
public void testGetDataSource_NameBound() throws Exception {
DataNodeDescriptor descriptor = new DataNodeDescriptor();
descriptor.setParameters("jdbc/TestDS");
JNDISetup.doSetup();
MockDataSource dataSource = new MockDataSource();
InitialContext context = new InitialContext();
context.bind(descriptor.getParameters(), dataSource);
try {
JNDIDataSourceFactory factory = new JNDIDataSourceFactory();
injector.injectMembers(factory);
assertSame(dataSource, factory.getDataSource(descriptor));
} finally {
// since the context is shared, must clear it after the test
context.unbind(descriptor.getParameters());
}
}
Aggregations