PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Controls.Primitives;
  23. using Microsoft.Practices.Composite.Events;
  24. using Microsoft.Practices.Composite.Logging;
  25. using Microsoft.Practices.Composite.Modularity;
  26. using Microsoft.Practices.Composite.Regions;
  27. using Microsoft.Practices.Composite.UnityExtensions.Tests.Mocks;
  28. using Microsoft.Practices.Composite.Wpf.Regions;
  29. using Microsoft.Practices.Unity;
  30. using Microsoft.VisualStudio.TestTools.UnitTesting;
  31. namespace Microsoft.Practices.Composite.UnityExtensions.Tests
  32. {
  33. [TestClass]
  34. public class UnityBootstrapperFixture
  35. {
  36. [TestMethod]
  37. public void CanCreateConcreteBootstrapper()
  38. {
  39. new DefaultBootstrapper();
  40. }
  41. [TestMethod]
  42. public void CanRunBootstrapper()
  43. {
  44. var bootstrapper = new DefaultBootstrapper();
  45. bootstrapper.Run();
  46. }
  47. [TestMethod]
  48. public void ShouldInitializeContainer()
  49. {
  50. var bootstrapper = new DefaultBootstrapper();
  51. var container = bootstrapper.GetBaseContainer();
  52. Assert.IsNull(container);
  53. bootstrapper.Run();
  54. container = bootstrapper.GetBaseContainer();
  55. Assert.IsNotNull(container);
  56. Assert.IsInstanceOfType(container, typeof(UnityContainer));
  57. }
  58. [TestMethod]
  59. public void ShouldCallInitializeModules()
  60. {
  61. var bootstrapper = new DefaultBootstrapper();
  62. bootstrapper.Run();
  63. Assert.IsTrue(bootstrapper.InitializeModulesCalled);
  64. }
  65. [TestMethod]
  66. public void ShouldRegisterDefaultMappings()
  67. {
  68. var bootstrapper = new DefaultBootstrapper();
  69. bootstrapper.Run();
  70. Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings);
  71. Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings.GetMapping(typeof(ItemsControl)));
  72. Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings.GetMapping(typeof(ContentControl)));
  73. // Assert.IsNotNull(bootstrapper.DefaultRegionAdapterMappings.GetMapping(typeof(Selector)));
  74. }
  75. [TestMethod]
  76. public void ShouldCallGetLogger()
  77. {
  78. var bootstrapper = new DefaultBootstrapper();
  79. bootstrapper.Run();
  80. Assert.IsTrue(bootstrapper.GetLoggerCalled);
  81. }
  82. [TestMethod]
  83. public void ShouldCallGetEnumerator()
  84. {
  85. var bootstrapper = new DefaultBootstrapper();
  86. bootstrapper.Run();
  87. Assert.IsTrue(bootstrapper.GetEnumeratorCalled);
  88. }
  89. [TestMethod]
  90. public void ShouldCallCreateSell()
  91. {
  92. var bootstrapper = new DefaultBootstrapper();
  93. bootstrapper.Run();
  94. Assert.IsTrue(bootstrapper.CreateShellCalled);
  95. }
  96. [TestMethod]
  97. public void ShouldCallConfigureTypeMappings()
  98. {
  99. var bootstrapper = new DefaultBootstrapper();
  100. bootstrapper.Run();
  101. Assert.IsTrue(bootstrapper.ConfigureContainerCalled);
  102. }
  103. [TestMethod]
  104. public void ShouldCallConfigureRegionAdapterMappings()
  105. {
  106. var bootstrapper = new DefaultBootstrapper();
  107. bootstrapper.Run();
  108. Assert.IsTrue(bootstrapper.ConfigureRegionAdapterMappingsCalled);
  109. }
  110. [TestMethod]
  111. public void NullLoggerThrows()
  112. {
  113. var bootstrapper = new DefaultBootstrapper();
  114. bootstrapper.ReturnNullDefaultLogger = true;
  115. AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "ILoggerFacade");
  116. }
  117. [TestMethod]
  118. public void NullModuleEnumeratorThrowsOnDefaultModuleInitialization()
  119. {
  120. var bootstrapper = new DefaultBootstrapper();
  121. bootstrapper.ModuleEnumerator = null;
  122. AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IModuleEnumerator");
  123. }
  124. [TestMethod]
  125. public void NotOvewrittenGetModuleEnumeratorThrowsOnDefaultModuleInitialization()
  126. {
  127. var bootstrapper = new DefaultBootstrapper();
  128. bootstrapper.OverrideGetModuleEnumerator = false;
  129. AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IModuleEnumerator");
  130. }
  131. [TestMethod]
  132. public void GetLoggerShouldHaveDefault()
  133. {
  134. var bootstrapper = new DefaultBootstrapper();
  135. Assert.IsNull(bootstrapper.DefaultLogger);
  136. bootstrapper.Run();
  137. Assert.IsNotNull(bootstrapper.DefaultLogger);
  138. Assert.IsInstanceOfType(bootstrapper.DefaultLogger, typeof(TraceLogger));
  139. }
  140. [TestMethod]
  141. public void ShouldAssignRegionManagerToReturnedShell()
  142. {
  143. var bootstrapper = new DefaultBootstrapper();
  144. //var shell = new DependencyObject();
  145. var shell = new UserControl();
  146. bootstrapper.CreateShellReturnValue = shell;
  147. Assert.IsNull(RegionManager.GetRegionManager(shell));
  148. bootstrapper.Run();
  149. Assert.IsNotNull(RegionManager.GetRegionManager(shell));
  150. }
  151. [TestMethod]
  152. public void ShouldNotFailIfReturnedNullShell()
  153. {
  154. var bootstrapper = new DefaultBootstrapper();
  155. bootstrapper.CreateShellReturnValue = null;
  156. bootstrapper.Run();
  157. }
  158. [TestMethod]
  159. public void NullModuleLoaderThrowsOnDefaultModuleInitialization()
  160. {
  161. var bootstrapper = new MockedBootstrapper();
  162. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
  163. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), null);
  164. AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IModuleLoader");
  165. }
  166. [TestMethod]
  167. public void ShouldRegisterDefaultTypeMappings()
  168. {
  169. var bootstrapper = new MockedBootstrapper();
  170. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
  171. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), new MockModuleLoader());
  172. bootstrapper.Run();
  173. Assert.IsTrue(bootstrapper.MockUnityContainer.Instances.ContainsKey(typeof(ILoggerFacade)));
  174. Assert.AreSame(bootstrapper.Logger, bootstrapper.MockUnityContainer.Instances[typeof(ILoggerFacade)]);
  175. Assert.IsTrue(bootstrapper.MockUnityContainer.Instances.ContainsKey(typeof(IUnityContainer)));
  176. Assert.AreSame(bootstrapper.MockUnityContainer, bootstrapper.MockUnityContainer.Instances[typeof(IUnityContainer)]);
  177. Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IContainerFacade)));
  178. Assert.AreEqual(typeof(UnityContainerAdapter), bootstrapper.MockUnityContainer.Types[typeof(IContainerFacade)]);
  179. Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IModuleLoader)));
  180. Assert.AreEqual(typeof(ModuleLoader), bootstrapper.MockUnityContainer.Types[typeof(IModuleLoader)]);
  181. Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IRegionManager)));
  182. Assert.AreEqual(typeof(RegionManager), bootstrapper.MockUnityContainer.Types[typeof(IRegionManager)]);
  183. Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(IEventAggregator)));
  184. Assert.AreEqual(typeof(EventAggregator), bootstrapper.MockUnityContainer.Types[typeof(IEventAggregator)]);
  185. Assert.IsTrue(bootstrapper.MockUnityContainer.Types.ContainsKey(typeof(RegionAdapterMappings)));
  186. Assert.AreEqual(typeof(RegionAdapterMappings), bootstrapper.MockUnityContainer.Types[typeof(RegionAdapterMappings)]);
  187. Assert.IsTrue(bootstrapper.MockUnityContainer.Instances.ContainsKey(typeof(IModuleEnumerator)));
  188. Assert.AreSame(bootstrapper.ModuleEnumerator, bootstrapper.MockUnityContainer.Instances[typeof(IModuleEnumerator)]);
  189. }
  190. [TestMethod]
  191. public void ShouldCallGetStartupLoadedModules()
  192. {
  193. var bootstrapper = new MockedBootstrapper();
  194. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
  195. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), new MockModuleLoader());
  196. bootstrapper.Run();
  197. Assert.IsTrue(bootstrapper.ModuleEnumerator.GetStartupLoadedModulesCalled);
  198. }
  199. [TestMethod]
  200. public void ShouldCallInitializeOnModuleLoader()
  201. {
  202. var bootstrapper = new MockedBootstrapper();
  203. var moduleLoader = new MockModuleLoader();
  204. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), new MockModuleEnumerator());
  205. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), moduleLoader);
  206. bootstrapper.Run();
  207. Assert.IsTrue(moduleLoader.InitializeCalled);
  208. }
  209. [TestMethod]
  210. public void ShouldCallInitializeOnModuleLoaderWithStartupModules()
  211. {
  212. var bootstrapper = new MockedBootstrapper();
  213. var moduleLoader = new MockModuleLoader();
  214. bootstrapper.ModuleEnumerator.StartupLoadedModules = new[] { new ModuleInfo("asm", "type", "name") };
  215. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleEnumerator), bootstrapper.ModuleEnumerator);
  216. bootstrapper.MockUnityContainer.ResolveBag.Add(typeof(IModuleLoader), moduleLoader);
  217. bootstrapper.Run();
  218. Assert.IsNotNull(moduleLoader.InitializeArgumentModuleInfos);
  219. Assert.AreEqual(1, moduleLoader.InitializeArgumentModuleInfos.Length);
  220. Assert.AreEqual("name", moduleLoader.InitializeArgumentModuleInfos[0].ModuleName);
  221. }
  222. [TestMethod]
  223. public void ReturningNullContainerThrows()
  224. {
  225. var bootstrapper = new MockedBootstrapper();
  226. bootstrapper.MockUnityContainer = null;
  227. AssertExceptionThrownOnRun(bootstrapper, typeof(InvalidOperationException), "IUnityContainer");
  228. }
  229. [TestMethod]
  230. public void ShouldCallTheMethodsInOrder()
  231. {
  232. var bootstrapper = new TestableOrderedBootstrapper();
  233. bootstrapper.Run();
  234. Assert.IsTrue(CompareOrder("LoggerFacade", "CreateContainer", bootstrapper.OrderedMethodCallList) < 0);
  235. Assert.IsTrue(CompareOrder("CreateContainer", "ConfigureContainer", bootstrapper.OrderedMethodCallList) < 0);
  236. Assert.IsTrue(CompareOrder("ConfigureContainer", "GetModuleEnumerator", bootstrapper.OrderedMethodCallList) < 0);
  237. Assert.IsTrue(CompareOrder("GetModuleEnumerator", "ConfigureRegionAdapterMappings", bootstrapper.OrderedMethodCallList) < 0);
  238. Assert.IsTrue(CompareOrder("ConfigureRegionAdapterMappings", "CreateShell", bootstrapper.OrderedMethodCallList) < 0);
  239. Assert.IsTrue(CompareOrder("CreateShell", "InitializeModules", bootstrapper.OrderedMethodCallList) < 0);
  240. }
  241. [TestMethod]
  242. public void ShouldLogBootstrapperSteps()
  243. {
  244. var bootstrapper = new TestableOrderedBootstrapper();
  245. bootstrapper.Run();
  246. var messages = bootstrapper.Logger.Messages;
  247. Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Creating Unity container")));
  248. Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Configuring container")));
  249. Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Configuring region adapters")));
  250. Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Creating shell")));
  251. Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Initializing modules")));
  252. Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Bootstrapper sequence completed")));
  253. }
  254. [TestMethod]
  255. public void ShouldNotRegisterDefaultServicesAndTypes()
  256. {
  257. var bootstrapper = new NonconfiguredBootstrapper();
  258. bootstrapper.Run(false);
  259. Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IEventAggregator)));
  260. Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IRegionManager)));
  261. Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(RegionAdapterMappings)));
  262. Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IContainerFacade)));
  263. Assert.IsFalse(bootstrapper.HasRegisteredType(typeof(IModuleLoader)));
  264. }
  265. // TODO: Resolve failure
  266. [TestMethod, Ignore]
  267. public void ShoudLogRegisterTypeIfMissingMessage()
  268. {
  269. var bootstrapper = new TestableOrderedBootstrapper();
  270. bootstrapper.AddCustomTypeMappings = true;
  271. bootstrapper.Run();
  272. var messages = bootstrapper.Logger.Messages;
  273. Assert.IsNotNull(messages.FirstOrDefault(msg => msg.Contains("Type 'IRegionManager' was already registered by the application")));
  274. }
  275. private static int CompareOrder(string firstString, string secondString, IList<string> list)
  276. {
  277. return list.IndexOf(firstString).CompareTo(list.IndexOf(secondString));
  278. }
  279. private static void AssertExceptionThrownOnRun(UnityBootstrapper bootstrapper, Type expectedExceptionType, string expectedExceptionMessageSubstring)
  280. {
  281. bool exceptionThrown = false;
  282. try
  283. {
  284. bootstrapper.Run();
  285. }
  286. catch (Exception ex)
  287. {
  288. Assert.AreEqual(expectedExceptionType, ex.GetType());
  289. StringAssert.Contains(ex.Message, expectedExceptionMessageSubstring);
  290. exceptionThrown = true;
  291. }
  292. if (!exceptionThrown)
  293. {
  294. Assert.Fail("Exception not thrown.");
  295. }
  296. }
  297. }
  298. class NonconfiguredBootstrapper : UnityBootstrapper
  299. {
  300. private MockUnityContainer mockContainer;
  301. protected override void InitializeModules()
  302. {
  303. }
  304. protected override IUnityContainer CreateContainer()
  305. {
  306. mockContainer = new MockUnityContainer();
  307. return mockContainer;
  308. }
  309. protected override DependencyObject CreateShell()
  310. {
  311. return null;
  312. }
  313. public bool HasRegisteredType(Type t)
  314. {
  315. return mockContainer.Types.ContainsKey(t);
  316. }
  317. }
  318. class DefaultBootstrapper : UnityBootstrapper
  319. {
  320. public bool GetEnumeratorCalled;
  321. public bool GetLoggerCalled;
  322. public bool InitializeModulesCalled;
  323. public bool CreateShellCalled;
  324. public bool ReturnNullDefaultLogger;
  325. public bool OverrideGetModuleEnumerator = true;
  326. public IModuleEnumerator ModuleEnumerator = new MockModuleEnumerator();
  327. public ILoggerFacade DefaultLogger;
  328. public RegionAdapterMappings DefaultRegionAdapterMappings;
  329. public DependencyObject CreateShellReturnValue;
  330. public bool ConfigureContainerCalled;
  331. public bool ConfigureRegionAdapterMappingsCalled;
  332. public IUnityContainer GetBaseContainer()
  333. {
  334. return base.Container;
  335. }
  336. protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
  337. {
  338. ConfigureRegionAdapterMappingsCalled = true;
  339. var regionAdapterMappings = base.ConfigureRegionAdapterMappings();
  340. DefaultRegionAdapterMappings = regionAdapterMappings;
  341. return regionAdapterMappings;
  342. }
  343. protected override void ConfigureContainer()
  344. {
  345. ConfigureContainerCalled = true;
  346. base.ConfigureContainer();
  347. }
  348. protected override IModuleEnumerator GetModuleEnumerator()
  349. {
  350. GetEnumeratorCalled = true;
  351. if (OverrideGetModuleEnumerator)
  352. {
  353. return ModuleEnumerator;
  354. }
  355. else
  356. {
  357. return base.GetModuleEnumerator();
  358. }
  359. }
  360. protected override ILoggerFacade LoggerFacade
  361. {
  362. get
  363. {
  364. GetLoggerCalled = true;
  365. if (ReturnNullDefaultLogger)
  366. {
  367. return null;
  368. }
  369. else
  370. {
  371. DefaultLogger = base.LoggerFacade;
  372. return DefaultLogger;
  373. }
  374. }
  375. }
  376. protected override void InitializeModules()
  377. {
  378. InitializeModulesCalled = true;
  379. base.InitializeModules();
  380. }
  381. protected override DependencyObject CreateShell()
  382. {
  383. CreateShellCalled = true;
  384. return CreateShellReturnValue;
  385. }
  386. }
  387. class MockedBootstrapper : UnityBootstrapper
  388. {
  389. public MockUnityContainer MockUnityContainer = new MockUnityContainer();
  390. public MockModuleEnumerator ModuleEnumerator = new MockModuleEnumerator();
  391. public MockLoggerAdapter Logger = new MockLoggerAdapter();
  392. protected override IUnityContainer CreateContainer()
  393. {
  394. return this.MockUnityContainer;
  395. }
  396. protected override IModuleEnumerator GetModuleEnumerator()
  397. {
  398. return ModuleEnumerator;
  399. }
  400. protected override ILoggerFacade LoggerFacade
  401. {
  402. get { return Logger; }
  403. }
  404. protected override DependencyObject CreateShell()
  405. {
  406. return null;
  407. }
  408. }
  409. class TestableOrderedBootstrapper : UnityBootstrapper
  410. {
  411. public IList<string> OrderedMethodCallList = new List<string>();
  412. public MockLoggerAdapter Logger = new MockLoggerAdapter();
  413. public bool AddCustomTypeMappings;
  414. protected override IUnityContainer CreateContainer()
  415. {
  416. OrderedMethodCallList.Add("CreateContainer");
  417. return base.CreateContainer();
  418. }
  419. protected override ILoggerFacade LoggerFacade
  420. {
  421. get
  422. {
  423. OrderedMethodCallList.Add("LoggerFacade");
  424. return Logger;
  425. }
  426. }
  427. protected override IModuleEnumerator GetModuleEnumerator()
  428. {
  429. OrderedMethodCallList.Add("GetModuleEnumerator");
  430. return new MockModuleEnumerator();
  431. }
  432. protected override void ConfigureContainer()
  433. {
  434. OrderedMethodCallList.Add("ConfigureContainer");
  435. if (AddCustomTypeMappings)
  436. {
  437. RegisterTypeIfMissing(typeof(IRegionManager), typeof(MockRegionManager), true);
  438. }
  439. base.ConfigureContainer();
  440. }
  441. protected override void InitializeModules()
  442. {
  443. OrderedMethodCallList.Add("InitializeModules");
  444. base.InitializeModules();
  445. }
  446. protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
  447. {
  448. OrderedMethodCallList.Add("ConfigureRegionAdapterMappings");
  449. return base.ConfigureRegionAdapterMappings();
  450. }
  451. protected override DependencyObject CreateShell()
  452. {
  453. OrderedMethodCallList.Add("CreateShell");
  454. return null;
  455. }
  456. }
  457. }