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

ComposablePartExtensibilityTests.cs « Composition « ComponentModel « 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: a1950429b7e72e699403bdaacc2fa6bdd58c1b10 (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
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.Composition.Factories;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.ComponentModel.Composition.UnitTesting;
using System.UnitTesting;

namespace System.ComponentModel.Composition
{
    [TestClass]
    public class ComposablePartExtensibilityTests
    {
        [TestMethod]
        public void PhaseTest()
        {
            CompositionContainer container = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();

            var part = new OrderingTestComposablePart();
            part.AddImport("Import1", ImportCardinality.ExactlyOne, true, false);
            part.AddExport("Export1", 1);
            part.CallOrder.Enqueue("Import:Import1");
            part.CallOrder.Enqueue("OnComposed");

            batch.AddExportedValue("Import1", 20);
            batch.AddPart(part);
            container.Compose(batch);

            // Export shouldn't be called until it is pulled on by someone.
            var export = container.GetExport<object>("Export1");

            part.CallOrder.Enqueue("Export:Export1");
            Assert.AreEqual(1, export.Value);

            Assert.IsTrue(part.CallOrder.Count == 0);
        }

        [TestMethod]
        public void ImportTest()
        {
            var exporter = new TestExportBinder();
            var importer = new TestImportBinder();

            CompositionContainer container = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(importer);
            batch.AddPart(exporter);
            container.Compose(batch);

            ExportsAssert.AreEqual(importer.SetImports["single"], 42);
            ExportsAssert.AreEqual(importer.SetImports["multi"], 1, 2, 3);
        }

        [TestMethod]
        public void ConstructorInjectionSimpleCase()
        {
            var container = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(new ConstructorInjectionComposablePart(typeof(Foo)));
            batch.AddExportedValue<IBar>(new Bar("Bar Value"));
            container.Compose(batch);

            var import = container.GetExport<Foo>();
            var foo = import.Value;

            Assert.AreEqual("Bar Value", foo.Bar.Value);
        }

        [TestMethod]
        public void ConstructorInjectionCycle()
        {
            var container = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(new ConstructorInjectionComposablePart(typeof(AClass)));
            batch.AddPart(new ConstructorInjectionComposablePart(typeof(BClass)));

            CompositionAssert.ThrowsErrors(ErrorId.ImportEngine_PartCannotSetImport,
                                           ErrorId.ImportEngine_PartCannotSetImport, RetryMode.DoNotRetry, () =>
            {
                container.Compose(batch);
            });
        }
    }

    internal class OrderingTestComposablePart : ConcreteComposablePart
    {
        public Queue<string> CallOrder = new Queue<string>();

        public OrderingTestComposablePart()
        {
        }

        public new  void AddExport(string contractName, object value)
        {
            var export = ExportFactory.Create(contractName, () =>
            {
                this.OnGetExport(contractName); return value;
            });

            base.AddExport(export);
        }

        private void OnGetExport(string contractName)
        {
            Assert.AreEqual("Export:" + contractName, CallOrder.Dequeue());
        }

        public override void SetImport(ImportDefinition definition, IEnumerable<Export> exports)
        {
            ContractBasedImportDefinition contractBasedImportDefinition = (ContractBasedImportDefinition)definition;
            Assert.AreEqual("Import:" + contractBasedImportDefinition.ContractName, CallOrder.Dequeue());
            base.SetImport(definition, exports);
        }

        public override void Activate()
        {
            Assert.AreEqual("OnComposed", CallOrder.Dequeue());
            base.Activate();
        }
    }

    internal class TestExportBinder : ConcreteComposablePart
    {
        public TestExportBinder()
        {
            AddExport("single", 42);
            AddExport("multi", 1);
            AddExport("multi", 2);
            AddExport("multi", 3);
        }
    }

    internal class TestImportBinder : ConcreteComposablePart
    {
        public TestImportBinder()
        {
            AddImport("single", ImportCardinality.ExactlyOne, true, false);
            AddImport("multi", ImportCardinality.ZeroOrMore, true, false);
        }
    }

    public class Foo
    {
        public Foo(IBar bar)
        {
            Bar = bar;
        }

        public IBar Bar { get; private set; }
    }

    public interface IBar
    {
        string Value { get; }
    }

    public class Bar : IBar
    {
        public Bar(string value)
        {
            Value = value;
        }
        public string Value { get; private set; }
    }

    public class FooBar
    {
        [Import("Foo")]
        public Foo Foo { get; set; }
    }

    public class AClass
    {
        public AClass(BClass b)
        {
        }

        public BClass B { get; private set; }
    }

    public class BClass
    {
        public BClass(AClass a)
        {
            this.A = a;
        }

        public AClass A { get; private set; }
    }

    internal class ConstructorInjectionComposablePart : ConcreteComposablePart
    {
        private Type _type;
        private ConstructorInfo _constructor;
        private Dictionary<ImportDefinition, object> _imports;
        private bool currentlyExecuting = false;

        public ConstructorInjectionComposablePart(Type type)
        {
            this._type = type;

            // Note that this just blindly takes the first constructor...
            this._constructor = this._type.GetConstructors().FirstOrDefault();
            Assert.IsNotNull(this._constructor);

            foreach (var param in this._constructor.GetParameters())
            {
                string name = AttributedModelServices.GetContractName(param.ParameterType);
                AddImport(name, ImportCardinality.ExactlyOne, true, true);
            }

            string contractName = AttributedModelServices.GetContractName(type);
            string typeIdentity = AttributedModelServices.GetTypeIdentity(type);
            var metadata = new Dictionary<string, object>();
            metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity);
            
            Export composableExport = ExportFactory.Create(
                contractName,
                metadata,
                GetInstance);
            this.AddExport(composableExport);

            this._imports = new Dictionary<ImportDefinition, object>();
        }

        private object GetInstance()
        {
            var result = CompositionResult.SucceededResult;

            // We only need this guard if we are pulling on the lazy exports during this call
            // but if we do the pulling in SetImport it isn't needed.
            //if (currentlyExecuting)
            //{
            //    var issue = CompositionError.Create("CM:CreationCycleDetected",
            //        "This failed because there is a creation cycle");
            //    return result.MergeIssue(issue).ToResult<object>(null);
            //}

            try
            {
                currentlyExecuting = true;

                List<object> constructorArgs = new List<object>();

                foreach (ImportDefinition import in this.ImportDefinitions
                    .Where(i => i.IsPrerequisite))
                {
                    object importValue;
                    if (!this._imports.TryGetValue(import, out importValue))
                    {
                        result = result.MergeError(CompositionError.Create(CompositionErrorId.ImportNotSetOnPart,
                            "The import '{0}' is required for construction of '{1}'", import.ToString(), _type.FullName));

                        continue;
                    }

                    constructorArgs.Add(importValue);
                }

                if (!result.Succeeded)
                {
                    throw new CompositionException(result.Errors);
                }

                object obj = this._constructor.Invoke(constructorArgs.ToArray());

                return obj;
            }
            finally
            {
                currentlyExecuting = false;
            }
        }

        public override void SetImport(ImportDefinition definition, IEnumerable<Export> exports)
        {
            _imports[definition] = exports.First().Value;
        }
    }
}