1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.Collections.Generic;
\r
6 using System.ComponentModel.Composition;
\r
8 using System.Linq.Expressions;
\r
9 using System.Runtime.Serialization;
\r
10 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
11 using System.ComponentModel.Composition.Factories;
\r
12 using System.UnitTesting;
\r
13 using System.ComponentModel.Composition.AttributedModel;
\r
14 using System.ComponentModel.Composition.Hosting;
\r
15 using System.ComponentModel.Composition.Primitives;
\r
16 using System.ComponentModel.Composition.UnitTesting;
\r
18 namespace System.ComponentModel.Composition
\r
21 public class CompositionContainerTests
\r
24 public void Constructor2_ArrayWithNullElementAsProvidersArgument_ShouldThrowArgumentException()
\r
26 ExceptionAssert.ThrowsArgument<ArgumentException>("providers", () =>
\r
28 new CompositionContainer(new ExportProvider[] { null });
\r
33 public void Constructor3_ArrayWithNullElementAsProvidersArgument_ShouldThrowArgumentException()
\r
35 var catalog = CatalogFactory.Create();
\r
37 ExceptionAssert.ThrowsArgument<ArgumentException>("providers", () =>
\r
39 new CompositionContainer(catalog, new ExportProvider[] { null });
\r
44 public void Constructor2_ArrayAsProvidersArgument_ShouldNotAllowModificationAfterConstruction()
\r
46 var providers = new ExportProvider[] { ExportProviderFactory.Create() };
\r
47 var container = new CompositionContainer(providers);
\r
49 providers[0] = null;
\r
51 Assert.IsNotNull(container.Providers[0]);
\r
55 public void Constructor3_ArrayAsProvidersArgument_ShouldNotAllowModificationAfterConstruction()
\r
57 var providers = new ExportProvider[] { ExportProviderFactory.Create() };
\r
58 var container = new CompositionContainer(CatalogFactory.Create(), providers);
\r
60 providers[0] = null;
\r
62 Assert.IsNotNull(container.Providers[0]);
\r
66 public void Constructor1_ShouldSetProvidersPropertyToEmptyCollection()
\r
68 var container = new CompositionContainer();
\r
70 EnumerableAssert.IsEmpty(container.Providers);
\r
74 public void Constructor2_EmptyArrayAsProvidersArgument_ShouldSetProvidersPropertyToEmpty()
\r
76 var container = new CompositionContainer(new ExportProvider[0]);
\r
78 EnumerableAssert.IsEmpty(container.Providers);
\r
82 public void Constructor3_EmptyArrayAsProvidersArgument_ShouldSetProvidersPropertyToEmpty()
\r
84 var container = new CompositionContainer(CatalogFactory.Create(), new ExportProvider[0]);
\r
86 EnumerableAssert.IsEmpty(container.Providers);
\r
90 public void Constructor1_ShouldSetCatalogPropertyToNull()
\r
92 var container = new CompositionContainer();
\r
94 Assert.IsNull(container.Catalog);
\r
98 public void Constructor2_ShouldSetCatalogPropertyToNull()
\r
100 var container = new CompositionContainer(new ExportProvider[0]);
\r
102 Assert.IsNull(container.Catalog);
\r
106 public void Constructor3_NullAsCatalogArgument_ShouldSetCatalogPropertyToNull()
\r
108 var container = new CompositionContainer((ComposablePartCatalog)null, new ExportProvider[0]);
\r
110 Assert.IsNull(container.Catalog);
\r
114 public void Constructor3_ValueAsCatalogArgument_ShouldSetCatalogProperty()
\r
116 var expectations = Expectations.GetCatalogs();
\r
118 foreach (var e in expectations)
\r
120 var container = new CompositionContainer(e, new ExportProvider[0]);
\r
122 Assert.AreSame(e, container.Catalog);
\r
127 public void Catalog_WhenDisposed_ShouldThrowObjectDisposed()
\r
129 var container = CreateCompositionContainer();
\r
130 container.Dispose();
\r
132 ExceptionAssert.ThrowsDisposed(container, () =>
\r
134 var catalog = container.Catalog;
\r
139 public void Providers_WhenDisposed_ShouldThrowObjectDisposed()
\r
141 var container = CreateCompositionContainer();
\r
142 container.Dispose();
\r
144 ExceptionAssert.ThrowsDisposed(container, () =>
\r
146 var providers = container.Providers;
\r
152 [WorkItem(579990)] // NullReferenceException
\r
153 public void ExportsChanged_Add_WhenDisposed_ShouldThrowObjectDisposed()
\r
155 var container = CreateCompositionContainer();
\r
156 container.Dispose();
\r
158 ExceptionAssert.ThrowsDisposed(container, () =>
\r
160 container.ExportsChanged += (o, s) => { };
\r
166 [WorkItem(579990)] // NullReferenceException
\r
167 public void ExportsChanged_Remove_WhenDisposed_ShouldThrowObjectDisposed()
\r
169 var container = CreateCompositionContainer();
\r
170 container.Dispose();
\r
172 ExceptionAssert.ThrowsDisposed(container, () =>
\r
174 container.ExportsChanged -= (o, s) => { };
\r
179 public void AddPart1_ImportOnlyPart_ShouldNotGetGarbageCollected()
\r
181 var container = CreateCompositionContainer();
\r
183 var import = PartFactory.CreateImporter("Value", ImportCardinality.ZeroOrMore);
\r
185 CompositionBatch batch = new CompositionBatch();
\r
186 batch.AddPart(import);
\r
187 container.Compose(batch);
\r
189 var weakRef = new WeakReference(import);
\r
193 GC.WaitForPendingFinalizers();
\r
195 Assert.IsNotNull(weakRef.Target, "Import only part should not have been collected!");
\r
197 GC.KeepAlive(container);
\r
201 public void Compose_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
203 var container = CreateCompositionContainer();
\r
204 container.Dispose();
\r
206 CompositionBatch batch = new CompositionBatch();
\r
207 ExceptionAssert.ThrowsDisposed(container, () =>
\r
209 container.Compose(batch);
\r
214 public void GetExportOfT1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
216 var container = CreateCompositionContainer();
\r
217 container.Dispose();
\r
219 ExceptionAssert.ThrowsDisposed(container, () =>
\r
221 container.GetExport<string>();
\r
226 public void GetExportOfT2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
228 var container = CreateCompositionContainer();
\r
229 container.Dispose();
\r
231 ExceptionAssert.ThrowsDisposed(container, () =>
\r
233 container.GetExport<string>("Contract");
\r
238 public void GetExportOfTTMetadataView1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
240 var container = CreateCompositionContainer();
\r
241 container.Dispose();
\r
243 ExceptionAssert.ThrowsDisposed(container, () =>
\r
245 container.GetExport<string, object>();
\r
250 public void GetExportOfTTMetadataView2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
252 var container = CreateCompositionContainer();
\r
253 container.Dispose();
\r
255 ExceptionAssert.ThrowsDisposed(container, () =>
\r
257 container.GetExport<string, object>("Contract");
\r
262 public void GetExports1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
264 var container = CreateCompositionContainer();
\r
265 container.Dispose();
\r
267 var definition = ImportDefinitionFactory.Create();
\r
268 ExceptionAssert.ThrowsDisposed(container, () =>
\r
270 container.GetExports(definition);
\r
275 public void GetExports2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
277 var container = CreateCompositionContainer();
\r
278 container.Dispose();
\r
280 ExceptionAssert.ThrowsDisposed(container, () =>
\r
282 container.GetExports(typeof(string), typeof(object), "Contract");
\r
287 public void GetExportsOfT1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
289 var container = CreateCompositionContainer();
\r
290 container.Dispose();
\r
292 ExceptionAssert.ThrowsDisposed(container, () =>
\r
294 container.GetExports<string>();
\r
299 public void GetExportsOfT2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
301 var container = CreateCompositionContainer();
\r
302 container.Dispose();
\r
304 ExceptionAssert.ThrowsDisposed(container, () =>
\r
306 container.GetExports<string>("Contract");
\r
311 public void GetExportsOfTTMetadataView1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
313 var container = CreateCompositionContainer();
\r
314 container.Dispose();
\r
316 ExceptionAssert.ThrowsDisposed(container, () =>
\r
318 container.GetExports<string, object>();
\r
323 public void GetExportsOfTTMetadataView2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
325 var container = CreateCompositionContainer();
\r
326 container.Dispose();
\r
328 ExceptionAssert.ThrowsDisposed(container, () =>
\r
330 container.GetExports<string, object>("Contract");
\r
335 public void GetExportedValueOfT1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
337 var container = CreateCompositionContainer();
\r
338 container.Dispose();
\r
340 ExceptionAssert.ThrowsDisposed(container, () =>
\r
342 container.GetExportedValue<string>();
\r
348 public void GetExportedValueOfT2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
350 var container = CreateCompositionContainer();
\r
351 container.Dispose();
\r
353 ExceptionAssert.ThrowsDisposed(container, () =>
\r
355 container.GetExportedValue<string>("Contract");
\r
360 public void GetExportedValueOrDefaultOfT1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
362 var container = CreateCompositionContainer();
\r
363 container.Dispose();
\r
365 ExceptionAssert.ThrowsDisposed(container, () =>
\r
367 container.GetExportedValueOrDefault<string>();
\r
372 public void GetExportedValueOrDefaultOfT2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
374 var container = CreateCompositionContainer();
\r
375 container.Dispose();
\r
377 ExceptionAssert.ThrowsDisposed(container, () =>
\r
379 container.GetExportedValueOrDefault<string>("Contract");
\r
384 public void GetExportedValuesOfT1_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
386 var container = CreateCompositionContainer();
\r
387 container.Dispose();
\r
389 ExceptionAssert.ThrowsDisposed(container, () =>
\r
391 container.GetExportedValues<string>();
\r
396 public void GetExportedValuesOfT2_WhenContainerDisposed_ShouldThrowObjectDisposed()
\r
398 var container = CreateCompositionContainer();
\r
399 container.Dispose();
\r
401 ExceptionAssert.ThrowsDisposed(container, () =>
\r
403 container.GetExportedValues<string>("Contract");
\r
408 public void GetExports1_NullAsImportDefinitionArgument_ShouldThrowArgumentNull()
\r
410 var container = CreateCompositionContainer();
\r
412 ExceptionAssert.ThrowsArgument<ArgumentNullException>("definition", () =>
\r
414 container.GetExports((ImportDefinition)null);
\r
419 public void GetExports2_NullAsTypeArgument_ShouldThrowArgumentNull()
\r
421 var container = CreateCompositionContainer();
\r
423 ExceptionAssert.ThrowsArgument<ArgumentNullException>("type", () =>
\r
425 container.GetExports((Type)null, typeof(string), "ContractName");
\r
430 public void GetExportOfT1_AskingForContractThatDoesNotExist_ShouldThrowCardinalityMismatch()
\r
432 var container = CreateCompositionContainer();
\r
434 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
436 container.GetExport<string>();
\r
441 public void GetExportOfT2_AskingForContractThatDoesNotExist_ShouldThrowCardinalityMismatch()
\r
443 var container = CreateCompositionContainer();
\r
445 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
447 container.GetExport<string>("Contract");
\r
452 public void GetExportOfTTMetadataView1_AskingForContractThatDoesNotExist_ShouldThrowCardinalityMismatch()
\r
454 var container = CreateCompositionContainer();
\r
456 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
458 container.GetExport<string, object>();
\r
463 public void GetExportOfTTMetadataView2_AskingForContractThatDoesNotExist_ShouldThrowCardinalityMismatch()
\r
465 var container = CreateCompositionContainer();
\r
467 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
469 container.GetExport<string, object>("Contract");
\r
474 public void GetExports1_DefinitionAskingForExactlyOneContractThatDoesNotExist_ShouldThrowCardinalityMismatch()
\r
476 var container = CreateCompositionContainer();
\r
478 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ExactlyOne);
\r
480 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
482 container.GetExports(definition);
\r
487 public void GetExports1_DefinitionAskingForExactlyZeroOrOneContractThatDoesNotExist_ShouldReturnEmpty()
\r
489 var container = CreateCompositionContainer();
\r
491 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrOne);
\r
493 var exports = container.GetExports(definition);
\r
495 EnumerableAssert.IsEmpty(exports);
\r
499 public void GetExports1_DefinitionAskingForExactlyZeroOrMoreContractThatDoesNotExist_ShouldReturnEmpty()
\r
501 var container = CreateCompositionContainer();
\r
503 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrMore);
\r
505 var exports = container.GetExports(definition);
\r
507 EnumerableAssert.IsEmpty(exports);
\r
511 public void GetExports2_AskingForContractThatDoesNotExist_ShouldReturnNoExports()
\r
513 var container = CreateCompositionContainer();
\r
515 var exports = container.GetExports(typeof(string), (Type)null, "Contract");
\r
517 EnumerableAssert.IsEmpty(exports);
\r
521 public void GetExportsOfT1_AskingForContractThatDoesNotExist_ShouldReturnEmpty()
\r
523 var container = CreateCompositionContainer();
\r
525 var exports = container.GetExports<string>();
\r
527 EnumerableAssert.IsEmpty(exports);
\r
531 public void GetExportsOfT2_AskingForContractThatDoesNotExist_ShouldReturnEmpty()
\r
533 var container = CreateCompositionContainer();
\r
535 var exports = container.GetExports<string>("Contract");
\r
537 EnumerableAssert.IsEmpty(exports);
\r
541 public void GetExportsOfTTMetadataView1_AskingForContractThatDoesNotExist_ShouldReturnEmpty()
\r
543 var container = CreateCompositionContainer();
\r
545 var exports = container.GetExports<string, object>();
\r
547 EnumerableAssert.IsEmpty(exports);
\r
551 public void GetExportsOfTTMetadataView2_AskingForContractThatDoesNotExist_ShouldReturnEmpty()
\r
553 var container = CreateCompositionContainer();
\r
555 var exports = container.GetExports<string, object>("Contract");
\r
557 EnumerableAssert.IsEmpty(exports);
\r
561 public void GetExportedValueOfT1_AskingForContractThatDoesNotExist_ShouldThrowCardinalityMismatch()
\r
563 var container = CreateCompositionContainer();
\r
565 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
567 container.GetExportedValue<string>();
\r
572 public void GetExportedValueOfT2_AskingForContractThatDoesNotExist_ShouldThrowCardinalityMismatch()
\r
574 var container = CreateCompositionContainer();
\r
576 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
578 container.GetExportedValue<string>("Contract");
\r
583 public void GetExportedValueOrDefaultOfT1_AskingForStringContractThatDoesNotExist_ShouldReturnNull()
\r
585 var container = CreateCompositionContainer();
\r
587 var exportedValue = container.GetExportedValueOrDefault<string>();
\r
589 Assert.IsNull(exportedValue);
\r
593 public void GetExportedValueOrDefaultOfT2_AskingForStringContractThatDoesNotExist_ShouldReturnNull()
\r
595 var container = CreateCompositionContainer();
\r
597 var exportedValue = container.GetExportedValueOrDefault<string>("Contract");
\r
599 Assert.IsNull(exportedValue);
\r
603 public void GetExportedValueOrDefaultOfT1_AskingForInt32ContractThatDoesNotExist_ShouldReturnZero()
\r
605 var container = CreateCompositionContainer();
\r
607 var exportedValue = container.GetExportedValueOrDefault<int>();
\r
609 Assert.AreEqual(0, exportedValue);
\r
613 public void GetExportedValueOrDefaultOfT2_AskingForInt32ContractThatDoesNotExist_ShouldReturnZero()
\r
615 var container = CreateCompositionContainer();
\r
617 var exportedValue = container.GetExportedValueOrDefault<int>("Contract");
\r
619 Assert.AreEqual(0, exportedValue);
\r
623 public void GetExportedValuesOfT1_AskingForContractThatDoesNotExist_ShouldReturnEmpty()
\r
625 var container = CreateCompositionContainer();
\r
627 var exports = container.GetExportedValues<string>();
\r
629 EnumerableAssert.IsEmpty(exports);
\r
633 public void GetExportedValuesOfT2_AskingForContractThatDoesNotExist_ShouldReturnEmpty()
\r
635 var container = CreateCompositionContainer();
\r
637 var exports = container.GetExports<string>("Contract");
\r
639 EnumerableAssert.IsEmpty(exports);
\r
643 public void GetExportOfT1_AskingForContractWithOneExport_ShouldReturnExport()
\r
645 var container = ContainerFactory.Create(new MicroExport(ContractFromType(typeof(String)), "Value"));
\r
647 var export = container.GetExport<string>();
\r
649 Assert.AreEqual("Value", export.Value);
\r
653 public void GetExportOfT2_AskingForContractWithOneExport_ShouldReturnExport()
\r
655 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
657 var export = container.GetExport<string>("Contract");
\r
659 Assert.AreEqual("Value", export.Value);
\r
663 public void GetExportOfTTMetadataView1_AskingForContractWithOneExport_ShouldReturnExport()
\r
665 var container = ContainerFactory.Create(new MicroExport(ContractFromType(typeof(String)), "Value"));
\r
667 var export = container.GetExport<string, object>();
\r
669 Assert.AreEqual("Value", export.Value);
\r
673 public void GetExportOfTTMetadataView2_AskingForContractWithOneExport_ShouldReturnExport()
\r
675 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
677 var export = container.GetExport<string, object>("Contract");
\r
679 Assert.AreEqual("Value", export.Value);
\r
683 public void GetExports1_AskingForExactlyOneContractWithOneExport_ShouldReturnExport()
\r
685 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
687 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ExactlyOne);
\r
689 var exports = container.GetExports(definition);
\r
691 ExportsAssert.AreEqual(exports, "Value");
\r
695 public void GetExports1_AskingForZeroOrOneContractWithOneExport_ShouldReturnExport()
\r
697 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
699 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrOne);
\r
701 var exports = container.GetExports(definition);
\r
703 ExportsAssert.AreEqual(exports, "Value");
\r
707 public void GetExports1_AskingForZeroOrMoreContractWithOneExport_ShouldReturnExport()
\r
709 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
711 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrMore);
\r
713 var exports = container.GetExports(definition);
\r
715 ExportsAssert.AreEqual(exports, "Value");
\r
719 public void GetExports2_AskingForContractWithOneExport_ShouldReturnOneExport()
\r
721 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
723 var exports = container.GetExports(typeof(string), (Type)null, "Contract");
\r
725 ExportsAssert.AreEqual(exports, "Value");
\r
729 public void GetExportsOfT1_AskingForContractWithOneExport_ShouldReturnOneExport()
\r
731 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value"));
\r
733 var exports = container.GetExports<string>();
\r
735 ExportsAssert.AreEqual(exports, "Value");
\r
739 public void GetExportsOfT2_AskingForContractWithOneExport_ShouldReturnOneExport()
\r
741 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
743 var exports = container.GetExports<string>("Contract");
\r
745 ExportsAssert.AreEqual(exports, "Value");
\r
749 public void GetExportsOfTTMetadataView1_AskingForContractWithOneExport_ShouldReturnOneExport()
\r
751 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value"));
\r
753 var exports = container.GetExports<string, object>();
\r
755 ExportsAssert.AreEqual(exports, "Value");
\r
759 public void GetExportsOfTTMetadataView2_AskingForContractWithOneExport_ShouldReturnOneExport()
\r
761 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
763 var exports = container.GetExports<string, object>("Contract");
\r
765 ExportsAssert.AreEqual(exports, "Value");
\r
769 public void GetExportedValueOfT1_AskingForContractWithOneExport_ShouldReturnExport()
\r
771 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value"));
\r
773 var exportedValue = container.GetExportedValue<string>();
\r
775 Assert.AreEqual("Value", exportedValue);
\r
779 public void GetExportedValueOfT2_AskingForContractWithOneExport_ShouldReturnExport()
\r
781 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
783 var exportedValue = container.GetExportedValue<string>("Contract");
\r
785 Assert.AreEqual("Value", exportedValue);
\r
789 public void GetExportedValueOrDefaultOfT1_AskingForContractWithOneExport_ShouldReturnExport()
\r
791 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value"));
\r
793 var exportedValue = container.GetExportedValueOrDefault<string>();
\r
795 Assert.AreEqual("Value", exportedValue);
\r
799 public void GetExportedValueOrDefaultOfT2_AskingForContractWithOneExport_ShouldReturnExport()
\r
801 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
803 var exportedValue = container.GetExportedValueOrDefault<string>("Contract");
\r
805 Assert.AreEqual("Value", exportedValue);
\r
809 public void GetExportedValuesOfT1_AskingForContractWithOneExport_ShouldReturnOneExport()
\r
811 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value"));
\r
813 var exportedValues = container.GetExportedValues<string>();
\r
815 EnumerableAssert.AreEqual(exportedValues, "Value");
\r
819 public void GetExportedValuesOfT2_AskingForContractWithOneExport_ShouldReturnOneExport()
\r
821 var container = ContainerFactory.Create(new MicroExport("Contract", "Value"));
\r
823 var exportedValues = container.GetExportedValues<string>("Contract");
\r
825 EnumerableAssert.AreEqual(exportedValues, "Value");
\r
829 public void GetExportOfT1_AskingForContractWithMultipleExports_ShouldThrowCardinalityMismatch()
\r
831 var container = ContainerFactory.Create(new MicroExport(ContractFromType(typeof(String)), "Value1", "Value2"));
\r
833 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
835 container.GetExport<string>();
\r
840 public void GetExportOfT2_AskingForContractWithMultipleExports_ShouldThrowCardinalityMismatch()
\r
842 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
844 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
846 container.GetExport<string>("Contract");
\r
851 public void GetExportOfTTMetadataView1_AskingForContractWithMultipleExports_ShouldThrowCardinalityMismatch()
\r
853 var container = ContainerFactory.Create(new MicroExport(ContractFromType(typeof(String)), "Value1", "Value2"));
\r
855 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
857 container.GetExport<string, object>();
\r
862 public void GetExportOfTTMetadataView2_AskingForContractWithMultipleExports_ShouldThrowCardinalityMismatch()
\r
864 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
866 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
868 container.GetExport<string, object>("Contract");
\r
873 public void GetExports1_AskingForExactlyOneContractWithMultipleExports_ShouldThrowCardinalityMismatch()
\r
875 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
877 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ExactlyOne);
\r
879 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
881 container.GetExports(definition);
\r
886 public void GetExports1_AskingForZeroOrOneContractWithMultipleExports_ShouldReturnZero()
\r
888 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
890 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrOne);
\r
892 Assert.AreEqual(0, container.GetExports(definition).Count());
\r
896 public void GetExports1_AskingForZeroOrMoreContractWithMultipleExports_ShouldReturnMultipleExports()
\r
898 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
900 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrMore);
\r
902 var exports = container.GetExports(definition);
\r
904 ExportsAssert.AreEqual(exports, "Value1", "Value2");
\r
908 public void GetExports2_AskingForContractWithMultipleExports_ShouldReturnMultipleExports()
\r
910 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
912 var exports = container.GetExports(typeof(string), (Type)null, "Contract");
\r
914 ExportsAssert.AreEqual(exports, "Value1", "Value2");
\r
918 public void GetExportsOfT1_AskingForContractWithMultipleExports_ShouldReturnMultipleExports()
\r
920 var container = ContainerFactory.Create(new MicroExport(typeof(String), "Value1", "Value2"));
\r
922 var exports = container.GetExports<string>();
\r
924 ExportsAssert.AreEqual(exports, "Value1", "Value2");
\r
928 public void GetExportsOfT2_AskingForContractWithMultipleExports_ShouldReturnMultipleExports()
\r
930 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
932 var exports = container.GetExports<string>("Contract");
\r
934 ExportsAssert.AreEqual(exports, "Value1", "Value2");
\r
938 public void GetExportsOfTTMetadataView1_AskingForContractWithMultipleExports_ShouldReturnMultipleExports()
\r
940 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value1", "Value2"));
\r
942 var exports = container.GetExports<string, object>();
\r
944 ExportsAssert.AreEqual(exports, "Value1", "Value2");
\r
948 public void GetExportsOfTTMetadataView2_AskingForContractWithMultipleExports_ShouldReturnMultipleExports()
\r
950 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
952 var exports = container.GetExports<string, object>("Contract");
\r
954 ExportsAssert.AreEqual(exports, "Value1", "Value2");
\r
958 public void GetExportedValueOfT1_AskingForContractWithMultipleExports_ShouldThrowCardinalityMismatch()
\r
960 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value1", "Value2"));
\r
962 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
964 container.GetExportedValue<string>();
\r
969 public void GetExportedValueOfT2_AskingForContractWithMultipleExports_ShouldThrowCardinalityMismatch()
\r
971 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
973 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
975 container.GetExportedValue<string>("Contract");
\r
980 public void GetExportedValueOrDefaultOfT1_AskingForContractWithMultipleExports_ShouldReturnZero()
\r
982 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value1", "Value2"));
\r
984 Assert.IsNull(container.GetExportedValueOrDefault<string>());
\r
988 public void GetExportedValueOrDefaultOfT2_AskingForContractWithMultipleExports_ShouldReturnZero()
\r
990 var container = ContainerFactory.Create(new MicroExport("Contract", "Value1", "Value2"));
\r
992 Assert.IsNull(container.GetExportedValueOrDefault<string>("Contract"));
\r
996 public void GetExportedValuesOfT1_AskingForContractWithMultipleExports_ShouldReturnMultipleExports()
\r
998 var container = ContainerFactory.Create(new MicroExport(typeof(string), "Value1", "Value2"));
\r
1000 var exportedValues = container.GetExportedValues<string>();
\r
1002 EnumerableAssert.AreEqual(exportedValues, "Value1", "Value2");
\r
1006 public void GetExportedValuesOfT2_AskingForContractWithMultipleExports_ShouldReturnMultipleExports()
\r
1008 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), "Value1", "Value2"));
\r
1010 var exportedValues = container.GetExportedValues<string>("Contract");
\r
1012 EnumerableAssert.AreEqual(exportedValues, "Value1", "Value2");
\r
1016 public void GetExports1_AskingForExactlyOneAndAll_ShouldThrowCardinalityMismatch()
\r
1018 var container = ContainerFactory.Create(new MicroExport("Contract1", "Value1", "Value2", "Value3"),
\r
1019 new MicroExport("Contract2", "Value4", "Value5", "Value6"));
\r
1021 var definition = ImportDefinitionFactory.Create(import => true, ImportCardinality.ExactlyOne);
\r
1023 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
1025 container.GetExports(definition);
\r
1030 public void GetExports1_AskingForZeroOrOneAndAll_ShouldReturnZero()
\r
1032 var container = ContainerFactory.Create(new MicroExport("Contract1", "Value1", "Value2", "Value3"),
\r
1033 new MicroExport("Contract2", "Value4", "Value5", "Value6"));
\r
1035 var definition = ImportDefinitionFactory.Create(import => true, ImportCardinality.ZeroOrOne);
\r
1037 Assert.AreEqual(0, container.GetExports(definition).Count());
\r
1041 public void GetExports1_AskingForZeroOrMoreAndAll_ShouldReturnAll()
\r
1043 var container = ContainerFactory.Create(new MicroExport("Contract1", "Value1", "Value2", "Value3"),
\r
1044 new MicroExport("Contract2", "Value4", "Value5", "Value6"));
\r
1046 var definition = ImportDefinitionFactory.Create(import => true, ImportCardinality.ZeroOrMore);
\r
1048 var exports = container.GetExports(definition);
\r
1050 ExportsAssert.AreEqual(exports, "Value1", "Value2", "Value3",
\r
1051 "Value4", "Value5", "Value6");
\r
1055 public void GetExportOfT1_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1057 var container = ContainerFactory.Create(new MicroExport(typeof(string), new object()));
\r
1059 var export = container.GetExport<string>();
\r
1061 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1063 var value = export.Value;
\r
1068 public void GetExportOfT2_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1070 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1072 var export = container.GetExport<string>("Contract");
\r
1074 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1076 var value = export.Value;
\r
1081 public void GetExportOfTTMetadataView1_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1083 var container = ContainerFactory.Create(new MicroExport(typeof(string), new object()));
\r
1085 var export = container.GetExport<string, object>();
\r
1087 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1089 var value = export.Value;
\r
1094 public void GetExportOfTTMetadataView2_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1096 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1098 var export = container.GetExport<string, object>("Contract");
\r
1100 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1102 var value = export.Value;
\r
1107 public void GetExports2_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1109 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1111 var exports = container.GetExports(typeof(string), (Type)null, "Contract");
\r
1113 Assert.AreEqual(1, exports.Count());
\r
1115 var export = exports.ElementAt(0);
\r
1117 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1119 var value = export.Value;
\r
1124 public void GetExportsOfT1_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1126 var container = ContainerFactory.Create(new MicroExport(typeof(string), new object()));
\r
1128 var exports = container.GetExports<string>();
\r
1130 Assert.AreEqual(1, exports.Count());
\r
1132 var export = exports.ElementAt(0);
\r
1134 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1136 var value = export.Value;
\r
1141 public void GetExportsOfT2_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1143 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1145 var exports = container.GetExports<string>("Contract");
\r
1147 Assert.AreEqual(1, exports.Count());
\r
1149 var export = exports.ElementAt(0);
\r
1151 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1153 var value = export.Value;
\r
1158 public void GetExportsOfTTMetadataView1_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1160 var container = ContainerFactory.Create(new MicroExport(typeof(string), new object()));
\r
1162 var exports = container.GetExports<string, object>();
\r
1164 Assert.AreEqual(1, exports.Count());
\r
1166 var export = exports.ElementAt(0);
\r
1168 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1170 var value = export.Value;
\r
1175 public void GetExportsOfTTMetadataView2_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1177 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1179 var exports = container.GetExports<string, object>("Contract");
\r
1181 Assert.AreEqual(1, exports.Count());
\r
1183 var export = exports.ElementAt(0);
\r
1185 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1187 var value = export.Value;
\r
1192 public void GetExportedValueOfT1_StringAsTTypeArgumentAskingForContractWithObjectExport_ShouldThrowContractMismatch()
\r
1194 var container = ContainerFactory.Create(new MicroExport(typeof(string), new object()));
\r
1196 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1198 container.GetExportedValue<string>();
\r
1203 public void GetExportedValueOfT2_StringAsTTypeArgumentAskingForContractWithObjectExport_ShouldThrowContractMismatch()
\r
1205 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1207 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1209 container.GetExportedValue<string>("Contract");
\r
1214 public void GetExportedValueOrDefaultOfT1_StringAsTTypeArgumentAskingForContractWithObjectExport_ShouldThrowContractMismatch()
\r
1216 var container = ContainerFactory.Create(new MicroExport(typeof(string), new object()));
\r
1218 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1220 container.GetExportedValueOrDefault<string>();
\r
1225 public void GetExportedValueOrDefaultOfT2_StringAsTTypeArgumentAskingForContractWithObjectExport_ShouldThrowContractMismatch()
\r
1227 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1229 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1231 container.GetExportedValueOrDefault<string>("Contract");
\r
1236 public void GetExportedValuesOfT1_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1238 var container = ContainerFactory.Create(new MicroExport(typeof(string), new object()));
\r
1240 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1242 container.GetExportedValues<string>();
\r
1247 public void GetExportedValuesOfT2_StringAsTTypeArgumentAskingForContractWithOneObjectExport_ShouldThrowContractMismatch()
\r
1249 var container = ContainerFactory.Create(new MicroExport("Contract", typeof(string), new object()));
\r
1251 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
1253 container.GetExportedValues<string>("Contract");
\r
1259 public void GetExportOfT1_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1261 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1262 var child = ContainerFactory.Create(parent);
\r
1264 var export = child.GetExport<string>();
\r
1266 Assert.AreEqual("Parent", export.Value);
\r
1270 public void GetExportOfT2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1272 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1273 var child = ContainerFactory.Create(parent);
\r
1275 var export = child.GetExport<string>("Contract");
\r
1277 Assert.AreEqual("Parent", export.Value);
\r
1281 public void GetExportOfTTMetadataView1_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1283 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1284 var child = ContainerFactory.Create(parent);
\r
1286 var export = child.GetExport<string, object>();
\r
1288 Assert.AreEqual("Parent", export.Value);
\r
1292 public void GetExportOfTTMetadataView2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1294 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1295 var child = ContainerFactory.Create(parent);
\r
1297 var export = child.GetExport<string, object>("Contract");
\r
1299 Assert.AreEqual("Parent", export.Value);
\r
1303 public void GetExports1_AskingForExactlyOneContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1305 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1306 var child = ContainerFactory.Create(parent);
\r
1308 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ExactlyOne);
\r
1310 var exports = child.GetExports(definition);
\r
1312 ExportsAssert.AreEqual(exports, "Parent");
\r
1316 public void GetExports1_AskingForZeroOrOneContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1318 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1319 var child = ContainerFactory.Create(parent);
\r
1321 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrOne);
\r
1323 var exports = child.GetExports(definition);
\r
1325 ExportsAssert.AreEqual(exports, "Parent");
\r
1329 public void GetExports1_AskingForZeroOrMoreContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1331 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1332 var child = ContainerFactory.Create(parent);
\r
1334 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrMore);
\r
1336 var exports = child.GetExports(definition);
\r
1338 ExportsAssert.AreEqual(exports, "Parent");
\r
1342 public void GetExports2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1344 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1345 var child = ContainerFactory.Create(parent);
\r
1347 var exports = child.GetExports(typeof(string), (Type)null, "Contract");
\r
1349 ExportsAssert.AreEqual(exports, "Parent");
\r
1353 public void GetExportsOfT1_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1355 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1356 var child = ContainerFactory.Create(parent);
\r
1358 var exports = child.GetExports<string>();
\r
1360 ExportsAssert.AreEqual(exports, "Parent");
\r
1364 public void GetExportsOfT2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1366 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1367 var child = ContainerFactory.Create(parent);
\r
1369 var exports = child.GetExports<string>("Contract");
\r
1371 ExportsAssert.AreEqual(exports, "Parent");
\r
1375 public void GetExportsOfTTMetadataView1_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1377 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1378 var child = ContainerFactory.Create(parent);
\r
1380 var exports = child.GetExports<string, object>();
\r
1382 ExportsAssert.AreEqual(exports, "Parent");
\r
1386 public void GetExportsOfTTMetadataView2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1388 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1389 var child = ContainerFactory.Create(parent);
\r
1391 var exports = child.GetExports<string, object>("Contract");
\r
1393 ExportsAssert.AreEqual(exports, "Parent");
\r
1397 public void GetExportedValueOfT1_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1399 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1400 var child = ContainerFactory.Create(parent);
\r
1402 var exportedValue = child.GetExportedValue<string>();
\r
1404 Assert.AreEqual("Parent", exportedValue);
\r
1408 public void GetExportedValueOfT2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1410 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1411 var child = ContainerFactory.Create(parent);
\r
1413 var exportedValue = child.GetExportedValue<string>("Contract");
\r
1415 Assert.AreEqual("Parent", exportedValue);
\r
1419 public void GetExportedValueOrDefaultOfT1_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1421 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1422 var child = ContainerFactory.Create(parent);
\r
1424 var exportedValue = child.GetExportedValueOrDefault<string>();
\r
1426 Assert.AreEqual("Parent", exportedValue);
\r
1430 public void GetExportedValueOrDefaultOfT2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1432 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1433 var child = ContainerFactory.Create(parent);
\r
1435 var exportedValue = child.GetExportedValueOrDefault<string>("Contract");
\r
1437 Assert.AreEqual("Parent", exportedValue);
\r
1441 public void GetExportedValuesOfT1_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1443 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1444 var child = ContainerFactory.Create(parent);
\r
1446 var exportedValues = child.GetExportedValues<string>();
\r
1448 EnumerableAssert.AreEqual(exportedValues, "Parent");
\r
1452 public void GetExportedValuesOfT2_AskingForContractFromChildWithExportInParentContainer_ShouldReturnExport()
\r
1454 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1455 var child = ContainerFactory.Create(parent);
\r
1457 var exportedValues = child.GetExportedValues<string>("Contract");
\r
1459 EnumerableAssert.AreEqual(exportedValues, "Parent");
\r
1463 public void GetExportOfT1_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1465 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1466 var child = ContainerFactory.Create(parent, new MicroExport(typeof(string), "Child"));
\r
1468 var export = child.GetExport<string>();
\r
1470 Assert.AreEqual("Child", export.Value);
\r
1474 public void GetExportOfT2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1476 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1477 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1479 var export = child.GetExport<string>("Contract");
\r
1481 Assert.AreEqual("Child", export.Value);
\r
1485 public void GetExportOfTTMetadataView1_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1487 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1488 var child = ContainerFactory.Create(parent, new MicroExport(typeof(string), "Child"));
\r
1490 var export = child.GetExport<string, object>();
\r
1492 Assert.AreEqual("Child", export.Value);
\r
1496 public void GetExportOfTTMetadataView2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1498 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1499 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1501 var export = child.GetExport<string, object>("Contract");
\r
1503 Assert.AreEqual("Child", export.Value);
\r
1507 public void GetExports1_AskingForExactlyOneContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1509 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1510 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1512 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ExactlyOne);
\r
1514 var exports = child.GetExports(definition);
\r
1516 ExportsAssert.AreEqual(exports, "Child");
\r
1520 public void GetExports1_AskingForZeroOrOneContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1522 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1523 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1525 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrOne);
\r
1527 var exports = child.GetExports(definition);
\r
1529 ExportsAssert.AreEqual(exports, "Child");
\r
1533 public void GetExports1_AskingForZeroOrMoreContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1535 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1536 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1538 var definition = ImportDefinitionFactory.Create("Contract", ImportCardinality.ZeroOrMore);
\r
1540 var exports = child.GetExports(definition);
\r
1542 ExportsAssert.AreEqual(exports, "Child", "Parent");
\r
1546 public void GetExports2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1548 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1549 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1551 var exports = child.GetExports(typeof(string), (Type)null, "Contract");
\r
1553 ExportsAssert.AreEqual(exports, "Child", "Parent");
\r
1557 public void GetExportsOfT1_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1559 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1560 var child = ContainerFactory.Create(parent, new MicroExport(typeof(string), "Child"));
\r
1562 var exports = child.GetExports<string>();
\r
1564 ExportsAssert.AreEqual(exports, "Child", "Parent");
\r
1568 public void GetExportsOfT2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1570 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1571 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1573 var exports = child.GetExports<string>("Contract");
\r
1575 ExportsAssert.AreEqual(exports, "Child", "Parent");
\r
1579 public void GetExportsOfTTMetadataView1_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1581 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1582 var child = ContainerFactory.Create(parent, new MicroExport(typeof(string), "Child"));
\r
1585 var exports = child.GetExports<string, object>();
\r
1587 ExportsAssert.AreEqual(exports, "Child", "Parent");
\r
1591 public void GetExportsOfTTMetadataView2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1593 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1594 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1596 var exports = child.GetExports<string, object>("Contract");
\r
1598 ExportsAssert.AreEqual(exports, "Child", "Parent");
\r
1602 public void GetExportedValueOfT1_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1604 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1605 var child = ContainerFactory.Create(parent, new MicroExport(typeof(string), "Child"));
\r
1607 var exportedValue = child.GetExportedValue<string>();
\r
1609 Assert.AreEqual("Child", exportedValue);
\r
1613 public void GetExportedValueOfT2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1615 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1616 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1618 var exportedValue = child.GetExportedValue<string>("Contract");
\r
1620 Assert.AreEqual("Child", exportedValue);
\r
1624 public void GetExportedValueOrDefaultOfT1_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1626 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1627 var child = ContainerFactory.Create(parent, new MicroExport(typeof(string), "Child"));
\r
1629 var exportedValue = child.GetExportedValueOrDefault<string>();
\r
1631 Assert.AreEqual("Child", exportedValue);
\r
1635 public void GetExportedValueOrDefaultOfT2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnChildExport()
\r
1637 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1638 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1640 var exportedValue = child.GetExportedValueOrDefault<string>("Contract");
\r
1642 Assert.AreEqual("Child", exportedValue);
\r
1646 public void GetExportedValuesOfT1_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1648 var parent = ContainerFactory.Create(new MicroExport(typeof(string), "Parent"));
\r
1649 var child = ContainerFactory.Create(parent, new MicroExport(typeof(string), "Child"));
\r
1651 var exportedValues = child.GetExportedValues<string>();
\r
1653 EnumerableAssert.AreEqual(exportedValues, "Child", "Parent");
\r
1657 public void GetExportedValuesOfT2_AskingForContractWithExportInBothParentAndChildContainers_ShouldReturnBothExports()
\r
1659 var parent = ContainerFactory.Create(new MicroExport("Contract", "Parent"));
\r
1660 var child = ContainerFactory.Create(parent, new MicroExport("Contract", "Child"));
\r
1662 var exportedValues = child.GetExportedValues<string>("Contract");
\r
1664 EnumerableAssert.AreEqual(exportedValues, "Child", "Parent");
\r
1668 public void GetExportOfTTMetadataView1_TypeAsMetadataViewTypeArgument_IsUsedAsMetadataConstraint()
\r
1670 var metadata = new Dictionary<string, object>();
\r
1671 metadata.Add("Metadata1", "MetadataValue1");
\r
1672 metadata.Add("Metadata2", "MetadataValue2");
\r
1673 metadata.Add("Metadata3", "MetadataValue3");
\r
1675 var container = ContainerFactory.Create(new MicroExport("Another", metadata, "Value1"),
\r
1676 new MicroExport(typeof(string), metadata, "Value1"),
\r
1677 new MicroExport(typeof(string), "Value2"));
\r
1679 var export = container.GetExport<string, IMetadataView>();
\r
1680 var metadataExport = (Lazy<string, IMetadataView>)export;
\r
1682 Assert.AreEqual("Value1", metadataExport.Value);
\r
1683 Assert.AreEqual("MetadataValue1", metadataExport.Metadata.Metadata1);
\r
1684 Assert.AreEqual("MetadataValue2", metadataExport.Metadata.Metadata2);
\r
1685 Assert.AreEqual("MetadataValue3", metadataExport.Metadata.Metadata3);
\r
1689 public void GetExportOfTTMetadataView2_TypeAsMetadataViewTypeArgument_IsUsedAsMetadataConstraint()
\r
1691 var metadata = new Dictionary<string, object>();
\r
1692 metadata.Add("Metadata1", "MetadataValue1");
\r
1693 metadata.Add("Metadata2", "MetadataValue2");
\r
1694 metadata.Add("Metadata3", "MetadataValue3");
\r
1696 var container = ContainerFactory.Create(new MicroExport("Another", metadata, "Value1"),
\r
1697 new MicroExport("Contract", metadata, "Value1"),
\r
1698 new MicroExport("Contract", "Value2"));
\r
1700 var export = container.GetExport<string, IMetadataView>("Contract");
\r
1701 var metadataExport = (Lazy<string, IMetadataView>)export;
\r
1703 Assert.AreEqual("Value1", metadataExport.Value);
\r
1704 Assert.AreEqual("MetadataValue1", metadataExport.Metadata.Metadata1);
\r
1705 Assert.AreEqual("MetadataValue2", metadataExport.Metadata.Metadata2);
\r
1706 Assert.AreEqual("MetadataValue3", metadataExport.Metadata.Metadata3);
\r
1710 public void GetExports1_TypeAsMetadataViewTypeArgument_IsUsedAsMetadataConstraint()
\r
1712 var metadata = new Dictionary<string, object>();
\r
1713 metadata.Add("Metadata1", "MetadataValue1");
\r
1714 metadata.Add("Metadata2", "MetadataValue2");
\r
1715 metadata.Add("Metadata3", "MetadataValue3");
\r
1717 var container = ContainerFactory.Create(new MicroExport("Another", metadata, "Value1"),
\r
1718 new MicroExport("Contract", metadata, "Value1"),
\r
1719 new MicroExport("Contract", "Value2"));
\r
1721 var definition = ImportDefinitionFactory.Create(
\r
1723 new Dictionary<string, Type> { { "Metadata1", typeof(object) }, { "Metadata2", typeof(object) }, { "Metadata3", typeof(object) } }
\r
1726 var exports = container.GetExports(definition);
\r
1728 Assert.AreEqual(1, exports.Count());
\r
1730 var export = exports.First();
\r
1732 Assert.AreEqual("Value1", export.Value);
\r
1733 EnumerableAssert.AreEqual(metadata, export.Metadata);
\r
1737 public void GetExports2_TypeAsMetadataViewTypeArgument_IsUsedAsMetadataConstraint()
\r
1739 var metadata = new Dictionary<string, object>();
\r
1740 metadata.Add("Metadata1", "MetadataValue1");
\r
1741 metadata.Add("Metadata2", "MetadataValue2");
\r
1742 metadata.Add("Metadata3", "MetadataValue3");
\r
1744 var container = ContainerFactory.Create(new MicroExport("Another", metadata, "Value1"),
\r
1745 new MicroExport("Contract", metadata, "Value1"),
\r
1746 new MicroExport("Contract", "Value2"));
\r
1748 var exports = container.GetExports(typeof(string), typeof(IMetadataView), "Contract");
\r
1750 Assert.AreEqual(1, exports.Count());
\r
1752 var export = exports.First();
\r
1753 IMetadataView exportMetadata = export.Metadata as IMetadataView;
\r
1755 Assert.AreEqual("Value1", export.Value);
\r
1756 Assert.IsNotNull(exportMetadata);
\r
1758 Assert.AreEqual("MetadataValue1", exportMetadata.Metadata1);
\r
1759 Assert.AreEqual("MetadataValue2", exportMetadata.Metadata2);
\r
1760 Assert.AreEqual("MetadataValue3", exportMetadata.Metadata3);
\r
1764 public void GetExportsOfTTMetadataView1_TypeAsMetadataViewTypeArgument_IsUsedAsMetadataConstraint()
\r
1766 var metadata = new Dictionary<string, object>();
\r
1767 metadata.Add("Metadata1", "MetadataValue1");
\r
1768 metadata.Add("Metadata2", "MetadataValue2");
\r
1769 metadata.Add("Metadata3", "MetadataValue3");
\r
1771 var container = ContainerFactory.Create(new MicroExport("Another", metadata, "Value1"),
\r
1772 new MicroExport(typeof(string), metadata, "Value1"),
\r
1773 new MicroExport(typeof(string), "Value2"));
\r
1775 var exports = container.GetExports<string, IMetadataView>();
\r
1777 Assert.AreEqual(1, exports.Count());
\r
1779 var export = (Lazy<string, IMetadataView>)exports.First();
\r
1781 Assert.AreEqual("Value1", export.Value);
\r
1782 Assert.AreEqual("MetadataValue1", export.Metadata.Metadata1);
\r
1783 Assert.AreEqual("MetadataValue2", export.Metadata.Metadata2);
\r
1784 Assert.AreEqual("MetadataValue3", export.Metadata.Metadata3);
\r
1788 public void GetExportsOfTTMetadataView2_TypeAsMetadataViewTypeArgument_IsUsedAsMetadataConstraint()
\r
1790 var metadata = new Dictionary<string, object>();
\r
1791 metadata.Add("Metadata1", "MetadataValue1");
\r
1792 metadata.Add("Metadata2", "MetadataValue2");
\r
1793 metadata.Add("Metadata3", "MetadataValue3");
\r
1795 var container = ContainerFactory.Create(new MicroExport("Another", metadata, "Value1"),
\r
1796 new MicroExport("Contract", metadata, "Value1"),
\r
1797 new MicroExport("Contract", "Value2"));
\r
1799 var exports = container.GetExports<string, IMetadataView>("Contract");
\r
1801 Assert.AreEqual(1, exports.Count());
\r
1803 var export = (Lazy<string, IMetadataView>)exports.First();
\r
1805 Assert.AreEqual("Value1", export.Value);
\r
1806 Assert.AreEqual("MetadataValue1", export.Metadata.Metadata1);
\r
1807 Assert.AreEqual("MetadataValue2", export.Metadata.Metadata2);
\r
1808 Assert.AreEqual("MetadataValue3", export.Metadata.Metadata3);
\r
1813 public void GetExports1_AskingForExactlyOneAndAllWhenContainerEmpty_ShouldThrowCardinalityMismatch()
\r
1815 var container = CreateCompositionContainer();
\r
1817 var definition = ImportDefinitionFactory.Create(export => true, ImportCardinality.ExactlyOne);
\r
1819 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
1821 container.GetExports(definition);
\r
1826 public void GetExports1_AskingForZeroOrOneAndAllWhenContainerEmpty_ShouldReturnEmpty()
\r
1828 var container = CreateCompositionContainer();
\r
1830 var definition = ImportDefinitionFactory.Create(export => true, ImportCardinality.ZeroOrOne);
\r
1832 var exports = container.GetExports(definition);
\r
1834 EnumerableAssert.IsEmpty(exports);
\r
1838 public void GetExports1_AskingForExactlyOneAndAllWhenContainerEmpty_ShouldReturnEmpty()
\r
1840 var container = CreateCompositionContainer();
\r
1842 var definition = ImportDefinitionFactory.Create(export => true, ImportCardinality.ZeroOrMore);
\r
1844 var exports = container.GetExports(definition);
\r
1846 EnumerableAssert.IsEmpty(exports);
\r
1851 [WorkItem(465976)]
\r
1852 public void RemovePart_PartNotInContainerAsPartArgument_ShouldNotCauseImportsToBeRebound()
\r
1854 const string contractName = "Contract";
\r
1856 var exporter = PartFactory.CreateExporter(new MicroExport(contractName, 1));
\r
1857 var importer = PartFactory.CreateImporter(contractName);
\r
1858 var container = ContainerFactory.Create(exporter, importer);
\r
1861 Assert.AreEqual(1, importer.Value);
\r
1862 Assert.AreEqual(1, importer.ImportSatisfiedCount);
\r
1864 var doesNotExistInContainer = PartFactory.CreateExporter(new MicroExport(contractName, 2));
\r
1866 CompositionBatch batch = new CompositionBatch();
\r
1867 batch.RemovePart(doesNotExistInContainer);
\r
1868 container.Compose(batch);
\r
1870 Assert.AreEqual(1, importer.ImportSatisfiedCount);
\r
1875 [WorkItem(436847)]
\r
1876 public void RemovePart_PartInContainerQueueAsPartArgument_ShouldNotLeavePartInContainer()
\r
1878 const string contractName = "Contract";
\r
1880 var exporter = PartFactory.CreateExporter(new MicroExport(contractName, 1));
\r
1881 var importer = PartFactory.CreateImporter(contractName);
\r
1882 var container = ContainerFactory.Create(exporter, importer);
\r
1884 CompositionBatch batch = new CompositionBatch();
\r
1885 batch.RemovePart(exporter);
\r
1886 container.Compose(batch);
\r
1888 Assert.IsNull(importer.Value);
\r
1889 Assert.AreEqual(2, importer.ImportSatisfiedCount);
\r
1893 public void RemovePart_PartAlreadyRemovedAsPartArgument_ShouldNotThrow()
\r
1895 var exporter = PartFactory.CreateExporter(new MicroExport("Contract", 1));
\r
1896 var container = ContainerFactory.Create(exporter);
\r
1898 Assert.AreEqual(1, container.GetExportedValue<int>("Contract"));
\r
1900 CompositionBatch batch = new CompositionBatch();
\r
1901 batch.RemovePart(exporter);
\r
1902 container.Compose(batch);
\r
1904 Assert.IsFalse(container.IsPresent("Contract"));
\r
1906 batch = new CompositionBatch();
\r
1907 batch.RemovePart(exporter);
\r
1908 container.Compose(batch);
\r
1910 Assert.IsFalse(container.IsPresent("Contract"));
\r
1914 public void TryComposeSimple()
\r
1916 var container = CreateCompositionContainer();
\r
1917 Int32Importer importer = new Int32Importer();
\r
1919 CompositionBatch batch = new CompositionBatch();
\r
1920 batch.AddParts(importer, new Int32Exporter(42));
\r
1921 container.Compose(batch);
\r
1923 Assert.AreEqual(42, importer.Value, "Expected value imported from export");
\r
1927 public void TryComposeSimpleFail()
\r
1929 var container = CreateCompositionContainer();
\r
1930 Int32Importer importer = new Int32Importer();
\r
1932 CompositionBatch batch = new CompositionBatch();
\r
1933 batch.AddParts(importer);
\r
1935 CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport, ErrorId.ImportEngine_ImportCardinalityMismatch, RetryMode.DoNotRetry, () =>
\r
1937 container.Compose(batch);
\r
1940 Assert.AreEqual(0, importer.Value, "Expected default value to remain");
\r
1944 public void ComposeDisposableChildContainer()
\r
1946 var outerContainer = CreateCompositionContainer();
\r
1947 Int32Importer outerImporter = new Int32Importer();
\r
1949 CompositionBatch outerBatch = new CompositionBatch();
\r
1950 var key = outerBatch.AddExportedValue("Value", 42);
\r
1951 outerBatch.AddPart(outerImporter);
\r
1952 outerContainer.Compose(outerBatch);
\r
1953 Assert.AreEqual(42, outerImporter.Value, "Expected value imported from export");
\r
1955 Int32Importer innerImporter = new Int32Importer();
\r
1956 var innerContainer = new CompositionContainer(outerContainer);
\r
1957 CompositionBatch innerBatch = new CompositionBatch();
\r
1958 innerBatch.AddPart(innerImporter);
\r
1960 innerContainer.Compose(innerBatch);
\r
1961 Assert.AreEqual(42, innerImporter.Value, "Expected value imported from export");
\r
1962 Assert.AreEqual(42, outerImporter.Value, "Expected value imported from export");
\r
1964 outerBatch = new CompositionBatch();
\r
1965 outerBatch.RemovePart(key);
\r
1966 key = outerBatch.AddExportedValue("Value", -5);
\r
1967 outerContainer.Compose(outerBatch);
\r
1968 Assert.AreEqual(-5, innerImporter.Value, "Expected update value imported from export");
\r
1969 Assert.AreEqual(-5, outerImporter.Value, "Expected updated value imported from export");
\r
1971 innerContainer.Dispose();
\r
1972 outerBatch = new CompositionBatch();
\r
1973 outerBatch.RemovePart(key);
\r
1974 key = outerBatch.AddExportedValue("Value", 500);
\r
1975 outerContainer.Compose(outerBatch);
\r
1976 Assert.AreEqual(500, outerImporter.Value, "Expected updated value imported from export");
\r
1977 Assert.AreEqual(-5, innerImporter.Value, "Expected value not updated");
\r
1981 public void RemoveValueTest()
\r
1983 var container = CreateCompositionContainer();
\r
1984 CompositionBatch batch = new CompositionBatch();
\r
1986 var key = batch.AddExportedValue("foo", "hello");
\r
1987 container.Compose(batch);
\r
1988 var result = container.GetExportedValue<string>("foo");
\r
1989 Assert.AreEqual("hello", result, "Should get the correct value");
\r
1991 batch = new CompositionBatch();
\r
1992 batch.RemovePart(key);
\r
1993 container.Compose(batch);
\r
1995 Assert.IsFalse(container.IsPresent("foo"));
\r
1997 batch = new CompositionBatch();
\r
1998 batch.RemovePart(key); // Remove should be idempotent
\r
1999 container.Compose(batch);
\r
2003 [TestProperty("Type", "Integration")]
\r
2004 public void OptionalImportsOfValueTypeBoundToDefaultValueShouldNotAffectAvailableValues()
\r
2006 var container = CreateCompositionContainer();
\r
2007 var importer = new OptionalImporter();
\r
2009 CompositionBatch batch = new CompositionBatch();
\r
2010 batch.AddPart(importer);
\r
2011 container.Compose(batch);
\r
2013 Assert.AreEqual(0, importer.ValueType);
\r
2015 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
2017 container.GetExportedValue<int>("ValueType");
\r
2022 [TestProperty("Type", "Integration")]
\r
2023 public void OptionalImportsOfNullableValueTypeBoundToDefaultValueShouldNotAffectAvailableValues()
\r
2025 var container = CreateCompositionContainer();
\r
2026 var importer = new OptionalImporter();
\r
2028 CompositionBatch batch = new CompositionBatch();
\r
2029 batch.AddPart(importer);
\r
2030 container.Compose(batch);
\r
2032 Assert.IsNull(importer.NullableValueType);
\r
2034 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
2036 container.GetExportedValue<int>("NullableValueType");
\r
2041 [TestProperty("Type", "Integration")]
\r
2042 public void OptionalImportsOfReferenceTypeBoundToDefaultValueShouldNotAffectAvailableValues()
\r
2044 var container = CreateCompositionContainer();
\r
2045 var importer = new OptionalImporter();
\r
2047 CompositionBatch batch = new CompositionBatch();
\r
2048 batch.AddPart(importer);
\r
2049 container.Compose(batch);
\r
2051 Assert.IsNull(importer.ReferenceType);
\r
2053 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
2055 container.GetExportedValue<int>("ReferenceType");
\r
2060 public void ExportsChanged_ExportNothing_ShouldNotFireExportsChanged()
\r
2062 var container = CreateCompositionContainer();
\r
2064 container.ExportsChanged += (sender, args) =>
\r
2066 Assert.Fail("Event should not be fired!");
\r
2069 CompositionBatch batch = new CompositionBatch();
\r
2070 container.Compose(batch);
\r
2074 public void ExportsChanged_ExportAdded_ShouldFireExportsChanged()
\r
2076 var container = CreateCompositionContainer();
\r
2077 IEnumerable<string> changedNames = null;
\r
2079 container.ExportsChanged += (sender, args) =>
\r
2081 Assert.AreSame(container, sender);
\r
2082 Assert.IsNull(changedNames, "Ensure this event only fires once!");
\r
2083 Assert.IsNotNull(args.AddedExports);
\r
2084 Assert.IsNotNull(args.RemovedExports);
\r
2085 Assert.IsNotNull(args.ChangedContractNames);
\r
2086 changedNames = args.ChangedContractNames;
\r
2089 CompositionBatch batch = new CompositionBatch();
\r
2090 batch.AddExportedValue("MyExport", new object());
\r
2091 container.Compose(batch);
\r
2093 EnumerableAssert.AreEqual(changedNames, "MyExport");
\r
2097 public void ExportsChanged_ExportRemoved_ShouldFireExportsChanged()
\r
2099 var container = CreateCompositionContainer();
\r
2100 IEnumerable<string> changedNames = null;
\r
2102 CompositionBatch batch = new CompositionBatch();
\r
2103 var part = batch.AddExportedValue("MyExport", new object());
\r
2104 container.Compose(batch);
\r
2106 container.ExportsChanged += (sender, args) =>
\r
2108 Assert.AreSame(container, sender);
\r
2109 Assert.IsNull(changedNames, "Ensure this event only fires once!");
\r
2110 Assert.IsNotNull(args.AddedExports);
\r
2111 Assert.IsNotNull(args.RemovedExports);
\r
2112 Assert.IsNotNull(args.ChangedContractNames);
\r
2113 changedNames = args.ChangedContractNames;
\r
2116 batch = new CompositionBatch();
\r
2117 batch.RemovePart(part);
\r
2118 container.Compose(batch);
\r
2120 EnumerableAssert.AreEqual(changedNames, "MyExport");
\r
2124 public void ExportsChanged_ExportAddAnother_ShouldFireExportsChanged()
\r
2126 var container = CreateCompositionContainer();
\r
2127 IEnumerable<string> changedNames = null;
\r
2129 CompositionBatch batch = new CompositionBatch();
\r
2130 batch.AddExportedValue("MyExport", new object());
\r
2131 container.Compose(batch);
\r
2133 container.ExportsChanged += (sender, args) =>
\r
2135 Assert.AreSame(container, sender);
\r
2136 Assert.IsNull(changedNames, "Ensure this event only fires once!");
\r
2137 Assert.IsNotNull(args.AddedExports);
\r
2138 Assert.IsNotNull(args.RemovedExports);
\r
2139 Assert.IsNotNull(args.ChangedContractNames);
\r
2140 changedNames = args.ChangedContractNames;
\r
2143 batch = new CompositionBatch();
\r
2144 // Adding another should cause an update.
\r
2145 batch.AddExportedValue("MyExport", new object());
\r
2146 container.Compose(batch);
\r
2149 EnumerableAssert.AreEqual(changedNames, "MyExport");
\r
2153 public void ExportsChanged_AddExportOnParent_ShouldFireExportsChangedOnBoth()
\r
2155 var parent = CreateCompositionContainer();
\r
2156 var child = new CompositionContainer(parent);
\r
2158 IEnumerable<string> parentNames = null;
\r
2159 parent.ExportsChanged += (sender, args) =>
\r
2161 Assert.AreSame(parent, sender);
\r
2162 parentNames = args.ChangedContractNames;
\r
2165 IEnumerable<string> childNames = null;
\r
2166 child.ExportsChanged += (sender, args) =>
\r
2168 Assert.AreSame(child, sender);
\r
2169 childNames = args.ChangedContractNames;
\r
2172 CompositionBatch batch = new CompositionBatch();
\r
2173 batch.AddExportedValue("MyExport", new object());
\r
2174 parent.Compose(batch);
\r
2176 EnumerableAssert.AreEqual(parentNames, "MyExport");
\r
2177 EnumerableAssert.AreEqual(childNames, "MyExport");
\r
2182 public void ExportsChanged_AddExportOnChild_ShouldFireExportsChangedOnChildOnly()
\r
2184 var parent = CreateCompositionContainer();
\r
2185 var child = new CompositionContainer(parent);
\r
2187 parent.ExportsChanged += (sender, args) =>
\r
2189 Assert.Fail("Should not fire on parent container!!");
\r
2192 IEnumerable<string> childNames = null;
\r
2193 child.ExportsChanged += (sender, args) =>
\r
2195 Assert.AreSame(child, sender);
\r
2196 childNames = args.ChangedContractNames;
\r
2199 CompositionBatch batch = new CompositionBatch();
\r
2200 batch.AddExportedValue("MyExport2", new object());
\r
2201 child.Compose(batch);
\r
2203 EnumerableAssert.AreEqual(childNames, "MyExport2");
\r
2208 public void Dispose_BeforeCompose_CanBeCallMultipleTimes()
\r
2210 var container = ContainerFactory.Create(PartFactory.Create(), PartFactory.Create());
\r
2211 container.Dispose();
\r
2212 container.Dispose();
\r
2213 container.Dispose();
\r
2217 public void Dispose_AfterCompose_CanBeCallMultipleTimes()
\r
2219 var container = ContainerFactory.Create(PartFactory.Create(), PartFactory.Create());
\r
2220 container.Dispose();
\r
2221 container.Dispose();
\r
2222 container.Dispose();
\r
2226 public void Dispose_CallsGCSuppressFinalize()
\r
2228 bool finalizerCalled = false;
\r
2230 var container = ContainerFactory.CreateDisposable(disposing =>
\r
2234 finalizerCalled = true;
\r
2239 container.Dispose();
\r
2242 GC.WaitForPendingFinalizers();
\r
2245 Assert.IsFalse(finalizerCalled);
\r
2249 public void Dispose_CallsDisposeBoolWithTrue()
\r
2251 var container = ContainerFactory.CreateDisposable(disposing =>
\r
2253 Assert.IsTrue(disposing);
\r
2256 container.Dispose();
\r
2260 public void Dispose_CallsDisposeBoolOnce()
\r
2262 int disposeCount = 0;
\r
2264 var container = ContainerFactory.CreateDisposable(disposing =>
\r
2269 container.Dispose();
\r
2271 Assert.AreEqual(1, disposeCount);
\r
2275 public void Dispose_ContainerAsExportedValue_CanBeDisposed()
\r
2277 using (var container = CreateCompositionContainer())
\r
2279 CompositionBatch batch = new CompositionBatch();
\r
2280 batch.AddExportedValue<ICompositionService>(container);
\r
2281 container.Compose(batch);
\r
2286 public void Dispose_ContainerAsPart_CanBeDisposed()
\r
2287 { // Tests that when we re-enter CompositionContainer.Dispose, that we don't
\r
2288 // stack overflow.
\r
2290 using (var container = CreateCompositionContainer())
\r
2292 var part = PartFactory.CreateExporter(new MicroExport(typeof(ICompositionService), container));
\r
2293 CompositionBatch batch = new CompositionBatch();
\r
2294 batch.AddPart(part);
\r
2295 container.Compose(batch);
\r
2297 Assert.AreSame(container, container.GetExportedValue<ICompositionService>());
\r
2302 public void ICompositionService_ShouldNotBeImplicitlyExported()
\r
2304 var container = CreateCompositionContainer();
\r
2306 Assert.IsFalse(container.IsPresent<ICompositionService>());
\r
2310 public void CompositionContainer_ShouldNotBeImplicitlyExported()
\r
2312 var container = CreateCompositionContainer();
\r
2314 Assert.IsFalse(container.IsPresent<CompositionContainer>());
\r
2318 public void ICompositionService_ShouldNotBeImplicitlyImported()
\r
2320 var importer = PartFactory.CreateImporter<ICompositionService>();
\r
2321 var container = ContainerFactory.Create(importer);
\r
2323 Assert.IsNull(importer.Value);
\r
2327 public void CompositionContainer_ShouldNotBeImplicitlyImported()
\r
2329 var importer = PartFactory.CreateImporter<CompositionContainer>();
\r
2330 var container = ContainerFactory.Create(importer);
\r
2332 Assert.IsNull(importer.Value);
\r
2336 public void ICompositionService_CanBeExported()
\r
2338 var container = CreateCompositionContainer();
\r
2339 CompositionBatch batch = new CompositionBatch();
\r
2340 batch.AddExportedValue<ICompositionService>(container);
\r
2341 container.Compose(batch);
\r
2343 Assert.AreSame(container, container.GetExportedValue<ICompositionService>());
\r
2347 public void CompositionContainer_CanBeExported()
\r
2349 var container = CreateCompositionContainer();
\r
2350 CompositionBatch batch = new CompositionBatch();
\r
2351 batch.AddExportedValue<CompositionContainer>(container);
\r
2352 container.Compose(batch);
\r
2354 Assert.AreSame(container, container.GetExportedValue<CompositionContainer>());
\r
2358 public void ReleaseExport_Null_ShouldThrowArugmentNull()
\r
2360 var container = CreateCompositionContainer();
\r
2362 ExceptionAssert.ThrowsArgument<ArgumentNullException>("export",
\r
2363 () => container.ReleaseExport(null));
\r
2367 public void ReleaseExports_Null_ShouldThrowArgumentNull()
\r
2369 var container = CreateCompositionContainer();
\r
2371 ExceptionAssert.ThrowsArgument<ArgumentNullException>("exports",
\r
2372 () => container.ReleaseExports(null));
\r
2376 public void ReleaseExports_ElementNull_ShouldThrowArgument()
\r
2378 var container = CreateCompositionContainer();
\r
2380 ExceptionAssert.ThrowsArgument<ArgumentException>("exports",
\r
2381 () => container.ReleaseExports(new Export[] { null }));
\r
2384 public class OptionalImporter
\r
2386 [Import("ValueType", AllowDefault = true)]
\r
2387 public int ValueType
\r
2393 [Import("NullableValueType", AllowDefault = true)]
\r
2394 public int? NullableValueType
\r
2400 [Import("ReferenceType", AllowDefault = true)]
\r
2401 public string ReferenceType
\r
2408 public class ExportSimpleIntWithException
\r
2410 [Export("SimpleInt")]
\r
2411 public int SimpleInt { get { throw new NotImplementedException(); } }
\r
2415 public void TryGetValueWithCatalogVerifyExecptionDuringGet()
\r
2417 var cat = CatalogFactory.CreateDefaultAttributed();
\r
2418 var container = new CompositionContainer(cat);
\r
2420 CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotGetExportedValue, ErrorId.ReflectionModel_ExportThrewException, () =>
\r
2422 container.GetExportedValue<int>("SimpleInt");
\r
2427 public void TryGetExportedValueWhileLockedForNotify()
\r
2429 var container = CreateCompositionContainer();
\r
2430 CompositionBatch batch = new CompositionBatch();
\r
2431 batch.AddParts(new CallbackImportNotify(delegate
\r
2433 container.GetExportedValueOrDefault<int>();
\r
2436 container.Compose(batch);
\r
2440 public void RawExportTests()
\r
2442 var container = CreateCompositionContainer();
\r
2444 CompositionBatch batch = new CompositionBatch();
\r
2445 batch.AddExportedValue("foo", 1);
\r
2446 container.Compose(batch);
\r
2448 Lazy<int> export = container.GetExport<int>("foo");
\r
2450 Assert.AreEqual(1, export.Value, "Should be the value I put in...");
\r
2455 [WorkItem(468388)]
\r
2456 public void ContainerXGetXTest()
\r
2458 CompositionContainer container = CreateCompositionContainer();
\r
2459 CompositionBatch batch = new CompositionBatch();
\r
2460 batch.AddPart(new MyExporterWithNoFoo());
\r
2461 container.Compose(batch);
\r
2462 ContainerXGetExportBoundValue(container);
\r
2467 [WorkItem(468388)]
\r
2468 public void ContainerXGetXByComponentCatalogTest()
\r
2470 CompositionContainer container = ContainerFactory.CreateWithDefaultAttributedCatalog();
\r
2471 ContainerXGetExportBoundValue(container);
\r
2474 private void ContainerXGetExportBoundValue(CompositionContainer container)
\r
2476 Assert.Fail("This scenario (required metadata warnings) no longer works, see 468388");
\r
2478 //string[] required = new string[] { "Foo" };
\r
2479 //string[] RequiredMetadataNotFound = new string[] { CompositionIssueId.RequiredMetadataNotFound, CompositionIssueId.CardinalityMismatch };
\r
2480 //container.TryGetExport<MyExporterWithNoFoo>().VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2481 //container.TryGetExport<MyExporterWithNoFoo>(required).VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2482 //container.TryGetExport<MyExporterWithNoFoo>("MyExporterWithNoFoo").VerifySuccess();
\r
2483 //container.TryGetExport<MyExporterWithNoFoo>("MyExporterWithNoFoo", required).VerifyFailure(RequiredMetadataNotFound);
\r
2484 //container.TryGetExports<MyExporterWithNoFoo>().VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2485 //container.TryGetExports<MyExporterWithNoFoo>(required).VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2486 //container.TryGetExports<MyExporterWithNoFoo>("MyExporterWithNoFoo").VerifySuccess();
\r
2487 //container.TryGetExports<MyExporterWithNoFoo>("MyExporterWithNoFoo", required).VerifyFailure(RequiredMetadataNotFound);
\r
2488 //container.TryGetExportedValue<MyExporterWithNoFoo>().VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2489 //container.TryGetExportedValue<MyExporterWithNoFoo>(required).VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2490 //container.TryGetExportedValue<MyExporterWithNoFoo>("MyExporterWithNoFoo").VerifySuccess();
\r
2491 //container.TryGetExportedValue<MyExporterWithNoFoo>("MyExporterWithNoFoo", required).VerifyFailure(RequiredMetadataNotFound);
\r
2492 //container.TryGetExportedValues<MyExporterWithNoFoo>().VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2493 //container.TryGetExportedValues<MyExporterWithNoFoo>(required).VerifyFailure(CompositionIssueId.CardinalityMismatch);
\r
2494 //container.TryGetExportedValues<MyExporterWithNoFoo>("MyExporterWithNoFoo").VerifySuccess();
\r
2495 //container.TryGetExportedValues<MyExporterWithNoFoo>("MyExporterWithNoFoo", required).VerifyFailure(RequiredMetadataNotFound);
\r
2497 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() => container.GetExportedValue<MyExporterWithNoFoo>());
\r
2498 Assert.IsNotNull(container.GetExportedValue<MyExporterWithNoFoo>("MyExporterWithNoFoo"));
\r
2501 [Export("MyExporterWithNoFoo")]
\r
2502 public class MyExporterWithNoFoo
\r
2506 [Export("MyExporterWithFoo")]
\r
2507 [ExportMetadata("Foo", "Foo value")]
\r
2508 public class MyExporterWithFoo
\r
2512 [Export("MyExporterWithFooBar")]
\r
2513 [ExportMetadata("Foo", "Foo value")]
\r
2514 [ExportMetadata("Bar", "Bar value")]
\r
2515 public class MyExporterWithFooBar
\r
2520 // Silverlight doesn't support strongly typed metadata
\r
2522 public void ConverterExportTests()
\r
2524 var container = CreateCompositionContainer();
\r
2525 CompositionBatch batch = new CompositionBatch();
\r
2526 batch.AddExportedValue("foo", 1);
\r
2527 container.Compose(batch);
\r
2529 var export = container.GetExport<int, IDictionary<string, object>>("foo");
\r
2530 Assert.AreEqual(1, export.Value, "Should be the value I put in...");
\r
2531 Assert.IsNotNull(export.Metadata, "Should have metadata (as an object)");
\r
2534 #endif //!SILVERLIGHT
\r
2537 public void RemoveFromWrongContainerTest()
\r
2539 CompositionContainer d1 = CreateCompositionContainer();
\r
2540 CompositionContainer d2 = CreateCompositionContainer();
\r
2542 CompositionBatch batch1 = new CompositionBatch();
\r
2543 var valueKey = batch1.AddExportedValue("a", 1);
\r
2544 d1.Compose(batch1);
\r
2546 CompositionBatch batch2 = new CompositionBatch();
\r
2547 batch2.RemovePart(valueKey);
\r
2548 // removing entry from wrong container, shoudl be a no-op
\r
2549 d2.Compose(batch2);
\r
2553 [TestProperty("Type", "Integration")]
\r
2554 public void AddPartSimple()
\r
2556 var container = CreateCompositionContainer();
\r
2557 var importer = new Int32Importer();
\r
2558 CompositionBatch batch = new CompositionBatch();
\r
2559 batch.AddPart(importer);
\r
2560 batch.AddPart(new Int32Exporter(42));
\r
2561 container.Compose(batch);
\r
2563 Assert.AreEqual(42, importer.Value, "Expected value imported from export");
\r
2567 [TestProperty("Type", "Integration")]
\r
2568 public void AddPart()
\r
2570 var container = CreateCompositionContainer();
\r
2571 Int32Importer importer = new Int32Importer();
\r
2572 CompositionBatch batch = new CompositionBatch();
\r
2573 batch.AddPart(AttributedModelServices.CreatePart(importer));
\r
2574 batch.AddPart(new Int32Exporter(42));
\r
2575 container.Compose(batch);
\r
2577 Assert.AreEqual(42, importer.Value, "Expected value imported from export");
\r
2581 public void ComposeReentrantChildContainerDisposed()
\r
2583 var container = CreateCompositionContainer();
\r
2584 Int32Importer outerImporter = new Int32Importer();
\r
2585 Int32Importer innerImporter = new Int32Importer();
\r
2586 Int32Exporter exporter = new Int32Exporter(42);
\r
2587 CompositionBatch batch = new CompositionBatch();
\r
2588 batch.AddPart(exporter);
\r
2589 container.Compose(batch);
\r
2590 CallbackExecuteCodeDuringCompose callback = new CallbackExecuteCodeDuringCompose(() =>
\r
2592 using (CompositionContainer innerContainer = new CompositionContainer(container))
\r
2594 CompositionBatch nestedBatch = new CompositionBatch();
\r
2595 nestedBatch.AddPart(innerImporter);
\r
2596 innerContainer.Compose(nestedBatch);
\r
2598 Assert.AreEqual(42, innerImporter.Value, "Expected value imported from export");
\r
2601 batch = new CompositionBatch();
\r
2602 batch.AddParts(outerImporter, callback);
\r
2603 container.Compose(batch);
\r
2605 Assert.AreEqual(42, outerImporter.Value, "Expected value imported from export");
\r
2606 Assert.AreEqual(42, innerImporter.Value, "Expected value imported from export");
\r
2610 public void ComposeSimple()
\r
2612 var container = CreateCompositionContainer();
\r
2613 Int32Importer importer = new Int32Importer();
\r
2615 CompositionBatch batch = new CompositionBatch();
\r
2616 batch.AddParts(importer, new Int32Exporter(42));
\r
2617 container.Compose(batch);
\r
2619 Assert.AreEqual(42, importer.Value, "Expected value imported from export");
\r
2623 public void ComposeSimpleFail()
\r
2625 var container = CreateCompositionContainer();
\r
2626 Int32Importer importer = new Int32Importer();
\r
2628 CompositionBatch batch = new CompositionBatch();
\r
2629 batch.AddPart(importer);
\r
2631 CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport, // Cannot set Int32Importer.Value because
\r
2632 ErrorId.ImportEngine_ImportCardinalityMismatch, // No exports are present that match contract
\r
2633 RetryMode.DoNotRetry, () =>
\r
2635 container.Compose(batch);
\r
2640 public void ExceptionDuringNotify()
\r
2642 var container = CreateCompositionContainer();
\r
2643 CompositionBatch batch = new CompositionBatch();
\r
2644 batch.AddParts(new CallbackImportNotify(delegate
\r
2646 throw new InvalidOperationException();
\r
2649 CompositionAssert.ThrowsError(ErrorId.ImportEngine_PartCannotActivate, // Cannot activate CallbackImportNotify because
\r
2650 ErrorId.ReflectionModel_PartOnImportsSatisfiedThrewException, // OnImportsSatisfied threw an exception
\r
2651 RetryMode.DoNotRetry, () =>
\r
2653 container.Compose(batch);
\r
2658 public void NeutralComposeWhileNotified()
\r
2660 var container = CreateCompositionContainer();
\r
2661 CompositionBatch batch = new CompositionBatch();
\r
2662 batch.AddParts(new CallbackImportNotify(delegate
\r
2664 // Is this really a supported scenario?
\r
2665 container.Compose(batch);
\r
2668 container.Compose(batch);
\r
2670 public class PartWithReentrantCompose : ComposablePart
\r
2672 private CompositionContainer _container;
\r
2674 public PartWithReentrantCompose(CompositionContainer container)
\r
2676 this._container = container;
\r
2679 public override IEnumerable<ExportDefinition> ExportDefinitions
\r
2683 this._container.ComposeExportedValue<string>("ExportedString");
\r
2684 return Enumerable.Empty<ExportDefinition>();
\r
2688 public override IEnumerable<ImportDefinition> ImportDefinitions
\r
2692 return Enumerable.Empty<ImportDefinition>();
\r
2696 public override object GetExportedValue(ExportDefinition definition)
\r
2698 throw new NotImplementedException();
\r
2701 public override void SetImport(ImportDefinition definition, IEnumerable<Export> exports)
\r
2703 throw new NotImplementedException();
\r
2708 public class SimpleExporter
\r
2714 public void ThreadSafeCompositionContainer()
\r
2716 TypeCatalog catalog = new TypeCatalog(typeof(SimpleExporter));
\r
2718 CompositionContainer container = new CompositionContainer(catalog, true);
\r
2719 Int32Importer importer = new Int32Importer();
\r
2721 CompositionBatch batch = new CompositionBatch();
\r
2722 batch.AddParts(importer, new Int32Exporter(42));
\r
2723 container.Compose(batch);
\r
2725 Assert.IsNotNull(container.GetExportedValue<SimpleExporter>());
\r
2726 Assert.AreEqual(42, importer.Value, "Expected value imported from export");
\r
2728 container.Dispose();
\r
2733 public void ReentrantencyDisabledWhileComposing()
\r
2735 var container = CreateCompositionContainer();
\r
2736 CompositionBatch batch = new CompositionBatch();
\r
2738 batch.AddPart(new PartWithReentrantCompose(container));
\r
2740 ExceptionAssert.Throws<InvalidOperationException>(() =>
\r
2741 container.Compose(batch));
\r
2743 private static Expression<Func<ExportDefinition, bool>> ConstraintFromContract(string contractName)
\r
2745 return ConstraintFactory.Create(contractName);
\r
2748 private static string ContractFromType(Type type)
\r
2750 return AttributedModelServices.GetContractName(type);
\r
2753 private static CompositionContainer CreateCompositionContainer()
\r
2755 return new CompositionContainer();
\r
2758 public interface IMetadataView
\r
2777 public void ComposeExportedValueOfT_NullStringAsExportedValueArgument_VerifyCanPullOnValue()
\r
2779 var container = CreateCompositionContainer();
\r
2781 var expectation = (string)null;
\r
2782 container.ComposeExportedValue<string>(expectation);
\r
2783 var actualValue = container.GetExportedValue<string>();
\r
2785 Assert.AreEqual(expectation, actualValue);
\r
2789 public void ComposeExportedValueOfT_StringAsExportedValueArgument_VerifyCanPullOnValue()
\r
2791 var expectations = new List<string>();
\r
2792 expectations.Add((string)null);
\r
2793 expectations.Add(String.Empty);
\r
2794 expectations.Add("Value");
\r
2796 foreach (var expectation in expectations)
\r
2798 var container = CreateCompositionContainer();
\r
2799 container.ComposeExportedValue<string>(expectation);
\r
2800 var actualValue = container.GetExportedValue<string>();
\r
2802 Assert.AreEqual(expectation, actualValue);
\r
2807 public void ComposeExportedValueOfT_StringAsIEnumerableOfCharAsExportedValueArgument_VerifyCanPullOnValue()
\r
2809 var expectations = new List<string>();
\r
2810 expectations.Add((string)null);
\r
2811 expectations.Add(String.Empty);
\r
2812 expectations.Add("Value");
\r
2814 foreach (var expectation in expectations)
\r
2816 var container = CreateCompositionContainer();
\r
2817 container.ComposeExportedValue<IEnumerable<char>>(expectation);
\r
2818 var actualValue = container.GetExportedValue<IEnumerable<char>>();
\r
2820 Assert.AreEqual(expectation, actualValue);
\r
2825 public void ComposeExportedValueOfT_ObjectAsExportedValueArgument_VerifyCanPullOnValue()
\r
2827 var expectations = new List<object>();
\r
2828 expectations.Add((string)null);
\r
2829 expectations.Add(String.Empty);
\r
2830 expectations.Add("Value");
\r
2831 expectations.Add(42);
\r
2832 expectations.Add(new object());
\r
2834 foreach (var expectation in expectations)
\r
2836 var container = CreateCompositionContainer();
\r
2837 container.ComposeExportedValue<object>(expectation);
\r
2838 var actualValue = container.GetExportedValue<object>();
\r
2840 Assert.AreEqual(expectation, actualValue);
\r
2845 public void ComposeExportedValueOfT_ExportedValue_ExportedUnderDefaultContractName()
\r
2847 string expectedContractName = AttributedModelServices.GetContractName(typeof(string));
\r
2848 var container = CreateCompositionContainer();
\r
2849 container.ComposeExportedValue<string>("Value");
\r
2851 var importDefinition = new ImportDefinition(ed => true, null, ImportCardinality.ZeroOrMore, false, false);
\r
2852 var exports = container.GetExports(importDefinition);
\r
2853 Assert.AreEqual(1, exports.Count());
\r
2854 Assert.AreEqual(expectedContractName, exports.Single().Definition.ContractName);
\r
2858 public void ComposeExportedValueOfT_ExportedValue_ExportContainsEmptyMetadata()
\r
2860 var container = CreateCompositionContainer();
\r
2861 container.ComposeExportedValue<string>("Value");
\r
2863 var importDefinition = new ImportDefinition(ed => true, null, ImportCardinality.ZeroOrMore, false, false);
\r
2864 var exports = container.GetExports(importDefinition);
\r
2865 Assert.AreEqual(1, exports.Count());
\r
2866 Assert.AreEqual(1, exports.Single().Metadata.Count); // contains type identity
\r
2870 public void ComposeExportedValueOfT_ExportedValue_LazyContainsEmptyMetadata()
\r
2872 var container = CreateCompositionContainer();
\r
2873 container.ComposeExportedValue<string>("Value");
\r
2875 var lazy = container.GetExport<string, IDictionary<string, object>>();
\r
2876 Assert.AreEqual(1, lazy.Metadata.Count); // contains type identity
\r
2880 public void ComposeExportedValueOfT_ExportedValue_ImportsAreNotDiscovered()
\r
2882 var container = CreateCompositionContainer();
\r
2883 var importer = new PartWithRequiredImport();
\r
2885 container.ComposeExportedValue<object>(importer);
\r
2887 var importDefinition = new ImportDefinition(ed => true, null, ImportCardinality.ZeroOrMore, false, false);
\r
2888 var exports = container.GetExports(importDefinition);
\r
2889 Assert.AreEqual(1, exports.Count()); // we only get one if the import was not discovered since the import is not satisfied
\r
2893 public void ComposeExportedValueOfT_NullAsContractName_ThrowsArgumentNullException()
\r
2895 var container = CreateCompositionContainer();
\r
2896 ExceptionAssert.ThrowsArgument<ArgumentNullException>("contractName", () =>
\r
2897 container.ComposeExportedValue<string>((string)null, "Value"));
\r
2901 public void ComposeExportedValueOfT_EmptyStringAsContractName_ThrowsArgumentException()
\r
2903 var container = CreateCompositionContainer();
\r
2904 ExceptionAssert.ThrowsArgument<ArgumentException>("contractName", () =>
\r
2905 container.ComposeExportedValue<string>(String.Empty, "Value"));
\r
2909 public void ComposeExportedValueOfT_ValidContractName_ValidExportedValue_VerifyCanPullOnValue()
\r
2911 var expectations = new List<Tuple<string, string>>();
\r
2912 expectations.Add(new Tuple<string, string>(" ", (string)null));
\r
2913 expectations.Add(new Tuple<string, string>(" ", String.Empty));
\r
2914 expectations.Add(new Tuple<string, string>(" ", "Value"));
\r
2915 expectations.Add(new Tuple<string, string>("ContractName", (string)null));
\r
2916 expectations.Add(new Tuple<string, string>("ContractName", String.Empty));
\r
2917 expectations.Add(new Tuple<string, string>("ContractName", "Value"));
\r
2919 foreach (var expectation in expectations)
\r
2921 var container = CreateCompositionContainer();
\r
2922 container.ComposeExportedValue<string>(expectation.Item1, expectation.Item2);
\r
2923 var actualValue = container.GetExportedValue<string>(expectation.Item1);
\r
2925 Assert.AreEqual(expectation.Item2, actualValue);
\r
2927 ExceptionAssert.Throws<ImportCardinalityMismatchException>(() =>
\r
2928 container.GetExportedValue<string>());
\r
2933 public void ComposeExportedValueOfT_ValidContractName_ExportedValue_ExportedUnderSpecifiedContractName()
\r
2935 string expectedContractName = "ContractName";
\r
2936 var container = CreateCompositionContainer();
\r
2937 container.ComposeExportedValue<string>(expectedContractName, "Value");
\r
2939 var importDefinition = new ImportDefinition(ed => true, null, ImportCardinality.ZeroOrMore, false, false);
\r
2940 var exports = container.GetExports(importDefinition);
\r
2941 Assert.AreEqual(1, exports.Count());
\r
2942 Assert.AreEqual(expectedContractName, exports.Single().Definition.ContractName);
\r
2947 [WorkItem(812029)]
\r
2948 public void ComposeExportedValueOfT_ValidContractName_ExportedValue_ExportContainsEmptyMetadata()
\r
2950 string expectedContractName = "ContractName";
\r
2951 var container = CreateCompositionContainer();
\r
2952 container.ComposeExportedValue<string>(expectedContractName, "Value");
\r
2954 var importDefinition = new ImportDefinition(ed => ed.ContractName == expectedContractName, null, ImportCardinality.ZeroOrMore, false, false);
\r
2955 var exports = container.GetExports(importDefinition);
\r
2956 Assert.AreEqual(1, exports.Count());
\r
2957 Assert.AreEqual(1, exports.Single().Metadata.Count); // contains type identity
\r
2961 public void ComposeExportedValueOfT_ValidContractName_ExportedValue_ImportsAreNotDiscovered()
\r
2963 var container = CreateCompositionContainer();
\r
2964 var importer = new PartWithRequiredImport();
\r
2966 container.ComposeExportedValue<object>("ContractName", importer);
\r
2968 var importDefinition = new ImportDefinition(ed => true, null, ImportCardinality.ZeroOrMore, false, false);
\r
2969 var exports = container.GetExports(importDefinition);
\r
2970 Assert.AreEqual(1, exports.Count()); // we only get one if the import was not discovered since the import is not satisfied
\r
2974 public void TestExportedValueCachesNullValue()
\r
2976 var container = ContainerFactory.Create();
\r
2977 var exporter = new ExportsMutableProperty();
\r
2978 exporter.Property = null;
\r
2979 container.ComposeParts(exporter);
\r
2980 Assert.IsNull(container.GetExportedValue<string>("Property"));
\r
2981 exporter.Property = "Value1";
\r
2982 // Exported value should have been cached and so it shouldn't change
\r
2983 Assert.IsNull(container.GetExportedValue<string>("Property"));
\r
2985 public class ExportsMutableProperty
\r
2987 [Export("Property")]
\r
2988 public string Property { get; set; }
\r
2992 public class PartWithRequiredImport
\r
2995 public object Import { get; set; }
\r