Search in sources :

Example 1 with AshwoodEntitySorter

use of org.apache.cayenne.ashwood.AshwoodEntitySorter in project cayenne by apache.

the class ServerModule method configure.

public void configure(Binder binder) {
    // configure global stack properties
    contributeProperties(binder).put(Constants.SERVER_MAX_ID_QUALIFIER_SIZE_PROPERTY, String.valueOf(DEFAULT_MAX_ID_QUALIFIER_SIZE));
    contributeProperties(binder).put(Constants.SERVER_CONTEXTS_SYNC_PROPERTY, String.valueOf(false));
    binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
    binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
    binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
    // configure known DbAdapter detectors in reverse order of popularity.
    // Users can add their own to install custom adapters automatically
    contributeAdapterDetectors(binder).add(FirebirdSniffer.class).add(OpenBaseSniffer.class).add(FrontBaseSniffer.class).add(IngresSniffer.class).add(SQLiteSniffer.class).add(DB2Sniffer.class).add(H2Sniffer.class).add(HSQLDBSniffer.class).add(SybaseSniffer.class).add(DerbySniffer.class).add(SQLServerSniffer.class).add(OracleSniffer.class).add(PostgresSniffer.class).add(MySQLSniffer.class).add(MariaDBSniffer.class);
    // installing Pk for adapters
    binder.bind(PkGeneratorFactoryProvider.class).to(PkGeneratorFactoryProvider.class);
    binder.bind(PkGenerator.class).to(JdbcPkGenerator.class);
    // set PkGenerators for current Adapters
    contributePkGenerators(binder).put(DB2Adapter.class.getName(), DB2PkGenerator.class).put(DerbyAdapter.class.getName(), DerbyPkGenerator.class).put(FrontBaseAdapter.class.getName(), FrontBasePkGenerator.class).put(H2Adapter.class.getName(), H2PkGenerator.class).put(IngresAdapter.class.getName(), IngresPkGenerator.class).put(MySQLAdapter.class.getName(), MySQLPkGenerator.class).put(OpenBaseAdapter.class.getName(), OpenBasePkGenerator.class).put(OracleAdapter.class.getName(), OraclePkGenerator.class).put(Oracle8Adapter.class.getName(), OraclePkGenerator.class).put(PostgresAdapter.class.getName(), PostgresPkGenerator.class).put(SQLServerAdapter.class.getName(), SybasePkGenerator.class).put(SybaseAdapter.class.getName(), SybasePkGenerator.class);
    // configure a filter chain with only one TransactionFilter as default
    contributeDomainFilters(binder);
    contributeDomainQueryFilters(binder);
    contributeDomainSyncFilters(binder).add(TransactionFilter.class);
    // init listener list
    contributeDomainListeners(binder);
    // configure extended types
    contributeDefaultTypes(binder).add(new VoidType()).add(new BigDecimalType()).add(new BooleanType()).add(new ByteType(false)).add(new CharType(false, true)).add(new DoubleType()).add(new FloatType()).add(new IntegerType()).add(new LongType()).add(new ShortType(false)).add(new ByteArrayType(false, true)).add(new DateType()).add(new TimeType()).add(new TimestampType()).add(new DurationType()).add(new UtilDateType()).add(new CalendarType<>(GregorianCalendar.class)).add(new CalendarType<>(Calendar.class)).add(GeoJsonType.class).add(WktType.class);
    contributeUserTypes(binder);
    contributeTypeFactories(binder).add(new InternalUnsupportedTypeFactory());
    // Custom ValueObjects types contribution
    contributeValueObjectTypes(binder).add(BigIntegerValueType.class).add(BigDecimalValueType.class).add(UUIDValueType.class).add(LocalDateValueType.class).add(LocalTimeValueType.class).add(LocalDateTimeValueType.class).add(PeriodValueType.class).add(CharacterValueType.class);
    binder.bind(ValueObjectTypeRegistry.class).to(DefaultValueObjectTypeRegistry.class);
    binder.bind(ValueComparisonStrategyFactory.class).to(DefaultValueComparisonStrategyFactory.class);
    // configure explicit configurations
    contributeProjectLocations(binder);
    binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
    binder.bind(EventManager.class).toProvider(EventManagerProvider.class);
    binder.bind(QueryCache.class).toProvider(MapQueryCacheProvider.class);
    binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
    binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
    // a service to provide the main stack DataDomain
    binder.bind(DataDomain.class).toProvider(DataDomainProvider.class);
    binder.bind(DataNodeFactory.class).to(DefaultDataNodeFactory.class);
    // will return DataDomain for request for a DataChannel
    binder.bind(DataChannel.class).toProvider(DomainDataChannelProvider.class);
    binder.bind(ObjectContextFactory.class).to(DataContextFactory.class);
    binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
    // a service to load project XML descriptors
    binder.bind(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.class);
    binder.bind(DataChannelDescriptorMerger.class).to(DefaultDataChannelDescriptorMerger.class);
    // a service to load DataMap XML descriptors
    binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
    // a locator of resources, such as XML descriptors
    binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocator.class);
    binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).to(ClassLoaderResourceLocator.class);
    // a global properties object
    binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
    // a service to load DataSourceFactories. DelegatingDataSourceFactory
    // will attempt to find the actual worker factory dynamically on each
    // call depending on DataNodeDescriptor data and the environment
    binder.bind(DataSourceFactory.class).to(DelegatingDataSourceFactory.class);
    binder.bind(SchemaUpdateStrategyFactory.class).to(DefaultSchemaUpdateStrategyFactory.class);
    // a default DBAdapterFactory used to load custom and automatic DbAdapters
    binder.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);
    // binding AshwoodEntitySorter without scope, as this is a stateful object and is
    // configured by the owning domain
    binder.bind(EntitySorter.class).to(AshwoodEntitySorter.class).withoutScope();
    binder.bind(BatchTranslatorFactory.class).to(DefaultBatchTranslatorFactory.class);
    binder.bind(SelectTranslatorFactory.class).to(DefaultSelectTranslatorFactory.class);
    // a default ObjectMapRetainStrategy used to create objects map for
    // ObjectStore
    binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
    // a default ObjectStoreFactory used to create ObjectStores for contexts
    binder.bind(ObjectStoreFactory.class).to(DefaultObjectStoreFactory.class);
    binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
    binder.bind(RowReaderFactory.class).to(DefaultRowReaderFactory.class);
    binder.bind(SQLTemplateProcessor.class).to(CayenneSQLTemplateProcessor.class);
    binder.bind(TemplateContextFactory.class).to(DefaultTemplateContextFactory.class);
    binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
    binder.bind(DataChannelMetaData.class).to(NoopDataChannelMetaData.class);
    binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
    binder.bind(DataDomainFlushActionFactory.class).to(DefaultDataDomainFlushActionFactory.class);
    binder.bind(DbRowOpSorter.class).to(DefaultDbRowOpSorter.class);
}
Also used : SchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.SchemaUpdateStrategyFactory) DefaultSchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.DefaultSchemaUpdateStrategyFactory) QueryCache(org.apache.cayenne.cache.QueryCache) FrontBasePkGenerator(org.apache.cayenne.dba.frontbase.FrontBasePkGenerator) PeriodValueType(org.apache.cayenne.access.types.PeriodValueType) ValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.ValueComparisonStrategyFactory) DefaultValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.DefaultValueComparisonStrategyFactory) DefaultDataDomainFlushActionFactory(org.apache.cayenne.access.flush.DefaultDataDomainFlushActionFactory) DataDomainFlushActionFactory(org.apache.cayenne.access.flush.DataDomainFlushActionFactory) EventBridge(org.apache.cayenne.event.EventBridge) ByteType(org.apache.cayenne.access.types.ByteType) FloatType(org.apache.cayenne.access.types.FloatType) DataChannelDescriptorMerger(org.apache.cayenne.configuration.DataChannelDescriptorMerger) DefaultDataChannelDescriptorMerger(org.apache.cayenne.configuration.DefaultDataChannelDescriptorMerger) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) HandlerFactory(org.apache.cayenne.configuration.xml.HandlerFactory) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) DataChannelMetaData(org.apache.cayenne.configuration.xml.DataChannelMetaData) UtilDateType(org.apache.cayenne.access.types.UtilDateType) UtilDateType(org.apache.cayenne.access.types.UtilDateType) DateType(org.apache.cayenne.access.types.DateType) DB2PkGenerator(org.apache.cayenne.dba.db2.DB2PkGenerator) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) DerbySniffer(org.apache.cayenne.dba.derby.DerbySniffer) OracleAdapter(org.apache.cayenne.dba.oracle.OracleAdapter) DataDomain(org.apache.cayenne.access.DataDomain) GeoJsonType(org.apache.cayenne.access.types.GeoJsonType) ShortType(org.apache.cayenne.access.types.ShortType) ObjectMapRetainStrategy(org.apache.cayenne.access.ObjectMapRetainStrategy) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) MySQLSniffer(org.apache.cayenne.dba.mysql.MySQLSniffer) DB2Sniffer(org.apache.cayenne.dba.db2.DB2Sniffer) ByteArrayType(org.apache.cayenne.access.types.ByteArrayType) TransactionManager(org.apache.cayenne.tx.TransactionManager) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) CharType(org.apache.cayenne.access.types.CharType) BigDecimalType(org.apache.cayenne.access.types.BigDecimalType) IngresSniffer(org.apache.cayenne.dba.ingres.IngresSniffer) VoidType(org.apache.cayenne.access.types.VoidType) DerbyAdapter(org.apache.cayenne.dba.derby.DerbyAdapter) LongType(org.apache.cayenne.access.types.LongType) IngresPkGenerator(org.apache.cayenne.dba.ingres.IngresPkGenerator) ObjectStoreFactory(org.apache.cayenne.configuration.ObjectStoreFactory) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) OraclePkGenerator(org.apache.cayenne.dba.oracle.OraclePkGenerator) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) DerbyPkGenerator(org.apache.cayenne.dba.derby.DerbyPkGenerator) OraclePkGenerator(org.apache.cayenne.dba.oracle.OraclePkGenerator) H2PkGenerator(org.apache.cayenne.dba.h2.H2PkGenerator) PostgresPkGenerator(org.apache.cayenne.dba.postgres.PostgresPkGenerator) OpenBasePkGenerator(org.apache.cayenne.dba.openbase.OpenBasePkGenerator) MySQLPkGenerator(org.apache.cayenne.dba.mysql.MySQLPkGenerator) IngresPkGenerator(org.apache.cayenne.dba.ingres.IngresPkGenerator) DB2PkGenerator(org.apache.cayenne.dba.db2.DB2PkGenerator) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) PkGenerator(org.apache.cayenne.dba.PkGenerator) FrontBasePkGenerator(org.apache.cayenne.dba.frontbase.FrontBasePkGenerator) SybasePkGenerator(org.apache.cayenne.dba.sybase.SybasePkGenerator) BigIntegerValueType(org.apache.cayenne.access.types.BigIntegerValueType) TimeType(org.apache.cayenne.access.types.TimeType) UUIDValueType(org.apache.cayenne.access.types.UUIDValueType) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) TimestampType(org.apache.cayenne.access.types.TimestampType) H2Adapter(org.apache.cayenne.dba.h2.H2Adapter) DefaultTemplateContextFactory(org.apache.cayenne.template.DefaultTemplateContextFactory) TemplateContextFactory(org.apache.cayenne.template.TemplateContextFactory) DurationType(org.apache.cayenne.access.types.DurationType) OpenBasePkGenerator(org.apache.cayenne.dba.openbase.OpenBasePkGenerator) InternalUnsupportedTypeFactory(org.apache.cayenne.access.types.InternalUnsupportedTypeFactory) EventManager(org.apache.cayenne.event.EventManager) LocalTimeValueType(org.apache.cayenne.access.types.LocalTimeValueType) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) BooleanType(org.apache.cayenne.access.types.BooleanType) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) DefaultRowReaderFactory(org.apache.cayenne.access.jdbc.reader.DefaultRowReaderFactory) DefaultDbRowOpSorter(org.apache.cayenne.access.flush.operation.DefaultDbRowOpSorter) DbRowOpSorter(org.apache.cayenne.access.flush.operation.DbRowOpSorter) PostgresAdapter(org.apache.cayenne.dba.postgres.PostgresAdapter) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) IntegerType(org.apache.cayenne.access.types.IntegerType) MySQLAdapter(org.apache.cayenne.dba.mysql.MySQLAdapter) DataChannel(org.apache.cayenne.DataChannel) OpenBaseSniffer(org.apache.cayenne.dba.openbase.OpenBaseSniffer) SybasePkGenerator(org.apache.cayenne.dba.sybase.SybasePkGenerator) SybaseAdapter(org.apache.cayenne.dba.sybase.SybaseAdapter) DoubleType(org.apache.cayenne.access.types.DoubleType) DefaultBatchTranslatorFactory(org.apache.cayenne.access.translator.batch.DefaultBatchTranslatorFactory) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) CayenneSQLTemplateProcessor(org.apache.cayenne.template.CayenneSQLTemplateProcessor) SQLTemplateProcessor(org.apache.cayenne.access.jdbc.SQLTemplateProcessor) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) OracleSniffer(org.apache.cayenne.dba.oracle.OracleSniffer) HSQLDBSniffer(org.apache.cayenne.dba.hsqldb.HSQLDBSniffer) CalendarType(org.apache.cayenne.access.types.CalendarType) DefaultSelectTranslatorFactory(org.apache.cayenne.access.translator.select.DefaultSelectTranslatorFactory) SelectTranslatorFactory(org.apache.cayenne.access.translator.select.SelectTranslatorFactory)

