Search in sources :

Example 16 with DataNodeDescriptor

use of org.apache.cayenne.configuration.DataNodeDescriptor 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 17 with DataNodeDescriptor

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

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Auto.

@Test
public void testCreatedAdapter_Auto() 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(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);
    };
    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) 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)

Example 18 with DataNodeDescriptor

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

the class JNDIDataSourceFactoryIT method testGetDataSource_NameNotBound.

@Test
public void testGetDataSource_NameNotBound() throws Exception {
    DataNodeDescriptor descriptor = new DataNodeDescriptor();
    descriptor.setParameters("jdbc/TestDS");
    JNDISetup.doSetup();
    JNDIDataSourceFactory factory = new JNDIDataSourceFactory();
    injector.injectMembers(factory);
    try {
        factory.getDataSource(descriptor);
        fail("Didn't throw on unbound name");
    } catch (NameNotFoundException e) {
    // expected
    }
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Test(org.junit.Test)

Example 19 with DataNodeDescriptor

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

the class AttributeDisplayEventType method fireLastDisplayEvent.

@Override
public void fireLastDisplayEvent() {
    DataChannelDescriptor dataChannel = (DataChannelDescriptor) controller.getProject().getRootNode();
    if (!dataChannel.getName().equals(preferences.getDomain())) {
        return;
    }
    DataNodeDescriptor dataNode = dataChannel.getNodeDescriptor(preferences.getNode());
    DataMap dataMap = dataChannel.getDataMap(preferences.getDataMap());
    if (dataMap == null) {
        return;
    }
    Entity entity = getLastEntity(dataMap);
    if (entity == null) {
        return;
    }
    Attribute[] attributes = getLastEntityAttributes(entity);
    EntityDisplayEvent entityDisplayEvent = new EntityDisplayEvent(this, entity, dataMap, dataNode, dataChannel);
    AttributeDisplayEvent attributeDisplayEvent = new AttributeDisplayEvent(this, attributes, entity, dataMap, dataChannel);
    if (entity instanceof ObjEntity) {
        controller.fireObjEntityDisplayEvent(entityDisplayEvent);
        controller.fireObjAttributeDisplayEvent(attributeDisplayEvent);
    } else if (entity instanceof DbEntity) {
        controller.fireDbEntityDisplayEvent(entityDisplayEvent);
        controller.fireDbAttributeDisplayEvent(attributeDisplayEvent);
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) Entity(org.apache.cayenne.map.Entity) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) AttributeDisplayEvent(org.apache.cayenne.modeler.event.AttributeDisplayEvent) ObjEntity(org.apache.cayenne.map.ObjEntity) EntityDisplayEvent(org.apache.cayenne.modeler.event.EntityDisplayEvent) DbEntity(org.apache.cayenne.map.DbEntity) Attribute(org.apache.cayenne.map.Attribute) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DataMap(org.apache.cayenne.map.DataMap)

Example 20 with DataNodeDescriptor

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

the class DataNodeDisplayEventType method fireLastDisplayEvent.

@Override
public void fireLastDisplayEvent() {
    DataChannelDescriptor dataChannel = (DataChannelDescriptor) controller.getProject().getRootNode();
    if (!dataChannel.getName().equals(preferences.getDomain())) {
        return;
    }
    DataNodeDescriptor dataNode = dataChannel.getNodeDescriptor(preferences.getNode());
    if (dataNode == null) {
        return;
    }
    DataNodeDisplayEvent dataNodeDisplayEvent = new DataNodeDisplayEvent(this, dataChannel, dataNode);
    controller.fireDataNodeDisplayEvent(dataNodeDisplayEvent);
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DataNodeDisplayEvent(org.apache.cayenne.modeler.event.DataNodeDisplayEvent) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor)

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