// ----------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. // ----------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.ComponentModel.Composition.Factories; using System.UnitTesting; using System.ComponentModel.Composition.UnitTesting; using System.ComponentModel.Composition.Hosting; namespace System.ComponentModel.Composition { [TestClass] public class CompositionContainerCollectionTests { public class SupportedImportCollectionAssignments { // Fields [ImportMany("Value")] public IEnumerable IEnumerableOfTField; [ImportMany("Value")] public IEnumerable IEnumerableOfObjectField; [ImportMany("Value")] public T[] ArrayOfTField; [ImportMany("Value")] public object[] ArrayOfObjectField; [ImportMany("Value")] public IEnumerable IEnumerableOfTProperty { get; set; } [ImportMany("Value")] public IEnumerable IEnumerableOfObjectProperty { get; set; } [ImportMany("Value")] public T[] ArrayOfTProperty { get; set; } [ImportMany("Value")] public object[] ArrayOfObjectProperty { get; set; } public void VerifyImports(params T[] expectedValues) { // Fields EnumerableAssert.AreEqual(IEnumerableOfTField, expectedValues); EnumerableAssert.AreEqual(IEnumerableOfObjectField, expectedValues.Cast()); EnumerableAssert.AreEqual(ArrayOfTField, expectedValues); EnumerableAssert.AreEqual(ArrayOfObjectField, expectedValues.Cast()); // Properties EnumerableAssert.AreEqual(IEnumerableOfTProperty, expectedValues); EnumerableAssert.AreEqual(IEnumerableOfObjectProperty, expectedValues.Cast()); EnumerableAssert.AreEqual(ArrayOfTProperty, expectedValues); EnumerableAssert.AreEqual(ArrayOfObjectProperty, expectedValues.Cast()); } } [TestMethod] public void ValidateSupportedImportCollectionAssignments() { var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new SupportedImportCollectionAssignments(); batch.AddPart(importer); batch.AddExportedValue("Value", 21); batch.AddExportedValue("Value", 32); batch.AddExportedValue("Value", 43); container.Compose(batch); importer.VerifyImports(21, 32, 43); } public class SupportedImportCollectionMutation { public SupportedImportCollectionMutation() { ICollectionOfTReadOnlyField = new List(); ListOfTReadOnlyField = new List(); CollectionOfTField = new Collection(); CollectionOfTReadOnlyField = new Collection(); _iCollectionOfTReadOnlyProperty = new List(); _listOfTReadOnlyProperty = new List(); CollectionOfTProperty = new Collection(); _collectionOfTReadOnlyProperty = new Collection(); #if !SILVERLIGHT ObservableCollectionOfTReadOnlyField = new ObservableCollection(); _observableCollectionOfTReadOnlyProperty = new ObservableCollection(); #endif // !SILVERLIGHT } [ImportMany("Value")] public readonly ICollection ICollectionOfTReadOnlyField; [ImportMany("Value")] public List ListOfTField; [ImportMany("Value")] public readonly List ListOfTReadOnlyField; [ImportMany("Value")] public Collection CollectionOfTField; [ImportMany("Value")] public Collection CollectionOfObjectField; [ImportMany("Value")] public readonly Collection CollectionOfTReadOnlyField; [ImportMany("Value")] public ICollection ICollectionOfTReadOnlyProperty { get { return _iCollectionOfTReadOnlyProperty; } } private ICollection _iCollectionOfTReadOnlyProperty; [ImportMany("Value")] public List ListOfTProperty { get; set; } [ImportMany("Value")] public List ListOfTReadOnlyProperty { get { return _listOfTReadOnlyProperty; } } private readonly List _listOfTReadOnlyProperty; [ImportMany("Value")] public Collection CollectionOfTProperty { get; set; } [ImportMany("Value")] public Collection CollectionOfTReadOnlyProperty { get { return _collectionOfTReadOnlyProperty; } } private readonly Collection _collectionOfTReadOnlyProperty; #if !SILVERLIGHT [ImportMany("Value")] public ObservableCollection ObservableCollectionOfTField; [ImportMany("Value")] public readonly ObservableCollection ObservableCollectionOfTReadOnlyField; [ImportMany("Value")] public ObservableCollection ObservableCollectionOfTProperty { get; set; } [ImportMany("Value")] public ObservableCollection ObservableCollectionOfTReadOnlyProperty { get { return _observableCollectionOfTReadOnlyProperty; } } private readonly ObservableCollection _observableCollectionOfTReadOnlyProperty; #endif // !SILVERLIGHT public void VerifyImports(params T[] expectedValues) { EnumerableAssert.AreEqual(ICollectionOfTReadOnlyField, expectedValues); EnumerableAssert.AreEqual(ListOfTField, expectedValues); EnumerableAssert.AreEqual(ListOfTReadOnlyField, expectedValues); EnumerableAssert.AreEqual(CollectionOfTField, expectedValues); EnumerableAssert.AreEqual(CollectionOfTReadOnlyField, expectedValues); EnumerableAssert.AreEqual(ICollectionOfTReadOnlyProperty, expectedValues); EnumerableAssert.AreEqual(ListOfTProperty, expectedValues); EnumerableAssert.AreEqual(ListOfTReadOnlyProperty, expectedValues); EnumerableAssert.AreEqual(CollectionOfTProperty, expectedValues); EnumerableAssert.AreEqual(CollectionOfTReadOnlyProperty, expectedValues); #if !SILVERLIGHT EnumerableAssert.AreEqual(ObservableCollectionOfTField, expectedValues); EnumerableAssert.AreEqual(ObservableCollectionOfTReadOnlyField, expectedValues); EnumerableAssert.AreEqual(ObservableCollectionOfTProperty, expectedValues); EnumerableAssert.AreEqual(ObservableCollectionOfTReadOnlyProperty, expectedValues); #endif // !SILVERLIGHT } } [TestMethod] public void ValidateSupportedImportCollectionMutation() { var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new SupportedImportCollectionMutation(); batch.AddPart(importer); batch.AddExportedValue("Value", 21); batch.AddExportedValue("Value", 32); batch.AddExportedValue("Value", 43); container.Compose(batch); importer.VerifyImports(21, 32, 43); } public class ImportCollectionNullValue { [ImportMany("Value")] public List NullValue { get; set; } } public class NamelessImporter { [ImportMany] public int[] ReadWriteIList { get; set; } [ImportMany] public Collection> ReadWriteMetadata { get; set; } } public class NamelessExporter { public NamelessExporter(int value) { Value = value; } [Export] public int Value { get; set; } } [TestMethod] public void ImportCollectionsNameless() { // Verifing that the contract name gets the correct value var container = ContainerFactory.Create(); NamelessImporter importer = new NamelessImporter(); NamelessExporter exporter42 = new NamelessExporter(42); NamelessExporter exporter0 = new NamelessExporter(0); CompositionBatch batch = new CompositionBatch(); batch.AddParts(importer, exporter42, exporter0); container.Compose(batch); EnumerableAssert.AreEqual(importer.ReadWriteIList, 42, 0); } public class InvalidImporterReadOnlyEnumerable { IEnumerable readOnlyEnumerable = new List(); [ImportMany("Value")] public IEnumerable ReadOnlyEnumerable { get { return readOnlyEnumerable; } } } [TestMethod] public void ImportCollectionsExceptionReadOnlyEnumerable() { ExpectedErrorOnPartActivate(new InvalidImporterReadOnlyEnumerable(), ErrorId.ReflectionModel_ImportCollectionNotWritable); } public class ImporterWriteOnlyExportCollection { [ImportMany("Value")] public Collection> WriteOnlyExportCollection { set { PublicExportCollection = value; } } public Collection> PublicExportCollection { get; set; } } [TestMethod] public void ImportCollections_WriteOnlyExportCollection() { var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new ImporterWriteOnlyExportCollection(); List values = new List() { 21, 32, 43 }; batch.AddPart(importer); values.ForEach(v => batch.AddExportedValue("Value", v)); container.Compose(batch); EnumerableAssert.AreEqual(values, importer.PublicExportCollection.Select(export => export.Value)); } public class ImporterWriteOnlyIEnumerableOfT { [ImportMany("Value")] public IEnumerable WriteOnlyIEnumerable { set { PublicIEnumerable = value; } } public IEnumerable PublicIEnumerable { get; set; } } [TestMethod] public void ImportCollections_WriteOnlyIEnumerableOfT() { var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new ImporterWriteOnlyIEnumerableOfT(); List values = new List() { 21, 32, 43 }; batch.AddPart(importer); values.ForEach(v => batch.AddExportedValue("Value", v)); container.Compose(batch); EnumerableAssert.AreEqual(values, importer.PublicIEnumerable); } public class ImporterWriteOnlyArray { [ImportMany("Value")] public int[] WriteOnlyArray { set { PublicArray = value; } } public int[] PublicArray { get; set; } } [TestMethod] public void ImportCollections_WriteOnlyArray() { var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new ImporterWriteOnlyArray(); List values = new List() { 21, 32, 43 }; batch.AddPart(importer); values.ForEach(v => batch.AddExportedValue("Value", v)); container.Compose(batch); EnumerableAssert.AreEqual(values, importer.PublicArray); } public class InvalidImporterNonCollection { [Import("Value")] public int Value { get; set; } } [TestMethod] public void ImportCollectionsExceptionNonCollection() { ExpectedChangeRejectedErrorOnSetImport(new InvalidImporterNonCollection(), ErrorId.ImportEngine_ImportCardinalityMismatch); } public class InvalidImporterNonAssignableCollection { [ImportMany("Value", typeof(int))] public IEnumerable StringCollection { get; set; } } [TestMethod] public void ImportCollectionsExceptionNonAssignableCollection() { ExpectedErrorOnSetImport(new InvalidImporterNonAssignableCollection(), ErrorId.ReflectionModel_ImportNotAssignableFromExport); } public class InvalidImporterNullReadOnlyICollection { ICollection readOnlyICollection = null; [ImportMany("Value")] public ICollection Values { get { return readOnlyICollection; } } } [TestMethod] public void ImportCollectionsExceptionNullReadOnlyICollection() { ExpectedErrorOnPartActivate(new InvalidImporterNullReadOnlyICollection(), ErrorId.ReflectionModel_ImportCollectionNull); } public class ImporterWeakIEnumerable { public ImporterWeakIEnumerable() { ReadWriteEnumerable = new IntCollection(); } [ImportMany("Value")] public IntCollection ReadWriteEnumerable { get; set; } public class IntCollection : IEnumerable { List ints = new List(); public void Add(int item) { ints.Add(item); } public void Clear() { ints.Clear(); } public bool Remove(int item) { return ints.Remove(item); } public IEnumerator GetEnumerator() { return ints.GetEnumerator(); } } } [TestMethod] public void ImportCollectionsExceptionWeakCollectionNotSupportingICollectionOfT() { ExpectedErrorOnPartActivate(new ImporterWeakIEnumerable(), ErrorId.ReflectionModel_ImportCollectionNotWritable); } public class ImporterThrowsOnGetting { [ImportMany("Value")] public List Value { get { throw new NotSupportedException(); } } } [TestMethod] 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 class CustomCollectionThrowsDuringConstruction : Collection { public CustomCollectionThrowsDuringConstruction() { throw new NotSupportedException(); } } public class ImportCustomCollectionThrowsDuringConstruction { public ImportCustomCollectionThrowsDuringConstruction() { } [ImportMany("Value")] public CustomCollectionThrowsDuringConstruction Values { get; set; } } [TestMethod] public void ImportCollections_ImportTypeThrowsOnConstruction() { ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringConstruction(), ErrorId.ReflectionModel_ImportCollectionConstructionThrewException); } public class CustomCollectionThrowsDuringClear : Collection { protected override void ClearItems() { throw new NotSupportedException(); } } public class ImportCustomCollectionThrowsDuringClear { public ImportCustomCollectionThrowsDuringClear() { } [ImportMany("Value")] public CustomCollectionThrowsDuringClear Values { get; set; } } [TestMethod] public void ImportCollections_ImportTypeThrowsOnClear() { ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringClear(), ErrorId.ReflectionModel_ImportCollectionClearThrewException); } public class CustomCollectionThrowsDuringAdd : Collection { protected override void InsertItem(int index, int item) { throw new NotSupportedException(); } } public class ImportCustomCollectionThrowsDuringAdd { public ImportCustomCollectionThrowsDuringAdd() { } [ImportMany("Value")] public CustomCollectionThrowsDuringAdd Values { get; set; } } [TestMethod] public void ImportCollections_ImportTypeThrowsOnAdd() { ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringAdd(), ErrorId.ReflectionModel_ImportCollectionAddThrewException); } public class CustomCollectionThrowsDuringIsReadOnly : ICollection { void ICollection.Add(int item) { throw new NotImplementedException(); } void ICollection.Clear() { throw new NotImplementedException(); } bool ICollection.Contains(int item) { throw new NotImplementedException(); } void ICollection.CopyTo(int[] array, int arrayIndex) { throw new NotImplementedException(); } int ICollection.Count { get { throw new NotImplementedException(); } } bool ICollection.IsReadOnly { get { throw new NotSupportedException(); } } bool ICollection.Remove(int item) { throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } public class ImportCustomCollectionThrowsDuringIsReadOnly { public ImportCustomCollectionThrowsDuringIsReadOnly() { Values = new CustomCollectionThrowsDuringIsReadOnly(); } [ImportMany("Value")] public CustomCollectionThrowsDuringIsReadOnly Values { get; set; } } [TestMethod] public void ImportCollections_ImportTypeThrowsOnIsReadOnly() { ExpectedErrorOnPartActivate(new ImportCustomCollectionThrowsDuringIsReadOnly(), ErrorId.ReflectionModel_ImportCollectionIsReadOnlyThrewException); } public class CollectionTypeWithNoIList : ICollection { private int _count = 0; public CollectionTypeWithNoIList() { } public void Add(T item) { // Do Nothing this._count++; } public void Clear() { // Do Nothings } public bool Contains(T item) { throw new NotImplementedException(); } public void CopyTo(T[] array, int arrayIndex) { throw new NotImplementedException(); } public int Count { get { return this._count; } } public bool IsReadOnly { get { return false; } } public bool Remove(T item) { throw new NotImplementedException(); } public IEnumerator GetEnumerator() { throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } public class ImportCollectionWithNoIList { [ImportMany("Value")] public CollectionTypeWithNoIList Values { get; set; } } [TestMethod] public void ImportCollections_NoIList_ShouldWorkFine() { var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new ImportCollectionWithNoIList(); batch.AddPart(importer); batch.AddExportedValue("Value", 42); batch.AddExportedValue("Value", 0); container.Compose(batch); Assert.AreEqual(2, importer.Values.Count); } public class CollectionWithMultipleInterfaces : ICollection, ICollection { private int _count = 0; public CollectionWithMultipleInterfaces() { } #region ICollection Members void ICollection.Add(int item) { throw new NotImplementedException(); } void ICollection.Clear() { throw new NotImplementedException(); } bool ICollection.Contains(int item) { throw new NotImplementedException(); } void ICollection.CopyTo(int[] array, int arrayIndex) { throw new NotImplementedException(); } int ICollection.Count { get { throw new NotImplementedException(); } } bool ICollection.IsReadOnly { get { throw new NotImplementedException(); } } bool ICollection.Remove(int item) { throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } #endregion #region ICollection Members void ICollection.Add(string item) { throw new NotImplementedException(); } void ICollection.Clear() { throw new NotImplementedException(); } bool ICollection.Contains(string item) { throw new NotImplementedException(); } void ICollection.CopyTo(string[] array, int arrayIndex) { throw new NotImplementedException(); } int ICollection.Count { get { throw new NotImplementedException(); } } bool ICollection.IsReadOnly { get { throw new NotImplementedException(); } } bool ICollection.Remove(string item) { throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } #endregion } public class ImportCollectionWithMultipleInterfaces { [ImportMany("Value")] public CollectionWithMultipleInterfaces Values { get; set; } } [TestMethod] public void ImportCollections_MultipleICollections_ShouldCauseNotWriteable() { ExpectedErrorOnPartActivate(new ImportCollectionWithMultipleInterfaces(), ErrorId.ReflectionModel_ImportCollectionNotWritable); } public class ImportManyNonCollectionTypeString { [ImportMany("Value")] public string Foo { get; set; } } [TestMethod] public void ImportManyOnNonCollectionTypeString_ShouldCauseNotWritable() { ExpectedErrorOnPartActivate(new ImportManyNonCollectionTypeString(), ErrorId.ReflectionModel_ImportCollectionNotWritable); } public class ImportManyNonCollectionTypeObject { [ImportMany("Value")] public object Foo { get; set; } } [TestMethod] public void ImportManyOnNonCollectionTypeObject_ShouldCauseNotWritable() { ExpectedErrorOnPartActivate(new ImportManyNonCollectionTypeObject(), ErrorId.ReflectionModel_ImportCollectionNotWritable); } #if !SILVERLIGHT // SILVERLIGHT doesn't have SetEnvironmentVariable set in this test. public class ExportADictionaryObject { [Export] public IDictionary MyDictionary { get { var dictionary = new Dictionary(); dictionary.Add("a", 42); dictionary.Add("b", "c"); return dictionary; } } } public class ImportADictionaryObject { [Import] public IDictionary MyDictionary { get; set; } } [TestMethod] public void ImportDictionaryAsSingleObject() { // Set variable to ensure the hack is turned off. Environment.SetEnvironmentVariable("ONLY_ALLOW_IMPORTMANY", "1"); var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new ImportADictionaryObject(); var exporter = new ExportADictionaryObject(); batch.AddPart(importer); batch.AddPart(exporter); container.Compose(batch); Assert.AreEqual(2, importer.MyDictionary.Count); } public class ExportACollectionObject { [Export] public Collection MyCollection { get { var collection = new Collection(); collection.Add("a"); collection.Add("b"); return collection; } } } public class ImportACollectionObject { [Import] public Collection MyCollection { get; set; } } [TestMethod] public void ImportCollectionAsSingleObject() { // Set variable to ensure the hack is turned off. Environment.SetEnvironmentVariable("ONLY_ALLOW_IMPORTMANY", "1"); var container = ContainerFactory.Create(); var batch = new CompositionBatch(); var importer = new ImportACollectionObject(); var exporter = new ExportACollectionObject(); batch.AddPart(importer); batch.AddPart(exporter); container.Compose(batch); Assert.AreEqual(2, importer.MyCollection.Count); } #endif public void ExpectedErrorOnPartActivate(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_PartCannotActivate, expectedErrorId, 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); }); } 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); }); } } }