Exemple #1
0
        public void Import_OptOut_AllowRecomposition()
        {
            var container = new CompositionContainer();
            var importer  = new Class_OptOut_AllowRecompositionImports();

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(importer);
            var valueKey = batch.AddExportedValue("Value", 21);

            container.Compose(batch);

            // Initial compose Value should be 21
            Assert.AreEqual(21, importer.Value);

            // Reset value to ensure it doesn't get set to same value again
            importer.Value = -21;

            // Recompose Value to be 42
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            // After rejection batch failures throw ChangeRejectedException to indicate that
            // the failure did not affect the container
            CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PreventedByExistingImport, () =>
            {
                container.Compose(batch);
            });

            Assert.AreEqual(-21, importer.Value, "Value should NOT have changed!");
        }
        public void NoResolverExceptionTest()
        {
            var container = ContainerFactory.Create();

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(new MultiExport());
            batch.AddPart(new SingleImport());

            CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport, ErrorId.ImportEngine_ImportCardinalityMismatch, RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void ExpectedChangeRejectedErrorOnSetImport(object importer, ErrorId expectedErrorId)
        {
            var container = ContainerFactory.Create();
            var batch     = new CompositionBatch();

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

            CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport,
                                                        expectedErrorId, RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void ImportValueExceptionMissing()
        {
            var      container = ContainerFactory.Create();
            Importer importer;

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(importer = new Importer());

            CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport,
                                                        ErrorId.ImportEngine_ImportCardinalityMismatch,
                                                        RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void ImportValueExceptionMultiple()
        {
            var      container  = ContainerFactory.Create();
            Importer importer   = new Importer();
            Exporter exporter42 = new Exporter(42);
            Exporter exporter6  = new Exporter(6);

            CompositionBatch batch = new CompositionBatch();

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

            CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport,
                                                        RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
        public void RebindingShouldNotHappenForConstructorArguments()
        {
            var container          = GetContainerWithCatalog();
            CompositionBatch batch = new CompositionBatch();

            var p1 = batch.AddExportedValue("MyConstructorCollectionItem", 1);

            batch.AddExportedValue("MyConstructorCollectionItem", 2);
            batch.AddExportedValue("MyConstructorCollectionItem", 3);
            container.Compose(batch);

            var a = container.GetExportedValue <AWithCollectionArgument>();

            EnumerableAssert.AreEqual(a.Values, 1, 2, 3);

            batch = new CompositionBatch();
            batch.AddExportedValue("MyConstructorCollectionItem", 4);
            batch.AddExportedValue("MyConstructorCollectionItem", 5);
            batch.AddExportedValue("MyConstructorCollectionItem", 6);
            // After rejection changes that are incompatible with existing assumptions are no
            // longer silently ignored.  The batch attempting to make this change is rejected
            // with a ChangeRejectedException
            CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PreventedByExistingImport, () =>
            {
                container.Compose(batch);
            });

            // The collection which is a constructor import should not be rebound
            EnumerableAssert.AreEqual(a.Values, 1, 2, 3);

            batch.RemovePart(p1);
            // After rejection changes that are incompatible with existing assumptions are no
            // longer silently ignored.  The batch attempting to make this change is rejected
            // with a ChangeRejectedException
            CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PreventedByExistingImport, () =>
            {
                container.Compose(batch);
            });

            // The collection which is a constructor import should not be rebound
            EnumerableAssert.AreEqual(a.Values, 1, 2, 3);
        }