PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/redist/test/ComponentModelUnitTest/System/ComponentModel/Composition/AdvancedValueComposition.cs

http://mef.codeplex.com
C# | 273 lines | 224 code | 46 blank | 3 comment | 18 complexity | 9cba12734f7d37539aac01554fabafab MD5 | raw file
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.Composition;
  7. using System.Linq;
  8. using System.Reflection;
  9. using Microsoft.VisualStudio.TestTools.UnitTesting;
  10. using System.ComponentModel.Composition.Factories;
  11. using System.ComponentModel.Composition.Hosting;
  12. using System.ComponentModel.Composition.UnitTesting;
  13. using System.ComponentModel.Composition.AttributedModel;
  14. using System.UnitTesting;
  15. namespace System.ComponentModel.Composition
  16. {
  17. [TestClass]
  18. public class AdvancedValueComposition
  19. {
  20. [TestMethod]
  21. public void RepeatedContainerUse()
  22. {
  23. var container = ContainerFactory.Create();
  24. TrivialExporter e = new TrivialExporter();
  25. CompositionBatch batch = new CompositionBatch();
  26. batch.AddPart(e);
  27. container.Compose(batch);
  28. batch = new CompositionBatch();
  29. batch.AddPart(new TrivialImporter());
  30. container.Compose(batch);
  31. Assert.IsTrue(e.done, "Initialization of importer should have set the done flag on E");
  32. }
  33. [TestMethod]
  34. public void FunctionsFieldsAndProperties()
  35. {
  36. Consumer c;
  37. var container = ContainerFactory.Create();
  38. CompositionBatch batch = new CompositionBatch();
  39. batch.AddPart(new RealAddProvider());
  40. batch.AddPart(c = new Consumer());
  41. container.Compose(batch);
  42. Assert.AreEqual(3, c.op(c.a, c.b), "1 + 2 == 3");
  43. }
  44. [TestMethod]
  45. public void FunctionsFieldsAndProperties2()
  46. {
  47. Consumer c;
  48. var container = ContainerFactory.Create();
  49. CompositionBatch batch = new CompositionBatch();
  50. batch.AddPart(new SubtractProvider());
  51. batch.AddPart(c = new Consumer());
  52. container.Compose(batch);
  53. Assert.AreEqual(-1, c.op(c.a, c.b), "1 - 2 == -1");
  54. }
  55. [TestMethod]
  56. public void FunctionsFieldsAndProperties2_WithCatalog()
  57. {
  58. var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
  59. ConsumerOfMultiple c = new ConsumerOfMultiple();
  60. CompositionBatch batch = new CompositionBatch();
  61. batch.AddPart(c);
  62. container.Compose(batch);
  63. foreach (var export in c.opInfo)
  64. {
  65. if ((string)export.Metadata["Var1"] == "add")
  66. {
  67. Assert.AreEqual(3, export.Value(1, 2), "1 + 2 == 3");
  68. }
  69. else if ((string)export.Metadata["Var1"] == "sub")
  70. {
  71. Assert.AreEqual(-1, export.Value(1, 2), "1 - 2 == -1");
  72. }
  73. else
  74. {
  75. Assert.Fail("Unexpected value");
  76. }
  77. }
  78. }
  79. [TestMethod]
  80. public void FunctionsFieldsAndProperties2_StronglyTypedMetadata()
  81. {
  82. var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
  83. var exports = container.GetExports<Func<int, int, int>, ITrans_ExportableTest>("Add");
  84. foreach (var export in exports)
  85. {
  86. if (export.Metadata.Var1 == "add")
  87. {
  88. Assert.AreEqual(3, export.Value(1, 2), "1 + 2 == 3");
  89. }
  90. else if (export.Metadata.Var1 == "sub")
  91. {
  92. Assert.AreEqual(-1, export.Value(1, 2), "1 - 2 == -1");
  93. }
  94. else
  95. {
  96. Assert.Fail("Unexpected value");
  97. }
  98. }
  99. }
  100. [TestMethod]
  101. public void InAdditionToCatalogTest()
  102. {
  103. var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
  104. IDictionary<string, object> multMetadata = new Dictionary<string, object>();
  105. multMetadata["Var1"]= "mult";
  106. multMetadata[CompositionConstants.ExportTypeIdentityMetadataName] = AttributedModelServices.GetTypeIdentity(typeof(Func<int, int, int>));
  107. var basicValue = ExportFactory.Create("Add", multMetadata, (() => (Func<int, int, int>)delegate(int a, int b) { return a * b; }));
  108. CompositionBatch batch = new CompositionBatch();
  109. batch.AddExport(basicValue);
  110. container.Compose(batch);
  111. var exports = container.GetExports<Func<int, int, int>, ITrans_ExportableTest>("Add");
  112. Assert.AreEqual(3, exports.Count(), "There should be 3 entries for 'Add'");
  113. foreach (var export in exports)
  114. {
  115. if (export.Metadata.Var1 == "mult")
  116. {
  117. Assert.AreEqual(2, export.Value(1, 2), "1 * 2 == 2");
  118. }
  119. else if (export.Metadata.Var1 == "add")
  120. {
  121. Assert.AreEqual(3, export.Value(1, 2), "1 + 2 == 3");
  122. }
  123. else if (export.Metadata.Var1 == "sub")
  124. {
  125. Assert.AreEqual(-1, export.Value(1, 2), "1 - 2 == -1");
  126. }
  127. else
  128. {
  129. Assert.Fail("Unexpected value");
  130. }
  131. }
  132. }
  133. [TestMethod]
  134. public void CollectionMetadataPropertyTest()
  135. {
  136. var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
  137. var export = container.GetExport<ComponentWithCollectionProperty, ITrans_CollectionOfStrings>();
  138. Assert.IsNotNull(export.Metadata, "Should have metadata");
  139. Assert.IsNotNull(export.Metadata.Values, "MetadataView should have collection of values");
  140. Assert.AreEqual(export.Metadata.Values.Count(), 3, "Should have 3 elements");
  141. Assert.AreEqual(export.Metadata.Values.First(), "One", "First should be 'One'");
  142. Assert.AreEqual(export.Metadata.Values.Skip(1).First(), "two", "First should be 'two'");
  143. Assert.AreEqual(export.Metadata.Values.Skip(2).First(), "3", "First should be '3'");
  144. }
  145. [TestMethod]
  146. public void ImportExportSansNameTest()
  147. {
  148. var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
  149. UnnamedImportAndExport unnamed = container.GetExportedValue<UnnamedImportAndExport>();
  150. Assert.IsNotNull(unnamed, "Should have found UnnamedImportAndExport component");
  151. Assert.IsNotNull(unnamed.ImportedValue, "Component's unnamed import should have been fulfilled");
  152. }
  153. [TestMethod]
  154. public void MultipleInstantiationOfStaticCatalogItem()
  155. {
  156. var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
  157. var unnamedVI = container.GetExport<StaticExport, object>();
  158. StaticExport first = unnamedVI.Value;
  159. StaticExport second = unnamedVI.Value;
  160. Assert.IsNotNull(first, "Should have created an instance");
  161. Assert.IsNotNull(second, "Should have created a second instance");
  162. Assert.IsTrue(object.ReferenceEquals(first, second), "Instances should be the same");
  163. var exports = container.GetExports<StaticExport, object>();
  164. Assert.AreEqual(1, exports.Count(), "There should still only be one exported value");
  165. }
  166. [TestMethod]
  167. public void MultipleInstantiationOfNonStaticCatalogItem()
  168. {
  169. var container = ContainerFactory.CreateWithDefaultAttributedCatalog();
  170. var export1 = container.GetExport<NonStaticExport, object>();
  171. var export2 = container.GetExport<NonStaticExport, object>();
  172. NonStaticExport first = export1.Value;
  173. NonStaticExport second = export2.Value;
  174. Assert.IsNotNull(first, "Should have created an instance");
  175. Assert.IsNotNull(second, "Should have created a second instance");
  176. Assert.IsFalse(object.ReferenceEquals(first, second), "Instances should be different");
  177. }
  178. [TestMethod]
  179. public void ImportIntoUntypedExportTest()
  180. {
  181. var container = ContainerFactory.Create();
  182. CompositionBatch batch = new CompositionBatch();
  183. batch.AddExportedValue("untyped", 42);
  184. var u = new UntypedExportImporter();
  185. var rb = AttributedModelServices.CreatePart(u);
  186. batch.AddPart(rb);
  187. container.Compose(batch);
  188. Assert.AreEqual(42, u.Export.Value);
  189. var us = new UntypedExportsImporter();
  190. batch = new CompositionBatch();
  191. batch.AddExportedValue("untyped", 19);
  192. batch.RemovePart(rb);
  193. batch.AddPart(us);
  194. container.Compose(batch);
  195. Assert.IsNotNull(us.Exports, "Should have an enumeration");
  196. Assert.AreEqual(2, us.Exports.Count(), "Should have 2 values");
  197. }
  198. [TestMethod]
  199. public void ImportIntoDerivationOfExportException()
  200. {
  201. var container = ContainerFactory.Create();
  202. CompositionBatch batch = new CompositionBatch();
  203. batch.AddExportedValue("derived", typeof(DerivedExport), 42);
  204. var d = new DerivedExportImporter();
  205. batch.AddPart(d);
  206. CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
  207. ErrorId.ReflectionModel_ImportNotAssignableFromExport, RetryMode.DoNotRetry, () =>
  208. {
  209. container.Compose(batch);
  210. });
  211. }
  212. [TestMethod]
  213. public void ImportIntoDerivationOfExportsException()
  214. {
  215. var container = ContainerFactory.Create();
  216. CompositionBatch batch = new CompositionBatch();
  217. batch.AddExportedValue("derived", typeof(DerivedExport), 42);
  218. var d = new DerivedExportsImporter();
  219. batch.AddPart(d);
  220. CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotSetImport,
  221. ErrorId.ReflectionModel_ImportNotAssignableFromExport, RetryMode.DoNotRetry, () =>
  222. {
  223. container.Compose(batch);
  224. });
  225. }
  226. }
  227. }