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

AdaptingCollectionTests.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: 442cfd5968851497e6f829a5cb02085131291117 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.Composition.Factories;

namespace System.ComponentModel.Composition
{
    public class FilteringCollection<T, M> : AdaptingCollection<T, M>
    {
        public FilteringCollection(Func<Lazy<T, M>, bool> filter)
            : base(e => e.Where(filter))
        {
        }
    }

    public class OrderingCollection<T, M> : AdaptingCollection<T, M>
    {
        public OrderingCollection(Func<Lazy<T, M>, object> keySelector)
            : this(keySelector, false)
        {
        }

        public OrderingCollection(Func<Lazy<T, M>, object> keySelector, bool descending)
            : base(e => descending ? e.OrderByDescending(keySelector) : e.OrderBy(keySelector))
        {
        }
    }

    public class AdaptingCollection<T> : AdaptingCollection<T, IDictionary<string, object>>
    {
        public AdaptingCollection(Func<IEnumerable<Lazy<T, IDictionary<string, object>>>, 
                                       IEnumerable<Lazy<T, IDictionary<string, object>>>> adaptor)
            : base(adaptor)
        {
        }
    }

    public class AdaptingCollection<T, M> : ICollection<Lazy<T, M>>, INotifyCollectionChanged
    {
        private readonly List<Lazy<T, M>> _allItems = new List<Lazy<T, M>>();
        private readonly Func<IEnumerable<Lazy<T, M>>, IEnumerable<Lazy<T, M>>> _adaptor = null;
        private List<Lazy<T, M>> _adaptedItems = null;

        public AdaptingCollection() : this(null)
        {
        }

        public AdaptingCollection(Func<IEnumerable<Lazy<T, M>>, IEnumerable<Lazy<T, M>>> adaptor)
        {
            this._adaptor = adaptor;
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public void ReapplyAdaptor()
        {
            if (this._adaptedItems != null)
            {
                this._adaptedItems = null;
                this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }

        protected virtual IEnumerable<Lazy<T, M>> Adapt(IEnumerable<Lazy<T, M>> collection)
        {
            if (this._adaptor != null)
            {
                return this._adaptor.Invoke(collection);
            }

            return collection;
        }

        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChanged;

            if (collectionChanged != null)
            {
                collectionChanged.Invoke(this, e);
            }
        }

        private List<Lazy<T, M>> AdaptedItems
        {
            get
            {
                if (this._adaptedItems == null)
                {
                    this._adaptedItems = Adapt(this._allItems).ToList();
                }

                return this._adaptedItems;
            }
        }

        #region ICollection Implementation
        // Accessors work directly against adapted collection
        public bool Contains(Lazy<T, M> item)
        {
            return this.AdaptedItems.Contains(item);
        }

        public void CopyTo(Lazy<T, M>[] array, int arrayIndex)
        {
            this.AdaptedItems.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.AdaptedItems.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public IEnumerator<Lazy<T, M>> GetEnumerator()
        {
            return this.AdaptedItems.GetEnumerator();
        }

        Collections.IEnumerator Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        // Mutation methods work against complete collection
        // and then force a reset of the adapted collection
        public void Add(Lazy<T, M> item)
        {
            this._allItems.Add(item);
            ReapplyAdaptor();
        }

        public void Clear()
        {
            this._allItems.Clear();
            ReapplyAdaptor();
        }

        public bool Remove(Lazy<T, M> item)
        {
            bool removed = this._allItems.Remove(item);
            ReapplyAdaptor();
            return removed;
        }
        #endregion
    }

    [TestClass]
    public class AdaptingCollectionTests
    {
        public interface IContract { }
        public interface INetworkAwareMetadata
        {
            [DefaultValue(false)]
            bool RequiresOnline { get; }
        }

        [Export(typeof(IContract))]
        [ExportMetadata("RequiresOnline", true)]
        public class NetworkExport : IContract { }

        [Export(typeof(IContract))]
        public class NonNetworkExport : IContract { }

        public class FilterExports
        {
            public FilterExports()
            {
                this.OnlineOnly = new AdaptingCollection<IContract, INetworkAwareMetadata>(e =>
                    e.Where(p => p.Metadata.RequiresOnline));

                this.OnlineOnly2 = new FilteringCollection<IContract, INetworkAwareMetadata>(p => p.Metadata.RequiresOnline);
            }

            [ImportMany]
            public AdaptingCollection<IContract, INetworkAwareMetadata> OnlineOnly { get; set; }

            [ImportMany]
            public FilteringCollection<IContract, INetworkAwareMetadata> OnlineOnly2 { get; set; }
        }

        [TestMethod]
        public void TestFilteringImports()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(NetworkExport), typeof(NonNetworkExport));

            var filterExports = new FilterExports();
            container.ComposeParts(filterExports);

            Assert.AreEqual(1, filterExports.OnlineOnly.Count);
            Assert.AreEqual(1, filterExports.OnlineOnly2.Count);
        }

        public interface IOrderMetadata
        {
            [DefaultValue(Int32.MaxValue)]
            int Order { get; }
        }

        [Export(typeof(IContract))]
        [ExportMetadata("Order", 2)]
        public class BExport : IContract { }

        [Export(typeof(IContract))]
        [ExportMetadata("Order", 1)]
        public class AExport : IContract { }

        [Export(typeof(IContract))]
        public class CExport : IContract { }

