diff options
Diffstat (limited to 'mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel')
5 files changed, 0 insertions, 1710 deletions
diff --git a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/.gitattributes b/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/.gitattributes deleted file mode 100644 index 3e0ba3aed3f..00000000000 --- a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/.gitattributes +++ /dev/null @@ -1,4 +0,0 @@ -/AllowNonPublicCompositionTests.cs -crlf -/AttributedModelCompositionTests.cs -crlf -/AttributedModelDiscoveryTests.cs -crlf -/INotifyImportTests.cs -crlf diff --git a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AllowNonPublicCompositionTests.cs b/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AllowNonPublicCompositionTests.cs deleted file mode 100644 index 0771d89264a..00000000000 --- a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AllowNonPublicCompositionTests.cs +++ /dev/null @@ -1,288 +0,0 @@ -#if !SILVERLIGHT
-
-// -----------------------------------------------------------------------
-// Copyright (c) Microsoft Corporation. All rights reserved.
-// -----------------------------------------------------------------------
-using System;
-using System.ComponentModel.Composition.Factories;
-using System.ComponentModel.Composition.Hosting;
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using System.ComponentModel.Composition.UnitTesting;
-
-namespace System.ComponentModel.Composition
-{
- [TestClass]
- public class AllowNonPublicCompositionTests
- {
- [TestMethod]
- public void PublicFromPublic()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var importer = new AllPublicImportOnly();
- batch.AddPart(importer);
- batch.AddPart(new AllPublicExportOnly() { ExportA = 5, ExportB = 10 });
- container.Compose(batch);
-
- Assert.AreEqual(5, importer.ImportA);
- Assert.AreEqual(10, importer.ImportB);
- }
- [TestMethod]
- public void PublicToSelf()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var importer = new AllPublic() { ExportA = 5, ExportB = 10 };
- batch.AddPart(importer);
- container.Compose(batch);
-
- Assert.AreEqual(5, importer.ImportA);
- Assert.AreEqual(10, importer.ImportB);
- }
- [TestMethod]
- public void PublicFromPrivate()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var importer = new AllPublicImportOnly();
- batch.AddPart(importer);
- batch.AddPart(new AllPrivateExportOnly(5, 10));
- container.Compose(batch);
-
- Assert.AreEqual(5, importer.ImportA);
- Assert.AreEqual(10, importer.ImportB);
- }
- [TestMethod]
- public void PrivateFromPublic()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var importer = new AllPrivateImportOnly();
- batch.AddPart(importer);
- batch.AddPart(new AllPublicExportOnly() { ExportA = 5, ExportB = 10 });
- container.Compose(batch);
-
- Assert.AreEqual(5, importer.PublicImportA);
- Assert.AreEqual(10, importer.PublicImportB);
- }
- [TestMethod]
- public void PrivateToSelf()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var importer = new AllPrivate(5, 10);
- batch.AddPart(importer);
- container.Compose(batch);
-
- Assert.AreEqual(5, importer.PublicImportA);
- Assert.AreEqual(10, importer.PublicImportB);
- }
- [TestMethod]
- public void PrivateData()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var importer = new PrivateDataImportExport(5);
- batch.AddPart(importer);
- container.Compose(batch);
-
- Assert.AreEqual(5, importer.X);
- }
- [TestMethod]
- public void TestPublicImportsExpectingPublicExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<PublicImportsExpectingPublicExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestInternalImportsExpectingPublicExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<InternalImportsExpectingPublicExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestPublicImportsExpectingInternalExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<PublicImportsExpectingInternalExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestInternalImportsExpectingInternalExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<InternalImportsExpectingInternalExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestPublicImportsExpectingProtectedExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<PublicImportsExpectingProtectedExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestInternalImportsExpectingProtectedExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<InternalImportsExpectingProtectedExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestPublicImportsExpectingProtectedInternalExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<PublicImportsExpectingProtectedInternalExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestInternalImportsExpectingProtectedInternalExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<InternalImportsExpectingProtectedInternalExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestPublicImportsExpectingPrivateExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<PublicImportsExpectingPrivateExports>().VerifyIsBound();
- }
- [TestMethod]
- public void TestInternalImportsExpectingPrivateExportsFromCatalog()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var container = new CompositionContainer(cat);
- container.GetExportedValue<InternalImportsExpectingPrivateExports>().VerifyIsBound();
- }
- }
-
- public interface IData { int X { get; set; } }
-
- public class PrivateDataImportExportWithContract
- {
- public PrivateDataImportExportWithContract(int x)
- {
- Out = new PrivateDataType() { X = x };
- }
- public int X { get { return In.X; } }
- [Export("a")]
- PrivateDataType Out { get; set; }
- [Import("a")]
- IData In { get; set; }
- }
-
- public class PrivateDataImportExport
- {
- public PrivateDataImportExport(int x)
- {
- Out = new PrivateDataType() { X = x };
- }
- public int X { get { return In.X; } }
- [Export]
- PrivateDataType Out { get; set; }
- [Import]
- PrivateDataType In { get; set; }
- }
-
- class PrivateDataType
- {
- public int X { get; set; }
- }
-
- public class AllPrivateNoAttribute
- {
- public AllPrivateNoAttribute(int exportA, int exportB) { ExportA = exportA; ExportB = exportB; }
-
- public int PublicImportA { get { return ImportA; } }
- public int PublicImportB { get { return ImportB; } }
-
- [Import("a")]
- int ImportA { get; set; }
- [Import("b")]
- int ImportB { get; set; }
- [Export("a")]
- int ExportA { get; set; }
- [Export("b")]
- int ExportB { get; set; }
- }
- public class AllPrivateNoAttributeImportOnly
- {
- public int PublicImportA { get { return ImportA; } }
- public int PublicImportB { get { return ImportB; } }
-
- [Import("a")]
- int ImportA { get; set; }
- [Import("b")]
- int ImportB { get; set; }
- }
-
- public class AllPrivate
- {
- public AllPrivate(int exportA, int exportB) { ExportA = exportA; ExportB = exportB; }
-
- public int PublicImportA { get { return ImportA; } }
- public int PublicImportB { get { return ImportB; } }
-
- [Import("a")]
- int ImportA { get; set; }
- [Import("b")]
- int ImportB { get; set; }
- [Export("a")]
- int ExportA { get; set; }
- [Export("b")]
- int ExportB { get; set; }
- }
-
- public class AllPrivateImportOnly
- {
- public int PublicImportA { get { return ImportA; } }
- public int PublicImportB { get { return ImportB; } }
-
- [Import("a")]
- int ImportA { get; set; }
- [Import("b")]
- int ImportB { get; set; }
- }
-
- public class AllPrivateExportOnly
- {
- public AllPrivateExportOnly(int exportA, int exportB) { ExportA = exportA; ExportB = exportB; }
-
- [Export("a")]
- int ExportA { get; set; }
- [Export("b")]
- int ExportB { get; set; }
- }
-
- public class AllPublic
- {
- [Import("a")]
- public int ImportA { get; set; }
- [Import("b")]
- public int ImportB { get; set; }
- [Export("a")]
- public int ExportA { get; set; }
- [Export("b")]
- public int ExportB { get; set; }
- }
- public class AllPublicImportOnly
- {
- [Import("a")]
- public int ImportA { get; set; }
- [Import("b")]
- public int ImportB { get; set; }
- }
- public class AllPublicExportOnly
- {
- [Export("a")]
- public int ExportA { get; set; }
- [Export("b")]
- public int ExportB { get; set; }
- }
-}
-
-#endif
diff --git a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AttributedModelCompositionTests.cs b/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AttributedModelCompositionTests.cs deleted file mode 100644 index 4a6adca7781..00000000000 --- a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AttributedModelCompositionTests.cs +++ /dev/null @@ -1,465 +0,0 @@ -// -----------------------------------------------------------------------
-// Copyright (c) Microsoft Corporation. All rights reserved.
-// -----------------------------------------------------------------------
-using System;
-using System.Linq;
-using System.ComponentModel.Composition.Factories;
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-using System.UnitTesting;
-using System.ComponentModel.Composition.Hosting;
-using System.ComponentModel.Composition.UnitTesting;
-
-namespace System.ComponentModel.Composition.AttributedModel
-{
- [TestClass]
- public class AttributedModelCompositionTests
- {
- [TestMethod]
- public void PartContainingOnlyStaticExports_ShouldNotCauseInstanceToBeCreated()
- {
- var container = ContainerFactory.CreateWithAttributedCatalog(typeof(HasOnlyStaticExports));
-
- Assert.AreEqual("Field", container.GetExportedValue<string>("Field"));
- Assert.AreEqual("Property", container.GetExportedValue<string>("Property"));
- Assert.IsNotNull("Method", container.GetExportedValue<Func<string>>("Method")());
-
- Assert.IsFalse(HasOnlyStaticExports.InstanceCreated);
- }
-
- [TestMethod]
- public void ExportOnAbstractBase_DoesNotReturnNull()
- { // 499393 - Classes inheriting from an exported
- // abstract class are exported as 'null'
-
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- var definition = PartDefinitionFactory.CreateAttributed(typeof(Derived));
- batch.AddPart(definition.CreatePart());
- container.Compose(batch);
-
- Assert.IsNotNull(container.GetExportedValueOrDefault<Base>());
- }
-
- [TestMethod]
- public void ReadOnlyFieldImport_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,
- ErrorId.ReflectionModel_ImportNotWritable,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- 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,
- ErrorId.ReflectionModel_ImportNotWritable, RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- 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, ErrorId.ReflectionModel_ExportNotReadable, () =>
- {
- container.GetExportedValue<string>("writeonly");
- });
- }
-
- [TestMethod]
- 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);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromInt16ToInt32()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Int32", typeof(Int32), (short)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
-
- }
-
- [TestMethod]
- public void ImportValueMismatchFromInt32ToInt16()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Int16", typeof(Int16), (int)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromInt32ToUInt32()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("UInt32", typeof(UInt32), (int)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromUInt32ToInt32()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Int32", typeof(Int32), (uint)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromInt32ToInt64()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Int64", typeof(Int64), (int)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromSingleToDouble()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Double", typeof(Double), (float)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromDoubleToSingle()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Single", typeof(Single), (double)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromSingleToInt32()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Int32", typeof(Int32), (float)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void ImportValueMismatchFromInt32ToSingle()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
-
- batch.AddPart(new ImportValueTypes());
- batch.AddExportedValue("Single", typeof(Single), (int)10);
-
- CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
- ErrorId.ReflectionModel_ImportNotAssignableFromExport,
- RetryMode.DoNotRetry, () =>
- {
- container.Compose(batch);
- });
- }
-
- [TestMethod]
- public void MemberExports()
- {
- var exporter = PartFactory.CreateAttributed(new ObjectWithMemberExports());
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- batch.AddPart(exporter);
- container.Compose(batch);
-
- var filedExports = container.GetExports<int>("field");
- ExportsAssert.AreEqual(filedExports, 1, 5);
-
- var readonlyExports = container.GetExports<int>("readonly");
- ExportsAssert.AreEqual(readonlyExports, 2, 6);
-
- var propertyExports = container.GetExports<int>("property");
- ExportsAssert.AreEqual(propertyExports, 3, 7);
-
- var methodExports = container.GetExportedValues<Func<int, int>>("func").Select(f => f(0));
- EnumerableAssert.AreEqual(methodExports, 4, 8);
- }
-
- [TestMethod]
- public void TestExportedValueCachesValue()
- {
- var container = ContainerFactory.Create();
- var exporter = new ExportsMutableProperty();
- exporter.Property = "Value1";
-
- container.ComposeParts(exporter);
-
- Assert.AreEqual("Value1", container.GetExportedValue<string>("Property"));
-
- exporter.Property = "Value2";
-
- // Exported value should have been cached and so it shouldn't change
- Assert.AreEqual("Value1", container.GetExportedValue<string>("Property"));
- }
-
- [TestMethod]
- [WorkItem(739354)]
- [Ignore]
- public void TestExportedValueCachesNullValue()
- {
- var container = ContainerFactory.Create();
- var exporter = new ExportsMutableProperty();
- exporter.Property = null;
-
- container.ComposeParts(exporter);
-
- Assert.IsNull(container.GetExportedValue<string>("Property"));
-
- exporter.Property = "Value1";
-
- // Exported value should have been cached and so it shouldn't change
- Assert.IsNull(container.GetExportedValue<string>("Property"));
- }
-
- public class ExportsMutableProperty
- {
- [Export("Property")]
- public string Property { get; set; }
- }
-
- public class ReadOnlyFieldImport
- {
- [Import("readonly")]
- public readonly string Value = "Value";
- }
-
- public class ReadOnlyPropertyImport
- {
- [Import("readonly")]
- public string Value
- {
- get { return "Value"; }
- }
- }
-
- public class WriteOnlyPropertyExport
- {
- [Export("writeonly")]
- public string Value
- {
- set { }
- }
- }
-
- public class ObjectWithMemberExports
- {
- [Export("field")]
- public static int staticField = 1;
-
- [Export("readonly")]
- public static readonly int staticReadonly = 2;
-
- [Export("property")]
- public static int StaticProperty { get { return 3; } }
-
- [Export("func")]
- public static int StaticMethod(int arg1) { return 4; }
-
- [Export("field")]
- public int instanceField = 5;
-
- [Export("readonly")]
- public readonly int instanceReadonly = 6;
-
- [Export("property")]
- public int InstanceProperty { get { return 7; } }
-
- [Export("func")]
- public int InstanceMethod(int arg1) { return 8; }
- }
-
- public class HasOnlyStaticExports
- {
- public static bool InstanceCreated;
-
- public HasOnlyStaticExports()
- {
- InstanceCreated = true;
- }
-
- [Export("Field")]
- public static string Field = "Field";
-
- [Export("Property")]
- public static string Property
- {
- get { return "Property"; }
- }
-
- [Export("Method")]
- public static string Method()
- {
- return "Method";
- }
- }
-
- [InheritedExport(typeof(Base))]
- public abstract class Base
- {
- }
-
- [Export(typeof(Derived))]
- public class Derived : Base
- {
- }
-
- public class ImportValueTypes
- {
- [Import("Int16", AllowDefault = true)]
- public short Int16
- {
- get;
- set;
- }
-
- [Import("Int32", AllowDefault = true)]
- public int Int32
- {
- get;
- set;
- }
-
- [Import("UInt32", AllowDefault = true)]
- [CLSCompliant(false)]
- public uint UInt32
- {
- get;
- set;
- }
-
- [Import("Int64", AllowDefault = true)]
- public long Int64
- {
- get;
- set;
- }
-
- [Import("Single", AllowDefault = true)]
- public float Single
- {
- get;
- set;
- }
-
- [Import("Double", AllowDefault = true)]
- public double Double
- {
- get;
- set;
- }
-
- [Import("DateTime", AllowDefault = true)]
- public DateTime DateTime
- {
- get;
- set;
- }
- }
- }
-}
diff --git a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AttributedModelDiscoveryTests.cs b/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AttributedModelDiscoveryTests.cs deleted file mode 100644 index 15a214662b2..00000000000 --- a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/AttributedModelDiscoveryTests.cs +++ /dev/null @@ -1,345 +0,0 @@ -// -----------------------------------------------------------------------
-// Copyright (c) Microsoft Corporation. All rights reserved.
-// -----------------------------------------------------------------------
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.ComponentModel.Composition.Factories;
-using System.ComponentModel.Composition.Hosting;
-using System.ComponentModel.Composition.ReflectionModel;
-using System.ComponentModel.Composition.Primitives;
-using System.ComponentModel.Composition.UnitTesting;
-using System.Linq;
-using System.Reflection;
-using System.UnitTesting;
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-
-namespace System.ComponentModel.Composition.AttributedModel
-{
- [TestClass]
- public class AttributedModelDiscoveryTests
- {
- [TestMethod]
- public void CreatePartDefinition_TypeWithExports_ShouldHaveMultipleExports()
- {
- var definition = CreateDefinition(typeof(PublicComponentWithPublicExports));
- EnumerableAssert.AreEqual(definition.ExportDefinitions.Select(e => e.ContractName), "PublicField", "PublicProperty", "PublicDelegate");
- }
-
- public abstract class BaseClassWithPropertyExports
- {
- [Export("MyPropBase")]
- public virtual int MyProp { get; set; }
- }
-
- public class DerivedClassWithInheritedPropertyExports : BaseClassWithPropertyExports
- {
- public override int MyProp { get; set; }
- }
-
- [WorkItem(551341)]
- [TestMethod]
- public void ShowIssueWithVirtualPropertiesInReflectionAPI()
- {
- PropertyInfo propInfo = typeof(BaseClassWithPropertyExports).GetProperty("MyProp");
-
- // pi.GetCustomAttributes does not find the inherited attributes
- var c1 = propInfo.GetCustomAttributes(true);
-
- // Attribute.GetCustomAttributes does find the inherited attributes
- var c2 = Attribute.GetCustomAttributes(propInfo, true);
-
- // This seems like it should be a bug in the reflection API's...
- Assert.AreNotEqual(c1, c2);
- }
-
- [TestMethod]
- public void CreatePartDefinition_TypeWithImports_ShouldHaveMultipleImports()
- {
- var definition = CreateDefinition(typeof(PublicImportsExpectingPublicExports));
- EnumerableAssert.AreEqual(definition.ImportDefinitions.Cast<ContractBasedImportDefinition>()
- .Select(i => i.ContractName), "PublicField", "PublicProperty", "PublicDelegate", "PublicIGetString");
- }
-
- public class AnyImplicitExport
- {
-
- }
-
- [TestMethod]
- public void CreatePartDefinition_AnyType_ShouldHaveMetadataWithAnyImplicitCreationPolicy()
- {
- var definition = CreateDefinition(typeof(AnyImplicitExport));
-
- Assert.AreEqual(CreationPolicy.Any, definition.Metadata.GetValue<CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName));
- }
-
- [PartCreationPolicy(CreationPolicy.Any)]
- public class AnyExport
- {
-
- }
-
- [TestMethod]
- public void CreatePartDefinition_AnyType_ShouldHaveMetadataWithAnyCreationPolicy()
- {
- var definition = CreateDefinition(typeof(AnyExport));
-
- Assert.AreEqual(CreationPolicy.Any, definition.Metadata.GetValue<CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName));
- }
-
-
- [PartCreationPolicy(CreationPolicy.Shared)]
- public class SharedExport
- {
-
- }
-
- [TestMethod]
- public void CreatePartDefinition_SharedType_ShouldHaveMetadataWithSharedCreationPolicy()
- {
- var definition = CreateDefinition(typeof(SharedExport));
-
- Assert.AreEqual(CreationPolicy.Shared, definition.Metadata.GetValue<CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName));
- }
-
- [PartCreationPolicy(CreationPolicy.NonShared)]
- public class NonSharedExport
- {
-
- }
-
- [TestMethod]
- public void CreatePartDefinition_NonSharedType_ShouldHaveMetadataWithNonSharedCreationPolicy()
- {
- var definition = CreateDefinition(typeof(NonSharedExport));
-
- Assert.AreEqual(CreationPolicy.NonShared, definition.Metadata.GetValue<CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName));
- }
-
- [PartMetadata(CompositionConstants.PartCreationPolicyMetadataName, CreationPolicy.NonShared)]
- [PartMetadata("ShouldNotBeIgnored", "Value")]
- public class PartWithIgnoredMetadata
- {
- }
-
- [TestMethod]
- public void CreatePartDefinition_SharedTypeMarkedWithNonSharedMetadata_ShouldHaveMetadatWithSharedCreationPolicy()
- {
- // Type should just contain all the default settings of Shared
- var definition = CreateDefinition(typeof(PartWithIgnoredMetadata));
-
- // CompositionConstants.PartCreationPolicyMetadataName should be ignored
- Assert.AreNotEqual(CreationPolicy.NonShared, definition.Metadata.GetValue<CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName));
-
- // Key ShouldNotBeIgnored should actully be in the dictionary
- Assert.AreEqual("Value", definition.Metadata["ShouldNotBeIgnored"]);
- }
-
- [PartMetadata("BaseOnlyName", 1)]
- [PartMetadata("OverrideName", 2)]
- public class BasePartWithMetdata
- {
-
- }
-
- [PartMetadata("DerivedOnlyName", 3)]
- [PartMetadata("OverrideName", 4)]
- public class DerivedPartWithMetadata : BasePartWithMetdata
- {
-
- }
-
- [TestMethod]
- public void CreatePartDefinition_InheritedPartMetadata_ShouldNotContainPartMetadataFromBase()
- {
- var definition = CreateDefinition(typeof(DerivedPartWithMetadata));
-
- Assert.IsFalse(definition.Metadata.ContainsKey("BaseOnlyName"), "Should not inherit part metadata from base.");
- Assert.AreEqual(3, definition.Metadata["DerivedOnlyName"]);
- Assert.AreEqual(4, definition.Metadata["OverrideName"]);
- }
-
- [TestMethod]
- public void CreatePartDefinition_NoMarkedOrDefaultConstructorAsPartTypeArgument_ShouldSetConstructorToNull()
- {
- var definition = CreateDefinition(typeof(ClassWithNoMarkedOrDefaultConstructor));
-
- Assert.IsNull(definition.GetConstructor());
- }
-
- [TestMethod]
- public void CreatePartDefinition_MultipleMarkedConstructorsAsPartTypeArgument_ShouldSetConstructors()
- {
- var definition = CreateDefinition(typeof(ClassWithMultipleMarkedConstructors));
-
- Assert.IsNull(definition.GetConstructor());
- }
-
- [TestMethod]
- public void CreatePartDefinition_OneMarkedConstructorsAsPartTypeArgument_ShouldSetConstructorToMarked()
- {
- var definition = CreateDefinition(typeof(SimpleConstructorInjectedObject));
-
- ConstructorInfo constructor = definition.GetConstructor();
- Assert.IsNotNull(constructor);
- Assert.AreEqual(typeof(SimpleConstructorInjectedObject).GetConstructors()[0], constructor);
- Assert.AreEqual(constructor.GetParameters().Length, definition.ImportDefinitions.OfType<ReflectionParameterImportDefinition>().Count());
- }
-
- [TestMethod]
- public void CreatePartDefinition_OneDefaultConstructorAsPartTypeArgument_ShouldSetConstructorToDefault()
- {
- var definition = CreateDefinition(typeof(PublicComponentWithPublicExports));
-
- ConstructorInfo constructor = definition.GetConstructor();
- Assert.IsNotNull(constructor);
-
- EnumerableAssert.IsEmpty(constructor.GetParameters());
- EnumerableAssert.IsEmpty(definition.ImportDefinitions.OfType<ReflectionParameterImportDefinition>());
- }
-
- [TestMethod]
- public void CreatePartDefinition_OneMarkedAndOneDefaultConstructorsAsPartTypeArgument_ShouldSetConstructorToMarked()
- {
- var definition = CreateDefinition(typeof(ClassWithOneMarkedAndOneDefaultConstructor));
- var marked = typeof(ClassWithOneMarkedAndOneDefaultConstructor).GetConstructors()[0];
- Assert.IsTrue(marked.IsDefined(typeof(ImportingConstructorAttribute), false));
-
- ConstructorInfo constructor = definition.GetConstructor();
- Assert.IsNotNull(constructor);
-
- Assert.AreEqual(marked, constructor);
- Assert.AreEqual(marked.GetParameters().Length, definition.ImportDefinitions.OfType<ReflectionParameterImportDefinition>().Count());
- }
-
- [TestMethod]
- public void CreatePartDefinition_NoConstructorBecauseStatic_ShouldHaveNullConstructor()
- {
- var definition = CreateDefinition(typeof(StaticExportClass));
-
- ConstructorInfo constructor = definition.GetConstructor();
- Assert.IsNull(constructor);
-
- EnumerableAssert.IsEmpty(definition.ImportDefinitions.OfType<ReflectionParameterImportDefinition>());
- }
-
- [TestMethod]
- public void CreatePartDefinition_TwoZeroParameterConstructors_ShouldPickNonStaticOne()
- {
- var definition = CreateDefinition(typeof(ClassWithTwoZeroParameterConstructors));
-
- ConstructorInfo constructor = definition.GetConstructor();
- Assert.IsNotNull(constructor);
- Assert.IsFalse(constructor.IsStatic);
-
- EnumerableAssert.IsEmpty(definition.ImportDefinitions.OfType<ReflectionParameterImportDefinition>());
- }
-
- [TestMethod]
- public void IsDiscoverable()
- {
- var expectations = new ExpectationCollection<Type, bool>();
- expectations.Add(typeof(ClassWithTwoZeroParameterConstructors), true);
- expectations.Add(typeof(SimpleConstructorInjectedObject), true);
- expectations.Add(typeof(StaticExportClass), true);
- expectations.Add(typeof(PublicComponentWithPublicExports), true);
- expectations.Add(typeof(ClassWithMultipleMarkedConstructors), true);
- expectations.Add(typeof(ClassWithNoMarkedOrDefaultConstructor), true);
- expectations.Add(typeof(ClassWhichOnlyHasDefaultConstructor), false);
- expectations.Add(typeof(ClassWithOnlyHasImportingConstructorButInherits), true);
- expectations.Add(typeof(ClassWithOnlyHasMultipleImportingConstructorButInherits), true);
-
- foreach (var e in expectations)
- {
- var definition = AttributedModelDiscovery.CreatePartDefinitionIfDiscoverable(e.Input, (ICompositionElement)null);
-
- bool result = (definition != null);
-
- Assert.AreEqual(e.Output, result);
- }
- }
-
- [TestMethod]
- public void CreatePartDefinition_EnsureIsDiscoverable()
- {
- var expectations = new ExpectationCollection<Type, bool>();
- expectations.Add(typeof(ClassWithTwoZeroParameterConstructors), true);
- expectations.Add(typeof(SimpleConstructorInjectedObject), true);
- expectations.Add(typeof(StaticExportClass), true);
- expectations.Add(typeof(PublicComponentWithPublicExports), true);
- expectations.Add(typeof(ClassWithMultipleMarkedConstructors), true);
- expectations.Add(typeof(ClassWithNoMarkedOrDefaultConstructor), true);
- expectations.Add(typeof(ClassWhichOnlyHasDefaultConstructor), false);
- expectations.Add(typeof(ClassWithOnlyHasImportingConstructorButInherits), true);
- expectations.Add(typeof(ClassWithOnlyHasMultipleImportingConstructorButInherits), true);
-
- foreach (var e in expectations)
- {
- var definition = AttributedModelServices.CreatePartDefinition(e.Input, null, true);
-
- bool result = (definition != null);
-
- Assert.AreEqual(e.Output, result);
- }
- }
-
- [TestMethod]
- public void CreatePartDefinition_NotEnsureIsDiscoverable()
- {
- var expectations = new ExpectationCollection<Type, bool>();
- expectations.Add(typeof(ClassWithTwoZeroParameterConstructors), true);
- expectations.Add(typeof(SimpleConstructorInjectedObject), true);
- expectations.Add(typeof(StaticExportClass), true);
- expectations.Add(typeof(PublicComponentWithPublicExports), true);
- expectations.Add(typeof(ClassWithMultipleMarkedConstructors), true);
- expectations.Add(typeof(ClassWithNoMarkedOrDefaultConstructor), true);
- expectations.Add(typeof(ClassWhichOnlyHasDefaultConstructor), false);
- expectations.Add(typeof(ClassWithOnlyHasImportingConstructorButInherits), true);
- expectations.Add(typeof(ClassWithOnlyHasMultipleImportingConstructorButInherits), true);
-
- foreach (var e in expectations)
- {
- var definition = AttributedModelServices.CreatePartDefinition(e.Input, null, false);
- Assert.IsNotNull(definition);
- }
- }
-
- [TestMethod]
- public void CreatePart_ObjectInstance_ShouldProduceSharedPart()
- {
- var part = AttributedModelServices.CreatePart(typeof(MyExport));
-
- Assert.AreEqual(CreationPolicy.Shared, part.Metadata.GetValue<CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName));
- }
-
-
- private ReflectionComposablePartDefinition CreateDefinition(Type type)
- {
- var definition = AttributedModelDiscovery.CreatePartDefinition(type, null, false, ElementFactory.Create());
-
- Assert.AreEqual(type, definition.GetPartType());
-
- return definition;
- }
-
- [InheritedExport]
- [InheritedExport]
- [InheritedExport]
- [Export]
- [InheritedExport]
- [InheritedExport]
- [InheritedExport]
- public class DuplicateMixedExporter1
- {
- }
-
- [TestMethod]
- [WorkItem(710352)]
- public void MixedDuplicateExports_ShouldOnlyCollapseInheritedExport()
- {
- var def = AttributedModelServices.CreatePartDefinition(typeof(DuplicateMixedExporter1), null);
- Assert.AreEqual(2, def.ExportDefinitions.Count(), "Should have 1 from the Export and only 1 collapsed InhertedExport");
- }
- }
-}
diff --git a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/INotifyImportTests.cs b/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/INotifyImportTests.cs deleted file mode 100644 index 4e08d0fece3..00000000000 --- a/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/AttributedModel/INotifyImportTests.cs +++ /dev/null @@ -1,608 +0,0 @@ -// -----------------------------------------------------------------------
-// Copyright (c) Microsoft Corporation. All rights reserved.
-// -----------------------------------------------------------------------
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.ComponentModel.Composition.Factories;
-using System.ComponentModel.Composition.Hosting;
-using System.ComponentModel.Composition.UnitTesting;
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-
-namespace System.ComponentModel.Composition.AttributedModel
-{
- [TestClass()]
- public class INotifyImportTests
- {
- [Export(typeof(PartWithoutImports))]
- public class PartWithoutImports : IPartImportsSatisfiedNotification
- {
- public bool ImportsSatisfiedInvoked { get; private set; }
- public void OnImportsSatisfied()
- {
- this.ImportsSatisfiedInvoked = true;
- }
- }
-
- [TestMethod]
- public void ImportsSatisfiedOnComponentWithoutImports()
- {
- CompositionContainer container = ContainerFactory.CreateWithAttributedCatalog(typeof(PartWithoutImports));
-
- PartWithoutImports partWithoutImports = container.GetExportedValue<PartWithoutImports>();
- Assert.IsNotNull(partWithoutImports);
-
- Assert.IsTrue(partWithoutImports.ImportsSatisfiedInvoked);
-
- }
-
- [TestMethod()]
- public void ImportCompletedTest()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var entrypoint = new UpperCaseStringComponent();
-
- batch.AddParts(new LowerCaseString("abc"), entrypoint);
- container.Compose(batch);
-
- batch = new CompositionBatch();
- batch.AddParts(new object());
- container.Compose(batch);
-
- Assert.AreEqual(entrypoint.LowerCaseStrings.Count, 1);
- Assert.AreEqual(entrypoint.ImportCompletedCallCount, 1);
- Assert.AreEqual(entrypoint.UpperCaseStrings.Count, 1);
- Assert.AreEqual(entrypoint.LowerCaseStrings[0].Value.String, "abc");
- Assert.AreEqual(entrypoint.UpperCaseStrings[0], "ABC");
- }
-
- [TestMethod]
- public void ImportCompletedWithRecomposing()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var entrypoint = new UpperCaseStringComponent();
-
- batch.AddParts(new LowerCaseString("abc"), entrypoint);
- container.Compose(batch);
-
- Assert.AreEqual(entrypoint.LowerCaseStrings.Count, 1);
- Assert.AreEqual(entrypoint.ImportCompletedCallCount, 1);
- Assert.AreEqual(entrypoint.UpperCaseStrings.Count, 1);
- Assert.AreEqual(entrypoint.LowerCaseStrings[0].Value.String, "abc");
- Assert.AreEqual(entrypoint.UpperCaseStrings[0], "ABC");
-
- // Add another component to verify recomposing
- batch = new CompositionBatch();
- batch.AddParts(new LowerCaseString("def"));
- container.Compose(batch);
-
- Assert.AreEqual(entrypoint.LowerCaseStrings.Count, 2);
- Assert.AreEqual(entrypoint.ImportCompletedCallCount, 2);
- Assert.AreEqual(entrypoint.UpperCaseStrings.Count, 2);
- Assert.AreEqual(entrypoint.LowerCaseStrings[1].Value.String, "def");
- Assert.AreEqual(entrypoint.UpperCaseStrings[1], "DEF");
-
- // Verify that adding a random component doesn't cause
- // the OnImportsSatisfied to be called again.
- batch = new CompositionBatch();
- batch.AddParts(new object());
- container.Compose(batch);
-
- Assert.AreEqual(entrypoint.LowerCaseStrings.Count, 2);
- Assert.AreEqual(entrypoint.ImportCompletedCallCount, 2);
- Assert.AreEqual(entrypoint.UpperCaseStrings.Count, 2);
- }
-
- [TestMethod]
- [Ignore]
- [WorkItem(700940)]
- public void ImportCompletedUsingSatisfyImportsOnce()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- var entrypoint = new UpperCaseStringComponent();
- var entrypointPart = AttributedModelServices.CreatePart(entrypoint);
-
- batch.AddParts(new LowerCaseString("abc"));
- container.Compose(batch);
- container.SatisfyImportsOnce(entrypointPart);
-
- Assert.AreEqual(1, entrypoint.LowerCaseStrings.Count);
- Assert.AreEqual(1, entrypoint.ImportCompletedCallCount);
- Assert.AreEqual(1, entrypoint.UpperCaseStrings.Count);
- Assert.AreEqual("abc", entrypoint.LowerCaseStrings[0].Value.String);
- Assert.AreEqual("ABC", entrypoint.UpperCaseStrings[0]);
-
- batch = new CompositionBatch();
- batch.AddParts(new object());
- container.Compose(batch);
- container.SatisfyImportsOnce(entrypointPart);
-
- Assert.AreEqual(1, entrypoint.LowerCaseStrings.Count);
- Assert.AreEqual(1, entrypoint.ImportCompletedCallCount);
- Assert.AreEqual(1, entrypoint.UpperCaseStrings.Count);
- Assert.AreEqual("abc", entrypoint.LowerCaseStrings[0].Value.String);
- Assert.AreEqual("ABC", entrypoint.UpperCaseStrings[0]);
-
- batch.AddParts(new LowerCaseString("def"));
- container.Compose(batch);
- container.SatisfyImportsOnce(entrypointPart);
-
- Assert.AreEqual(2, entrypoint.LowerCaseStrings.Count);
- Assert.AreEqual(2, entrypoint.ImportCompletedCallCount);
- Assert.AreEqual(2, entrypoint.UpperCaseStrings.Count);
- Assert.AreEqual("abc", entrypoint.LowerCaseStrings[0].Value.String);
- Assert.AreEqual("ABC", entrypoint.UpperCaseStrings[0]);
- Assert.AreEqual("def", entrypoint.LowerCaseStrings[1].Value.String);
- Assert.AreEqual("DEF", entrypoint.UpperCaseStrings[1]);
- }
-
- [TestMethod]
- [Ignore]
- [WorkItem(654513)]
- public void ImportCompletedCalledAfterAllImportsAreFullyComposed()
- {
- int importSatisfationCount = 0;
- var importer1 = new MyEventDrivenFullComposedNotifyImporter1();
- var importer2 = new MyEventDrivenFullComposedNotifyImporter2();
-
- Action<object, EventArgs> verificationAction = (object sender, EventArgs e) =>
- {
- Assert.IsTrue(importer1.AreAllImportsFullyComposed);
- Assert.IsTrue(importer2.AreAllImportsFullyComposed);
- ++importSatisfationCount;
- };
-
- importer1.ImportsSatisfied += new EventHandler(verificationAction);
- importer2.ImportsSatisfied += new EventHandler(verificationAction);
-
- // importer1 added first
- var batch = new CompositionBatch();
- batch.AddParts(importer1, importer2);
-
- var container = ContainerFactory.Create();
- container.ComposeExportedValue<ICompositionService>(container);
- container.Compose(batch);
- Assert.AreEqual(2, importSatisfationCount);
-
- // importer2 added first
- importSatisfationCount = 0;
- batch = new CompositionBatch();
- batch.AddParts(importer2, importer1);
-
- container = ContainerFactory.Create();
- container.ComposeExportedValue<ICompositionService>(container);
- container.Compose(batch);
- Assert.AreEqual(2, importSatisfationCount);
- }
-
- [TestMethod]
- public void ImportCompletedAddPartAndBindComponent()
- {
- var container = ContainerFactory.Create();
- CompositionBatch batch = new CompositionBatch();
- batch.AddParts(new CallbackImportNotify(delegate
- {
- batch = new CompositionBatch();
- batch.AddPart(new object());
- container.Compose(batch);
- }));
-
- container.Compose(batch);
- }
-
- [TestMethod]
- public void ImportCompletedChildNeedsParentContainer()
- {
- var cat = CatalogFactory.CreateDefaultAttributed();
- var parent = new CompositionContainer(cat);
-
- CompositionBatch parentBatch = new CompositionBatch();
- CompositionBatch childBatch = new CompositionBatch();
- CompositionBatch child2Batch = new CompositionBatch();
-
- parentBatch.AddExportedValue<ICompositionService>(parent);
- parent.Compose(parentBatch);
- var child = new CompositionContainer(parent);
- var child2 = new CompositionContainer(parent);
-
- var parentImporter = new MyNotifyImportImporter(parent);
- var childImporter = new MyNotifyImportImporter(child);
- var child2Importer = new MyNotifyImportImporter(child2);
-
- parentBatch = new CompositionBatch();
- parentBatch.AddPart(parentImporter);
- childBatch.AddPart(childImporter);
- child2Batch.AddPart(child2Importer);
-
- parent.Compose(parentBatch);
- child.Compose(childBatch);
- child2.Compose(child2Batch);
-
-
- Assert.AreEqual(1, parentImporter.ImportCompletedCallCount);
- Assert.AreEqual(1, childImporter.ImportCompletedCallCount);
- Assert.AreEqual(1, child2Importer.ImportCompletedCallCount);
-
- MyNotifyImportExporter parentExporter = parent.GetExportedValue<MyNotifyImportExporter>("MyNotifyImportExporter");
- Assert.AreEqual(1, parentExporter.ImportCompletedCallCount);
-
- MyNotifyImportExporter childExporter = child.GetExportedValue<MyNotifyImportExporter>("MyNotifyImportExporter");
- Assert.AreEqual(1, childExporter.ImportCompletedCallCount);
-
- MyNotifyImportExporter child2Exporter = child2.GetExportedValue<MyNotifyImportExporter>("MyNotifyImportExporter");
- Assert.AreEqual(1, child2Exporter.ImportCompletedCallCount);
- }
-
- [TestMethod]
- 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.AreEqual(0, parentImporter.ImportCompletedCallCount);
- Assert.AreEqual(1, childImporter.ImportCompletedCallCount);
-
- // Parent will become bound at this point.
- MyNotifyImportExporter parentExporter = parent.GetExportedValue<MyNotifyImportExporter>("MyNotifyImportExporter");
- parent.Compose(parentBatch);
- Assert.AreEqual(1, parentImporter.ImportCompletedCallCount);
- Assert.AreEqual(1, parentExporter.ImportCompletedCallCount);
-
- MyNotifyImportExporter childExporter = child.GetExportedValue<MyNotifyImportExporter>("MyNotifyImportExporter");
- Assert.AreEqual(1, childExporter.ImportCompletedCallCount);
- }
-
- [TestMethod]
- 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.AreEqual(1, parentImporter.ImportCompletedCallCount);
- Assert.AreEqual(1, childImporter.ImportCompletedCallCount);
-
- MyNotifyImportExporter parentExporter = parent.GetExportedValue<MyNotifyImportExporter>("MyNotifyImportExporter");
- Assert.AreEqual(1, parentExporter.ImportCompletedCallCount);
-
- MyNotifyImportExporter childExporter = child.GetExportedValue<MyNotifyImportExporter>("MyNotifyImportExporter");
- Assert.AreEqual(1, childExporter.ImportCompletedCallCount);
- }
-
- [TestMethod]
- 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");
- }
-
- [TestMethod]
- 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");
- }
- }
-
- public class NotifyImportExportee
- {
- public NotifyImportExportee(int id)
- {
- Id = id;
- InstanceCount++;
- }
-
- public int Id { get; set; }
-
- public static int InstanceCount { get; set; }
- }
-
- public class NotifyImportExporter
- {
-
- public NotifyImportExporter()
- {
- }
-
- [Export()]
- [ExportMetadata("Filter", false)]
- public NotifyImportExportee Export1
- {
- get
- {
- return new NotifyImportExportee(1);
- }
- }
-
- [Export()]
- [ExportMetadata("Filter", true)]
- public NotifyImportExportee Export2
- {
- get
- {
- return new NotifyImportExportee(2);
- }
- }
-
- [Export()]
- [ExportMetadata("Filter", false)]
- public NotifyImportExportee Export3
- {
- get
- {
- return new NotifyImportExportee(3);
- }
- }
-
- }
-
- [Export("NotifyImportExportsLazy")]
- public class NotifyImportExportsLazy : IPartImportsSatisfiedNotification
- {
- public NotifyImportExportsLazy()
- {
- NeedRefresh = false;
- }
-
- [ImportMany(typeof(NotifyImportExportee))]
- public Collection<Lazy<NotifyImportExportee, IDictionary<string, object>>> Imports { get; set; }
-
- public bool NeedRefresh { get; set; }
-
- public void OnImportsSatisfied()
- {
- NeedRefresh = true;
- }
-
- internal Collection<NotifyImportExportee> realImports = new Collection<NotifyImportExportee>();
-
- public Collection<NotifyImportExportee> RealImports
- {
- get
- {
- if (NeedRefresh)
- {
- realImports.Clear();
- foreach (var import in Imports)
- {
- if (!((bool)import.Metadata["Filter"]))
- {
- realImports.Add(import.Value);
- }
- }
- NeedRefresh = false;
- }
- return realImports;
- }
- }
- }
-
- [Export("NotifyImportExportsEager")]
- public class NotifyImportExportsEager : IPartImportsSatisfiedNotification
- {
- public NotifyImportExportsEager()
- {
- }
-
- [ImportMany]
- public Collection<Lazy<NotifyImportExportee, IDictionary<string, object>>> Imports { get; set; }
-
- public void OnImportsSatisfied()
- {
- realImports.Clear();
- foreach (var import in Imports)
- {
- if (!((bool)import.Metadata["Filter"]))
- {
- realImports.Add(import.Value);
- }
- }
- }
-
- internal Collection<NotifyImportExportee> realImports = new Collection<NotifyImportExportee>();
-
- public Collection<NotifyImportExportee> RealImports
- {
- get
- {
- return realImports;
- }
- }
- }
-
- public class MyEventDrivenNotifyImporter : IPartImportsSatisfiedNotification
- {
- [Import]
- public ICompositionService ImportSomethingSoIGetImportCompletedCalled { get; set; }
-
- public event EventHandler ImportsSatisfied;
-
- public void OnImportsSatisfied()
- {
- if (this.ImportsSatisfied != null)
- {
- this.ImportsSatisfied(this, new EventArgs());
- }
- }
- }
-
- [Export]
- public class MyEventDrivenFullComposedNotifyImporter1 : MyEventDrivenNotifyImporter
- {
- [Import]
- public MyEventDrivenFullComposedNotifyImporter2 FullyComposedImport { get; set; }
-
- public bool AreAllImportsSet
- {
- get
- {
- return (this.ImportSomethingSoIGetImportCompletedCalled != null)
- && (this.FullyComposedImport != null);
- }
- }
-
- public bool AreAllImportsFullyComposed
- {
- get
- {
- return this.AreAllImportsSet && this.FullyComposedImport.AreAllImportsSet;
- }
- }
- }
-
- [Export]
- public class MyEventDrivenFullComposedNotifyImporter2 : MyEventDrivenNotifyImporter
- {
- [Import]
- public MyEventDrivenFullComposedNotifyImporter1 FullyComposedImport { get; set; }
-
- public bool AreAllImportsSet
- {
- get
- {
- return (this.ImportSomethingSoIGetImportCompletedCalled != null)
- && (this.FullyComposedImport != null);
- }
- }
-
- public bool AreAllImportsFullyComposed
- {
- get
- {
- return this.AreAllImportsSet && this.FullyComposedImport.AreAllImportsSet;
- }
- }
- }
-
- [Export("MyNotifyImportExporter")]
- public class MyNotifyImportExporter : IPartImportsSatisfiedNotification
- {
- [Import]
- public ICompositionService ImportSomethingSoIGetImportCompletedCalled { get; set; }
-
- public int ImportCompletedCallCount { get; set; }
- public void OnImportsSatisfied()
- {
- ImportCompletedCallCount++;
- }
- }
-
- public class MyNotifyImportImporter : IPartImportsSatisfiedNotification
- {
- private CompositionContainer container;
- public MyNotifyImportImporter(CompositionContainer container)
- {
- this.container = container;
- }
- [Import("MyNotifyImportExporter")]
- public MyNotifyImportExporter MyNotifyImportExporter { get; set; }
-
- public int ImportCompletedCallCount { get; set; }
- public void OnImportsSatisfied()
- {
- ImportCompletedCallCount++;
- }
- }
-
- [Export("LowerCaseString")]
- public class LowerCaseString
- {
- public string String { get; private set; }
- public LowerCaseString(string s)
- {
- String = s.ToLower();
- }
- }
-
- public class UpperCaseStringComponent : IPartImportsSatisfiedNotification
- {
- public UpperCaseStringComponent()
- {
- UpperCaseStrings = new List<string>();
- }
- Collection<Lazy<LowerCaseString>> lowerCaseString = new Collection<Lazy<LowerCaseString>>();
-
- [ImportMany("LowerCaseString", AllowRecomposition = true)]
- public Collection<Lazy<LowerCaseString>> LowerCaseStrings {
- get { return lowerCaseString; }
- set { lowerCaseString = value; }
- }
-
- public List<string> UpperCaseStrings { get; set; }
-
- public int ImportCompletedCallCount { get; set; }
-
- // This method gets called whenever a bind is completed and any of
- // of the imports have changed, but ar safe to use now.
- public void OnImportsSatisfied()
- {
- UpperCaseStrings.Clear();
- foreach (var i in LowerCaseStrings)
- UpperCaseStrings.Add(i.Value.String.ToUpper());
-
- ImportCompletedCallCount++;
- }
- }
-}
|