Search in sources :

Example 81 with Collections

use of java.util.Collections in project cayenne by apache.

the class DataChannelProjectSaverTest method testSaveAs_RecoverFromSaveError.

@Test
public void testSaveAs_RecoverFromSaveError() throws Exception {
    FileProjectSaver saver = new FileProjectSaver(Collections.<ProjectExtension>emptyList()) {

        @Override
        void saveToTempFile(SaveUnit unit, PrintWriter printWriter) {
            throw new CayenneRuntimeException("Test Exception");
        }
    };
    Module testModule = binder -> {
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
        binder.bind(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.class);
        binder.bind(ProjectLoader.class).to(DataChannelProjectLoader.class);
        binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
        binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
        binder.bind(DataChannelMetaData.class).to(NoopDataChannelMetaData.class);
        binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    injector.injectMembers(saver);
    String testConfigName = "PROJECT2";
    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/cayenne-" + testConfigName + ".xml");
    Resource source = new URLResource(url);
    Project project = injector.getInstance(ProjectLoader.class).loadProject(source);
    File outFile = setupTestDirectory("testSaveAs_RecoverFromSaveError");
    assertEquals(0, outFile.list().length);
    try {
        saver.saveAs(project, new URLResource(outFile.toURI().toURL()));
        fail("No exception was thrown..");
    } catch (CayenneRuntimeException e) {
        // expected
        assertEquals(0, outFile.list().length);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) HandlerFactory(org.apache.cayenne.configuration.xml.HandlerFactory) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) Project2Case(org.apache.cayenne.project.unit.Project2Case) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) URL(java.net.URL) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) Module(org.apache.cayenne.di.Module) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) XMLReader(org.xml.sax.XMLReader) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) DIBootstrap(org.apache.cayenne.di.DIBootstrap) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension) PrintWriter(java.io.PrintWriter) URLResource(org.apache.cayenne.resource.URLResource) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DataChannelMetaData(org.apache.cayenne.configuration.xml.DataChannelMetaData) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) File(java.io.File) Resource(org.apache.cayenne.resource.Resource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Assert(org.junit.Assert) Collections(java.util.Collections) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) URLResource(org.apache.cayenne.resource.URLResource) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) File(java.io.File) XMLReader(org.xml.sax.XMLReader) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 82 with Collections

use of java.util.Collections 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)

Example 83 with Collections

use of java.util.Collections in project cayenne by apache.

the class DataContextFactoryTest method testCreateDataContextValidation.

@Test
public void testCreateDataContextValidation() throws Exception {
    final EventManager eventManager = new MockEventManager();
    final DataDomain domain = new DataDomain("d1");
    domain.setValidatingObjectsOnCommit(true);
    Module testModule = binder -> {
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(DataDomain.class).toInstance(domain);
        binder.bind(EventManager.class).toInstance(eventManager);
        binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
        binder.bind(RuntimeProperties.class).toInstance(new DefaultRuntimeProperties(Collections.<String, String>emptyMap()));
        binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
        binder.bind(ObjectStoreFactory.class).to(DefaultObjectStoreFactory.class);
        binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
        binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        binder.bind(DataDomainFlushActionFactory.class).to(DefaultDataDomainFlushActionFactory.class);
        binder.bind(DbRowOpSorter.class).to(DefaultDbRowOpSorter.class);
        binder.bind(EntitySorter.class).to(AshwoodEntitySorter.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    domain.setDataRowStoreFactory(injector.getInstance(DataRowStoreFactory.class));
    DataContextFactory factory = new DataContextFactory();
    injector.injectMembers(factory);
    DataContext c1 = (DataContext) factory.createContext();
    assertTrue(c1.isValidatingObjectsOnCommit());
    domain.setValidatingObjectsOnCommit(false);
    DataContext c2 = (DataContext) factory.createContext();
    assertFalse(c2.isValidatingObjectsOnCommit());
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DataContext(org.apache.cayenne.access.DataContext) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) TransactionManager(org.apache.cayenne.tx.TransactionManager) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) DefaultDataDomainFlushActionFactory(org.apache.cayenne.access.flush.DefaultDataDomainFlushActionFactory) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ObjectMapRetainStrategy(org.apache.cayenne.access.ObjectMapRetainStrategy) ObjectStoreFactory(org.apache.cayenne.configuration.ObjectStoreFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataDomain(org.apache.cayenne.access.DataDomain) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) EntitySorter(org.apache.cayenne.map.EntitySorter) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DefaultDbRowOpSorter(org.apache.cayenne.access.flush.operation.DefaultDbRowOpSorter) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Assert(org.junit.Assert) DbRowOpSorter(org.apache.cayenne.access.flush.operation.DbRowOpSorter) Collections(java.util.Collections) DataDomainFlushActionFactory(org.apache.cayenne.access.flush.DataDomainFlushActionFactory) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) DefaultDataDomainFlushActionFactory(org.apache.cayenne.access.flush.DefaultDataDomainFlushActionFactory) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DataDomain(org.apache.cayenne.access.DataDomain) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DataContext(org.apache.cayenne.access.DataContext) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) AshwoodEntitySorter(org.apache.cayenne.ashwood.AshwoodEntitySorter) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) DefaultDbRowOpSorter(org.apache.cayenne.access.flush.operation.DefaultDbRowOpSorter) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) MockEventManager(org.apache.cayenne.event.MockEventManager) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) Test(org.junit.Test)

