update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / ComponentModel / Composition / ReflectionModel / ReflectionModelServicesTests.cs
blob4cf4d95b94ef21ba34ad1238eea467eaca389924
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.ComponentModel.Composition.Factories;
6 using System.ComponentModel.Composition.Hosting;
7 using System.ComponentModel.Composition.Primitives;
8 using System.Reflection;
9 using Microsoft.Internal;
10 using Microsoft.VisualStudio.TestTools.UnitTesting;
11 using System.Collections.Generic;
12 using System.Linq;
13 using System.UnitTesting;
14 using System.Threading;
16 namespace System.ComponentModel.Composition.ReflectionModel
18 [TestClass]
19 public class ReflectionModelServicesTests
21 [TestMethod]
22 public void CreatePartDefinition()
24 Type expectedType = typeof(TestPart);
25 Lazy<Type> expectedLazyType = expectedType.AsLazy();
26 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
27 expectedMetadata["Key1"] = 1;
28 expectedMetadata["Key2"] = "Value2";
30 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
31 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
33 ICompositionElement expectedOrigin = new MockOrigin();
35 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
36 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
37 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
38 expectedMetadata.AsLazy(), expectedOrigin);
39 Assert.IsNotNull(partDefinition);
41 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
42 Assert.IsNotNull(definition);
44 Assert.AreSame(expectedType, definition.GetPartType());
45 Assert.IsTrue(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
46 Assert.IsTrue(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
47 Assert.IsTrue(definition.ExportDefinitions.SequenceEqual(expectedExports.Cast<ExportDefinition>()));
48 Assert.IsTrue(definition.ImportDefinitions.SequenceEqual(expectedImports.Cast<ImportDefinition>()));
49 Assert.AreSame(expectedOrigin, ((ICompositionElement)definition).Origin);
50 Assert.IsNotNull(((ICompositionElement)definition).DisplayName);
51 Assert.IsFalse(definition.IsDisposalRequired);
54 [TestMethod]
55 public void CreatePartDefinition_Disposable()
57 Type expectedType = typeof(TestPart);
58 Lazy<Type> expectedLazyType = expectedType.AsLazy();
59 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
60 expectedMetadata["Key1"] = 1;
61 expectedMetadata["Key2"] = "Value2";
63 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
64 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
66 ICompositionElement expectedOrigin = new MockOrigin();
68 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, true,
69 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
70 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
71 expectedMetadata.AsLazy(), expectedOrigin);
72 Assert.IsNotNull(partDefinition);
74 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
75 Assert.IsNotNull(definition);
77 Assert.AreSame(expectedType, definition.GetPartType());
78 Assert.IsTrue(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
79 Assert.IsTrue(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
80 Assert.IsTrue(definition.ExportDefinitions.SequenceEqual(expectedExports.Cast<ExportDefinition>()));
81 Assert.IsTrue(definition.ImportDefinitions.SequenceEqual(expectedImports.Cast<ImportDefinition>()));
82 Assert.AreSame(expectedOrigin, ((ICompositionElement)definition).Origin);
83 Assert.IsNotNull(((ICompositionElement)definition).DisplayName);
84 Assert.IsTrue(definition.IsDisposalRequired);
87 [TestMethod]
88 public void CreatePartDefinition_NullMetadataAllowed()
90 Type expectedType = typeof(TestPart);
91 Lazy<Type> expectedLazyType = expectedType.AsLazy();
93 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
94 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
96 ICompositionElement expectedOrigin = new MockOrigin();
98 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
99 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
100 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
101 null, expectedOrigin);
102 Assert.IsNotNull(partDefinition);
104 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
105 Assert.IsNotNull(definition);
106 Assert.IsNotNull(definition.Metadata);
107 Assert.AreEqual(0, definition.Metadata.Count);
110 [TestMethod]
111 public void CreatePartDefinition_EvaluatedNullMetadataAllowed()
113 Type expectedType = typeof(TestPart);
114 Lazy<Type> expectedLazyType = expectedType.AsLazy();
115 IDictionary<string, object> expectedMetadata = null;
117 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
118 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
120 ICompositionElement expectedOrigin = new MockOrigin();
122 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
123 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
124 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
125 expectedMetadata.AsLazy(), expectedOrigin);
126 Assert.IsNotNull(partDefinition);
128 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
129 Assert.IsNotNull(definition);
130 Assert.IsNotNull(definition.Metadata);
131 Assert.AreEqual(0, definition.Metadata.Count);
135 [TestMethod]
136 public void CreatePartDefinition_NullExportsAllowed()
138 Type expectedType = typeof(TestPart);
139 Lazy<Type> expectedLazyType = expectedType.AsLazy();
141 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
142 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
143 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
145 ICompositionElement expectedOrigin = new MockOrigin();
147 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
148 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
149 null,
150 expectedMetadata.AsLazy(), expectedOrigin);
151 Assert.IsNotNull(partDefinition);
153 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
154 Assert.IsNotNull(definition);
155 Assert.IsNotNull(definition.ExportDefinitions);
156 Assert.AreEqual(0, definition.ExportDefinitions.Count());
159 [TestMethod]
160 public void CreatePartDefinition_EvaluatedNullExportsAllowed()
162 Type expectedType = typeof(TestPart);
163 Lazy<Type> expectedLazyType = expectedType.AsLazy();
165 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
166 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
167 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
169 ICompositionElement expectedOrigin = new MockOrigin();
171 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
172 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
173 new Lazy<IEnumerable<ExportDefinition>>(() => null),
174 expectedMetadata.AsLazy(), expectedOrigin);
175 Assert.IsNotNull(partDefinition);
177 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
178 Assert.IsNotNull(definition);
179 Assert.IsNotNull(definition.ExportDefinitions);
180 Assert.AreEqual(0, definition.ExportDefinitions.Count());
183 [TestMethod]
184 public void CreatePartDefinition_ExportsMustBeOfRightType()
186 Type expectedType = typeof(TestPart);
187 Lazy<Type> expectedLazyType = expectedType.AsLazy();
189 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
190 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
191 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
193 ICompositionElement expectedOrigin = new MockOrigin();
195 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
196 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
197 new Lazy<IEnumerable<ExportDefinition>>(() => CreateInvalidExports()),
198 expectedMetadata.AsLazy(), expectedOrigin);
199 Assert.IsNotNull(partDefinition);
201 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
202 Assert.IsNotNull(definition);
204 ExceptionAssert.Throws<InvalidOperationException>(() =>
206 definition.ExportDefinitions.Count();
210 [TestMethod]
211 public void CreatePartDefinition_NullImportsAllowed()
213 Type expectedType = typeof(TestPart);
214 Lazy<Type> expectedLazyType = expectedType.AsLazy();
216 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
217 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
218 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
220 ICompositionElement expectedOrigin = new MockOrigin();
222 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
223 null,
224 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
225 expectedMetadata.AsLazy(), expectedOrigin);
226 Assert.IsNotNull(partDefinition);
228 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
229 Assert.IsNotNull(definition);
230 Assert.IsNotNull(definition.ImportDefinitions);
231 Assert.AreEqual(0, definition.ImportDefinitions.Count());
234 [TestMethod]
235 public void CreatePartDefinition_EvaluatedNullImportsAllowed()
237 Type expectedType = typeof(TestPart);
238 Lazy<Type> expectedLazyType = expectedType.AsLazy();
240 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
241 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
242 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
244 ICompositionElement expectedOrigin = new MockOrigin();
246 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
247 new Lazy<IEnumerable<ImportDefinition>>(() => null),
248 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
249 expectedMetadata.AsLazy(), expectedOrigin);
250 Assert.IsNotNull(partDefinition);
252 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
253 Assert.IsNotNull(definition);
254 Assert.IsNotNull(definition.ImportDefinitions);
255 Assert.AreEqual(0, definition.ImportDefinitions.Count());
258 [TestMethod]
259 public void CreatePartDefinition_ImportsMustBeOfRightType()
261 Type expectedType = typeof(TestPart);
262 Lazy<Type> expectedLazyType = expectedType.AsLazy();
264 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
265 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
266 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
268 ICompositionElement expectedOrigin = new MockOrigin();
270 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
271 new Lazy<IEnumerable<ImportDefinition>>(() => CreateInvalidImports()),
272 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
273 expectedMetadata.AsLazy(), expectedOrigin);
274 Assert.IsNotNull(partDefinition);
276 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
277 Assert.IsNotNull(definition);
278 ExceptionAssert.Throws<InvalidOperationException>(() =>
280 definition.ImportDefinitions.Count();
285 [TestMethod]
286 public void CreatePartDefinition_NullTypeNotAllowed()
288 Type expectedType = typeof(TestPart);
289 Lazy<Type> expectedLazyType = expectedType.AsLazy();
290 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
291 expectedMetadata["Key1"] = 1;
292 expectedMetadata["Key2"] = "Value2";
294 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
295 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
297 ICompositionElement expectedOrigin = new MockOrigin();
299 ExceptionAssert.ThrowsArgument<ArgumentNullException>("partType", () =>
301 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(null, false,
302 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
303 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
304 expectedMetadata.AsLazy(), expectedOrigin);
309 [TestMethod]
310 public void CreatePartDefinition_NullEvaluatedTypeNotAllowed()
312 Type expectedType = typeof(TestPart);
313 Lazy<Type> expectedLazyType = expectedType.AsLazy();
315 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
316 expectedMetadata["Key1"] = 1;
317 expectedMetadata["Key2"] = "Value2";
319 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
320 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
322 ICompositionElement expectedOrigin = new MockOrigin();
324 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(new Lazy<Type>(() => null), false,
325 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
326 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
327 expectedMetadata.AsLazy(), expectedOrigin);
329 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
330 Assert.IsNotNull(definition);
332 ExceptionAssert.Throws<InvalidOperationException>(() =>
334 definition.GetPartType();
338 [TestMethod]
339 public void GetPartType()
341 Type expectedType = typeof(TestPart);
342 Lazy<Type> expectedLazyType = expectedType.AsLazy();
343 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
344 expectedMetadata["Key1"] = 1;
345 expectedMetadata["Key2"] = "Value2";
347 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
348 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
350 ICompositionElement expectedOrigin = new MockOrigin();
352 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
353 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
354 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
355 expectedMetadata.AsLazy(), expectedOrigin);
356 Assert.IsNotNull(partDefinition);
358 Lazy<Type> lazyPartType = ReflectionModelServices.GetPartType(partDefinition);
359 Assert.AreEqual(expectedLazyType, lazyPartType);
362 [TestMethod]
363 public void GetPartType_NullAsPart_ShouldThrowArgumentNull()
365 ExceptionAssert.ThrowsArgument<ArgumentNullException>("partDefinition", () =>
367 ReflectionModelServices.GetPartType(null);
371 [TestMethod]
372 public void GetPartType_InvalidPart_ShouldThrowArgument()
374 ExceptionAssert.ThrowsArgument<ArgumentException>("partDefinition", () =>
376 ReflectionModelServices.GetPartType(new InvalidPartDefinition());
381 [TestMethod]
382 public void IsDisposalRequired_ForNonDisposable()
384 Type expectedType = typeof(TestPart);
385 Lazy<Type> expectedLazyType = expectedType.AsLazy();
386 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
387 expectedMetadata["Key1"] = 1;
388 expectedMetadata["Key2"] = "Value2";
390 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
391 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
393 ICompositionElement expectedOrigin = new MockOrigin();
395 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
396 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
397 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
398 expectedMetadata.AsLazy(), expectedOrigin);
399 Assert.IsNotNull(partDefinition);
401 bool isDisposalRequired = ReflectionModelServices.IsDisposalRequired(partDefinition);
402 Assert.IsFalse(isDisposalRequired);
405 [TestMethod]
406 public void IsDisposalRequired_ForDisposable()
408 Type expectedType = typeof(TestPart);
409 Lazy<Type> expectedLazyType = expectedType.AsLazy();
410 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
411 expectedMetadata["Key1"] = 1;
412 expectedMetadata["Key2"] = "Value2";
414 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
415 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
417 ICompositionElement expectedOrigin = new MockOrigin();
419 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, true,
420 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
421 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
422 expectedMetadata.AsLazy(), expectedOrigin);
423 Assert.IsNotNull(partDefinition);
425 bool isDisposalRequired = ReflectionModelServices.IsDisposalRequired(partDefinition);
426 Assert.IsTrue(isDisposalRequired);
430 [TestMethod]
431 public void IsDisposalRequired_NullAsPart_ShouldThrowArgumentNull()
433 ExceptionAssert.ThrowsArgument<ArgumentNullException>("partDefinition", () =>
435 ReflectionModelServices.IsDisposalRequired(null);
439 [TestMethod]
440 public void IsDisposalRequired_InvalidPart_ShouldThrowArgument()
442 ExceptionAssert.ThrowsArgument<ArgumentException>("partDefinition", () =>
444 ReflectionModelServices.IsDisposalRequired(new InvalidPartDefinition());
448 [TestMethod]
449 public void CreateExportDefinition()
451 PropertyInfo property = typeof(TestPart).GetProperties().First();
452 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
454 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
455 expectedMetadata["Key1"] = 1;
456 expectedMetadata["Key2"] = "Value2";
458 string expectedContractName = "Foo";
460 ICompositionElement expectedOrigin = new MockOrigin();
462 ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
463 Assert.IsNotNull(exportDefinition);
464 ReflectionMemberExportDefinition definition = exportDefinition as ReflectionMemberExportDefinition;
465 Assert.IsNotNull(definition);
467 Assert.AreEqual(expectedContractName, definition.ContractName);
468 Assert.IsTrue(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
469 Assert.IsTrue(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
470 Assert.AreEqual(expectedOrigin, ((ICompositionElement)definition).Origin);
471 Assert.AreEqual(expectedLazyMember, definition.ExportingLazyMember);
474 [TestMethod]
475 public void CreateExportDefinition_NullAsContractName_ThrowsNullArgument()
477 PropertyInfo property = typeof(TestPart).GetProperties().First();
478 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
480 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
481 expectedMetadata["Key1"] = 1;
482 expectedMetadata["Key2"] = "Value2";
485 ICompositionElement expectedOrigin = new MockOrigin();
487 ExceptionAssert.ThrowsArgument<ArgumentNullException>("contractName", () =>
489 ReflectionModelServices.CreateExportDefinition(expectedLazyMember, null, expectedMetadata.AsLazy(), expectedOrigin);
493 public void CreateExportDefinition_NullAsMetadata_Allowed()
495 PropertyInfo property = typeof(TestPart).GetProperties().First();
496 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
498 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
499 expectedMetadata["Key1"] = 1;
500 expectedMetadata["Key2"] = "Value2";
502 string expectedContractName = "Foo";
503 ICompositionElement expectedOrigin = new MockOrigin();
505 ExportDefinition definition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
506 Assert.IsNotNull(definition.Metadata);
507 Assert.AreEqual(0, definition.Metadata.Count);
510 [TestMethod]
511 public void CreateExportDefinition_InvalidLazymemberInfo_ShouldThrowArtument()
513 EventInfo _event = typeof(TestPart).GetEvents().First();
514 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(_event);
516 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
517 expectedMetadata["Key1"] = 1;
518 expectedMetadata["Key2"] = "Value2";
520 string expectedContractName = "Foo";
522 ICompositionElement expectedOrigin = new MockOrigin();
524 ExceptionAssert.ThrowsArgument<ArgumentException>("exportingMember", () =>
526 ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
530 [TestMethod]
531 public void GetExportingMember()
533 PropertyInfo property = typeof(TestPart).GetProperties().First();
534 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
536 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
537 expectedMetadata["Key1"] = 1;
538 expectedMetadata["Key2"] = "Value2";
540 string expectedContractName = "Foo";
542 ICompositionElement expectedOrigin = new MockOrigin();
544 ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
545 Assert.IsNotNull(exportDefinition);
547 LazyMemberInfo lazyMember = ReflectionModelServices.GetExportingMember(exportDefinition);
548 Assert.AreEqual(expectedLazyMember, lazyMember);
551 [TestMethod]
552 public void GetExportingMember_NullAsExportDefinition_ShouldThrowArhumentNull()
554 ExceptionAssert.ThrowsArgument<ArgumentNullException>("exportDefinition", () =>
556 ReflectionModelServices.GetExportingMember(null);
560 [TestMethod]
561 public void GetExportingMember_InvalidExportDefinition_ShouldThrowArhumentNull()
563 ExceptionAssert.ThrowsArgument<ArgumentException>("exportDefinition", () =>
565 ReflectionModelServices.GetExportingMember(new ExportDefinition("Foo", null));
569 [TestMethod]
570 public void CreateImportDefinition_Member()
572 PropertyInfo property = typeof(TestPart).GetProperties().First();
573 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
575 string expectedContractName = "Foo";
576 string expectedRequiredTypeIdentity = "Bar";
577 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
578 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
579 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
580 bool expectedRecomposable = true;
582 ICompositionElement expectedOrigin = new MockOrigin();
584 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
585 expectedLazyMember,
586 expectedContractName,
587 expectedRequiredTypeIdentity,
588 expectedRequiredMetadata,
589 expectedCardinality,
590 expectedRecomposable,
591 expectedCreationPolicy,
592 expectedOrigin);
593 Assert.IsNotNull(importDefinition);
595 ReflectionMemberImportDefinition definition = importDefinition as ReflectionMemberImportDefinition;
596 Assert.IsNotNull(definition);
598 Assert.AreEqual(expectedLazyMember, definition.ImportingLazyMember);
599 Assert.AreEqual(definition.ContractName, expectedContractName);
600 Assert.AreEqual(definition.RequiredTypeIdentity, expectedRequiredTypeIdentity);
601 Assert.IsTrue(definition.RequiredMetadata.SequenceEqual(expectedRequiredMetadata));
602 Assert.AreEqual(definition.Cardinality, expectedCardinality);
603 Assert.AreEqual(definition.RequiredCreationPolicy, expectedCreationPolicy);
604 Assert.AreEqual(definition.IsRecomposable, expectedRecomposable);
605 Assert.AreSame(expectedOrigin, ((ICompositionElement)definition).Origin);
606 Assert.IsFalse(definition.IsPrerequisite);
609 [TestMethod]
610 public void CreateImportDefinition_Member_InvalidMember_ShouldThrowArgument()
612 MethodInfo method = typeof(TestPart).GetMethods().First();
613 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(method);
615 string expectedContractName = "Foo";
616 string expectedRequiredTypeIdentity = "Bar";
617 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
618 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
619 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
620 bool expectedRecomposable = true;
622 ICompositionElement expectedOrigin = new MockOrigin();
624 ExceptionAssert.ThrowsArgument<ArgumentException>("importingMember", () =>
626 ReflectionModelServices.CreateImportDefinition(
627 expectedLazyMember,
628 expectedContractName,
629 expectedRequiredTypeIdentity,
630 expectedRequiredMetadata,
631 expectedCardinality,
632 expectedRecomposable,
633 expectedCreationPolicy,
634 expectedOrigin);
638 [TestMethod]
639 public void GetImporingMember()
641 PropertyInfo property = typeof(TestPart).GetProperties().First();
642 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
644 string expectedContractName = "Foo";
645 string expectedRequiredTypeIdentity = "Bar";
646 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
647 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
648 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
649 bool expectedRecomposable = true;
651 ICompositionElement expectedOrigin = new MockOrigin();
653 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
654 expectedLazyMember,
655 expectedContractName,
656 expectedRequiredTypeIdentity,
657 expectedRequiredMetadata,
658 expectedCardinality,
659 expectedRecomposable,
660 expectedCreationPolicy,
661 expectedOrigin);
662 Assert.IsNotNull(importDefinition);
664 LazyMemberInfo lazyMember = ReflectionModelServices.GetImportingMember(importDefinition);
665 Assert.AreEqual(expectedLazyMember, lazyMember);
668 [TestMethod]
669 public void GetImporingMember_NullAsImport_ShouldThrowArgumentNull()
671 ExceptionAssert.ThrowsArgument<ArgumentNullException>("importDefinition", () =>
673 ReflectionModelServices.GetImportingMember(null);
677 [TestMethod]
678 public void GetImporingMember_InvalidImport_ShouldThrowArgument()
680 ExceptionAssert.ThrowsArgument<ArgumentException>("importDefinition", () =>
682 ReflectionModelServices.GetImportingMember(new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any));
688 [TestMethod]
689 public void CreateImportDefinition_Parameter()
692 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
693 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
695 string expectedContractName = "Foo";
696 string expectedRequiredTypeIdentity = "Bar";
697 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
698 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
699 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
701 ICompositionElement expectedOrigin = new MockOrigin();
703 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
704 expectedLazyParameter,
705 expectedContractName,
706 expectedRequiredTypeIdentity,
707 expectedRequiredMetadata,
708 expectedCardinality,
709 expectedCreationPolicy,
710 expectedOrigin);
711 Assert.IsNotNull(importDefinition);
713 ReflectionParameterImportDefinition definition = importDefinition as ReflectionParameterImportDefinition;
714 Assert.IsNotNull(definition);
716 Assert.AreEqual(expectedLazyParameter, definition.ImportingLazyParameter);
717 Assert.AreEqual(definition.ContractName, expectedContractName);
718 Assert.AreEqual(definition.RequiredTypeIdentity, expectedRequiredTypeIdentity);
719 Assert.IsTrue(definition.RequiredMetadata.SequenceEqual(expectedRequiredMetadata));
720 Assert.AreEqual(definition.Cardinality, expectedCardinality);
721 Assert.AreEqual(definition.RequiredCreationPolicy, expectedCreationPolicy);
722 Assert.IsFalse(definition.IsRecomposable);
723 Assert.AreSame(expectedOrigin, ((ICompositionElement)definition).Origin);
724 Assert.IsTrue(definition.IsPrerequisite);
727 [TestMethod]
728 public void CreateImportDefinition_Parameter_NullAsParamater_ShouldThrowArgumentNull()
730 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
731 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
733 string expectedContractName = "Foo";
734 string expectedRequiredTypeIdentity = "Bar";
735 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
736 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
737 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
739 ICompositionElement expectedOrigin = new MockOrigin();
741 ExceptionAssert.ThrowsArgument<ArgumentNullException>("parameter", () =>
743 ReflectionModelServices.CreateImportDefinition(
744 null,
745 expectedContractName,
746 expectedRequiredTypeIdentity,
747 expectedRequiredMetadata,
748 expectedCardinality,
749 expectedCreationPolicy,
750 expectedOrigin);
754 [TestMethod]
755 public void GetImportingParameter()
757 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
758 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
760 string expectedContractName = "Foo";
761 string expectedRequiredTypeIdentity = "Bar";
762 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
763 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
764 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
766 ICompositionElement expectedOrigin = new MockOrigin();
767 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
768 expectedLazyParameter,
769 expectedContractName,
770 expectedRequiredTypeIdentity,
771 expectedRequiredMetadata,
772 expectedCardinality,
773 expectedCreationPolicy,
774 expectedOrigin);
775 Assert.IsNotNull(importDefinition);
777 Lazy<ParameterInfo> lazyParameter = ReflectionModelServices.GetImportingParameter(importDefinition);
778 Assert.AreEqual(expectedLazyParameter, lazyParameter);
781 [TestMethod]
782 public void GetImportingParameter_NullAsImport_ShouldThrowArgumentNull()
784 ExceptionAssert.ThrowsArgument<ArgumentNullException>("importDefinition", () =>
786 ReflectionModelServices.GetImportingParameter(null);
790 [TestMethod]
791 public void GetImportingParameter_InvalidImport_ShouldThrowArgument()
793 ExceptionAssert.ThrowsArgument<ArgumentException>("importDefinition", () =>
795 ReflectionModelServices.GetImportingParameter(new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any));
799 [TestMethod]
800 public void IsImportingParameter_OnParameterImport()
802 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
803 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
805 string expectedContractName = "Foo";
806 string expectedRequiredTypeIdentity = "Bar";
807 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
808 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
809 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
811 ICompositionElement expectedOrigin = new MockOrigin();
812 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
813 expectedLazyParameter,
814 expectedContractName,
815 expectedRequiredTypeIdentity,
816 expectedRequiredMetadata,
817 expectedCardinality,
818 expectedCreationPolicy,
819 expectedOrigin);
820 Assert.IsNotNull(importDefinition);
822 Assert.IsTrue(ReflectionModelServices.IsImportingParameter(importDefinition));
825 [TestMethod]
826 public void IsImportingParameter_OnMemberImport()
828 PropertyInfo property = typeof(TestPart).GetProperties().First();
829 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
831 string expectedContractName = "Foo";
832 string expectedRequiredTypeIdentity = "Bar";
833 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
834 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
835 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
836 bool expectedRecomposable = true;
838 ICompositionElement expectedOrigin = new MockOrigin();
840 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
841 expectedLazyMember,
842 expectedContractName,
843 expectedRequiredTypeIdentity,
844 expectedRequiredMetadata,
845 expectedCardinality,
846 expectedRecomposable,
847 expectedCreationPolicy,
848 expectedOrigin);
849 Assert.IsNotNull(importDefinition);
851 Assert.IsFalse(ReflectionModelServices.IsImportingParameter(importDefinition));
854 [TestMethod]
855 public void IsImportingParameter_NullAsImport_ShouldThrowArgumentNull()
857 ExceptionAssert.ThrowsArgument<ArgumentNullException>("importDefinition", () =>
859 ReflectionModelServices.IsImportingParameter(null);
863 [TestMethod]
864 public void IsImportingParameter_InvalidImport_ShouldThrowArgument()
866 ExceptionAssert.ThrowsArgument<ArgumentException>("importDefinition", () =>
868 ReflectionModelServices.IsImportingParameter(new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any));
872 [TestMethod]
873 public void IsExportFactoryImportDefinition_NullImport_ShouldThrowArgumentNull()
875 ExceptionAssert.ThrowsArgumentNull("importDefinition", () =>
876 ReflectionModelServices.IsExportFactoryImportDefinition(null));
879 [TestMethod]
880 public void IsExportFactoryImportDefinition_InvalidImport_ShouldThrowArgument()
882 ExceptionAssert.ThrowsArgument("importDefinition", () =>
883 ReflectionModelServices.IsExportFactoryImportDefinition(CreateInvalidImport()));
886 [TestMethod]
887 public void IsExportFactoryImportDefinition_NonPartCreatorImport_ShouldReturnFalse()
889 var import = ReflectionModelServices.CreateImportDefinition(
890 new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ReflectionModelServicesTests) }), // bogus member
891 "Foo",
892 "Foo",
893 Enumerable.Empty<KeyValuePair<string, Type>>(),
894 ImportCardinality.ZeroOrMore,
895 false,
896 CreationPolicy.Any,
897 null);
899 Assert.IsFalse(ReflectionModelServices.IsExportFactoryImportDefinition(import));
902 [TestMethod]
903 public void IsExportFactoryImportDefinition_PartCreatorImport_ShouldReturnTrue()
905 var import = ReflectionModelServices.CreateImportDefinition(
906 new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ReflectionModelServicesTests) }), // bogus member
907 "Foo",
908 "Foo",
909 Enumerable.Empty<KeyValuePair<string, Type>>(),
910 ImportCardinality.ZeroOrMore,
911 false,
912 CreationPolicy.Any,
913 true, //isPartCreator
914 null);
916 Assert.IsTrue(ReflectionModelServices.IsExportFactoryImportDefinition(import));
919 [TestMethod]
920 public void GetPartCreatorProductImportDefinition_NullImport_ShouldThrowArgumentNull()
922 ExceptionAssert.ThrowsArgumentNull("importDefinition", () =>
923 ReflectionModelServices.GetPartCreatorProductImportDefinition(null));
926 [TestMethod]
927 public void GetPartCreatorProductImportDefinition_InvalidImport_ShouldThrowArgument()
929 ExceptionAssert.ThrowsArgument("importDefinition", () =>
930 ReflectionModelServices.GetPartCreatorProductImportDefinition(CreateInvalidImport()));
933 [TestMethod]
934 public void GetPartCreatorProductImportDefinition_()
939 [TestMethod]
940 public void GetPartCreatorProductImportDefinition_PartCreatorImport_()
942 LazyMemberInfo bogusMember = new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ReflectionModelServicesTests) });
943 var import = ReflectionModelServices.CreateImportDefinition(
944 bogusMember,
945 "Foo",
946 "Foo",
947 Enumerable.Empty<KeyValuePair<string, Type>>(),
948 ImportCardinality.ZeroOrMore,
949 false,
950 CreationPolicy.Any,
951 true, //isPartCreator
952 null);
954 var productImport = ReflectionModelServices.GetPartCreatorProductImportDefinition(import);
956 var import2 = ReflectionModelServices.CreateImportDefinition(
957 bogusMember,
958 productImport.ContractName,
959 productImport.RequiredTypeIdentity,
960 productImport.RequiredMetadata,
961 productImport.Cardinality,
962 productImport.IsRecomposable,
963 productImport.RequiredCreationPolicy,
964 true, //isPartCreator
965 null);
967 Assert.AreEqual(import.ContractName, import2.ContractName);
968 Assert.AreEqual(import.Cardinality, import2.Cardinality);
969 Assert.AreEqual(import.IsRecomposable, import2.IsRecomposable);
970 Assert.AreEqual(import.RequiredCreationPolicy, import2.RequiredCreationPolicy);
971 Assert.AreEqual(import.RequiredTypeIdentity, import2.RequiredTypeIdentity);
972 EnumerableAssert.AreEqual(import.RequiredMetadata, import2.RequiredMetadata);
975 private static IEnumerable<ImportDefinition> CreateInvalidImports()
977 yield return new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any);
980 private static ImportDefinition CreateInvalidImport()
982 return new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any);
985 private static IEnumerable<ExportDefinition> CreateInvalidExports()
987 yield return new ExportDefinition("Foo", null);
991 class InvalidPartDefinition : ComposablePartDefinition
993 public override ComposablePart CreatePart()
995 throw new NotImplementedException();
998 public override IEnumerable<ExportDefinition> ExportDefinitions
1000 get { throw new NotImplementedException(); }
1003 public override IEnumerable<ImportDefinition> ImportDefinitions
1005 get { throw new NotImplementedException(); }
1009 private static List<ImportDefinition> CreateImports(Type type)
1011 List<ImportDefinition> imports = new List<ImportDefinition>();
1012 foreach (PropertyInfo property in type.GetProperties())
1014 imports.Add(new ReflectionMemberImportDefinition(new LazyMemberInfo(property), "Contract", (string)null, new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) }, ImportCardinality.ZeroOrOne, true, CreationPolicy.Any, new TypeOrigin(type)));
1017 return imports;
1020 private static List<ExportDefinition> CreateExports(Type type)
1022 List<ExportDefinition> exports = new List<ExportDefinition>();
1023 foreach (PropertyInfo property in type.GetProperties())
1025 exports.Add(ReflectionModelServices.CreateExportDefinition(new LazyMemberInfo(property), "Contract", new Lazy<IDictionary<string, object>>(() => null), new TypeOrigin(type)));
1028 return exports;
1031 public class TestPart
1033 public TestPart(int arg1)
1037 public int field1;
1038 public string field2;
1039 public int Property1 { get; set; }
1040 public string Property2
1042 get { return null; }
1045 this.Event.Invoke(this, null);
1048 public event EventHandler Event;
1051 private class TypeOrigin : ICompositionElement
1053 private readonly Type _type;
1054 private readonly ICompositionElement _orgin;
1056 public TypeOrigin(Type type)
1057 : this(type, null)
1061 public TypeOrigin(Type type, ICompositionElement origin)
1063 this._type = type;
1064 this._orgin = origin;
1067 public string DisplayName
1071 return this._type.GetDisplayName();
1075 public ICompositionElement Origin
1079 return this._orgin;
1084 private class MockOrigin : ICompositionElement
1086 public string DisplayName
1088 get { throw new NotImplementedException(); }
1091 public ICompositionElement Origin
1093 get { throw new NotImplementedException(); }