Example 2 with AshwoodEntitySorter

use of org.apache.cayenne.ashwood.AshwoodEntitySorter in project cayenne by apache.

the class SchemaBuilder method rebuildSchema.

/**
 * Completely rebuilds test schema.
 */
// TODO - this method changes the internal state of the object ... refactor
public void rebuildSchema() {
    // generate schema combining all DataMaps that require schema support.
    // Schema generation is done like that instead of per DataMap on demand
    // to avoid conflicts when dropping and generating PK objects.
    DataMap[] maps = new DataMap[MAPS_REQUIRING_SCHEMA_SETUP.length];
    for (int i = 0; i < maps.length; i++) {
        URL mapURL = getClass().getClassLoader().getResource(MAPS_REQUIRING_SCHEMA_SETUP[i]);
        maps[i] = loader.load(new URLResource(mapURL));
    }
    this.domain = new DataDomain("temp");
    domain.setEventManager(new DefaultEventManager(2));
    domain.setEntitySorter(new AshwoodEntitySorter());
    domain.setQueryCache(new MapQueryCache(50));
    try {
        for (DataMap map : maps) {
            initNode(map);
        }
        if ("true".equalsIgnoreCase(System.getProperty(SKIP_SCHEMA_KEY))) {
            logger.info("skipping schema generation... ");
        } else {
            dropSchema();
            dropPKSupport();
            createSchema();
            createPKSupport();
        }
    } catch (Exception e) {
        throw new RuntimeException("Error rebuilding schema", e);
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataDomain(org.apache.cayenne.access.DataDomain) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) URL(java.net.URL) SQLException(java.sql.SQLException) DataMap(org.apache.cayenne.map.DataMap)

Example 3 with AshwoodEntitySorter

use of org.apache.cayenne.ashwood.AshwoodEntitySorter in project cayenne by apache.

the class DbGenerator method prepareDbEntities.

/**
 * Helper method that orders DbEntities to satisfy referential constraints
 * and returns an ordered list. It also filters out DerivedDbEntities.
 */
private void prepareDbEntities(Collection<DbEntity> excludedEntities) {
    if (excludedEntities == null) {
        excludedEntities = Collections.emptyList();
    }
    List<DbEntity> tables = new ArrayList<>();
    List<DbEntity> tablesWithAutoPk = new ArrayList<>();
    for (DbEntity nextEntity : map.getDbEntities()) {
        // tables with no columns are not included
        if (nextEntity.getAttributes().size() == 0) {
            logObj.info("Skipping entity with no attributes: " + nextEntity.getName());
            continue;
        }
        // check if this entity is explicitly excluded
        if (excludedEntities.contains(nextEntity)) {
            continue;
        }
        // tables with invalid DbAttributes are not included
        boolean invalidAttributes = false;
        for (final DbAttribute attr : nextEntity.getAttributes()) {
            if (attr.getType() == TypesMapping.NOT_DEFINED) {
                logObj.info("Skipping entity, attribute type is undefined: " + nextEntity.getName() + "." + attr.getName());
                invalidAttributes = true;
                break;
            }
        }
        if (invalidAttributes) {
            continue;
        }
        tables.add(nextEntity);
        // check if an automatic PK generation can be potentially supported
        // in this entity. For now simply check that the key is not
        // propagated
        Iterator<DbRelationship> relationships = nextEntity.getRelationships().iterator();
        // create a copy of the original PK list,
        // since the list will be modified locally
        List<DbAttribute> pkAttributes = new ArrayList<>(nextEntity.getPrimaryKeys());
        while (pkAttributes.size() > 0 && relationships.hasNext()) {
            DbRelationship nextRelationship = relationships.next();
            if (!nextRelationship.isToMasterPK()) {
                continue;
            }
            // so
            for (DbJoin join : nextRelationship.getJoins()) {
                pkAttributes.remove(join.getSource());
            }
        }
        // is not propagated via relationship
        if (pkAttributes.size() > 0) {
            tablesWithAutoPk.add(nextEntity);
        }
    }
    // sort table list
    if (tables.size() > 1) {
        EntitySorter sorter = new AshwoodEntitySorter();
        sorter.setEntityResolver(new EntityResolver(Collections.singleton(map)));
        sorter.sortDbEntities(tables, false);
    }
    this.dbEntitiesInInsertOrder = tables;
    this.dbEntitiesRequiringAutoPK = tablesWithAutoPk;
}
Also used : EntitySorter(org.apache.cayenne.map.EntitySorter) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DbEntity(org.apache.cayenne.map.DbEntity) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList) DbAttribute(org.apache.cayenne.map.DbAttribute) DbJoin(org.apache.cayenne.map.DbJoin) EntityResolver(org.apache.cayenne.map.EntityResolver)

