PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/ExportCollectionTests.cs

https://github.com/iainlane/mono
C# | 208 lines | 156 code | 46 blank | 6 comment | 2 complexity | 17e0f52f455775a5e28c700fa0905934 MD5 | raw file
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Linq;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel.Composition.Factories;
  9. using System.ComponentModel.Composition.Hosting;
  10. using System.ComponentModel.Composition.Primitives;
  11. using System.ComponentModel.Composition.UnitTesting;
  12. using System.UnitTesting;
  13. using Microsoft.VisualStudio.TestTools.UnitTesting;
  14. namespace System.ComponentModel.Composition
  15. {
  16. [TestClass]
  17. public class ExportCollectionTests
  18. {
  19. public interface ICustomMetadata
  20. {
  21. bool PropertyName { get; }
  22. }
  23. public class Importer
  24. {
  25. [ImportMany("Value")]
  26. public Collection<Lazy<object>> CollectionPlain { get; set; }
  27. [ImportMany("Value")]
  28. public Collection<Lazy<object, IDictionary<string, object>>> CollectionPlainRawMetadata { get; set; }
  29. [ImportMany("EmptyValue")]
  30. public Collection<Lazy<object>> CollectionPlainEmpty { get; set; }
  31. [ImportMany("EmptyValue")]
  32. public Collection<Lazy<object, IDictionary<string, object>>> CollectionPlainEmptyRawMetadata { get; set; }
  33. [ImportMany("Value")]
  34. public Collection<Lazy<int>> CollectionTyped { get; set; }
  35. [ImportMany("Value")]
  36. public Collection<Lazy<int, IDictionary<string, object>>> CollectionTypedRawMetadata { get; set; }
  37. [ImportMany("EmptyValue")]
  38. public Collection<Lazy<int>> CollectionTypedEmpty { get; set; }
  39. [ImportMany("Value")]
  40. public Collection<Lazy<int, ICustomMetadata>> CollectionTypedMetadata { get; set; }
  41. [ImportMany("EmptyValue")]
  42. public Collection<Lazy<int, ICustomMetadata>> CollectionTypedMetadataEmpty { get; set; }
  43. [ImportMany("Value")]
  44. public IEnumerable<int> ReadWriteEnumerable { get; set; }
  45. [ImportMany("EmptyValue")]
  46. public IEnumerable<int> ReadWriteEnumerableEmpty { get; set; }
  47. [ImportMany("Value")]
  48. public IEnumerable<Lazy<object>> MetadataUntypedEnumerable { get; set; }
  49. [ImportMany("Value")]
  50. public IEnumerable<Lazy<object, IDictionary<string, object>>> MetadataUntypedEnumerableRawMetadata { get; set; }
  51. [ImportMany("EmptyValue")]
  52. public IEnumerable<Lazy<object>> MetadataUntypedEnumerableEmpty { get; set; }
  53. [ImportMany("EmptyValue")]
  54. public IEnumerable<Lazy<object, IDictionary<string, object>>> MetadataUntypedEnumerableEmptyRawMetadata { get; set; }
  55. [ImportMany("Value")]
  56. public IEnumerable<Lazy<int>> MetadataTypedEnumerable { get; set; }
  57. [ImportMany("Value")]
  58. public IEnumerable<Lazy<int, IDictionary<string, object>>> MetadataTypedEnumerableRawMetadata { get; set; }
  59. [ImportMany("EmptyValue")]
  60. public IEnumerable<Lazy<int>> MetadataTypedEnumerableEmpty { get; set; }
  61. [ImportMany("Value")]
  62. public IEnumerable<Lazy<int, ICustomMetadata>> MetadataFullyTypedEnumerable { get; set; }
  63. [ImportMany("EmptyValue")]
  64. public IEnumerable<Lazy<int, ICustomMetadata>> MetadataFullyTypedEnumerableEmpty { get; set; }
  65. public void VerifyImport(params int[] expectedValues)
  66. {
  67. object[] untypedExpectedValues = expectedValues.Cast<object>().ToArray();
  68. ExportsAssert.AreEqual(CollectionPlain, untypedExpectedValues);
  69. ExportsAssert.AreEqual(CollectionPlainRawMetadata, untypedExpectedValues);
  70. EnumerableAssert.IsTrueForAll(CollectionPlainRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
  71. EnumerableAssert.IsEmpty(CollectionPlainEmpty);
  72. EnumerableAssert.IsEmpty(CollectionPlainEmptyRawMetadata);
  73. // Add a new Export to this collection to ensure that it doesn't
  74. // modifiy the other collections because they should each have there
  75. // own collection instance
  76. CollectionPlain.Add(ExportFactory.Create<object>("Value"));
  77. ExportsAssert.AreEqual(CollectionTyped, expectedValues);
  78. ExportsAssert.AreEqual(CollectionTypedRawMetadata, expectedValues);
  79. EnumerableAssert.IsTrueForAll(CollectionTypedRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
  80. EnumerableAssert.IsEmpty(CollectionTypedEmpty);
  81. ExportsAssert.AreEqual(CollectionTypedMetadata, expectedValues);
  82. #if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata
  83. EnumerableAssert.IsTrueForAll(CollectionTypedMetadata, i => true == i.Metadata.PropertyName);
  84. #endif //!SILVERLIGHT
  85. EnumerableAssert.IsEmpty(CollectionTypedMetadataEmpty);
  86. EnumerableAssert.AreEqual(ReadWriteEnumerable, expectedValues);
  87. EnumerableAssert.IsEmpty(ReadWriteEnumerableEmpty);
  88. ExportsAssert.AreEqual(MetadataUntypedEnumerable, untypedExpectedValues);
  89. ExportsAssert.AreEqual(MetadataUntypedEnumerableRawMetadata, untypedExpectedValues);
  90. EnumerableAssert.IsTrueForAll(MetadataUntypedEnumerableRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
  91. EnumerableAssert.IsEmpty(MetadataUntypedEnumerableEmpty);
  92. EnumerableAssert.IsEmpty(MetadataUntypedEnumerableEmptyRawMetadata);
  93. ExportsAssert.AreEqual(MetadataTypedEnumerable, expectedValues);
  94. ExportsAssert.AreEqual(MetadataTypedEnumerableRawMetadata, expectedValues);
  95. EnumerableAssert.IsTrueForAll(MetadataTypedEnumerableRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
  96. EnumerableAssert.IsEmpty(MetadataTypedEnumerableEmpty);
  97. ExportsAssert.AreEqual(MetadataFullyTypedEnumerable, expectedValues);
  98. #if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata
  99. EnumerableAssert.IsTrueForAll(MetadataFullyTypedEnumerable, i => true == i.Metadata.PropertyName);
  100. #endif //!SILVERLIGHT
  101. EnumerableAssert.IsEmpty(MetadataFullyTypedEnumerableEmpty);
  102. }
  103. }
  104. public class ExporterDefault21
  105. {
  106. public ExporterDefault21() { Value = 21; }
  107. public ExporterDefault21(int v) { Value = v; }
  108. [Export("Value")]
  109. [ExportMetadata("PropertyName", true)]
  110. public int Value { get; set; }
  111. }
  112. public class ExporterDefault42
  113. {
  114. public ExporterDefault42() { Value = 42; }
  115. public ExporterDefault42(int v) { Value = v; }
  116. [Export("Value")]
  117. [ExportMetadata("PropertyName", true)]
  118. public int Value { get; set; }
  119. }
  120. [TestMethod]
  121. [TestProperty("Type", "Integration")]
  122. public void ImportCollectionsFromContainerOnly()
  123. {
  124. var container = ContainerFactory.Create();
  125. Importer importer = new Importer();
  126. CompositionBatch batch = new CompositionBatch();
  127. batch.AddParts(importer
  128. , new ExporterDefault21()
  129. , new ExporterDefault21(22)
  130. , new ExporterDefault42()
  131. , new ExporterDefault42(43));
  132. container.Compose(batch);
  133. importer.VerifyImport(21, 22, 42, 43);
  134. }
  135. [TestMethod]
  136. [TestProperty("Type", "Integration")]
  137. public void ImportCollectionsFromCatalogOnly()
  138. {
  139. var cat = CatalogFactory.CreateDefaultAttributed();
  140. var container = new CompositionContainer(cat);
  141. Importer importer = new Importer();
  142. CompositionBatch batch = new CompositionBatch();
  143. batch.AddParts(importer);
  144. container.Compose(batch);
  145. importer.VerifyImport(21, 42);
  146. }
  147. [TestMethod]
  148. [TestProperty("Type", "Integration")]
  149. public void ImportCollectionsFormContainerAndCatalog()
  150. {
  151. var cat = CatalogFactory.CreateDefaultAttributed();
  152. var container = new CompositionContainer(cat);
  153. Importer importer = new Importer();
  154. CompositionBatch batch = new CompositionBatch();
  155. batch.AddParts(importer
  156. , new ExporterDefault21(22)
  157. , new ExporterDefault42(43));
  158. container.Compose(batch);
  159. importer.VerifyImport(22, 43, 21, 42);
  160. }
  161. }
  162. }