        public class OrderExportsByMetadata
        {
            public OrderExportsByMetadata()
            {
                this.OrderedItems = new AdaptingCollection<IContract, IOrderMetadata>(e =>
                    e.OrderBy(p => p.Metadata.Order));

                this.OrderedItems2 = new OrderingCollection<IContract, IOrderMetadata>(p => p.Metadata.Order);
            }

            [ImportMany]
            public AdaptingCollection<IContract, IOrderMetadata> OrderedItems { get; set; }

            [ImportMany]
            public OrderingCollection<IContract, IOrderMetadata> OrderedItems2 { get; set; }
        }

        [TestMethod]
        public void TestOrderingImportsByMetadata()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(BExport), typeof(AExport), typeof(CExport));
            var orderExports = new OrderExportsByMetadata();

            container.ComposeParts(orderExports);

            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(0).Value, typeof(AExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(1).Value, typeof(BExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(2).Value, typeof(CExport));

            Assert.IsInstanceOfType(orderExports.OrderedItems2.ElementAt(0).Value, typeof(AExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems2.ElementAt(1).Value, typeof(BExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems2.ElementAt(2).Value, typeof(CExport));
        }

        public class OrderExportsByName
        {
            public OrderExportsByName(bool descending)
            {
                if (descending)
                {
                    this.OrderedItems = new AdaptingCollection<IContract>(e =>
                        e.OrderByDescending(p => p.Value.GetType().FullName));
                }
                else
                {
                    this.OrderedItems = new AdaptingCollection<IContract>(e =>
                        e.OrderBy(p => p.Value.GetType().FullName));
                }
            }

            [ImportMany]
            public AdaptingCollection<IContract> OrderedItems { get; set; }
        }


        [TestMethod]
        public void TestOrderingImportsByTypeName()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(BExport), typeof(AExport), typeof(CExport));
            var orderExports = new OrderExportsByName(false);

            container.ComposeParts(orderExports);

            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(0).Value, typeof(AExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(1).Value, typeof(BExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(2).Value, typeof(CExport));

            orderExports = new OrderExportsByName(true);

            container.ComposeParts(orderExports);

            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(0).Value, typeof(CExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(1).Value, typeof(BExport));
            Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(2).Value, typeof(AExport));
        }

        public interface IDynamicFilteredMetadata
        {
            bool Dynamic { get; }
        }

        [Export(typeof(IContract))]
        [ExportMetadata("Dynamic", true)]
        public class Dynamic1 : IContract { }

        [Export(typeof(IContract))]
        [ExportMetadata("Dynamic", true)]
        public class Dynamic2 : IContract { }

        [Export(typeof(IContract))]
        [ExportMetadata("Dynamic", false)]
        public class NonDynamic1 : IContract { }

        public class DynamicFilteredCollection<T, M> : AdaptingCollection<T, M> where M : IDynamicFilteredMetadata
        {
            public DynamicFilteredCollection()
            {
            }

            private bool _includeDynamic = false;
            public bool IncludeDynamic 
            {
                get { return this._includeDynamic; }
                set 
                {
                    if (this._includeDynamic != value)
                    {
                        this.ReapplyAdaptor();
                    }

                    this._includeDynamic = value;
                }
            }

            protected override IEnumerable<Lazy<T, M>> Adapt(IEnumerable<Lazy<T, M>> collection)
            {
                return collection.Where(p => !p.Metadata.Dynamic || IncludeDynamic);
            }
        }

        public class DynamicExports
        {
            [ImportMany]
            public DynamicFilteredCollection<IContract, IDynamicFilteredMetadata> DynamicCollection { get; set; }
        }

        [TestMethod]
        public void TestDyamicallyFilteringImports()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Dynamic1), typeof(Dynamic2), typeof(NonDynamic1));
            var dynamicExports = new DynamicExports();

            container.ComposeParts(dynamicExports);

            Assert.AreEqual(1, dynamicExports.DynamicCollection.Count);

            dynamicExports.DynamicCollection.IncludeDynamic = true;

            Assert.AreEqual(3, dynamicExports.DynamicCollection.Count);
        }

        public class DynamicExportsNoSubType
        {
            public DynamicExportsNoSubType()
            {
                this.DynamicCollection = new AdaptingCollection<IContract, IDynamicFilteredMetadata>(e =>
                    e.Where(p => !p.Metadata.Dynamic || this.IncludeDynamic));
            }

            private bool _includeDynamic = false;
            public bool IncludeDynamic
            {
                get { return this._includeDynamic; }
                set
                {
                    if (this._includeDynamic != value)
                    {
                        this.DynamicCollection.ReapplyAdaptor();
                    }

                    this._includeDynamic = value;
                }
            }

            [ImportMany]
            public AdaptingCollection<IContract, IDynamicFilteredMetadata> DynamicCollection { get; set; }
        }

        [TestMethod]
        public void TestDyamicallyFilteringNoSubTypeImports()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Dynamic1), typeof(Dynamic2), typeof(NonDynamic1));
            var dynamicExports = new DynamicExportsNoSubType();

            container.ComposeParts(dynamicExports);

            Assert.AreEqual(1, dynamicExports.DynamicCollection.Count);

            dynamicExports.IncludeDynamic = true;

            Assert.AreEqual(3, dynamicExports.DynamicCollection.Count);
        }
    }
}