Example 84 with Collections

use of java.util.Collections in project meecrowave by apache.

the class ProxyServlet method doRequest.

protected CompletionStage<Response> doRequest(final Routes.Route route, final HttpServletRequest req, final HttpServletResponse response, final String prefix) throws IOException {
    WebTarget target = route.client.target(route.responseConfiguration.target);
    target = target.path(prefix);
    final Map<String, String> queryParams = ofNullable(req.getQueryString()).filter(it -> !it.isEmpty()).map(queries -> Stream.of(queries.split("&")).map(it -> {
        final int eq = it.indexOf('=');
        if (eq > 0) {
            return new AbstractMap.SimpleEntry<>(it.substring(0, eq), it.substring(eq + 1));
        }
        return new AbstractMap.SimpleEntry<>(it, "");
    }).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))).orElseGet(Collections::emptyMap);
    for (final Map.Entry<String, String> q : queryParams.entrySet()) {
        target = target.queryParam(q.getKey(), q.getValue());
    }
    final String type = route.requestConfiguration.addedHeaders != null ? route.requestConfiguration.addedHeaders.getOrDefault("Content-Type", req.getContentType()) : req.getContentType();
    Invocation.Builder request = type != null ? target.request(type) : target.request();
    final Enumeration<String> headerNames = req.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        final String name = headerNames.nextElement();
        if (!filterHeader(route.requestConfiguration.skippedHeaders, name)) {
            request = request.header(name, list(req.getHeaders(name)));
        }
    }
    if (route.requestConfiguration.addedHeaders != null) {
        for (final Map.Entry<String, String> header : route.requestConfiguration.addedHeaders.entrySet()) {
            request = request.header(header.getKey(), header.getValue());
        }
    }
    final Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (final Cookie cookie : cookies) {
            if (filterCookie(route.requestConfiguration.skippedCookies, cookie.getName(), cookie.getValue())) {
                continue;
            }
            request = request.cookie(new javax.ws.rs.core.Cookie(cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getVersion()));
        }
    }
    final CompletionStageRxInvoker rx = request.rx();
    final CompletionStage<Response> result;
    if (isWrite(req)) {
        result = rx.method(req.getMethod(), Entity.entity(req.getInputStream(), ofNullable(req.getContentType()).orElse(MediaType.WILDCARD)));
    } else {
        result = rx.method(req.getMethod());
    }
    return result;
}
Also used : Optional.empty(java.util.Optional.empty) Collections.list(java.util.Collections.list) Routes(org.apache.meecrowave.proxy.servlet.configuration.Routes) Enumeration(java.util.Enumeration) ServletException(javax.servlet.ServletException) ConfigurationLoader(org.apache.meecrowave.proxy.servlet.service.ConfigurationLoader) NewCookie(javax.ws.rs.core.NewCookie) Function(java.util.function.Function) HttpMethod(javax.ws.rs.HttpMethod) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Collectors.toMap(java.util.stream.Collectors.toMap) ServletOutputStream(javax.servlet.ServletOutputStream) Map(java.util.Map) Cookie(javax.servlet.http.Cookie) PrintWriter(java.io.PrintWriter) ServletConfig(javax.servlet.ServletConfig) HttpServlet(javax.servlet.http.HttpServlet) Optional.ofNullable(java.util.Optional.ofNullable) CompletionStageRxInvoker(javax.ws.rs.client.CompletionStageRxInvoker) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Invocation(javax.ws.rs.client.Invocation) Entity(javax.ws.rs.client.Entity) AbstractMap(java.util.AbstractMap) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) WebTarget(javax.ws.rs.client.WebTarget) Collections(java.util.Collections) InputStream(java.io.InputStream) NewCookie(javax.ws.rs.core.NewCookie) Cookie(javax.servlet.http.Cookie) HttpHeaders(javax.ws.rs.core.HttpHeaders) Invocation(javax.ws.rs.client.Invocation) AbstractMap(java.util.AbstractMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Collections(java.util.Collections) CompletionStageRxInvoker(javax.ws.rs.client.CompletionStageRxInvoker) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Example 85 with Collections

use of java.util.Collections in project gradle by gradle.

the class DefaultLocalComponentMetadata method copy.

/**
 * Creates a copy of this metadata, transforming the artifacts and dependencies of this component.
 */
public DefaultLocalComponentMetadata copy(ComponentIdentifier componentIdentifier, Transformer<LocalComponentArtifactMetadata, LocalComponentArtifactMetadata> artifacts) {
    DefaultLocalComponentMetadata copy = new DefaultLocalComponentMetadata(moduleVersionId, componentIdentifier, status, attributesSchema);
    for (DefaultLocalConfigurationMetadata configuration : allConfigurations.values()) {
        copy.addConfiguration(configuration.getName(), configuration.description, configuration.extendsFrom, configuration.hierarchy, configuration.visible, configuration.transitive, configuration.attributes, configuration.canBeConsumed, configuration.consumptionDeprecation, configuration.canBeResolved, configuration.capabilities, Collections::emptyList);
    }
    // Artifacts
    // Keep track of transformed artifacts as a given artifact may appear in multiple variants
    Map<LocalComponentArtifactMetadata, LocalComponentArtifactMetadata> transformedArtifacts = new HashMap<>();
    for (Map.Entry<String, LocalComponentArtifactMetadata> entry : allArtifacts.entries()) {
        LocalComponentArtifactMetadata oldArtifact = entry.getValue();
        LocalComponentArtifactMetadata newArtifact = copyArtifact(oldArtifact, artifacts, transformedArtifacts);
        copy.allArtifacts.put(entry.getKey(), newArtifact);
    }
    // Variants
    for (Map.Entry<String, DefaultVariantMetadata> entry : allVariants.entries()) {
        DefaultVariantMetadata oldVariant = entry.getValue();
        ImmutableList.Builder<LocalComponentArtifactMetadata> newArtifacts = new ImmutableList.Builder<>();
        for (ComponentArtifactMetadata oldArtifact : oldVariant.getArtifacts()) {
            newArtifacts.add(copyArtifact((LocalComponentArtifactMetadata) oldArtifact, artifacts, transformedArtifacts));
        }
        copy.allVariants.put(entry.getKey(), new DefaultVariantMetadata(oldVariant.getName(), oldVariant.getIdentifier(), oldVariant.asDescribable(), oldVariant.getAttributes(), newArtifacts.build(), oldVariant.getCapabilities()));
    }
    for (DefaultLocalConfigurationMetadata configuration : allConfigurations.values()) {
        configuration.realizeDependencies();
        DefaultLocalConfigurationMetadata configurationCopy = copy.allConfigurations.get(configuration.getName());
        // Dependencies
        configurationCopy.definedDependencies.addAll(configuration.definedDependencies);
        configurationCopy.definedFiles.addAll(configuration.definedFiles);
        // Exclude rules
        configurationCopy.definedExcludes.addAll(configuration.definedExcludes);
    }
    return copy;
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) LocalConfigurationMetadataBuilder(org.gradle.api.internal.artifacts.ivyservice.moduleconverter.dependencies.LocalConfigurationMetadataBuilder) DeprecationMessageBuilder(org.gradle.internal.deprecation.DeprecationMessageBuilder) ComponentArtifactMetadata(org.gradle.internal.component.model.ComponentArtifactMetadata) Collections(java.util.Collections) HashMap(java.util.HashMap) Map(java.util.Map) DefaultVariantMetadata(org.gradle.internal.component.model.DefaultVariantMetadata)

Aggregations

Collections (java.util.Collections)113 List (java.util.List)59 ArrayList (java.util.ArrayList)41 Test (org.junit.Test)39 Map (java.util.Map)37 Collectors (java.util.stream.Collectors)34 Arrays (java.util.Arrays)28 HashMap (java.util.HashMap)26 Set (java.util.Set)25 HashSet (java.util.HashSet)23 IOException (java.io.IOException)19 Collection (java.util.Collection)19 Optional (java.util.Optional)19 TimeUnit (java.util.concurrent.TimeUnit)16 URI (java.net.URI)13 Assert (org.junit.Assert)13 Function (java.util.function.Function)12 Stream (java.util.stream.Stream)12 Before (org.junit.Before)12 Logger (org.slf4j.Logger)12