[ActiveIssue("https://github.com/dotnet/runtime/issues/24240", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void RecursiveNonSharedPartCreation()
        {
            var catalog   = new TypeCatalog(Assembly.GetExecutingAssembly().GetTypes());
            var container = new CompositionContainer(catalog);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <DirectCycleNonSharedPart>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleNonSharedPart>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleNonSharedPart1>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleNonSharedPart2>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleWithSharedPartAndNonSharedPart>();
            });

            Assert.NotNull(container.GetExportedValue <CycleSharedPart>());
            Assert.NotNull(container.GetExportedValue <CycleSharedPart1>());
            Assert.NotNull(container.GetExportedValue <CycleSharedPart2>());
            Assert.NotNull(container.GetExportedValue <NoCycleNonSharedPart>());
        }
Exemple #2
0
        public void GetExportManualDisposeThenRecompose_NonSharedDisposableRecomposablePart_ShouldThrowComposition()
        {
            var catalog   = new TypeCatalog(typeof(NonSharedPartDisposableRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey           = batch.AddExportedValue("Value", 21);

            container.Compose(batch);

            var export        = container.GetExport <NonSharedPartDisposableRecomposable>();
            var exportedValue = export.Value;

            Assert.Equal(21, exportedValue.Value);

            exportedValue.Dispose();

            // Recompose should cause a ObjectDisposedException.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);

            CompositionAssert.ThrowsError(
                ErrorId.ImportEngine_PartCannotActivate,                       // Cannot activate part because
                ErrorId.ReflectionModel_ImportThrewException,                  // Import threw an exception
                RetryMode.DoNotRetry,
                () =>
            {
                container.Compose(batch);
            });
        }
Exemple #3
0
        [ActiveIssue("https://github.com/dotnet/runtime/issues/24240", TestPlatforms.AnyUnix)] // typeof(System.Reflection.ReflectionTypeLoadException)
        public void RecursiveNonSharedPartCreationDisableSilentRejection()
        {
            var catalog   = new AssemblyCatalog(typeof(AssemblyCatalogTests).Assembly);
            var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <DirectCycleNonSharedPart>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleNonSharedPart>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleNonSharedPart1>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleNonSharedPart2>();
            });

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <CycleWithSharedPartAndNonSharedPart>();
            });

            Assert.NotNull(container.GetExportedValue <CycleSharedPart>());
            Assert.NotNull(container.GetExportedValue <CycleSharedPart1>());
            Assert.NotNull(container.GetExportedValue <CycleSharedPart2>());
            Assert.NotNull(container.GetExportedValue <NoCycleNonSharedPart>());
        }
        public void StaticConstructor()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(PartWithStaticConstructor));

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue,
                                          ErrorId.ImportEngine_PartCannotActivate,
                                          () => container.GetExportedValue <PartWithStaticConstructor>());
        }
        public void ImportMany_ConstructorParameter_OnNonAssiganbleType_ShouldThrowCompositionException()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(InvalidImportManyCI));

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue,
                                          ErrorId.ImportEngine_PartCannotActivate,
                                          ErrorId.ReflectionModel_ImportManyOnParameterCanOnlyBeAssigned,
                                          () => container.GetExportedValue <InvalidImportManyCI>());
        }
