Search in sources :

Example 1 with MockDataSourceFactory1

use of org.apache.cayenne.configuration.mock.MockDataSourceFactory1 in project cayenne by apache.

the class DefaultDataSourceFactoryLoaderTest method testGetDataSourceFactory_UnusedProperties.

@Test
public void testGetDataSourceFactory_UnusedProperties() 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(null);
    when(properties.get(Constants.JDBC_USERNAME_PROPERTY)).thenReturn("username");
    when(properties.get(Constants.JDBC_PASSWORD_PROPERTY)).thenReturn("12345");
    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);
    assertFalse(factory instanceof PropertyDataSourceFactory);
    nodeDescriptor.setName("node2");
    when(properties.get(Constants.JDBC_MIN_CONNECTIONS_PROPERTY + ".X.node2")).thenReturn("3");
    when(properties.get(Constants.JDBC_PASSWORD_PROPERTY + ".X.node2")).thenReturn("123456");
    factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
    assertNotNull(factory);
    assertFalse(factory instanceof PropertyDataSourceFactory);
    nodeDescriptor.setName("node3");
    when(properties.get(Constants.JDBC_URL_PROPERTY + ".X.node3")).thenReturn("url");
    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 2 with MockDataSourceFactory1

use of org.apache.cayenne.configuration.mock.MockDataSourceFactory1 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 3 with MockDataSourceFactory1

use of org.apache.cayenne.configuration.mock.MockDataSourceFactory1 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)

Aggregations

DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)3 MockDataSourceFactory1 (org.apache.cayenne.configuration.mock.MockDataSourceFactory1)3 Test (org.junit.Test)3 Constants (org.apache.cayenne.configuration.Constants)2 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)2 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)2 DataSourceInfo (org.apache.cayenne.conn.DataSourceInfo)2 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)2 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)2 DIBootstrap (org.apache.cayenne.di.DIBootstrap)2 Injector (org.apache.cayenne.di.Injector)2 Key (org.apache.cayenne.di.Key)2 Module (org.apache.cayenne.di.Module)2 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)2 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)2 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)2 Slf4jJdbcEventLogger (org.apache.cayenne.log.Slf4jJdbcEventLogger)2 ResourceLocator (org.apache.cayenne.resource.ResourceLocator)2 MockResourceLocator (org.apache.cayenne.resource.mock.MockResourceLocator)2 Assert.assertFalse (org.junit.Assert.assertFalse)2