Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PartCreatorTests.cs « Integration « System « ComponentModelUnitTest « Tests « System.ComponentModel.Composition « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ce835664bb06feab5fe51444b2b55d677cfa06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.AttributedModel;
using System.ComponentModel.Composition.Primitives;
using System.ComponentModel.Composition.Factories;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.UnitTesting;
using System.Linq;
using System.Reflection;
using System.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.ComponentModel.Composition.ReflectionModel;

#if SILVERLIGHT

namespace Tests.Integration
{
    [TestClass]
    public class PartCreatorTests
    {
        public interface IId
        {
            int Id { get; }
        }

        public interface IIdTypeMetadata
        {
            string IdType { get; }
            string ExportTypeIdentity { get; }
        }


        [Export(typeof(IId))]
        [ExportMetadata("IdType", "PostiveIncrement")]
        public class UniqueExport : IId, IDisposable
        {
            private static int lastId = 0;

            public UniqueExport()
            {
                Id = lastId++;
            }

            public int Id { get; private set; }

            public void Dispose()
            {
                Id = -1;
            }
        }

        [Export]
        [CLSCompliant(false)]
        public class PartCreatorImporter
        {
            [ImportingConstructor]
            public PartCreatorImporter(
                PartCreator<IId> idCreatorTCtor, 
                PartCreator<IId, IIdTypeMetadata> idCreatorTMCtor)
            {
                this._idCreatorTCtor = idCreatorTCtor;
                this._idCreatorTMCtor = idCreatorTMCtor;
            }

            private PartCreator<IId> _idCreatorTCtor;
            private PartCreator<IId, IIdTypeMetadata> _idCreatorTMCtor;

            [Import(typeof(IId))]
            public PartCreator<IId> _idCreatorTField = null; // public so these can work on SL

            [Import]
            public PartCreator<IId, IIdTypeMetadata> _idCreatorTMField = null; // public so these can work on SL

            [Import]
            public PartCreator<IId> IdCreatorTProperty { get; set; }

            [Import(typeof(IId))]
            public PartCreator<IId, IIdTypeMetadata> IdCreatorTMProperty { get; set; }

            [ImportMany]
            public PartCreator<IId>[] IdCreatorsTProperty { get; set; }

            [ImportMany]
            public PartCreator<IId, IIdTypeMetadata>[] IdCreatorsTMProperty { get; set; }

            public void AssertValid()
            {
                var ids = new int[] 
                {
                    VerifyPartCreator(this._idCreatorTCtor),
                    VerifyPartCreator(this._idCreatorTMCtor),
                    VerifyPartCreator(this._idCreatorTField),
                    VerifyPartCreator(this._idCreatorTMField),
                    VerifyPartCreator(this.IdCreatorTProperty),
                    VerifyPartCreator(this.IdCreatorTMProperty),
                    VerifyPartCreator(this.IdCreatorsTProperty[0]),
                    VerifyPartCreator(this.IdCreatorsTMProperty[0])
                };

                Assert.AreEqual(1, this.IdCreatorsTProperty.Length, "Should only be one PartCreator");
                Assert.AreEqual(1, this.IdCreatorsTMProperty.Length, "Should only be one PartCreator");

                CollectionAssert.AllItemsAreUnique(ids, "There should be no duplicate ids");
            }

            private int VerifyPartCreator(PartCreator<IId> creator)
            {
                var val1 = creator.CreatePart();
                var val2 = creator.CreatePart();

                Assert.AreNotEqual(val1.ExportedValue, val2.ExportedValue, "Values should not be the same");
                Assert.AreNotEqual(val1.ExportedValue.Id, val2.ExportedValue.Id, "Value Ids should not be the same");

                Assert.IsTrue(val1.ExportedValue.Id >= 0, "Id should be positive");

                val1.Dispose();

                Assert.IsTrue(val1.ExportedValue.Id < 0, "Disposal of the value should set the id to negative");

                return creator.CreatePart().ExportedValue.Id;
            }

