[5a4bff]: / Implementation / APUnitTestFramework / DependencyResolver.cs  Maximize  Restore  History

Download this file

438 lines (373 with data), 15.8 kB

  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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using _3S.CoDeSys.Core;
using _3S.CoDeSys.Core.ComponentModel;
using _3S.CoDeSys.Core.Components;
using _3S.CoDeSys.Core.Views;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;

namespace _3S.APUnitTestFramework
{
    class DependencyResolver
    {
        public DependencyResolver(Testbed testbed, string profileName, Profile profile)
        {
            if (testbed == null)
                throw new ArgumentNullException(nameof(testbed));

            _testbed = testbed;

            var byTypeGuid = new Dictionary<Guid, IValueProvider>();
            var byItfType = new Dictionary<Type, List<IValueProvider>>();
            foreach (var kvp in testbed.Mocks)
            {
                var prototype = kvp.Value.CreatePrototypeFunc();
                var valueProvider = new MockValueProvider(kvp.Key, prototype, kvp.Value);
                byTypeGuid[kvp.Key] = valueProvider;

                foreach (var itfType in prototype.GetType().GetInterfaces())
                {
                    List<IValueProvider> valueProviderList;
                    byItfType.TryGetValue(itfType, out valueProviderList);
                    if (valueProviderList == null)
                    {
                        valueProviderList = new List<IValueProvider>();
                        byItfType[itfType] = valueProviderList;
                    }
                    valueProviderList.Add(valueProvider);
                }
            }

            _mockFactoriesByTypeGuid = byTypeGuid;
            _mockFactoriesByItfType = byItfType.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToArray());
            _profileName = profileName ?? ComponentManager.Singleton.ActiveProfileName;
            _profile = profile ?? ComponentManager.Singleton.ActiveProfile;
        }

        public IValueProvider ResolveSingle(Type itfType, bool optional, MemberInfo member)
        {
            IValueProvider[] results;
            _mockFactoriesByItfType.TryGetValue(itfType, out results);

            if (results == null)
            {
                // We did not find an implementation among the mocks.
                // Try the additional plug-ins, if any. This discovery is similar to the original
                // component model implementation. In the case of any doubts, look there.

                if (_systemTypeInfos == null)
                    _systemTypeInfos = ComponentManager.Singleton.GetSystemTypes();

                var itfTypeFullName = itfType.FullName;
                Guid systemTypeGuid;
                try
                {
                    var enum1 = Enumerable.Where(_systemTypeInfos, typeInfo => typeInfo.TypeName == itfTypeFullName || typeInfo.ItfTypesFast.Contains(itfTypeFullName));
                    var enum2 = Enumerable.Select(enum1, typeInfo => typeInfo.TypeGuid);
                    systemTypeGuid = Enumerable.SingleOrDefault(enum2);
                }
                catch
                {
                    var exception = new MultipleInstancesForInjectionException(member);
                    _testbed.RaiseDependencyError(exception, itfType, null);
                    return new ExceptionValueProvider(exception);
                }

                if (systemTypeGuid != Guid.Empty)
                    return new SystemInstanceValueProvider(systemTypeGuid);

                if (itfType.IsAssignableFrom(SystemInstances.MessageService.GetType()))
                    return new MessageServiceValueProvider();

                IValueProvider vp = null;
                foreach (var t in ComponentManager.Singleton.PlugInCache.GetTypesFast(itfTypeFullName, null))
                {
                    if (vp == null)
                    {
                        vp = new TypeGuidValueProvider(t);
                    }
                    else
                    {
                        var exception = new MultipleInstancesForInjectionException(member);
                        _testbed.RaiseDependencyError(exception, itfType, null);
                        return new ExceptionValueProvider(exception);
                    }
                }

                if (vp != null)
                    results = new[] { vp };
            }

            if (results == null && optional)
            {
                return new NullValueProvider();
            }
            else if (results == null)
            {
                var exception = new NoInstanceForInjectionException(member);
                _testbed.RaiseDependencyError(exception, itfType, null);
                return new ExceptionValueProvider(exception);
            }
            else if (results.Length == 1)
            {
                return results[0];
            }
            else
            {
                var exception = new MultipleInstancesForInjectionException(member);
                _testbed.RaiseDependencyError(exception, itfType, null);
                return new ExceptionValueProvider(exception);
            }
        }

