예제 #1
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void GetValuesByType()
        {
            var cat = CatalogFactory.CreateDefaultAttributed();

            var container = new CompositionContainer(cat);

            string itestName = AttributedModelServices.GetContractName(typeof(ITest));

            var e1 = container.GetExportedValues <ITest>();
            var e2 = container.GetExports <ITest, object>(itestName);

            Assert.IsType <T1>(e1.First());
            Assert.IsType <T2>(e1.Skip(1).First());

            Assert.IsType <T1>(e2.First().Value);
            Assert.IsType <T2>(e2.Skip(1).First().Value);

            CompositionContainer childContainer = new CompositionContainer(container);
            CompositionBatch     batch          = new CompositionBatch();

            batch.AddPart(new T1());
            container.Compose(batch);
            var t1 = childContainer.GetExportedValue <ITest>();
            var t2 = childContainer.GetExport <ITest, object>(itestName);

            Assert.IsType <T1>(t1);
            Assert.IsType <T1>(t2.Value);
        }
예제 #2
0
        [ActiveIssue(25498, TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void ImportCompletedBindChildIndirectlyThroughParentContainerBind()
        {
            var cat    = CatalogFactory.CreateDefaultAttributed();
            var parent = new CompositionContainer(cat);

            CompositionBatch parentBatch = new CompositionBatch();
            CompositionBatch childBatch  = new CompositionBatch();

            parentBatch.AddExportedValue <ICompositionService>(parent);
            parent.Compose(parentBatch);
            var child = new CompositionContainer(parent);

            var parentImporter = new MyNotifyImportImporter(parent);
            var childImporter  = new MyNotifyImportImporter(child);

            parentBatch = new CompositionBatch();
            parentBatch.AddPart(parentImporter);
            childBatch.AddParts(childImporter, new MyNotifyImportExporter());

            parent.Compose(parentBatch);
            child.Compose(childBatch);

            Assert.Equal(1, parentImporter.ImportCompletedCallCount);
            Assert.Equal(1, childImporter.ImportCompletedCallCount);

            MyNotifyImportExporter parentExporter = parent.GetExportedValue <MyNotifyImportExporter>("MyNotifyImportExporter");

            Assert.Equal(1, parentExporter.ImportCompletedCallCount);

            MyNotifyImportExporter childExporter = child.GetExportedValue <MyNotifyImportExporter>("MyNotifyImportExporter");

            Assert.Equal(1, childExporter.ImportCompletedCallCount);
        }
        public void TestInternalImportsExpectingPrivateExportsFromCatalog()
        {
            var cat       = CatalogFactory.CreateDefaultAttributed();
            var container = new CompositionContainer(cat);

            container.GetExportedValue <InternalImportsExpectingPrivateExports>().VerifyIsBound();
        }
예제 #4
0
        public void GetValuesByType()
        {
            var cat = CatalogFactory.CreateDefaultAttributed();

            var container = new CompositionContainer(cat);

            string itestName = AttributedModelServices.GetContractName(typeof(ITest));

            var e1 = container.GetExportedValues <ITest>();
            var e2 = container.GetExports <ITest, object>(itestName);

            Assert.IsInstanceOfType(e1.First(), typeof(T1), "First should be T1");
            Assert.IsInstanceOfType(e1.Skip(1).First(), typeof(T2), "Second should be T2");

            Assert.IsInstanceOfType(e2.First().Value, typeof(T1), "First should be T1");
            Assert.IsInstanceOfType(e2.Skip(1).First().Value, typeof(T2), "Second should be T2");

            CompositionContainer childContainer = new CompositionContainer(container);
            CompositionBatch     batch          = new CompositionBatch();

            batch.AddPart(new T1());
            container.Compose(batch);
            var t1 = childContainer.GetExportedValue <ITest>();
            var t2 = childContainer.GetExport <ITest, object>(itestName);

            Assert.IsInstanceOfType(t1, typeof(T1), "First (resolved) should be T1");
            Assert.IsInstanceOfType(t2.Value, typeof(T1), "First (resolved) should be T1");
        }
예제 #5
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void ValueTypeFromCatalog()
        {
            var catalog         = CatalogFactory.CreateDefaultAttributed();
            var container       = new CompositionContainer(catalog);
            int singletonResult = container.GetExportedValue <int>("{AssemblyCatalogResolver}SingletonValueType");

            Assert.Equal(17, singletonResult);
            int factoryResult = container.GetExportedValue <int>("{AssemblyCatalogResolver}FactoryValueType");

            Assert.Equal(18, factoryResult);
        }
예제 #6
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void ComponentCatalogResolverGetStaticExport()
        {
            var catalog = CatalogFactory.CreateDefaultAttributed();
            var catalogExportProvider = new CatalogExportProvider(catalog);

            catalogExportProvider.SourceProvider = catalogExportProvider;

            var exports = catalogExportProvider.GetExports(ImportFromContract("StaticString"));

            Assert.Equal(1, exports.Count());
            Assert.Equal("StaticString", exports.First().Value);
        }
예제 #7
0
        public void ImportCollectionsFromCatalogOnly()
        {
            var      cat       = CatalogFactory.CreateDefaultAttributed();
            var      container = new CompositionContainer(cat);
            Importer importer  = new Importer();

            CompositionBatch batch = new CompositionBatch();

            batch.AddParts(importer);
            container.Compose(batch);

            importer.VerifyImport(21, 42);
        }
예제 #8
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void BasicTestWithRequiredMetadata_WrongTypeConstraint()
        {
            var catalog = CatalogFactory.CreateDefaultAttributed();
            var catalogExportProvider = new CatalogExportProvider(catalog);

            catalogExportProvider.SourceProvider = catalogExportProvider;

            Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithNoFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count());

            Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count());
            Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithFoo", new string[] { "Foo", "Bar" }, new Type[] { typeof(int), typeof(int) })).Count());

            Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithNoFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count());
            Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count());
        }