Exemple #6
0
        private static void AssertCycle(params Type[] types)
        {
            foreach (Type type in types)
            {
                var export = GetExport(type, types);

                CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
                {
                    var value = export.Value;
                });
            }
        }
        public void ImportValueExceptionLazily()
        {
            var catalog     = new AssemblyCatalog(typeof(ImportImporterInvalidSetterExceptionLazily).Assembly);
            var container   = ContainerFactory.CreateWithAttributedCatalog(typeof(ImportImporterInvalidSetterExceptionLazily), typeof(ImporterInvalidSetterException));
            var invalidLazy = container.GetExportedValue <ImportImporterInvalidSetterExceptionLazily>();

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue,
                                          ErrorId.ImportEngine_PartCannotActivate,
                                          ErrorId.ReflectionModel_ImportThrewException, RetryMode.DoNotRetry, () =>
            {
                var value = invalidLazy.Value.Value;
            });
        }
        public void Value_SingleValueAsErrorsArgument_ShouldThrowComposition()
        {
            var errorIds = Expectations.GetEnumValues <CompositionErrorId>();

            foreach (var errorId in errorIds)
            {
                var result = CreateCompositionResult <string>(errorId);

                CompositionAssert.ThrowsError((ErrorId)errorId, () =>
                {
                    var value = result.Value;
                });
            }
        }
        public void WriteOnlyPropertyExport_ShouldThrowComposition()
        {
            var importer           = PartFactory.CreateAttributed(new WriteOnlyPropertyExport());
            var container          = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();

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

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                container.GetExportedValue <string>("writeonly");
            });
        }
        public void ImportValueMismatchFromInt32ToDateTime()
        {
            var container          = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(new ImportValueTypes());
            batch.AddExportedValue("DateTime", typeof(DateTime), 42);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                          ErrorId.ReflectionModel_ImportNotAssignableFromExport,
                                          RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void ReadOnlyPropertyImport_ShouldThrowComposition()
        {
            var importer           = PartFactory.CreateAttributed(new ReadOnlyPropertyImport());
            var container          = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(importer);
            batch.AddExportedValue("readonly", "new value");

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate,
                                          RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void ExpectedErrorOnSetImport(object importer, ErrorId expectedErrorId)
        {
            var container = ContainerFactory.Create();
            var batch     = new CompositionBatch();

            batch.AddPart(importer);
            batch.AddExportedValue("Value", 42);
            batch.AddExportedValue("Value", 0);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                          expectedErrorId, RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        private static void AssertCycle(Dependency partADependency, Dependency partBDependency)
        {
            var exportA = GetExport("A", partADependency, partBDependency);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                var value = exportA.Value;
            });

            var exportB = GetExport("B", partADependency, partBDependency);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, () =>
            {
                var value = exportB.Value;
            });
        }
        public void ImportCollectionsExceptionGettingValue()
        {
            var container = ContainerFactory.Create();
            ImporterThrowsOnGetting importer = new ImporterThrowsOnGetting();
            CompositionBatch        batch    = new CompositionBatch();

            batch.AddPart(importer);
            batch.AddExportedValue("Value", 42);
            batch.AddExportedValue("Value", 0);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate,
                                          ErrorId.ReflectionModel_ImportCollectionGetThrewException, RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void ImportIntoDerivationOfExportsException()
        {
            var container = ContainerFactory.Create();

            CompositionBatch batch = new CompositionBatch();

            batch.AddExportedValue("derived", typeof(DerivedExport), 42);
            var d = new DerivedExportsImporter();

            batch.AddPart(d);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                          ErrorId.ReflectionModel_ImportNotAssignableFromExport, RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void ImportValues()
        {
            var      container  = ContainerFactory.Create();
            Importer importer   = new Importer();
            Exporter exporter42 = new Exporter(42);

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(importer);
            batch.AddPart(exporter42);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
                                          ErrorId.ReflectionModel_ImportNotAssignableFromExport, RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void GetExportedValue_MissingPostImports_ShouldThrowComposition()
        {
            var part = CreatePart(typeof(MySharedPartExport));

            // Signal that the composition should be finished
            part.Activate();

            var definition = part.ExportDefinitions.First();

            // Dev10:484204 - This used to cause a failure but after we made
            // ReflectionComposablePart internal we needed to back remove this
            // validation for post imports to make declarative composition work.
            CompositionAssert.ThrowsError(ErrorId.ImportNotSetOnPart, () =>
            {
                part.GetExportedValue(definition);
            });
        }
        public void ImportValueExceptionSetterException()
        {
            var container = ContainerFactory.Create();
            ImporterInvalidSetterException importer = new ImporterInvalidSetterException();
            Exporter exporter42 = new Exporter(42);

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(importer);
            batch.AddPart(exporter42);

            CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate,
                                          ErrorId.ReflectionModel_ImportThrewException,
                                          RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }