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 Microsoft.VisualStudio.TestTools.UnitTesting;
\r
10 using System.ComponentModel.Composition.UnitTesting;
\r
11 using System.ComponentModel.Composition.Factories;
\r
12 using System.UnitTesting;
\r
13 using System.ComponentModel.Composition.Hosting;
\r
15 namespace System.ComponentModel.Composition
\r
18 public class ComponentServicesTests
\r
21 public void GetValuesByType()
\r
23 var cat = CatalogFactory.CreateDefaultAttributed();
\r
25 var container = new CompositionContainer(cat);
\r
27 string itestName = AttributedModelServices.GetContractName(typeof(ITest));
\r
29 var e1 = container.GetExportedValues<ITest>();
\r
30 var e2 = container.GetExports<ITest, object>(itestName);
\r
32 Assert.IsInstanceOfType(e1.First(), typeof(T1), "First should be T1");
\r
33 Assert.IsInstanceOfType(e1.Skip(1).First(), typeof(T2), "Second should be T2");
\r
35 Assert.IsInstanceOfType(e2.First().Value, typeof(T1), "First should be T1");
\r
36 Assert.IsInstanceOfType(e2.Skip(1).First().Value, typeof(T2), "Second should be T2");
\r
38 CompositionContainer childContainer = new CompositionContainer(container);
\r
39 CompositionBatch batch = new CompositionBatch();
\r
40 batch.AddPart(new T1());
\r
41 container.Compose(batch);
\r
42 var t1 = childContainer.GetExportedValue<ITest>();
\r
43 var t2 = childContainer.GetExport<ITest, object>(itestName);
\r
45 Assert.IsInstanceOfType(t1, typeof(T1), "First (resolved) should be T1");
\r
46 Assert.IsInstanceOfType(t2.Value, typeof(T1), "First (resolved) should be T1");
\r
50 public void GetValueTest()
\r
52 var container = ContainerFactory.Create();
\r
54 string name = AttributedModelServices.GetContractName(typeof(ITest));
\r
55 CompositionBatch batch = new CompositionBatch();
\r
56 batch.AddExportedValue(name, t);
\r
57 container.Compose(batch);
\r
58 ITest value = container.GetExportedValue<ITest>();
\r
59 Assert.AreEqual(t, value, "TryGetExportedValue should return t (by type)");
\r
60 value = container.GetExportedValue<ITest>(name);
\r
61 Assert.AreEqual(t, value, "TryGetExportedValue should return t (given name)");
\r
65 public void GetValuesTest()
\r
67 var container = ContainerFactory.Create();
\r
69 string name = AttributedModelServices.GetContractName(typeof(ITest));
\r
70 CompositionBatch batch = new CompositionBatch();
\r
71 batch.AddExportedValue(name, t);
\r
72 container.Compose(batch);
\r
73 IEnumerable<ITest> values = container.GetExportedValues<ITest>();
\r
74 Assert.AreEqual(t, values.First(), "TryGetExportedValues should return t (by type)");
\r
75 values = container.GetExportedValues<ITest>(name);
\r
76 Assert.AreEqual(t, values.First(), "TryGetExportedValues should return t (by name)");
\r
82 public void NoResolverExceptionTest()
\r
84 var container = ContainerFactory.Create();
\r
86 CompositionBatch batch = new CompositionBatch();
\r
87 batch.AddPart(new MultiExport());
\r
88 batch.AddPart(new SingleImport());
\r
90 CompositionAssert.ThrowsChangeRejectedError(ErrorId.ImportEngine_PartCannotSetImport, ErrorId.ImportEngine_ImportCardinalityMismatch, RetryMode.DoNotRetry, () =>
\r
92 container.Compose(batch);
\r
97 public class MultiExport
\r
100 [ExportMetadata("Value", "One")]
\r
101 public int M1 { get { return 1; } }
\r
104 [ExportMetadata("Value", "Two")]
\r
105 public int M2 { get { return 2; } }
\r
108 [ExportMetadata("Value", "Three")]
\r
109 public int M3 { get { return 3; } }
\r
112 public interface ITest
\r
117 [Export(typeof(ITest))]
\r
118 public class T1 : ITest
\r
120 public void Do() { }
\r
123 [Export(typeof(ITest))]
\r
124 public class T2 : ITest
\r
126 public void Do() { }
\r