예제 #9
0
        public void ImportCollectionsFormContainerAndCatalog()
        {
            var      cat       = CatalogFactory.CreateDefaultAttributed();
            var      container = new CompositionContainer(cat);
            Importer importer  = new Importer();

            CompositionBatch batch = new CompositionBatch();

            batch.AddParts(importer
                           , new ExporterDefault21(22)
                           , new ExporterDefault42(43));

            container.Compose(batch);

            importer.VerifyImport(22, 43, 21, 42);
        }
예제 #10
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void ComponentCatalogResolverComponentCatalogExportReference()
        {
            var catalog = CatalogFactory.CreateDefaultAttributed();
            var catalogExportProvider = new CatalogExportProvider(catalog);

            catalogExportProvider.SourceProvider = catalogExportProvider;

            var exports = catalogExportProvider.GetExports(ImportFromContract(AttributedModelServices.GetContractName(typeof(MyExporterWithValidMetadata))));

            Assert.Equal(1, exports.Count());

            var export = exports.First();

            Assert.Equal("world", export.Metadata["hello"]);

            Assert.IsType <MyExporterWithValidMetadata>(export.Value);
        }
예제 #11
0
        [ActiveIssue(25498, TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void ImportCompletedGetExportedValueEager()
        {
            var cat = CatalogFactory.CreateDefaultAttributed();
            CompositionContainer container = new CompositionContainer(cat);

            NotifyImportExportee.InstanceCount = 0;
            var notifyee = container.GetExportedValue <NotifyImportExportsEager>("NotifyImportExportsEager");

            Assert.NotNull(notifyee);
            Assert.NotNull(notifyee.Imports);
            Assert.Equal(3, notifyee.Imports.Count);
            Assert.Equal(2, NotifyImportExportee.InstanceCount);
            Assert.Equal(2, notifyee.realImports.Count);
            Assert.Equal(2, notifyee.RealImports.Count);
            Assert.Equal(1, notifyee.RealImports[0].Id);
            Assert.Equal(3, notifyee.RealImports[1].Id);
            Assert.Equal(2, NotifyImportExportee.InstanceCount);
        }
예제 #12
0
        public void ImportCompletedGetExportedValueEager()
        {
            var cat = CatalogFactory.CreateDefaultAttributed();
            CompositionContainer container = new CompositionContainer(cat);

            NotifyImportExportee.InstanceCount = 0;
            var notifyee = container.GetExportedValue <NotifyImportExportsEager>("NotifyImportExportsEager");

            Assert.IsNotNull(notifyee, "Expecting bound type");
            Assert.IsNotNull(notifyee.Imports, "Expecting Imports to be populated");
            Assert.AreEqual(3, notifyee.Imports.Count, "Expecting 3 Exports before filtering");
            Assert.AreEqual(2, NotifyImportExportee.InstanceCount, "Expecting concrete instances already pulled");
            Assert.AreEqual(2, notifyee.realImports.Count, "Expecting collection to be populated");
            Assert.AreEqual(2, notifyee.RealImports.Count, "Expecting 2 real values after import");
            Assert.AreEqual(1, notifyee.RealImports[0].Id, "Expecting distinct activated instance");
            Assert.AreEqual(3, notifyee.RealImports[1].Id, "Expecting distinct activated instance");
            Assert.AreEqual(2, NotifyImportExportee.InstanceCount, "Expecting no more instances after read");
        }
예제 #13
0
        public void ImportCompletedGetExportedValueLazy()
        {
            var cat = CatalogFactory.CreateDefaultAttributed();
            CompositionContainer container = new CompositionContainer(cat);

            NotifyImportExportee.InstanceCount = 0;
            NotifyImportExportsLazy notifyee = container.GetExportedValue <NotifyImportExportsLazy>("NotifyImportExportsLazy");

            Assert.IsNotNull(notifyee, "Expecting bound type");
            Assert.IsNotNull(notifyee.Imports, "Expecting Imports to be populated");
            Assert.IsTrue(notifyee.NeedRefresh, "Expecting import to put class in pending state");
            Assert.AreEqual(3, notifyee.Imports.Count, "Expecting 3 Exports before filtering");
            Assert.AreEqual(0, NotifyImportExportee.InstanceCount, "Not instance expected before pull");
            Assert.AreEqual(0, notifyee.realImports.Count, "Expecting collection to be empty before pull");
            Assert.AreEqual(2, notifyee.RealImports.Count, "Expecting 2 real values after pull");
            Assert.AreEqual(1, notifyee.RealImports[0].Id, "Expecting distinct activated instance");
            Assert.AreEqual(3, notifyee.RealImports[1].Id, "Expecting distinct  activated instance");
            Assert.AreEqual(2, NotifyImportExportee.InstanceCount, "2 instances expected after pull");
        }
        public void CanBeCollectedAfterDispose()
        {
            AggregateExportProvider exportProvider = new AggregateExportProvider();
            var catalog   = new AggregateCatalog(CatalogFactory.CreateDefaultAttributed());
            var container = new CompositionContainer(catalog, exportProvider);

            WeakReference weakContainer = new WeakReference(container);

            container.Dispose();
            container = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.False(weakContainer.IsAlive);

            GC.KeepAlive(exportProvider);
            GC.KeepAlive(catalog);
        }
        public void CanBeCollectedAfterDispose()
        {
            AggregateExportProvider sourceExportProvider = new AggregateExportProvider();
            var catalog = new AggregateCatalog(CatalogFactory.CreateDefaultAttributed());
            var catalogExportProvider = new CatalogExportProvider(catalog);

            catalogExportProvider.SourceProvider = sourceExportProvider;

            WeakReference weakCatalogExportProvider = new WeakReference(catalogExportProvider);

            catalogExportProvider.Dispose();
            catalogExportProvider = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.IsFalse(weakCatalogExportProvider.IsAlive);

            GC.KeepAlive(sourceExportProvider);
            GC.KeepAlive(catalog);
        }
예제 #16
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void BasicTest()
        {
            var catalog = CatalogFactory.CreateDefaultAttributed();
            var catalogExportProvider = new CatalogExportProvider(catalog);

            catalogExportProvider.SourceProvider = catalogExportProvider;
            var testName             = AttributedModelServices.GetContractName(typeof(CatalogComponentTest));
            var testNameNonComponent = AttributedModelServices.GetContractName(typeof(CatalogComponentTestNonComponentPart));
            var testInterfaceName    = AttributedModelServices.GetContractName(typeof(ICatalogComponentTest));

            Assert.Equal(1, catalogExportProvider.GetExports(ImportFromContract(testName)).Count());
            Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContract(testNameNonComponent)).Count());

            var exports = catalogExportProvider.GetExports(ImportFromContract(testInterfaceName));

            Assert.Equal(2, exports.Count());

            foreach (var i in exports)
            {
                Assert.NotNull(i.Value);
            }
        }
