1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
5 using System.Collections.Generic;
6 using System.ComponentModel.Composition.Primitives;
8 using System.Reflection;
9 using System.UnitTesting;
10 using Microsoft.Internal;
13 namespace System.ComponentModel.Composition.ReflectionModel
15 public class ReflectionModelServicesTests
18 public void CreatePartDefinition()
20 Type expectedType = typeof(TestPart);
21 Lazy<Type> expectedLazyType = expectedType.AsLazy();
22 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
23 expectedMetadata["Key1"] = 1;
24 expectedMetadata["Key2"] = "Value2";
26 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
27 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
29 ICompositionElement expectedOrigin = new MockOrigin();
31 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
32 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
33 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
34 expectedMetadata.AsLazy(), expectedOrigin);
35 Assert.NotNull(partDefinition);
37 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
38 Assert.NotNull(definition);
40 Assert.Same(expectedType, definition.GetPartType());
41 Assert.True(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
42 Assert.True(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
43 Assert.True(definition.ExportDefinitions.SequenceEqual(expectedExports.Cast<ExportDefinition>()));
44 Assert.True(definition.ImportDefinitions.SequenceEqual(expectedImports.Cast<ImportDefinition>()));
45 Assert.Same(expectedOrigin, ((ICompositionElement)definition).Origin);
46 Assert.NotNull(((ICompositionElement)definition).DisplayName);
47 Assert.False(definition.IsDisposalRequired);
51 public void CreatePartDefinition_Disposable()
53 Type expectedType = typeof(TestPart);
54 Lazy<Type> expectedLazyType = expectedType.AsLazy();
55 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
56 expectedMetadata["Key1"] = 1;
57 expectedMetadata["Key2"] = "Value2";
59 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
60 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
62 ICompositionElement expectedOrigin = new MockOrigin();
64 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, true,
65 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
66 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
67 expectedMetadata.AsLazy(), expectedOrigin);
68 Assert.NotNull(partDefinition);
70 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
71 Assert.NotNull(definition);
73 Assert.Same(expectedType, definition.GetPartType());
74 Assert.True(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
75 Assert.True(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
76 Assert.True(definition.ExportDefinitions.SequenceEqual(expectedExports.Cast<ExportDefinition>()));
77 Assert.True(definition.ImportDefinitions.SequenceEqual(expectedImports.Cast<ImportDefinition>()));
78 Assert.Same(expectedOrigin, ((ICompositionElement)definition).Origin);
79 Assert.NotNull(((ICompositionElement)definition).DisplayName);
80 Assert.True(definition.IsDisposalRequired);
84 public void CreatePartDefinition_NullMetadataAllowed()
86 Type expectedType = typeof(TestPart);
87 Lazy<Type> expectedLazyType = expectedType.AsLazy();
89 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
90 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
92 ICompositionElement expectedOrigin = new MockOrigin();
94 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
95 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
96 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
97 null, expectedOrigin);
98 Assert.NotNull(partDefinition);
100 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
101 Assert.NotNull(definition);
102 Assert.NotNull(definition.Metadata);
103 Assert.Equal(0, definition.Metadata.Count);
107 public void CreatePartDefinition_EvaluatedNullMetadataAllowed()
109 Type expectedType = typeof(TestPart);
110 Lazy<Type> expectedLazyType = expectedType.AsLazy();
111 IDictionary<string, object> expectedMetadata = null;
113 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
114 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
116 ICompositionElement expectedOrigin = new MockOrigin();
118 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
119 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
120 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
121 expectedMetadata.AsLazy(), expectedOrigin);
122 Assert.NotNull(partDefinition);
124 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
125 Assert.NotNull(definition);
126 Assert.NotNull(definition.Metadata);
127 Assert.Equal(0, definition.Metadata.Count);
131 public void CreatePartDefinition_NullExportsAllowed()
133 Type expectedType = typeof(TestPart);
134 Lazy<Type> expectedLazyType = expectedType.AsLazy();
136 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
137 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
138 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
140 ICompositionElement expectedOrigin = new MockOrigin();
142 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
143 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
145 expectedMetadata.AsLazy(), expectedOrigin);
146 Assert.NotNull(partDefinition);
148 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
149 Assert.NotNull(definition);
150 Assert.NotNull(definition.ExportDefinitions);
151 Assert.Equal(0, definition.ExportDefinitions.Count());
155 public void CreatePartDefinition_EvaluatedNullExportsAllowed()
157 Type expectedType = typeof(TestPart);
158 Lazy<Type> expectedLazyType = expectedType.AsLazy();
160 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
161 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
162 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
164 ICompositionElement expectedOrigin = new MockOrigin();
166 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
167 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
168 new Lazy<IEnumerable<ExportDefinition>>(() => null),
169 expectedMetadata.AsLazy(), expectedOrigin);
170 Assert.NotNull(partDefinition);
172 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
173 Assert.NotNull(definition);
174 Assert.NotNull(definition.ExportDefinitions);
175 Assert.Equal(0, definition.ExportDefinitions.Count());
179 public void CreatePartDefinition_ExportsMustBeOfRightType()
181 Type expectedType = typeof(TestPart);
182 Lazy<Type> expectedLazyType = expectedType.AsLazy();
184 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
185 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
186 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
188 ICompositionElement expectedOrigin = new MockOrigin();
190 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
191 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
192 new Lazy<IEnumerable<ExportDefinition>>(() => CreateInvalidExports()),
193 expectedMetadata.AsLazy(), expectedOrigin);
194 Assert.NotNull(partDefinition);
196 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
197 Assert.NotNull(definition);
199 ExceptionAssert.Throws<InvalidOperationException>(() =>
201 definition.ExportDefinitions.Count();
206 public void CreatePartDefinition_NullImportsAllowed()
208 Type expectedType = typeof(TestPart);
209 Lazy<Type> expectedLazyType = expectedType.AsLazy();
211 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
212 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
213 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
215 ICompositionElement expectedOrigin = new MockOrigin();
217 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
219 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
220 expectedMetadata.AsLazy(), expectedOrigin);
221 Assert.NotNull(partDefinition);
223 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
224 Assert.NotNull(definition);
225 Assert.NotNull(definition.ImportDefinitions);
226 Assert.Equal(0, definition.ImportDefinitions.Count());
230 public void CreatePartDefinition_EvaluatedNullImportsAllowed()
232 Type expectedType = typeof(TestPart);
233 Lazy<Type> expectedLazyType = expectedType.AsLazy();
235 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
236 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
237 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
239 ICompositionElement expectedOrigin = new MockOrigin();
241 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
242 new Lazy<IEnumerable<ImportDefinition>>(() => null),
243 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
244 expectedMetadata.AsLazy(), expectedOrigin);
245 Assert.NotNull(partDefinition);
247 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
248 Assert.NotNull(definition);
249 Assert.NotNull(definition.ImportDefinitions);
250 Assert.Equal(0, definition.ImportDefinitions.Count());
254 public void CreatePartDefinition_ImportsMustBeOfRightType()
256 Type expectedType = typeof(TestPart);
257 Lazy<Type> expectedLazyType = expectedType.AsLazy();
259 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
260 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
261 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
263 ICompositionElement expectedOrigin = new MockOrigin();
265 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
266 new Lazy<IEnumerable<ImportDefinition>>(() => CreateInvalidImports()),
267 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
268 expectedMetadata.AsLazy(), expectedOrigin);
269 Assert.NotNull(partDefinition);
271 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
272 Assert.NotNull(definition);
273 ExceptionAssert.Throws<InvalidOperationException>(() =>
275 definition.ImportDefinitions.Count();
281 public void CreatePartDefinition_NullTypeNotAllowed()
283 Type expectedType = typeof(TestPart);
284 Lazy<Type> expectedLazyType = expectedType.AsLazy();
285 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
286 expectedMetadata["Key1"] = 1;
287 expectedMetadata["Key2"] = "Value2";
289 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
290 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
292 ICompositionElement expectedOrigin = new MockOrigin();
294 Assert.Throws<ArgumentNullException>("partType", () =>
296 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(null, false,
297 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
298 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
299 expectedMetadata.AsLazy(), expectedOrigin);
304 public void CreatePartDefinition_NullEvaluatedTypeNotAllowed()
306 Type expectedType = typeof(TestPart);
307 Lazy<Type> expectedLazyType = expectedType.AsLazy();
309 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
310 expectedMetadata["Key1"] = 1;
311 expectedMetadata["Key2"] = "Value2";
313 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
314 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
316 ICompositionElement expectedOrigin = new MockOrigin();
318 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(new Lazy<Type>(() => null), false,
319 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
320 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
321 expectedMetadata.AsLazy(), expectedOrigin);
323 ReflectionComposablePartDefinition definition = partDefinition as ReflectionComposablePartDefinition;
324 Assert.NotNull(definition);
326 ExceptionAssert.Throws<InvalidOperationException>(() =>
328 definition.GetPartType();
333 public void GetPartType()
335 Type expectedType = typeof(TestPart);
336 Lazy<Type> expectedLazyType = expectedType.AsLazy();
337 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
338 expectedMetadata["Key1"] = 1;
339 expectedMetadata["Key2"] = "Value2";
341 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
342 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
344 ICompositionElement expectedOrigin = new MockOrigin();
346 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
347 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
348 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
349 expectedMetadata.AsLazy(), expectedOrigin);
350 Assert.NotNull(partDefinition);
352 Lazy<Type> lazyPartType = ReflectionModelServices.GetPartType(partDefinition);
353 Assert.Equal(expectedLazyType, lazyPartType);
357 public void GetPartType_NullAsPart_ShouldThrowArgumentNull()
359 Assert.Throws<ArgumentNullException>("partDefinition", () =>
361 ReflectionModelServices.GetPartType(null);
366 public void GetPartType_InvalidPart_ShouldThrowArgument()
368 Assert.Throws<ArgumentException>("partDefinition", () =>
370 ReflectionModelServices.GetPartType(new InvalidPartDefinition());
375 public void IsDisposalRequired_ForNonDisposable()
377 Type expectedType = typeof(TestPart);
378 Lazy<Type> expectedLazyType = expectedType.AsLazy();
379 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
380 expectedMetadata["Key1"] = 1;
381 expectedMetadata["Key2"] = "Value2";
383 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
384 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
386 ICompositionElement expectedOrigin = new MockOrigin();
388 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, false,
389 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
390 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
391 expectedMetadata.AsLazy(), expectedOrigin);
392 Assert.NotNull(partDefinition);
394 bool isDisposalRequired = ReflectionModelServices.IsDisposalRequired(partDefinition);
395 Assert.False(isDisposalRequired);
399 public void IsDisposalRequired_ForDisposable()
401 Type expectedType = typeof(TestPart);
402 Lazy<Type> expectedLazyType = expectedType.AsLazy();
403 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
404 expectedMetadata["Key1"] = 1;
405 expectedMetadata["Key2"] = "Value2";
407 IEnumerable<ImportDefinition> expectedImports = CreateImports(expectedType);
408 IEnumerable<ExportDefinition> expectedExports = CreateExports(expectedType);
410 ICompositionElement expectedOrigin = new MockOrigin();
412 ComposablePartDefinition partDefinition = ReflectionModelServices.CreatePartDefinition(expectedLazyType, true,
413 new Lazy<IEnumerable<ImportDefinition>>(() => expectedImports),
414 new Lazy<IEnumerable<ExportDefinition>>(() => expectedExports),
415 expectedMetadata.AsLazy(), expectedOrigin);
416 Assert.NotNull(partDefinition);
418 bool isDisposalRequired = ReflectionModelServices.IsDisposalRequired(partDefinition);
419 Assert.True(isDisposalRequired);
423 public void IsDisposalRequired_NullAsPart_ShouldThrowArgumentNull()
425 Assert.Throws<ArgumentNullException>("partDefinition", () =>
427 ReflectionModelServices.IsDisposalRequired(null);
432 public void IsDisposalRequired_InvalidPart_ShouldThrowArgument()
434 Assert.Throws<ArgumentException>("partDefinition", () =>
436 ReflectionModelServices.IsDisposalRequired(new InvalidPartDefinition());
441 public void CreateExportDefinition()
443 PropertyInfo property = typeof(TestPart).GetProperties().First();
444 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
446 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
447 expectedMetadata["Key1"] = 1;
448 expectedMetadata["Key2"] = "Value2";
450 string expectedContractName = "Foo";
452 ICompositionElement expectedOrigin = new MockOrigin();
454 ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
455 Assert.NotNull(exportDefinition);
456 ReflectionMemberExportDefinition definition = exportDefinition as ReflectionMemberExportDefinition;
457 Assert.NotNull(definition);
459 Assert.Equal(expectedContractName, definition.ContractName);
460 Assert.True(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
461 Assert.True(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
462 Assert.Equal(expectedOrigin, ((ICompositionElement)definition).Origin);
463 Assert.Equal(expectedLazyMember, definition.ExportingLazyMember);
467 public void CreateExportDefinition_NullAsContractName_ThrowsNullArgument()
469 PropertyInfo property = typeof(TestPart).GetProperties().First();
470 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
472 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
473 expectedMetadata["Key1"] = 1;
474 expectedMetadata["Key2"] = "Value2";
476 ICompositionElement expectedOrigin = new MockOrigin();
478 Assert.Throws<ArgumentNullException>("contractName", () =>
480 ReflectionModelServices.CreateExportDefinition(expectedLazyMember, null, expectedMetadata.AsLazy(), expectedOrigin);
485 public void CreateExportDefinition_NullAsMetadata_Allowed()
487 PropertyInfo property = typeof(TestPart).GetProperties().First();
488 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
490 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
491 expectedMetadata["Key1"] = 1;
492 expectedMetadata["Key2"] = "Value2";
494 string expectedContractName = "Foo";
495 ICompositionElement expectedOrigin = new MockOrigin();
497 ExportDefinition definition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
498 Assert.NotNull(definition.Metadata);
499 Assert.Equal(2, definition.Metadata.Count);
503 public void CreateExportDefinition_InvalidLazymemberInfo_ShouldThrowArtument()
505 EventInfo _event = typeof(TestPart).GetEvents().First();
506 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(_event);
508 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
509 expectedMetadata["Key1"] = 1;
510 expectedMetadata["Key2"] = "Value2";
512 string expectedContractName = "Foo";
514 ICompositionElement expectedOrigin = new MockOrigin();
516 Assert.Throws<ArgumentException>("exportingMember", () =>
518 ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
523 public void GetExportingMember()
525 PropertyInfo property = typeof(TestPart).GetProperties().First();
526 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
528 IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
529 expectedMetadata["Key1"] = 1;
530 expectedMetadata["Key2"] = "Value2";
532 string expectedContractName = "Foo";
534 ICompositionElement expectedOrigin = new MockOrigin();
536 ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
537 Assert.NotNull(exportDefinition);
539 LazyMemberInfo lazyMember = ReflectionModelServices.GetExportingMember(exportDefinition);
540 Assert.Equal(expectedLazyMember, lazyMember);
544 public void GetExportingMember_NullAsExportDefinition_ShouldThrowArhumentNull()
546 Assert.Throws<ArgumentNullException>("exportDefinition", () =>
548 ReflectionModelServices.GetExportingMember(null);
553 public void GetExportingMember_InvalidExportDefinition_ShouldThrowArhumentNull()
555 Assert.Throws<ArgumentException>("exportDefinition", () =>
557 ReflectionModelServices.GetExportingMember(new ExportDefinition("Foo", null));
562 public void CreateImportDefinition_Member()
564 PropertyInfo property = typeof(TestPart).GetProperties().First();
565 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
567 string expectedContractName = "Foo";
568 string expectedRequiredTypeIdentity = "Bar";
569 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
570 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
571 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
572 bool expectedRecomposable = true;
574 ICompositionElement expectedOrigin = new MockOrigin();
576 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
578 expectedContractName,
579 expectedRequiredTypeIdentity,
580 expectedRequiredMetadata,
582 expectedRecomposable,
583 expectedCreationPolicy,
585 Assert.NotNull(importDefinition);
587 ReflectionMemberImportDefinition definition = importDefinition as ReflectionMemberImportDefinition;
588 Assert.NotNull(definition);
590 Assert.Equal(expectedLazyMember, definition.ImportingLazyMember);
591 Assert.Equal(definition.ContractName, expectedContractName);
592 Assert.Equal(definition.RequiredTypeIdentity, expectedRequiredTypeIdentity);
593 Assert.True(definition.RequiredMetadata.SequenceEqual(expectedRequiredMetadata));
594 Assert.Equal(definition.Cardinality, expectedCardinality);
595 Assert.Equal(definition.RequiredCreationPolicy, expectedCreationPolicy);
596 Assert.Equal(definition.IsRecomposable, expectedRecomposable);
597 Assert.Same(expectedOrigin, ((ICompositionElement)definition).Origin);
598 Assert.False(definition.IsPrerequisite);
602 public void CreateImportDefinition_Member_InvalidMember_ShouldThrowArgument()
604 MethodInfo method = typeof(TestPart).GetMethods().First();
605 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(method);
607 string expectedContractName = "Foo";
608 string expectedRequiredTypeIdentity = "Bar";
609 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
610 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
611 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
612 bool expectedRecomposable = true;
614 ICompositionElement expectedOrigin = new MockOrigin();
616 Assert.Throws<ArgumentException>("importingMember", () =>
618 ReflectionModelServices.CreateImportDefinition(
620 expectedContractName,
621 expectedRequiredTypeIdentity,
622 expectedRequiredMetadata,
624 expectedRecomposable,
625 expectedCreationPolicy,
631 public void GetImporingMember()
633 PropertyInfo property = typeof(TestPart).GetProperties().First();
634 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
636 string expectedContractName = "Foo";
637 string expectedRequiredTypeIdentity = "Bar";
638 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
639 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
640 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
641 bool expectedRecomposable = true;
643 ICompositionElement expectedOrigin = new MockOrigin();
645 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
647 expectedContractName,
648 expectedRequiredTypeIdentity,
649 expectedRequiredMetadata,
651 expectedRecomposable,
652 expectedCreationPolicy,
654 Assert.NotNull(importDefinition);
656 LazyMemberInfo lazyMember = ReflectionModelServices.GetImportingMember(importDefinition);
657 Assert.Equal(expectedLazyMember, lazyMember);
661 public void GetImporingMember_NullAsImport_ShouldThrowArgumentNull()
663 Assert.Throws<ArgumentNullException>("importDefinition", () =>
665 ReflectionModelServices.GetImportingMember(null);
670 public void GetImporingMember_InvalidImport_ShouldThrowArgument()
672 Assert.Throws<ArgumentException>("importDefinition", () =>
674 ReflectionModelServices.GetImportingMember(new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any));
679 public void CreateImportDefinition_Parameter()
682 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
683 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
685 string expectedContractName = "Foo";
686 string expectedRequiredTypeIdentity = "Bar";
687 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
688 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
689 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
691 ICompositionElement expectedOrigin = new MockOrigin();
693 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
694 expectedLazyParameter,
695 expectedContractName,
696 expectedRequiredTypeIdentity,
697 expectedRequiredMetadata,
699 expectedCreationPolicy,
701 Assert.NotNull(importDefinition);
703 ReflectionParameterImportDefinition definition = importDefinition as ReflectionParameterImportDefinition;
704 Assert.NotNull(definition);
706 Assert.Equal(expectedLazyParameter, definition.ImportingLazyParameter);
707 Assert.Equal(definition.ContractName, expectedContractName);
708 Assert.Equal(definition.RequiredTypeIdentity, expectedRequiredTypeIdentity);
709 Assert.True(definition.RequiredMetadata.SequenceEqual(expectedRequiredMetadata));
710 Assert.Equal(definition.Cardinality, expectedCardinality);
711 Assert.Equal(definition.RequiredCreationPolicy, expectedCreationPolicy);
712 Assert.False(definition.IsRecomposable);
713 Assert.Same(expectedOrigin, ((ICompositionElement)definition).Origin);
714 Assert.True(definition.IsPrerequisite);
718 public void CreateImportDefinition_Parameter_NullAsParamater_ShouldThrowArgumentNull()
720 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
721 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
723 string expectedContractName = "Foo";
724 string expectedRequiredTypeIdentity = "Bar";
725 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
726 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
727 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
729 ICompositionElement expectedOrigin = new MockOrigin();
731 Assert.Throws<ArgumentNullException>("parameter", () =>
733 ReflectionModelServices.CreateImportDefinition(
735 expectedContractName,
736 expectedRequiredTypeIdentity,
737 expectedRequiredMetadata,
739 expectedCreationPolicy,
745 public void GetImportingParameter()
747 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
748 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
750 string expectedContractName = "Foo";
751 string expectedRequiredTypeIdentity = "Bar";
752 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
753 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
754 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
756 ICompositionElement expectedOrigin = new MockOrigin();
757 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
758 expectedLazyParameter,
759 expectedContractName,
760 expectedRequiredTypeIdentity,
761 expectedRequiredMetadata,
763 expectedCreationPolicy,
765 Assert.NotNull(importDefinition);
767 Lazy<ParameterInfo> lazyParameter = ReflectionModelServices.GetImportingParameter(importDefinition);
768 Assert.Equal(expectedLazyParameter, lazyParameter);
772 public void GetImportingParameter_NullAsImport_ShouldThrowArgumentNull()
774 Assert.Throws<ArgumentNullException>("importDefinition", () =>
776 ReflectionModelServices.GetImportingParameter(null);
781 public void GetImportingParameter_InvalidImport_ShouldThrowArgument()
783 Assert.Throws<ArgumentException>("importDefinition", () =>
785 ReflectionModelServices.GetImportingParameter(new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any));
790 public void IsImportingParameter_OnParameterImport()
792 ParameterInfo parameter = typeof(TestPart).GetConstructor(new Type[] { typeof(int) }).GetParameters()[0];
793 Lazy<ParameterInfo> expectedLazyParameter = parameter.AsLazy();
795 string expectedContractName = "Foo";
796 string expectedRequiredTypeIdentity = "Bar";
797 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
798 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
799 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
801 ICompositionElement expectedOrigin = new MockOrigin();
802 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
803 expectedLazyParameter,
804 expectedContractName,
805 expectedRequiredTypeIdentity,
806 expectedRequiredMetadata,
808 expectedCreationPolicy,
810 Assert.NotNull(importDefinition);
812 Assert.True(ReflectionModelServices.IsImportingParameter(importDefinition));
816 public void IsImportingParameter_OnMemberImport()
818 PropertyInfo property = typeof(TestPart).GetProperties().First();
819 LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);
821 string expectedContractName = "Foo";
822 string expectedRequiredTypeIdentity = "Bar";
823 KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
824 ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
825 CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
826 bool expectedRecomposable = true;
828 ICompositionElement expectedOrigin = new MockOrigin();
830 ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
832 expectedContractName,
833 expectedRequiredTypeIdentity,
834 expectedRequiredMetadata,
836 expectedRecomposable,
837 expectedCreationPolicy,
839 Assert.NotNull(importDefinition);
841 Assert.False(ReflectionModelServices.IsImportingParameter(importDefinition));
845 public void IsImportingParameter_NullAsImport_ShouldThrowArgumentNull()
847 Assert.Throws<ArgumentNullException>("importDefinition", () =>
849 ReflectionModelServices.IsImportingParameter(null);
854 public void IsImportingParameter_InvalidImport_ShouldThrowArgument()
856 Assert.Throws<ArgumentException>("importDefinition", () =>
858 ReflectionModelServices.IsImportingParameter(new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any));
863 public void IsExportFactoryImportDefinition_NullImport_ShouldThrowArgumentNull()
865 Assert.Throws<ArgumentNullException>("importDefinition", () =>
866 ReflectionModelServices.IsExportFactoryImportDefinition(null));
870 public void IsExportFactoryImportDefinition_InvalidImport_ShouldReturnFalse()
872 Assert.False(ReflectionModelServices.IsExportFactoryImportDefinition(CreateInvalidImport()));
876 public void IsExportFactoryImportDefinition_NonPartCreatorImport_ShouldReturnFalse()
878 var import = ReflectionModelServices.CreateImportDefinition(
879 new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ReflectionModelServicesTests) }), // bogus member
882 Enumerable.Empty<KeyValuePair<string, Type>>(),
883 ImportCardinality.ZeroOrMore,
888 Assert.False(ReflectionModelServices.IsExportFactoryImportDefinition(import));
892 public void IsExportFactoryImportDefinition_PartCreatorImport_ShouldReturnTrue()
894 var import = ReflectionModelServices.CreateImportDefinition(
895 new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ReflectionModelServicesTests) }), // bogus member
898 Enumerable.Empty<KeyValuePair<string, Type>>(),
899 ImportCardinality.ZeroOrMore,
902 MetadataServices.EmptyMetadata,
903 true, //isPartCreator
906 Assert.True(ReflectionModelServices.IsExportFactoryImportDefinition(import));
910 public void GetExportFactoryProductImportDefinition_NullImport_ShouldThrowArgumentNull()
912 Assert.Throws<ArgumentNullException>("importDefinition", () =>
913 ReflectionModelServices.GetExportFactoryProductImportDefinition(null));
917 public void GetExportFactoryProductImportDefinition_InvalidImport_ShouldThrowArgument()
919 Assert.Throws<ArgumentException>("importDefinition", () =>
920 ReflectionModelServices.GetExportFactoryProductImportDefinition(CreateInvalidImport()));
924 public void GetExportFactoryProductImportDefinition_()
930 public void GetExportFactoryProductImportDefinition_PartCreatorImport_()
932 LazyMemberInfo bogusMember = new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ReflectionModelServicesTests) });
933 var import = ReflectionModelServices.CreateImportDefinition(
937 Enumerable.Empty<KeyValuePair<string, Type>>(),
938 ImportCardinality.ZeroOrMore,
942 true, //isPartCreator
945 var productImport = ReflectionModelServices.GetExportFactoryProductImportDefinition(import);
947 var import2 = ReflectionModelServices.CreateImportDefinition(
949 productImport.ContractName,
950 productImport.RequiredTypeIdentity,
951 productImport.RequiredMetadata,
952 productImport.Cardinality,
953 productImport.IsRecomposable,
954 productImport.RequiredCreationPolicy,
955 productImport.Metadata,
956 true, //isPartCreator
959 Assert.Equal(import.ContractName, import2.ContractName);
960 Assert.Equal(import.Cardinality, import2.Cardinality);
961 Assert.Equal(import.IsRecomposable, import2.IsRecomposable);
962 Assert.Equal(import.RequiredCreationPolicy, import2.RequiredCreationPolicy);
963 Assert.Equal(import.RequiredTypeIdentity, import2.RequiredTypeIdentity);
964 EnumerableAssert.AreEqual(import.RequiredMetadata, import2.RequiredMetadata);
967 private static IEnumerable<ImportDefinition> CreateInvalidImports()
969 yield return new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any);
972 private static ImportDefinition CreateInvalidImport()
974 return new ContractBasedImportDefinition("Foo", "Foo", null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any);
977 private static IEnumerable<ExportDefinition> CreateInvalidExports()
979 yield return new ExportDefinition("Foo", null);
982 class InvalidPartDefinition : ComposablePartDefinition
984 public override ComposablePart CreatePart()
986 throw new NotImplementedException();
989 public override IEnumerable<ExportDefinition> ExportDefinitions
991 get { throw new NotImplementedException(); }
994 public override IEnumerable<ImportDefinition> ImportDefinitions
996 get { throw new NotImplementedException(); }
1000 private static List<ImportDefinition> CreateImports(Type type)
1002 List<ImportDefinition> imports = new List<ImportDefinition>();
1003 foreach (PropertyInfo property in type.GetProperties())
1005 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, false, CreationPolicy.Any, MetadataServices.EmptyMetadata, new TypeOrigin(type)));
1011 private static List<ExportDefinition> CreateExports(Type type)
1013 List<ExportDefinition> exports = new List<ExportDefinition>();
1014 foreach (PropertyInfo property in type.GetProperties())
1016 exports.Add(ReflectionModelServices.CreateExportDefinition(new LazyMemberInfo(property), "Contract", new Lazy<IDictionary<string, object>>(() => null), new TypeOrigin(type)));
1022 public class TestPart
1024 public TestPart(int arg1)
1029 public string field2;
1030 public int Property1 { get; set; }
1031 public string Property2
1033 get { return null; }
1036 this.Event.Invoke(this, null);
1039 public event EventHandler Event;
1042 private class TypeOrigin : ICompositionElement
1044 private readonly Type _type;
1045 private readonly ICompositionElement _orgin;
1047 public TypeOrigin(Type type)
1052 public TypeOrigin(Type type, ICompositionElement origin)
1055 this._orgin = origin;
1058 public string DisplayName
1062 return this._type.GetDisplayName();
1066 public ICompositionElement Origin
1075 private class MockOrigin : ICompositionElement
1077 public string DisplayName
1079 get { throw new NotImplementedException(); }
1082 public ICompositionElement Origin
1084 get { throw new NotImplementedException(); }