PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/spikes/AGCompositeApplicationLibrary/AGComposite.UnityExtensions.Tests/UnityBootstrapperFixture.cs

#
C# | 567 lines | 438 code | 110 blank | 19 comment | 4 complexity | bf3f5758cabbb128bbaf149caa1bd5fc MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===============================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===============================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===============================================================================
 17
 18using System;
 19using System.Collections.Generic;
 20using System.Linq;
 21using System.Windows;
 22using System.Windows.Controls;
 23using System.Windows.Controls.Primitives;
 24using Microsoft.Practices.Composite.Events;
 25using Microsoft.Practices.Composite.Logging;
 26using Microsoft.Practices.Composite.Modularity;
 27using Microsoft.Practices.Composite.Regions;
 28using Microsoft.Practices.Composite.UnityExtensions.Tests.Mocks;
 29using Microsoft.Practices.Composite.Wpf.Regions;
 30using Microsoft.Practices.Unity;
 31using Microsoft.VisualStudio.TestTools.UnitTesting;
 32
 33namespace Microsoft.Practices.Composite.UnityExtensions.Tests
 34{
 35    [TestClass]
 36    public class UnityBootstrapperFixture
 37    {
 38        [TestMethod]
 39        public void CanCreateConcreteBootstrapper()
 40        {
 41            new DefaultBootstrapper();
 42        }
 43
 44        [TestMethod]
 45        public void CanRunBootstrapper()
 46        {
 47            var bootstrapper = new DefaultBootstrapper();
 48            bootstrapper.Run();
 49        }
 50
 51        [TestMethod]
 52        public void ShouldInitializeContainer()
 53        {
 54            var bootstrapper = new DefaultBootstrapper();
 55            var container = bootstrapper.GetBaseContainer();
 56
 57            Assert.IsNull(container);
 58
 59            bootstrapper.Run();
 60
 61            container = bootstrapper.GetBaseContainer();
 62
 63            Assert.IsNotNull(container);
 64            Assert.IsInstanceOfType(container, typeof(UnityContainer));
 65        }
 66
 67        [TestMethod]
 68        public void ShouldCallInitializeModules()
 69        {
 70            var bootstrapper = new DefaultBootstrapper();
 71            bootstrapper.Run();
 72
 73            Assert.IsTrue(bootstrapper.InitializeModulesCalled);
 74        }
 75
 76        [TestMethod]
 77        public void ShouldRegisterDefaultMappings()
 78        {
 79            var bootstrapper = new DefaultBootstrapper();
 80            bootstrapper.Run();
 81
 82            Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings);
 83            Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings.GetMapping(typeof(ItemsControl)));
 84            Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings.GetMapping(typeof(ContentControl)));
 85            // Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings.GetMapping(typeof(Selector)));
 86        }
 87
 88        [TestMethod]
 89        public void ShouldCallGetLogger()
 90        {
 91            var bootstrapper = new DefaultBootstrapper();
 92
 93            bootstrapper.Run();
 94
 95            Assert.IsTrue(bootstrapper.GetLoggerCalled);
 96        }
 97
 98        [TestMethod]
 99        public void ShouldCallGetEnumerator()
