1
// -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
6 using System
.Collections
.Generic
;
7 using System
.Collections
.ObjectModel
;
8 using System
.ComponentModel
.Composition
.Factories
;
9 using System
.ComponentModel
.Composition
.Hosting
;
10 using System
.ComponentModel
.Composition
.Primitives
;
11 using System
.ComponentModel
.Composition
.UnitTesting
;
12 using System
.UnitTesting
;
13 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
15 namespace System
.ComponentModel
.Composition
18 public class ExportCollectionTests
20 public interface ICustomMetadata
22 bool PropertyName { get; }
28 public Collection
<Lazy
<object>> CollectionPlain { get; set; }
31 public Collection
<Lazy
<object, IDictionary
<string, object>>> CollectionPlainRawMetadata { get; set; }
33 [ImportMany("EmptyValue")]
34 public Collection
<Lazy
<object>> CollectionPlainEmpty { get; set; }
36 [ImportMany("EmptyValue")]
37 public Collection
<Lazy
<object, IDictionary
<string, object>>> CollectionPlainEmptyRawMetadata { get; set; }
40 public Collection
<Lazy
<int>> CollectionTyped { get; set; }
43 public Collection
<Lazy
<int, IDictionary
<string, object>>> CollectionTypedRawMetadata { get; set; }
45 [ImportMany("EmptyValue")]
46 public Collection
<Lazy
<int>> CollectionTypedEmpty { get; set; }
49 public Collection
<Lazy
<int, ICustomMetadata
>> CollectionTypedMetadata { get; set; }
51 [ImportMany("EmptyValue")]
52 public Collection
<Lazy
<int, ICustomMetadata
>> CollectionTypedMetadataEmpty { get; set; }
55 public IEnumerable
<int> ReadWriteEnumerable { get; set; }
57 [ImportMany("EmptyValue")]
58 public IEnumerable
<int> ReadWriteEnumerableEmpty { get; set; }
61 public IEnumerable
<Lazy
<object>> MetadataUntypedEnumerable { get; set; }
64 public IEnumerable
<Lazy
<object, IDictionary
<string, object>>> MetadataUntypedEnumerableRawMetadata { get; set; }
66 [ImportMany("EmptyValue")]
67 public IEnumerable
<Lazy
<object>> MetadataUntypedEnumerableEmpty { get; set; }
69 [ImportMany("EmptyValue")]
70 public IEnumerable
<Lazy
<object, IDictionary
<string, object>>> MetadataUntypedEnumerableEmptyRawMetadata { get; set; }
73 public IEnumerable
<Lazy
<int>> MetadataTypedEnumerable { get; set; }
76 public IEnumerable
<Lazy
<int, IDictionary
<string, object>>> MetadataTypedEnumerableRawMetadata { get; set; }
78 [ImportMany("EmptyValue")]
79 public IEnumerable
<Lazy
<int>> MetadataTypedEnumerableEmpty { get; set; }
82 public IEnumerable
<Lazy
<int, ICustomMetadata
>> MetadataFullyTypedEnumerable { get; set; }
84 [ImportMany("EmptyValue")]
85 public IEnumerable
<Lazy
<int, ICustomMetadata
>> MetadataFullyTypedEnumerableEmpty { get; set; }
87 public void VerifyImport(params int[] expectedValues
)
89 object[] untypedExpectedValues
= expectedValues
.Cast
<object>().ToArray();
91 ExportsAssert
.AreEqual(CollectionPlain
, untypedExpectedValues
);
92 ExportsAssert
.AreEqual(CollectionPlainRawMetadata
, untypedExpectedValues
);
93 EnumerableAssert
.IsTrueForAll(CollectionPlainRawMetadata
, i
=> true.Equals(i
.Metadata
["PropertyName"]));
94 EnumerableAssert
.IsEmpty(CollectionPlainEmpty
);
95 EnumerableAssert
.IsEmpty(CollectionPlainEmptyRawMetadata
);
97 // Add a new Export to this collection to ensure that it doesn't
98 // modifiy the other collections because they should each have there
99 // own collection instance
100 CollectionPlain
.Add(ExportFactory
.Create
<object>("Value"));
102 ExportsAssert
.AreEqual(CollectionTyped
, expectedValues
);
103 ExportsAssert
.AreEqual(CollectionTypedRawMetadata
, expectedValues
);
104 EnumerableAssert
.IsTrueForAll(CollectionTypedRawMetadata
, i
=> true.Equals(i
.Metadata
["PropertyName"]));
105 EnumerableAssert
.IsEmpty(CollectionTypedEmpty
);
107 ExportsAssert
.AreEqual(CollectionTypedMetadata
, expectedValues
);
108 #if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata
109 EnumerableAssert
.IsTrueForAll(CollectionTypedMetadata
, i
=> true == i
.Metadata
.PropertyName
);
110 #endif //!SILVERLIGHT
111 EnumerableAssert
.IsEmpty(CollectionTypedMetadataEmpty
);
113 EnumerableAssert
.AreEqual(ReadWriteEnumerable
, expectedValues
);
114 EnumerableAssert
.IsEmpty(ReadWriteEnumerableEmpty
);
116 ExportsAssert
.AreEqual(MetadataUntypedEnumerable
, untypedExpectedValues
);
117 ExportsAssert
.AreEqual(MetadataUntypedEnumerableRawMetadata
, untypedExpectedValues
);
118 EnumerableAssert
.IsTrueForAll(MetadataUntypedEnumerableRawMetadata
, i
=> true.Equals(i
.Metadata
["PropertyName"]));
119 EnumerableAssert
.IsEmpty(MetadataUntypedEnumerableEmpty
);
120 EnumerableAssert
.IsEmpty(MetadataUntypedEnumerableEmptyRawMetadata
);
122 ExportsAssert
.AreEqual(MetadataTypedEnumerable
, expectedValues
);
123 ExportsAssert
.AreEqual(MetadataTypedEnumerableRawMetadata
, expectedValues
);
124 EnumerableAssert
.IsTrueForAll(MetadataTypedEnumerableRawMetadata
, i
=> true.Equals(i
.Metadata
["PropertyName"]));
125 EnumerableAssert
.IsEmpty(MetadataTypedEnumerableEmpty
);
127 ExportsAssert
.AreEqual(MetadataFullyTypedEnumerable
, expectedValues
);
128 #if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata
129 EnumerableAssert
.IsTrueForAll(MetadataFullyTypedEnumerable
, i
=> true == i
.Metadata
.PropertyName
);
130 #endif //!SILVERLIGHT
131 EnumerableAssert
.IsEmpty(MetadataFullyTypedEnumerableEmpty
);
135 public class ExporterDefault21
137 public ExporterDefault21() { Value = 21; }
138 public ExporterDefault21(int v
) { Value = v; }
141 [ExportMetadata("PropertyName", true)]
142 public int Value { get; set; }
145 public class ExporterDefault42
147 public ExporterDefault42() { Value = 42; }
148 public ExporterDefault42(int v
) { Value = v; }
151 [ExportMetadata("PropertyName", true)]
152 public int Value { get; set; }
157 [TestProperty("Type", "Integration")]
158 public void ImportCollectionsFromContainerOnly()
160 var container
= ContainerFactory
.Create();
161 Importer importer
= new Importer();
163 CompositionBatch batch
= new CompositionBatch();
164 batch
.AddParts(importer
165 , new ExporterDefault21()
166 , new ExporterDefault21(22)
167 , new ExporterDefault42()
168 , new ExporterDefault42(43));
170 container
.Compose(batch
);
172 importer
.VerifyImport(21, 22, 42, 43);
176 [TestProperty("Type", "Integration")]
177 public void ImportCollectionsFromCatalogOnly()
179 var cat
= CatalogFactory
.CreateDefaultAttributed();
180 var container
= new CompositionContainer(cat
);
181 Importer importer
= new Importer();
183 CompositionBatch batch
= new CompositionBatch();
184 batch
.AddParts(importer
);
185 container
.Compose(batch
);
187 importer
.VerifyImport(21, 42);
191 [TestProperty("Type", "Integration")]
192 public void ImportCollectionsFormContainerAndCatalog()
194 var cat
= CatalogFactory
.CreateDefaultAttributed();
195 var container
= new CompositionContainer(cat
);
196 Importer importer
= new Importer();
198 CompositionBatch batch
= new CompositionBatch();
199 batch
.AddParts(importer
200 , new ExporterDefault21(22)
201 , new ExporterDefault42(43));
203 container
.Compose(batch
);
205 importer
.VerifyImport(22, 43, 21, 42);