Example 4 with AshwoodEntitySorter

use of org.apache.cayenne.ashwood.AshwoodEntitySorter in project cayenne by apache.

the class DataDomainProviderTest method testGet.

@Test
public void testGet() {
    // create dependencies
    final String testConfigName = "testConfig";
    final DataChannelDescriptor testDescriptor = new DataChannelDescriptor();
    DataMap map1 = new DataMap("map1");
    testDescriptor.getDataMaps().add(map1);
    DataMap map2 = new DataMap("map2");
    testDescriptor.getDataMaps().add(map2);
    DataNodeDescriptor nodeDescriptor1 = new DataNodeDescriptor();
    nodeDescriptor1.setName("node1");
    nodeDescriptor1.getDataMapNames().add("map1");
    nodeDescriptor1.setAdapterType(OracleAdapter.class.getName());
    nodeDescriptor1.setDataSourceFactoryType(MockDataSourceFactory.class.getName());
    nodeDescriptor1.setParameters("jdbc/testDataNode1");
    nodeDescriptor1.setSchemaUpdateStrategyType(ThrowOnPartialOrCreateSchemaStrategy.class.getName());
    testDescriptor.getNodeDescriptors().add(nodeDescriptor1);
    DataNodeDescriptor nodeDescriptor2 = new DataNodeDescriptor();
    nodeDescriptor2.setName("node2");
    nodeDescriptor2.getDataMapNames().add("map2");
    nodeDescriptor2.setParameters("testDataNode2.driver.xml");
    testDescriptor.getNodeDescriptors().add(nodeDescriptor2);
    final DataChannelDescriptorLoader testLoader = new DataChannelDescriptorLoader() {

        @Override
        public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
            return new ConfigurationTree<>(testDescriptor, null);
        }
    };
    final EventManager eventManager = new MockEventManager();
    final TestListener mockListener = new TestListener();
    Module testModule = binder -> {
        final ClassLoaderManager classLoaderManager = new DefaultClassLoaderManager();
        binder.bind(ClassLoaderManager.class).toInstance(classLoaderManager);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        ServerModule.contributeProperties(binder);
        ServerModule.contributeAdapterDetectors(binder).add(FirebirdSniffer.class).add(OpenBaseSniffer.class).add(FrontBaseSniffer.class).add(IngresSniffer.class).add(SQLiteSniffer.class).add(DB2Sniffer.class).add(H2Sniffer.class).add(HSQLDBSniffer.class).add(SybaseSniffer.class).add(DerbySniffer.class).add(SQLServerSniffer.class).add(OracleSniffer.class).add(PostgresSniffer.class).add(MySQLSniffer.class).add(MariaDBSniffer.class);
        ServerModule.contributeDomainFilters(binder);
        ServerModule.contributeDomainQueryFilters(binder);
        ServerModule.contributeDomainSyncFilters(binder);
        ServerModule.contributeDomainListeners(binder).add(mockListener);
        ServerModule.contributeProjectLocations(binder).add(testConfigName);
        binder.bind(PkGenerator.class).to(JdbcPkGenerator.class);
        binder.bind(PkGeneratorFactoryProvider.class).to(PkGeneratorFactoryProvider.class);
        ServerModule.contributePkGenerators(binder).put(DB2Adapter.class.getName(), DB2PkGenerator.class).put(DerbyAdapter.class.getName(), DerbyPkGenerator.class).put(FrontBaseAdapter.class.getName(), FrontBasePkGenerator.class).put(H2Adapter.class.getName(), H2PkGenerator.class).put(IngresAdapter.class.getName(), IngresPkGenerator.class).put(MySQLAdapter.class.getName(), MySQLPkGenerator.class).put(OpenBaseAdapter.class.getName(), OpenBasePkGenerator.class).put(OracleAdapter.class.getName(), OraclePkGenerator.class).put(Oracle8Adapter.class.getName(), OraclePkGenerator.class).put(PostgresAdapter.class.getName(), PostgresPkGenerator.class).put(SQLServerAdapter.class.getName(), SybasePkGenerator.class).put(SybaseAdapter.class.getName(), SybasePkGenerator.class);
        // configure extended types
        ServerModule.contributeDefaultTypes(binder);
        ServerModule.contributeUserTypes(binder);
        ServerModule.contributeTypeFactories(binder);
        binder.bind(EventManager.class).toInstance(eventManager);
        binder.bind(EntitySorter.class).toInstance(new AshwoodEntitySorter());
        binder.bind(SchemaUpdateStrategyFactory.class).to(DefaultSchemaUpdateStrategyFactory.class);
        final ResourceLocator locator = new ClassLoaderResourceLocator(classLoaderManager) {

            public Collection<Resource> findResources(String name) {
                // if this is the request we are getting, just let it go through..
                if (name.endsWith("types.xml")) {
                    return super.findResources(name);
                }
                assertEquals(testConfigName, name);
                return Collections.<Resource>singleton(new MockResource());
            }
        };
        binder.bind(ResourceLocator.class).toInstance(locator);
        binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).toInstance(locator);
        binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
        binder.bind(DataChannelDescriptorMerger.class).to(DefaultDataChannelDescriptorMerger.class);
        binder.bind(DataChannelDescriptorLoader.class).toInstance(testLoader);
        binder.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
        binder.bind(BatchTranslatorFactory.class).to(DefaultBatchTranslatorFactory.class);
        binder.bind(SelectTranslatorFactory.class).to(DefaultSelectTranslatorFactory.class);
        binder.bind(DataSourceFactory.class).toInstance(new MockDataSourceFactory());
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(QueryCache.class).toInstance(mock(QueryCache.class));
        binder.bind(RowReaderFactory.class).toInstance(mock(RowReaderFactory.class));
        binder.bind(DataNodeFactory.class).to(DefaultDataNodeFactory.class);
        binder.bind(SQLTemplateProcessor.class).toInstance(mock(SQLTemplateProcessor.class));
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        ServerModule.contributeValueObjectTypes(binder);
        binder.bind(ValueObjectTypeRegistry.class).to(DefaultValueObjectTypeRegistry.class);
        binder.bind(ValueComparisonStrategyFactory.class).to(DefaultValueComparisonStrategyFactory.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    // create and initialize provide instance to test
    DataDomainProvider provider = new DataDomainProvider();
    injector.injectMembers(provider);
    DataChannel channel = provider.get();
    assertNotNull(channel);
    assertTrue(channel instanceof DataDomain);
    DataDomain domain = (DataDomain) channel;
    assertSame(eventManager, domain.getEventManager());
    assertEquals(2, domain.getDataMaps().size());
    assertTrue(domain.getDataMaps().contains(map1));
    assertTrue(domain.getDataMaps().contains(map2));
    assertEquals(2, domain.getDataNodes().size());
    DataNode node1 = domain.getDataNode("node1");
    assertNotNull(node1);
    assertEquals(1, node1.getDataMaps().size());
    assertSame(map1, node1.getDataMaps().iterator().next());
    assertSame(node1, domain.lookupDataNode(map1));
    assertEquals(nodeDescriptor1.getDataSourceFactoryType(), node1.getDataSourceFactory());
    assertNotNull(node1.getDataSource());
    assertNotNull(node1.getSchemaUpdateStrategy());
    assertEquals(nodeDescriptor1.getSchemaUpdateStrategyType(), node1.getSchemaUpdateStrategy().getClass().getName());
    assertNotNull(node1.getAdapter());
    assertEquals(OracleAdapter.class, node1.getAdapter().getClass());
    DataNode node2 = domain.getDataNode("node2");
    assertNotNull(node2);
    assertEquals(1, node2.getDataMaps().size());
    assertSame(map2, node2.getDataMaps().iterator().next());
    assertSame(node2, domain.lookupDataNode(map2));
    assertNull(node2.getDataSourceFactory());
    assertNotNull(node2.getDataSource());
    assertNotNull(node2.getSchemaUpdateStrategy());
    assertEquals(SkipSchemaUpdateStrategy.class.getName(), node2.getSchemaUpdateStrategy().getClass().getName());
    assertNotNull(node2.getAdapter());
    // check that we have mock listener passed correctly
    Persistent mockPersistent = mock(Persistent.class);
    ObjectId mockObjectId = mock(ObjectId.class);
    when(mockObjectId.getEntityName()).thenReturn("mock-entity-name");
    when(mockPersistent.getObjectId()).thenReturn(mockObjectId);
    domain.getEntityResolver().getCallbackRegistry().performCallbacks(LifecycleEvent.POST_LOAD, mockPersistent);
    assertEquals("Should call postLoadCallback() method", 1, TestListener.counter.get());
}
Also used : SchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.SchemaUpdateStrategyFactory) DerbySniffer(org.apache.cayenne.dba.derby.DerbySniffer) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DerbyPkGenerator(org.apache.cayenne.dba.derby.DerbyPkGenerator) Module(org.apache.cayenne.di.Module) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) OraclePkGenerator(org.apache.cayenne.dba.oracle.OraclePkGenerator) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) EntitySorter(org.apache.cayenne.map.EntitySorter) H2PkGenerator(org.apache.cayenne.dba.h2.H2PkGenerator) DefaultBatchTranslatorFactory(org.apache.cayenne.access.translator.batch.DefaultBatchTranslatorFactory) FrontBaseSniffer(org.apache.cayenne.dba.frontbase.FrontBaseSniffer) PostgresPkGenerator(org.apache.cayenne.dba.postgres.PostgresPkGenerator) H2Sniffer(org.apache.cayenne.dba.h2.H2Sniffer) SybaseSniffer(org.apache.cayenne.dba.sybase.SybaseSniffer) PostLoad(org.apache.cayenne.annotation.PostLoad) Mockito.mock(org.mockito.Mockito.mock) DataChannelDescriptorMerger(org.apache.cayenne.configuration.DataChannelDescriptorMerger) OpenBasePkGenerator(org.apache.cayenne.dba.openbase.OpenBasePkGenerator) DefaultSelectTranslatorFactory(org.apache.cayenne.access.translator.select.DefaultSelectTranslatorFactory) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) MySQLPkGenerator(org.apache.cayenne.dba.mysql.MySQLPkGenerator) OpenBaseSniffer(org.apache.cayenne.dba.openbase.OpenBaseSniffer) DefaultSchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.DefaultSchemaUpdateStrategyFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) DerbyAdapter(org.apache.cayenne.dba.derby.DerbyAdapter) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) IngresAdapter(org.apache.cayenne.dba.ingres.IngresAdapter) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) FirebirdSniffer(org.apache.cayenne.dba.firebird.FirebirdSniffer) IngresPkGenerator(org.apache.cayenne.dba.ingres.IngresPkGenerator) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) SkipSchemaUpdateStrategy(org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ObjectId(org.apache.cayenne.ObjectId) Assert(org.junit.Assert) DataMap(org.apache.cayenne.map.DataMap) DataNode(org.apache.cayenne.access.DataNode) RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DefaultDataChannelDescriptorMerger(org.apache.cayenne.configuration.DefaultDataChannelDescriptorMerger) MySQLSniffer(org.apache.cayenne.dba.mysql.MySQLSniffer) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DB2Adapter(org.apache.cayenne.dba.db2.DB2Adapter) OracleAdapter(org.apache.cayenne.dba.oracle.OracleAdapter) OracleSniffer(org.apache.cayenne.dba.oracle.OracleSniffer) DataDomain(org.apache.cayenne.access.DataDomain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThrowOnPartialOrCreateSchemaStrategy(org.apache.cayenne.access.dbsync.ThrowOnPartialOrCreateSchemaStrategy) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) SQLServerSniffer(org.apache.cayenne.dba.sqlserver.SQLServerSniffer) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Key(org.apache.cayenne.di.Key) MariaDBSniffer(org.apache.cayenne.dba.mariadb.MariaDBSniffer) ValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.ValueComparisonStrategyFactory) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) DB2Sniffer(org.apache.cayenne.dba.db2.DB2Sniffer) Collection(java.util.Collection) MySQLAdapter(org.apache.cayenne.dba.mysql.MySQLAdapter) HSQLDBSniffer(org.apache.cayenne.dba.hsqldb.HSQLDBSniffer) PostgresAdapter(org.apache.cayenne.dba.postgres.PostgresAdapter) Oracle8Adapter(org.apache.cayenne.dba.oracle.Oracle8Adapter) Resource(org.apache.cayenne.resource.Resource) ConfigurationException(org.apache.cayenne.ConfigurationException) DB2PkGenerator(org.apache.cayenne.dba.db2.DB2PkGenerator) SQLiteSniffer(org.apache.cayenne.dba.sqlite.SQLiteSniffer) LifecycleEvent(org.apache.cayenne.map.LifecycleEvent) DefaultValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.DefaultValueComparisonStrategyFactory) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) MockResource(org.apache.cayenne.resource.mock.MockResource) PkGenerator(org.apache.cayenne.dba.PkGenerator) IngresSniffer(org.apache.cayenne.dba.ingres.IngresSniffer) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) PostgresSniffer(org.apache.cayenne.dba.postgres.PostgresSniffer) QueryCache(org.apache.cayenne.cache.QueryCache) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) Persistent(org.apache.cayenne.Persistent) SQLServerAdapter(org.apache.cayenne.dba.sqlserver.SQLServerAdapter) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) FrontBasePkGenerator(org.apache.cayenne.dba.frontbase.FrontBasePkGenerator) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) SQLTemplateProcessor(org.apache.cayenne.access.jdbc.SQLTemplateProcessor) MockDataSourceFactory(org.apache.cayenne.configuration.mock.MockDataSourceFactory) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Constants(org.apache.cayenne.configuration.Constants) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) SybaseAdapter(org.apache.cayenne.dba.sybase.SybaseAdapter) Collections(java.util.Collections) SelectTranslatorFactory(org.apache.cayenne.access.translator.select.SelectTranslatorFactory) H2Adapter(org.apache.cayenne.dba.h2.H2Adapter) SybasePkGenerator(org.apache.cayenne.dba.sybase.SybasePkGenerator) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) SkipSchemaUpdateStrategy(org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy) H2PkGenerator(org.apache.cayenne.dba.h2.H2PkGenerator) DataMap(org.apache.cayenne.map.DataMap) DefaultValueComparisonStrategyFactory(org.apache.cayenne.reflect.generic.DefaultValueComparisonStrategyFactory) DefaultSchemaUpdateStrategyFactory(org.apache.cayenne.access.dbsync.DefaultSchemaUpdateStrategyFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) DataNode(org.apache.cayenne.access.DataNode) PostgresPkGenerator(org.apache.cayenne.dba.postgres.PostgresPkGenerator) H2Sniffer(org.apache.cayenne.dba.h2.H2Sniffer) DerbyPkGenerator(org.apache.cayenne.dba.derby.DerbyPkGenerator) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultBatchTranslatorFactory(org.apache.cayenne.access.translator.batch.DefaultBatchTranslatorFactory) FirebirdSniffer(org.apache.cayenne.dba.firebird.FirebirdSniffer) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) OracleAdapter(org.apache.cayenne.dba.oracle.OracleAdapter) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) DataDomain(org.apache.cayenne.access.DataDomain) ThrowOnPartialOrCreateSchemaStrategy(org.apache.cayenne.access.dbsync.ThrowOnPartialOrCreateSchemaStrategy) DefaultDataChannelDescriptorMerger(org.apache.cayenne.configuration.DefaultDataChannelDescriptorMerger) Resource(org.apache.cayenne.resource.Resource) MockResource(org.apache.cayenne.resource.mock.MockResource) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) MySQLPkGenerator(org.apache.cayenne.dba.mysql.MySQLPkGenerator) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) Collection(java.util.Collection) Module(org.apache.cayenne.di.Module) SybaseSniffer(org.apache.cayenne.dba.sybase.SybaseSniffer) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) MockEventManager(org.apache.cayenne.event.MockEventManager) FrontBaseSniffer(org.apache.cayenne.dba.frontbase.FrontBaseSniffer) MariaDBSniffer(org.apache.cayenne.dba.mariadb.MariaDBSniffer) SQLServerAdapter(org.apache.cayenne.dba.sqlserver.SQLServerAdapter) ConfigurationTree(org.apache.cayenne.configuration.ConfigurationTree) PostgresSniffer(org.apache.cayenne.dba.postgres.PostgresSniffer) OraclePkGenerator(org.apache.cayenne.dba.oracle.OraclePkGenerator) MockResource(org.apache.cayenne.resource.mock.MockResource) MockDataSourceFactory(org.apache.cayenne.configuration.mock.MockDataSourceFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) SQLiteSniffer(org.apache.cayenne.dba.sqlite.SQLiteSniffer) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) SQLServerSniffer(org.apache.cayenne.dba.sqlserver.SQLServerSniffer) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) DefaultSelectTranslatorFactory(org.apache.cayenne.access.translator.select.DefaultSelectTranslatorFactory) ObjectId(org.apache.cayenne.ObjectId) IngresAdapter(org.apache.cayenne.dba.ingres.IngresAdapter) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Persistent(org.apache.cayenne.Persistent) DataChannel(org.apache.cayenne.DataChannel) SybasePkGenerator(org.apache.cayenne.dba.sybase.SybasePkGenerator) Oracle8Adapter(org.apache.cayenne.dba.oracle.Oracle8Adapter) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) DB2Adapter(org.apache.cayenne.dba.db2.DB2Adapter) Test(org.junit.Test)