100        {
101            var bootstrapper = new DefaultBootstrapper();
102
103            bootstrapper.Run();
104
105            Assert.IsTrue(bootstrapper.GetEnumeratorCalled);
106        }
107
108        [TestMethod]
109        public void ShouldCallCreateSell()
110        {
111            var bootstrapper = new DefaultBootstrapper();
112
113            bootstrapper.Run();
114
115            Assert.IsTrue(bootstrapper.CreateShellCalled);
116        }
117
118        [TestMethod]
119        public void ShouldCallConfigureTypeMappings()
120        {
121            var bootstrapper = new DefaultBootstrapper();
122
123            bootstrapper.Run();
124
125            Assert.IsTrue(bootstrapper.ConfigureContainerCalled);
126        }
127
128        [TestMethod]
129        public void ShouldCallConfigureRegionAdapterMappings()
130        {
131            var bootstrapper = new DefaultBootstrapper();
132
133            bootstrapper.Run();
134
135            Assert.IsTrue(bootstrapper.ConfigureRegionAdapterMappingsCalled);
136        }
137
138        [TestMethod]
139        public void NullLoggerThrows()
140        {
141            var bootstrapper = new DefaultBootstrapper();
142            bootstrapper.ReturnNullDefaultLogger = true;
143
144            AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "ILoggerFacade");
145        }
146
147        [TestMethod]
148        public void NullModuleEnumeratorThrowsOnDefaultModuleInitialization()
149        {
150            var bootstrapper = new DefaultBootstrapper();
151            bootstrapper.ModuleEnumerator = null;
152
153            AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IModuleEnumerator");
154        }
155
156        [TestMethod]
157        public void NotOvewrittenGetModuleEnumeratorThrowsOnDefaultModuleInitialization()
158        {
159            var bootstrapper = new DefaultBootstrapper();
160            bootstrapper.OverrideGetModuleEnumerator = false;
161
162            AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IModuleEnumerator");
163        }
164
165        [TestMethod]
166        public void GetLoggerShouldHaveDefault()
167        {
168            var bootstrapper = new DefaultBootstrapper();
169            Assert.IsNull(bootstrapper.DefaultLogger);
170            bootstrapper.Run();
171
172            Assert.IsNotNull(bootstrapper.DefaultLogger);
173            Assert.IsInstanceOfType(bootstrapper.DefaultLogger, typeof(TraceLogger));
174        }
175
176        [TestMethod]
177        public void ShouldAssignRegionManagerToReturnedShell()
178        {
179            var bootstrapper = new DefaultBootstrapper();
180            //var shell = new DependencyObject();
181            var shell = new UserControl();
182            bootstrapper.CreateShellReturnValue = shell;
183
184            Assert.IsNull(RegionManager.GetRegionManager(shell));
185
186            bootstrapper.Run();
187
188            Assert.IsNotNull(RegionManager.GetRegionManager(shell));
189        }
190
191        [TestMethod]
192        public void ShouldNotFailIfReturnedNullShell()
193        {
194            var bootstrapper = new DefaultBootstrapper();
195            bootstrapper.CreateShellReturnValue = null;
196            bootstrapper.Run();
197        }
198
199        [TestMethod]
200        public void NullModuleLoaderThrowsOnDefaultModuleInitialization()
201        {
202            var bootstrapper = new MockedBootstrapper();
203
204            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
205            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), null);
206
207            AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IModuleLoader");
208        }
209
210        [TestMethod]
211        public void ShouldRegisterDefaultTypeMappings()
212        {
213            var bootstrapper = new MockedBootstrapper();
214
215            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
216            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), new MockModuleLoader());
217
218            bootstrapper.Run();
219
220            Assert.IsTrue(bootstrapper.MockUnityContainer.Instances.ContainsKey(typeof(ILoggerFacade)));
221            Assert.AreSame(bootstrapper.Logger, bootstrapper.MockUnityContainer.Instances[typeof(ILoggerFacade)]);
222
223            Assert.IsTrue(bootstrapper.MockUnityContainer.Instances.ContainsKey(typeof(IUnityContainer)));
224            Assert.AreSame(bootstrapper.MockUnityContainer, bootstrapper.MockUnityContainer.Instances[typeof(IUnityContainer)]);
225
226            Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IContainerFacade)));
227            Assert.AreEqual(typeof(UnityContainerAdapter), bootstrapper.MockUnityContainer.Types[typeof(IContainerFacade)]);
228
229            Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IModuleLoader)));
230            Assert.AreEqual(typeof(ModuleLoader), bootstrapper.MockUnityContainer.Types[typeof(IModuleLoader)]);
231
232            Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IRegionManager)));
233            Assert.AreEqual(typeof(RegionManager), bootstrapper.MockUnityContainer.Types[typeof(IRegionManager)]);
234
235            Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IEventAggregator)));
236            Assert.AreEqual(typeof(EventAggregator), bootstrapper.MockUnityContainer.Types[typeof(IEventAggregator)]);
237
238            Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(RegionAdapterMappings)));
239            Assert.AreEqual(typeof(RegionAdapterMappings), bootstrapper.MockUnityContainer.Types[typeof(RegionAdapterMappings)]);
240
241            Assert.IsTrue(bootstrapper.MockUnityContainer.Instances.ContainsKey(typeof(IModuleEnumerator)));
242            Assert.AreSame(bootstrapper.ModuleEnumerator, bootstrapper.MockUnityContainer.Instances[typeof(IModuleEnumerator)]);
243        }
244
245        [TestMethod]
246        public void ShouldCallGetStartupLoadedModules()
247        {
248            var bootstrapper = new MockedBootstrapper();
249
250            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
251            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), new MockModuleLoader());
252
253            bootstrapper.Run();
254
255            Assert.IsTrue(bootstrapper.ModuleEnumerator.GetStartupLoadedModulesCalled);
256        }
257
258        [TestMethod]
259        public void ShouldCallInitializeOnModuleLoader()
260        {
261            var bootstrapper = new MockedBootstrapper();
262
263            var moduleLoader = new MockModuleLoader();
264            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), new MockModuleEnumerator());
265            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), moduleLoader);
266
267            bootstrapper.Run();
268
269            Assert.IsTrue(moduleLoader.InitializeCalled);
270        }
271
272        [TestMethod]
273        public void ShouldCallInitializeOnModuleLoaderWithStartupModules()
274        {
275            var bootstrapper = new MockedBootstrapper();
276            var moduleLoader = new MockModuleLoader();
277
278            bootstrapper.ModuleEnumerator.StartupLoadedModules = new[] { new ModuleInfo("asm", "type", "name") };
279
280            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
281            bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), moduleLoader);
282
283
284            bootstrapper.Run();
285
286            Assert.IsNotNull(moduleLoader.InitializeArgumentModuleInfos);
287            Assert.AreEqual(1, moduleLoader.InitializeArgumentModuleInfos.Length);
288            Assert.AreEqual("name", moduleLoader.InitializeArgumentModuleInfos[0].ModuleName);
289        }
290
291        [TestMethod]
292        public void ReturningNullContainerThrows()
293        {
294            var bootstrapper = new MockedBootstrapper();
295            bootstrapper.MockUnityContainer = null;
296
297            AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IUnityContainer");
298        }
299
300        [TestMethod]
301        public void ShouldCallTheMethodsInOrder()
302        {
303            var bootstrapper = new TestableOrderedBootstrapper();
304            bootstrapper.Run();
305
306            Assert.IsTrue(CompareOrder("LoggerFacade", "CreateContainer", bootstrapper.OrderedMethodCallList) < 0);
307            Assert.IsTrue(CompareOrder("CreateContainer", "ConfigureContainer", bootstrapper.OrderedMethodCallList) < 0);
308            Assert.IsTrue(CompareOrder("ConfigureContainer", "GetModuleEnumerator", bootstrapper.OrderedMethodCallList) < 0);
309            Assert.IsTrue(CompareOrder("GetModuleEnumerator", "ConfigureRegionAdapterMappings", bootstrapper.OrderedMethodCallList) < 0);
310            Assert.IsTrue(CompareOrder("ConfigureRegionAdapterMappings", "CreateShell", bootstrapper.OrderedMethodCallList) < 0);
311            Assert.IsTrue(CompareOrder("CreateShell", "InitializeModules", bootstrapper.OrderedMethodCallList) < 0);
312        }
313
314        [TestMethod]
315        public void ShouldLogBootstrapperSteps()
316        {
317            var bootstrapper = new TestableOrderedBootstrapper();
318            bootstrapper.Run();
319            var messages = bootstrapper.Logger.Messages;
320
321            Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Creating Unity container")));
322            Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Configuring container")));
323            Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Configuring region adapters")));
324            Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Creating shell")));
325            Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Initializing modules")));
326            Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Bootstrapper sequence completed")));
327        }
328
329        [TestMethod]
330        public void ShouldNotRegisterDefaultServicesAndTypes()
331        {
332            var bootstrapper = new NonconfiguredBootstrapper();
333            bootstrapper.Run(false);
334
335
336            Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IEventAggregator)));
337            Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IRegionManager)));
338            Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(RegionAdapterMappings)));
339            Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IContainerFacade)));
340            Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IModuleLoader)));
341        }
342
343        // TODO: Resolve failure
344        [TestMethod, Ignore]
345        public void ShoudLogRegisterTypeIfMissingMessage()
346        {
347            var bootstrapper = new TestableOrderedBootstrapper();
348            bootstrapper.AddCustomTypeMappings = true;
349            bootstrapper.Run();
350            var messages = bootstrapper.Logger.Messages;
351
352            Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Type 'IRegionManager' was already registered by the application")));
353        }
354
355        private static int CompareOrder(string firstString, string secondString, IList<string> list)
356        {
357            return list.IndexOf(firstString).CompareTo(list.IndexOf(secondString));
358        }
359
360        private static void AssertExceptionThrownOnRun(UnityBootstrapper bootstrapper, Type expectedExceptionType, string expectedExceptionMessageSubstring)
361        {
362            bool exceptionThrown = false;
363            try
364            {
365                bootstrapper.Run();
366            }
367            catch (Exception ex)
368            {
369                Assert.AreEqual(expectedExceptionType, ex.GetType());
370                StringAssert.Contains(ex.Message, expectedExceptionMessageSubstring);
371                exceptionThrown = true;
372            }
373            if (!exceptionThrown)
374            {
375                Assert.Fail("Exception not thrown.");
376            }
377        }
378    }
379
380    class NonconfiguredBootstrapper : UnityBootstrapper
381    {
382        private MockUnityContainer mockContainer;
383
384        protected override void InitializeModules()
385        {
386        }
387
388        protected override IUnityContainer CreateContainer()
389        {
390            mockContainer = new MockUnityContainer();
391            return mockContainer;
392        }
393
394        protected override DependencyObject CreateShell()
395        {
396            return null;
397        }
398
399        public bool HasRegisteredType(Type t)
400        {
401            return mockContainer.Types.ContainsKey(t);
402        }
403    }
404
405    class DefaultBootstrapper : UnityBootstrapper
406    {
407        public bool GetEnumeratorCalled;
408        public bool GetLoggerCalled;
409        public bool InitializeModulesCalled;
410        public bool CreateShellCalled;
411        public bool ReturnNullDefaultLogger;
412        public bool OverrideGetModuleEnumerator = true;
413        public IModuleEnumerator ModuleEnumerator = new MockModuleEnumerator();
414        public ILoggerFacade DefaultLogger;
415        public RegionAdapterMappings DefaultRegionAdapterMappings;
416        public DependencyObject CreateShellReturnValue;
417        public bool ConfigureContainerCalled;
418        public bool ConfigureRegionAdapterMappingsCalled;
419
420        public IUnityContainer GetBaseContainer()
421        {
422            return base.Container;
423        }
424
425        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
426        {
427            ConfigureRegionAdapterMappingsCalled = true;
428            var regionAdapterMappings = base.ConfigureRegionAdapterMappings();
429
430            DefaultRegionAdapterMappings = regionAdapterMappings;
431
432            return regionAdapterMappings;
433        }
434
435        protected override void ConfigureContainer()
436        {
437            ConfigureContainerCalled = true;
438            base.ConfigureContainer();
439        }
440
441        protected override IModuleEnumerator GetModuleEnumerator()
442        {
443            GetEnumeratorCalled = true;
444            if (OverrideGetModuleEnumerator)
445            {
446                return ModuleEnumerator;
447            }
448            else
449            {
450                return base.GetModuleEnumerator();
451            }
452        }
453
454        protected override ILoggerFacade LoggerFacade
455        {
456            get
457            {
458                GetLoggerCalled = true;
459                if (ReturnNullDefaultLogger)
460                {
461                    return null;
462                }
463                else
464                {
465                    DefaultLogger = base.LoggerFacade;
466                    return DefaultLogger;
467                }
468            }
469        }
470
471        protected override void InitializeModules()
472        {
473            InitializeModulesCalled = true;
474            base.InitializeModules();
475        }
476
477        protected override DependencyObject CreateShell()
478        {
479            CreateShellCalled = true;
480
481            return CreateShellReturnValue;
482        }
483    }
484
485    class MockedBootstrapper : UnityBootstrapper
486    {
487        public MockUnityContainer MockUnityContainer = new MockUnityContainer();
488        public MockModuleEnumerator ModuleEnumerator = new MockModuleEnumerator();
489        public MockLoggerAdapter Logger = new MockLoggerAdapter();
490
491        protected override IUnityContainer CreateContainer()
492        {
493            return this.MockUnityContainer;
494        }
495
496        protected override IModuleEnumerator GetModuleEnumerator()
497        {
498            return ModuleEnumerator;
499        }
500
501        protected override ILoggerFacade LoggerFacade
502        {
503            get { return Logger; }
504        }
505
506        protected override DependencyObject CreateShell()
507        {
508            return null;
509        }
510    }
511
512    class TestableOrderedBootstrapper : UnityBootstrapper
513    {
514        public IList<string> OrderedMethodCallList = new List<string>();
515        public MockLoggerAdapter Logger = new MockLoggerAdapter();
516        public bool AddCustomTypeMappings;
517
518        protected override IUnityContainer CreateContainer()
519        {
520            OrderedMethodCallList.Add("CreateContainer");
521            return base.CreateContainer();
522        }
523
524        protected override ILoggerFacade LoggerFacade
525        {
526            get
527            {
528                OrderedMethodCallList.Add("LoggerFacade");
529                return Logger;
530            }
531        }
532
533        protected override IModuleEnumerator GetModuleEnumerator()
534        {
535            OrderedMethodCallList.Add("GetModuleEnumerator");
536            return new MockModuleEnumerator();
537        }
538
539        protected override void ConfigureContainer()
540        {
541            OrderedMethodCallList.Add("ConfigureContainer");
542            if (AddCustomTypeMappings)
543            {
544                RegisterTypeIfMissing(typeof(IRegionManager), typeof(MockRegionManager), true);
545            }
546            base.ConfigureContainer();
547        }
548
549        protected override void InitializeModules()
550        {
551            OrderedMethodCallList.Add("InitializeModules");
552            base.InitializeModules();
553        }
554
555        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
556        {
557            OrderedMethodCallList.Add("ConfigureRegionAdapterMappings");
558            return base.ConfigureRegionAdapterMappings();
559        }
560
561        protected override DependencyObject CreateShell()
562        {
563            OrderedMethodCallList.Add("CreateShell");
564            return null;
565        }
566    }
567}