예제 #17
0
        [ActiveIssue(25498, TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void ImportCompletedChildDoesnotNeedParentContainer()
        {
            var cat    = CatalogFactory.CreateDefaultAttributed();
            var parent = new CompositionContainer(cat);

            CompositionBatch parentBatch = new CompositionBatch();
            CompositionBatch childBatch  = new CompositionBatch();

            parentBatch.AddExportedValue <ICompositionService>(parent);
            parent.Compose(parentBatch);

            var child = new CompositionContainer(parent);

            var parentImporter = new MyNotifyImportImporter(parent);
            var childImporter  = new MyNotifyImportImporter(child);

            parentBatch = new CompositionBatch();
            parentBatch.AddPart(parentImporter);

            childBatch.AddParts(childImporter, new MyNotifyImportExporter());

            child.Compose(childBatch);

            Assert.Equal(0, parentImporter.ImportCompletedCallCount);
            Assert.Equal(1, childImporter.ImportCompletedCallCount);

            // Parent will become bound at this point.
            MyNotifyImportExporter parentExporter = parent.GetExportedValue <MyNotifyImportExporter>("MyNotifyImportExporter");

            parent.Compose(parentBatch);
            Assert.Equal(1, parentImporter.ImportCompletedCallCount);
            Assert.Equal(1, parentExporter.ImportCompletedCallCount);

            MyNotifyImportExporter childExporter = child.GetExportedValue <MyNotifyImportExporter>("MyNotifyImportExporter");

            Assert.Equal(1, childExporter.ImportCompletedCallCount);
        }
예제 #18
0
        public static IEnumerable <ComposablePartCatalog> GetCatalogs()
        {
            yield return(CatalogFactory.Create());

            yield return(CatalogFactory.CreateDefaultAttributed());
        }