        public IValueProvider ResolveSpecific(Guid typeGuid, Type itfType, bool optional, MemberInfo member)
        {
            IValueProvider result;
            _mockFactoriesByTypeGuid.TryGetValue(typeGuid, out result);

            if (result == null)
            {
                // We did not find an implementation among the mocks.
                // Try the additional plug-ins, if any. This discovery is similar to the original
                // component model implementation. In the case of any doubts, look there.

                var exists = false;
                try
                {
                    exists = ComponentManager.Singleton.TryGetPlugInType(typeGuid, true) != null;
                }
                catch { }

                if (exists)
                    return new TypeGuidValueProvider(typeGuid);
            }

            if (result == null && optional)
            {
                return new NullValueProvider();
            }
            else if (result == null)
            {
                var exception = new NoInstanceForInjectionException(member);
                _testbed.RaiseDependencyError(exception, itfType, typeGuid);
                return new ExceptionValueProvider(exception);
            }
            else
            {
                return result;
            }
        }

        public IEnumerable<IValueProvider> ResolveMultiple(Type itfType, bool optional, MemberInfo member)
        {
            IValueProvider[] results;
            _mockFactoriesByItfType.TryGetValue(itfType, out results);

            if (results == null)
            {
                // We did not find an implementation among the mocks.
                // Try the additional plug-ins, if any. This discovery is similar to the original
                // component model implementation. In the case of any doubts, look there.

                var typeGuids = ComponentManager.Singleton.PlugInCache.GetTypesFast(itfType.FullName, null);
                if (Enumerable.Any(typeGuids))
                    results = typeGuids.Select(typeGuid => new TypeGuidValueProvider(typeGuid)).ToArray();
            }

            if (results == null && optional)
            {
                return new IValueProvider[0];
            }
            else if (results == null)
            {
                var exception = new NoInstanceForInjectionException(member);
                _testbed.RaiseDependencyError(exception, itfType, null);
                return new ExceptionValueProvider(exception);
            }
            else
            {
                return results;
            }
        }

        public void ResolveProfile(MemberInfo member, out string profileName, out Profile profile)
        {
            profileName = _profileName;
            profile = _profile;
        }

        public IWinFormWrapper ResolveFrameForm(MemberInfo member)
        {
            if (_frameForm == null)
                _frameForm = ResolveSingle(typeof(IWinFormWrapper), true, member).CreateInstance<IWinFormWrapper>();

            return _frameForm;
        }