            private int VerifyPartCreator(PartCreator<IId, IIdTypeMetadata> creator)
            {
                var val = VerifyPartCreator((PartCreator<IId>)creator);

                Assert.AreEqual("PostiveIncrement", creator.Metadata.IdType, "IdType should be PositiveIncrement");
                Assert.AreEqual(AttributedModelServices.GetTypeIdentity(typeof(ComposablePartDefinition)), creator.Metadata.ExportTypeIdentity);

                return val;
            }
        }

        [TestMethod]
        public void PartCreatorStandardImports_ShouldWorkProperly()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(UniqueExport), typeof(PartCreatorImporter));
            var partCreatorImporter = container.GetExportedValue<PartCreatorImporter>();

            partCreatorImporter.AssertValid();
        }

        [Export]
        public class Foo : IDisposable
        {
            public bool IsDisposed { get; private set; }

            public void Dispose()
            {
                this.IsDisposed = true;
            }
        }

        [Export]
        public class SimplePartCreatorImporter
        {
            [Import]
            public PartCreator<Foo> FooFactory { get; set; }
        }

        [TestMethod]
        public void PartCreatorOfT_RecompositionSingle_ShouldBlockChanges()
        {
            var aggCat = new AggregateCatalog();
            var typeCat = new TypeCatalog(typeof(Foo));
            aggCat.Catalogs.Add(new TypeCatalog(typeof(SimplePartCreatorImporter)));
            aggCat.Catalogs.Add(typeCat);

            var container = new CompositionContainer(aggCat);

            var fooFactory = container.GetExportedValue<SimplePartCreatorImporter>();

            ExceptionAssert.Throws<ChangeRejectedException>(() =>
                aggCat.Catalogs.Remove(typeCat));

            ExceptionAssert.Throws<ChangeRejectedException>(() =>
                aggCat.Catalogs.Add(new TypeCatalog(typeof(Foo))));
        }

        [Export]
        public class ManyPartCreatorImporter
        {
            [ImportMany(AllowRecomposition = true)]
            public PartCreator<Foo>[] FooFactories { get; set; }
        }

        [TestMethod]
        public void FactoryOfT_RecompositionImportMany_ShouldSucceed()
        {
            var aggCat = new AggregateCatalog();
            var typeCat = new TypeCatalog(typeof(Foo));
            aggCat.Catalogs.Add(new TypeCatalog(typeof(ManyPartCreatorImporter)));
            aggCat.Catalogs.Add(typeCat);

            var container = new CompositionContainer(aggCat);

            var fooFactories = container.GetExportedValue<ManyPartCreatorImporter>();

            Assert.AreEqual(1, fooFactories.FooFactories.Length);

            aggCat.Catalogs.Add(new TypeCatalog(typeof(Foo)));

            Assert.AreEqual(2, fooFactories.FooFactories.Length);
        }

        public class PartCreatorExplicitCP
        {
            [Import(RequiredCreationPolicy = CreationPolicy.Any)]
            public PartCreator<Foo> FooCreatorAny { get; set; }

            [Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
            public PartCreator<Foo> FooCreatorNonShared { get; set; }

            [Import(RequiredCreationPolicy = CreationPolicy.Shared)]
            public PartCreator<Foo> FooCreatorShared { get; set; }

            [ImportMany(RequiredCreationPolicy = CreationPolicy.Any)]
            public PartCreator<Foo>[] FooCreatorManyAny { get; set; }

            [ImportMany(RequiredCreationPolicy = CreationPolicy.NonShared)]
            public PartCreator<Foo>[] FooCreatorManyNonShared { get; set; }

            [ImportMany(RequiredCreationPolicy = CreationPolicy.Shared)]
            public PartCreator<Foo>[] FooCreatorManyShared { get; set; }
        }

        [TestMethod]
        public void PartCreator_ExplicitCreationPolicy_CPShouldBeIgnored()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Foo));

            var part = new PartCreatorExplicitCP();

            container.SatisfyImportsOnce(part);

            // specifying the required creation policy explicit on the import 
            // of a PartCreator will be ignored because the PartCreator requires
            // the part it wraps to be either Any or NonShared to work properly.
            Assert.IsNotNull(part.FooCreatorAny);
            Assert.IsNotNull(part.FooCreatorNonShared);
            Assert.IsNotNull(part.FooCreatorShared);

            Assert.AreEqual(1, part.FooCreatorManyAny.Length);
            Assert.AreEqual(1, part.FooCreatorManyNonShared.Length);
            Assert.AreEqual(1, part.FooCreatorManyShared.Length);
        }

        public class PartCreatorImportRequiredMetadata
        {
            [ImportMany]
            public PartCreator<Foo>[] FooCreator { get; set; }

            [ImportMany]
            public PartCreator<Foo, IIdTypeMetadata>[] FooCreatorWithMetadata { get; set; }
        }

        [TestMethod]
        public void PartCreator_ImportRequiredMetadata_MissingMetadataShouldCauseImportToBeExcluded()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Foo));

            var part = new PartCreatorImportRequiredMetadata();

            container.SatisfyImportsOnce(part);

            Assert.AreEqual(1, part.FooCreator.Length, "Should contain the one Foo");
            Assert.AreEqual(0, part.FooCreatorWithMetadata.Length, "Should NOT contain Foo because it is missing the required Id metadata property");
        }

        [Export(typeof(Foo))]
        [PartCreationPolicy(CreationPolicy.Shared)]
        public class SharedFoo : Foo
        {
        }

        [TestMethod]
        public void PartCreator_ImportShouldNotImportSharedPart()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(SharedFoo));

            var foo = container.GetExportedValue<Foo>();
            Assert.IsNotNull(foo, "Ensure that a Foo actually exists in the container");

            var part = new PartCreatorImportRequiredMetadata();

            container.SatisfyImportsOnce(part);

            Assert.AreEqual(0, part.FooCreator.Length, "Should not contain the SharedFoo because the PartCreator should only wrap Any/NonShared parts");
        }


        [TestMethod]
        public void PartCreator_QueryContainerDirectly_ShouldWork()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Foo));

            var importDef = ReflectionModelServices.CreateImportDefinition(
                new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(PartCreatorTests) }), // Give it a bogus member
                AttributedModelServices.GetContractName(typeof(Foo)),
                AttributedModelServices.GetTypeIdentity(typeof(Foo)),
                Enumerable.Empty<KeyValuePair<string, Type>>(),
                ImportCardinality.ZeroOrMore,
                true,
                CreationPolicy.Any,
                true, // isPartCreator
                null);

            var exports = container.GetExports(importDef);

            var partCreator = exports.Single();

            // Manually walk the steps of using a raw part creator which is modeled as a PartDefinition with
            // a single ExportDefinition.
            var partDef = (ComposablePartDefinition)partCreator.Value;
            var part = partDef.CreatePart();
            var foo = (Foo)part.GetExportedValue(partDef.ExportDefinitions.Single());

            Assert.IsNotNull(foo);

            var foo1 = (Foo)part.GetExportedValue(partDef.ExportDefinitions.Single());
            Assert.AreEqual(foo, foo1, "Retrieving the exported value from the same part should return the same value");

            // creating a new part should result in getting a new exported value
            var part2 = partDef.CreatePart();
            var foo2 = (Foo)part2.GetExportedValue(partDef.ExportDefinitions.Single());

            Assert.AreNotEqual(foo, foo2, "New part should equate to a new exported value");

            // Disposing of part should cause foo to be disposed
            ((IDisposable)part).Dispose();
            Assert.IsTrue(foo.IsDisposed);
        }

        [Export]
        public class PartImporter<PartType>
        {
            [Import]
            public PartCreator<PartType> Creator { get; set; }
        }

        [Export]
        public class SimpleExport
        {
        }

        [TestMethod]
        public void PartCreator_SimpleRejectionRecurrection_ShouldWork()
        {
            var importTypeCat = new TypeCatalog(typeof(PartImporter<SimpleExport>));
            var aggCatalog = new AggregateCatalog(importTypeCat);
            var container = ContainerFactory.Create(aggCatalog);
            var exports = container.GetExports<PartImporter<SimpleExport>>();
            Assert.AreEqual(0, exports.Count());

            aggCatalog.Catalogs.Add(new TypeCatalog(typeof(SimpleExport)));

            exports = container.GetExports<PartImporter<SimpleExport>>();
            Assert.AreEqual(1, exports.Count());
        }
    }
}

#endif