Aggregations

AshwoodEntitySorter (org.apache.cayenne.ashwood.AshwoodEntitySorter)4 DataDomain (org.apache.cayenne.access.DataDomain)3 DataChannel (org.apache.cayenne.DataChannel)2 DataRowStoreFactory (org.apache.cayenne.access.DataRowStoreFactory)2 DefaultDataRowStoreFactory (org.apache.cayenne.access.DefaultDataRowStoreFactory)2 DefaultSchemaUpdateStrategyFactory (org.apache.cayenne.access.dbsync.DefaultSchemaUpdateStrategyFactory)2 SchemaUpdateStrategyFactory (org.apache.cayenne.access.dbsync.SchemaUpdateStrategyFactory)2 SQLTemplateProcessor (org.apache.cayenne.access.jdbc.SQLTemplateProcessor)2 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)2 BatchTranslatorFactory (org.apache.cayenne.access.translator.batch.BatchTranslatorFactory)2 DefaultBatchTranslatorFactory (org.apache.cayenne.access.translator.batch.DefaultBatchTranslatorFactory)2 DefaultSelectTranslatorFactory (org.apache.cayenne.access.translator.select.DefaultSelectTranslatorFactory)2 SelectTranslatorFactory (org.apache.cayenne.access.translator.select.SelectTranslatorFactory)2 DefaultValueObjectTypeRegistry (org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry)2 EntitySorter (org.apache.cayenne.map.EntitySorter)2 URL (java.net.URL)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1