        private Testbed _testbed;
        private IDictionary<Guid, IValueProvider> _mockFactoriesByTypeGuid;
        private IDictionary<Type, IValueProvider[]> _mockFactoriesByItfType;
        private string _profileName;
        private Profile _profile;
        private IWinFormWrapper _frameForm;
        private TypeInformation[] _systemTypeInfos;
    }

    interface IValueProvider
    {
        Guid TypeGuid { get; }
        Type Type { get; }
        TypeInformation TypeInformation { get; }
        T CreateInstance<T>() where T : class;
    }

    class MockValueProvider : IValueProvider
    {
        public MockValueProvider(Guid typeGuid, object prototype, MockConstructor mockConstructor)
        {
            if (mockConstructor == null)
                throw new ArgumentNullException(nameof(mockConstructor));

            TypeGuid = typeGuid;
            Type = prototype.GetType();
            _mockConstructor = mockConstructor;

            // As the TypeInformation type does not have any suitable public constructors for us,
            // we must (partially) construct the instance using reflection and fake XML,
            // unfortunately.

            XmlDocument plugInInfoXmlDocument = new XmlDocument();
            plugInInfoXmlDocument.AppendChild(plugInInfoXmlDocument.CreateElement("ROOT"));
            plugInInfoXmlDocument.DocumentElement.SetAttribute("AssemblyName", Assembly.GetExecutingAssembly().FullName);
            plugInInfoXmlDocument.DocumentElement.SetAttribute("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            plugInInfoXmlDocument.DocumentElement.SetAttribute("GUID", XmlConvert.ToString(Guid.NewGuid()));
            plugInInfoXmlDocument.DocumentElement.SetAttribute("Version", "1.0.0.0");
            plugInInfoXmlDocument.DocumentElement.SetAttribute("System", XmlConvert.ToString(false));
            plugInInfoXmlDocument.DocumentElement.SetAttribute("Company", "The Unit Test Company");
            plugInInfoXmlDocument.DocumentElement.SetAttribute("Copyright", "Copyright © 2016 by The Unit Test Company. All rights reserved.");
            plugInInfoXmlDocument.DocumentElement.SetAttribute("Description", "Generated mock plug-in for the Unit Test Framework.");
            plugInInfoXmlDocument.DocumentElement.SetAttribute("Product", "CODESYS Automation Platform Unit Test Framework");
            plugInInfoXmlDocument.DocumentElement.SetAttribute("Title", "CODESYS Automation Platform Unit Test Framework");
            XmlElement typeElement = plugInInfoXmlDocument.CreateElement("Type");
            plugInInfoXmlDocument.DocumentElement.AppendChild(typeElement);
            typeElement.SetAttribute("GUID", XmlConvert.ToString(TypeGuid));
            typeElement.SetAttribute("Name", Type.FullName);
            typeElement.SetAttribute("ItfTypes", string.Join(";", prototype.GetType().GetInterfaces().Select(itf => itf.FullName)));

            var plugInInfoCtor = typeof(PlugInInformation).GetConstructor(
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                new[] { typeof(XmlElement) },
                null);
            var plugInInfo = (PlugInInformation)plugInInfoCtor.Invoke(new object[] { plugInInfoXmlDocument.DocumentElement });

            TypeInformation = plugInInfo.TypeInfosFast.First(); // We added exactly one type above, so we should able to get it back now.
        }

        public Guid TypeGuid { get; private set; }
        public Type Type { get; private set; }
        public TypeInformation TypeInformation { get; private set; }

        public T CreateInstance<T>() where T : class
        {
            if (_mockConstructor.SystemInstance)
            {
                if (_systemInstance == null)
                    _systemInstance = _mockConstructor.CreateFunc();
                return (T)_systemInstance;
            }
            else
            {
                return (T)_mockConstructor.CreateFunc();
            }
        }

        private MockConstructor _mockConstructor;
        private object _systemInstance;
    }

    class NullValueProvider : IValueProvider
    {
        public Guid TypeGuid
        {
            get { return Guid.Empty; }
        }

        public Type Type
        {
            get { return null; }
        }

        public TypeInformation TypeInformation
        {
            get { return null; }
        }

        public T CreateInstance<T>() where T : class
        {
            return null;
        }
    }

    class SystemInstanceValueProvider : IValueProvider
    {
        public SystemInstanceValueProvider(Guid typeGuid)
        {
            TypeGuid = typeGuid;
        }

        public Type Type
        {
            get { return ComponentManager.Singleton.TryGetPlugInType(TypeGuid, true); }
        }

        public Guid TypeGuid { get; private set; }

        public TypeInformation TypeInformation
        {
            get { return ComponentManager.Singleton.PlugInCache.GetTypeInformation(TypeGuid, null); }
        }

        public T CreateInstance<T>() where T : class
        {
            return ComponentManager.Singleton.TryGetSystemInstance<T>();
        }
    }

    class MessageServiceValueProvider : IValueProvider
    {
        public Type Type
        {
            get { return SystemInstances.MessageService.GetType(); }
        }

        public Guid TypeGuid
        {
            get
            {
                if (s_typeGuid == null)
                {
                    var attr = TypeGuidAttribute.FromObject(SystemInstances.MessageService);
                    s_typeGuid = attr != null ? attr.Guid : Guid.Empty;
                }
                return s_typeGuid.Value;
            }
        }

        private static Guid? s_typeGuid;

        public TypeInformation TypeInformation
        {
            get { return ComponentManager.Singleton.PlugInCache.GetTypeInformation(TypeGuid, null); }
        }

        public T CreateInstance<T>() where T : class
        {
            return (T)SystemInstances.MessageService;
        }
    }

    class TypeGuidValueProvider : IValueProvider
    {
        public TypeGuidValueProvider(Guid typeGuid)
        {
            TypeGuid = typeGuid;
        }

        public Type Type
        {
            get { return ComponentManager.Singleton.TryGetPlugInType(TypeGuid, true); }
        }

        public Guid TypeGuid { get; private set; }

        public TypeInformation TypeInformation
        {
            get { return ComponentManager.Singleton.PlugInCache.GetTypeInformation(TypeGuid, null); }
        }

        public T CreateInstance<T>() where T : class
        {
            return ComponentManager.Singleton.TryCreateInstance<T>(TypeGuid);
        }
    }

    class ExceptionValueProvider : IValueProvider, IEnumerable<IValueProvider>
    {
        public ExceptionValueProvider(Exception exception)
        {
            _exception = exception;
        }

        public Type Type
        {
            get { throw _exception; }
        }

        public Guid TypeGuid
        {
            get { throw _exception; }
        }

        public TypeInformation TypeInformation
        {
            get { throw _exception; }
        }

        public T CreateInstance<T>() where T : class
        {
            throw _exception;
        }

        public IEnumerator<IValueProvider> GetEnumerator()
        {
            throw _exception;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw _exception;
        }

        private Exception _exception;
    }
}