PageRenderTime 50ms CodeModel.GetById 7ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 1ms

/V2.2/trunk/CAL/Desktop/Composite.Tests/Modularity/ModuleCatalogFixture.cs

#
C# | 510 lines | 394 code | 95 blank | 21 comment | 3 complexity | d9a299384009682f3c5976752497942a 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//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.IO;
 20using System.Linq;
 21using System.Reflection;
 22using Microsoft.Practices.Composite.Modularity;
 23using Microsoft.VisualStudio.TestTools.UnitTesting;
 24
 25namespace Microsoft.Practices.Composite.Tests.Modularity
 26{
 27    [TestClass]
 28    public class ModuleCatalogFixture
 29    {
 30        [TestMethod]
 31        public void CanCreateCatalogFromList()
 32        {
 33            var moduleInfo = new ModuleInfo("MockModule", "type");
 34            List<ModuleInfo> moduleInfos = new List<ModuleInfo> { moduleInfo };
 35
 36            var moduleCatalog = new ModuleCatalog(moduleInfos);
 37
 38            Assert.AreEqual(1, moduleCatalog.Modules.Count());
 39            Assert.AreEqual(moduleInfo, moduleCatalog.Modules.ElementAt(0));
 40        }
 41
 42        [TestMethod]
 43        public void CanGetDependenciesForModule()
 44        {
 45            // A <- B
 46            var moduleInfoA = CreateModuleInfo("A");
 47            var moduleInfoB = CreateModuleInfo("B", "A");
 48            List<ModuleInfo> moduleInfos = new List<ModuleInfo>
 49                                               {
 50                                                   moduleInfoA
 51                                                   , moduleInfoB
 52                                               };
 53            var moduleCatalog = new ModuleCatalog(moduleInfos);
 54
 55            IEnumerable<ModuleInfo> dependentModules = moduleCatalog.GetDependentModules(moduleInfoB);
 56
 57            Assert.AreEqual(1, dependentModules.Count());
 58            Assert.AreEqual(moduleInfoA, dependentModules.ElementAt(0));
 59        }
 60
 61        [TestMethod]
 62        public void CanCompleteListWithTheirDependencies()
 63        {
 64            // A <- B <- C
 65            var moduleInfoA = CreateModuleInfo("A");
 66            var moduleInfoB = CreateModuleInfo("B", "A");
 67            var moduleInfoC = CreateModuleInfo("C", "B");
 68            var moduleInfoOrphan = CreateModuleInfo("X", "B");
 69
 70            List<ModuleInfo> moduleInfos = new List<ModuleInfo>
 71                                               {
 72                                                   moduleInfoA
 73                                                   , moduleInfoB
 74                                                   , moduleInfoC
 75                                                   , moduleInfoOrphan
 76                                               };
 77            var moduleCatalog = new ModuleCatalog(moduleInfos);
 78
 79            IEnumerable<ModuleInfo> dependantModules = moduleCatalog.CompleteListWithDependencies(new[] { moduleInfoC });
 80
 81            Assert.AreEqual(3, dependantModules.Count());
 82            Assert.IsTrue(dependantModules.Contains(moduleInfoA));
 83            Assert.IsTrue(dependantModules.Contains(moduleInfoB));
 84            Assert.IsTrue(dependantModules.Contains(moduleInfoC));
 85        }
 86
 87        [TestMethod]
 88        [ExpectedException(typeof(CyclicDependencyFoundException))]
 89        public void ShouldThrowOnCyclicDependency()
 90        {
 91            // A <- B <- C <- A
 92            var moduleInfoA = CreateModuleInfo("A", "C");
 93            var moduleInfoB = CreateModuleInfo("B", "A");
 94            var moduleInfoC = CreateModuleInfo("C", "B");
 95
 96            List<ModuleInfo> moduleInfos = new List<ModuleInfo>
 97                                               {
 98                                                   moduleInfoA
 99                                                   , moduleInfoB
100                                                   , moduleInfoC
101                                               };
102            new ModuleCatalog(moduleInfos).Validate();
103        }
104
105
106        [TestMethod]
107        [ExpectedException(typeof(DuplicateModuleException))]
108        public void ShouldThrowOnDuplicateModule()
109        {
110
111            var moduleInfoA1 = CreateModuleInfo("A");
112            var moduleInfoA2 = CreateModuleInfo("A");
113
114            List<ModuleInfo> moduleInfos = new List<ModuleInfo>
115                                               {
116                                                   moduleInfoA1
117                                                   , moduleInfoA2
118                                               };
119            new ModuleCatalog(moduleInfos).Validate();
120        }
121
122        [TestMethod]
123        [ExpectedException(typeof(ModularityException))]
124        public void ShouldThrowOnMissingDependency()
125        {
126            var moduleInfoA = CreateModuleInfo("A", "B");
127
128            List<ModuleInfo> moduleInfos = new List<ModuleInfo>
129                                               {
130                                                   moduleInfoA
131                                               };
132            new ModuleCatalog(moduleInfos).Validate();
133        }
134
135        [TestMethod]
136        public void CanAddModules()
137        {
138            var catalog = new ModuleCatalog();
139
140            catalog.AddModule(typeof(MockModule));
141
142            Assert.AreEqual(1, catalog.Modules.Count());
143            Assert.AreEqual("MockModule", catalog.Modules.First().ModuleName);
144        }
145
146        [TestMethod]
147        public void CanAddGroups()
148        {
149            var catalog = new ModuleCatalog();
150
151            ModuleInfo moduleInfo = new ModuleInfo();
152            ModuleInfoGroup group = new ModuleInfoGroup { moduleInfo };
153            catalog.Items.Add(group);
154
155            Assert.AreEqual(1, catalog.Modules.Count());
156            Assert.AreSame(moduleInfo, catalog.Modules.ElementAt(0));
157        }
158
159        [TestMethod]
160        public void ShouldAggregateGroupsAndLooseModuleInfos()
161        {
162            var catalog = new ModuleCatalog();
163            ModuleInfo moduleInfo1 = new ModuleInfo();
164            ModuleInfo moduleInfo2 = new ModuleInfo();
165            ModuleInfo moduleInfo3 = new ModuleInfo();
166
167            catalog.Items.Add(new ModuleInfoGroup() { moduleInfo1 });
168            catalog.Items.Add(new ModuleInfoGroup() { moduleInfo2 });
169            catalog.AddModule(moduleInfo3);
170
171            Assert.AreEqual(3, catalog.Modules.Count());
172            Assert.IsTrue(catalog.Modules.Contains(moduleInfo1));
173            Assert.IsTrue(catalog.Modules.Contains(moduleInfo2));
174            Assert.IsTrue(catalog.Modules.Contains(moduleInfo3));
175        }
176
177        [TestMethod]
178        [ExpectedException(typeof(ArgumentNullException))]
179        public void CompleteListWithDependenciesThrowsWithNull()
180        {
181            var catalog = new ModuleCatalog();
182            catalog.CompleteListWithDependencies(null);
183        }
184
185        [TestMethod]
186        public void LooseModuleIfDependentOnModuleInGroupThrows()
187        {
188            var catalog = new ModuleCatalog();
189            catalog.Items.Add(new ModuleInfoGroup() { CreateModuleInfo("ModuleA") });
190            catalog.AddModule(CreateModuleInfo("ModuleB", "ModuleA"));
191
192            try
193            {
194                catalog.Validate();
195            }
196            catch (Exception ex)
197            {
198                Assert.IsInstanceOfType(ex, typeof(ModularityException));
199                Assert.AreEqual("ModuleB", ((ModularityException)ex).ModuleName);
200
201                return;
202            }
203
204            Assert.Fail("Exception not thrown.");
205        }
206
207        [TestMethod]
208        public void ModuleInGroupDependsOnModuleInOtherGroupThrows()
209        {
210            var catalog = new ModuleCatalog();
211            catalog.Items.Add(new ModuleInfoGroup() { CreateModuleInfo("ModuleA") });
212            catalog.Items.Add(new ModuleInfoGroup() { CreateModuleInfo("ModuleB", "ModuleA") });
213
214            try
215            {
216                catalog.Validate();
217            }
218            catch (Exception ex)
219            {
220                Assert.IsInstanceOfType(ex, typeof(ModularityException));
221                Assert.AreEqual("ModuleB", ((ModularityException)ex).ModuleName);
222
223                return;
224            }
225
226            Assert.Fail("Exception not thrown.");
227        }
228
229        [TestMethod]
230        public void ShouldRevalidateWhenAddingNewModuleIfValidated()
231        {
232            var testableCatalog = new TestableModuleCatalog();
233            testableCatalog.Items.Add(new ModuleInfoGroup() { CreateModuleInfo("ModuleA") });
234            testableCatalog.Validate();            
235            testableCatalog.Items.Add(new ModuleInfoGroup() { CreateModuleInfo("ModuleB") });
236            Assert.IsTrue(testableCatalog.ValidateCalled);
237        }
238
239        [TestMethod]
240        public void ModuleInGroupCanDependOnModuleInSameGroup()
241        {
242            var catalog = new ModuleCatalog();
243            var moduleA = CreateModuleInfo("ModuleA");
244            var moduleB = CreateModuleInfo("ModuleB", "ModuleA");
245            catalog.Items.Add(new ModuleInfoGroup()
246                                  {
247                                      moduleA,
248                                      moduleB
249                                  });
250
251            var moduleBDependencies = catalog.GetDependentModules(moduleB);
252
253            Assert.AreEqual(1, moduleBDependencies.Count());
254            Assert.AreEqual(moduleA, moduleBDependencies.First());
255
256        }
257
258        [TestMethod]
259        public void StartupModuleDependentOnAnOnDemandModuleThrows()
260        {
261            var catalog = new ModuleCatalog();
262            var moduleOnDemand = CreateModuleInfo("ModuleA");
263            moduleOnDemand.InitializationMode = InitializationMode.OnDemand;
264            catalog.AddModule(moduleOnDemand);
265            catalog.AddModule(CreateModuleInfo("ModuleB", "ModuleA"));
266
267            try
268            {
269                catalog.Validate();
270            }
271            catch (Exception ex)
272            {
273                Assert.IsInstanceOfType(ex, typeof(ModularityException));
274                Assert.AreEqual("ModuleB", ((ModularityException)ex).ModuleName);
275
276                return;
277            }
278
279            Assert.Fail("Exception not thrown.");
280        }
281
282        [TestMethod]
283        public void ShouldReturnInCorrectRetrieveOrderWhenCompletingListWithDependencies()
284        {
285            // A <- B <- C <- D,    C <- X
286            var moduleA = CreateModuleInfo("A");
287            var moduleB = CreateModuleInfo("B", "A");
288            var moduleC = CreateModuleInfo("C", "B");
289            var moduleD = CreateModuleInfo("D", "C");
290            var moduleX = CreateModuleInfo("X", "C");
291
292            var moduleCatalog = new ModuleCatalog();
293            // Add the modules in random order
294            moduleCatalog.AddModule(moduleB);
295            moduleCatalog.AddModule(moduleA);
296            moduleCatalog.AddModule(moduleD);
297            moduleCatalog.AddModule(moduleX);
298            moduleCatalog.AddModule(moduleC);
299
300            var dependantModules = moduleCatalog.CompleteListWithDependencies(new[] { moduleD, moduleX }).ToList();
301
302            Assert.AreEqual(5, dependantModules.Count);
303            Assert.IsTrue(dependantModules.IndexOf(moduleA) < dependantModules.IndexOf(moduleB));
304            Assert.IsTrue(dependantModules.IndexOf(moduleB) < dependantModules.IndexOf(moduleC));
305            Assert.IsTrue(dependantModules.IndexOf(moduleC) < dependantModules.IndexOf(moduleD));
306            Assert.IsTrue(dependantModules.IndexOf(moduleC) < dependantModules.IndexOf(moduleX));
307        }
308
309        [TestMethod]
310        public void CanLoadCatalogFromXaml()
311        {
312            Stream stream =
313                Assembly.GetExecutingAssembly().GetManifestResourceStream(
314                    "Microsoft.Practices.Composite.Tests.Modularity.ModuleCatalogXaml.SimpleModuleCatalog.xaml");
315
316            var catalog = ModuleCatalog.CreateFromXaml(stream);
317            Assert.IsNotNull(catalog);
318
319            Assert.AreEqual(4, catalog.Modules.Count());
320        }
321
322
323        [TestMethod]
324        public void ShouldLoadAndValidateOnInitialize()
325        {
326            var catalog = new TestableModuleCatalog();
327
328            var testableCatalog = new TestableModuleCatalog();
329            Assert.IsFalse(testableCatalog.LoadCalled);
330            Assert.IsFalse(testableCatalog.ValidateCalled);
331
332            testableCatalog.Initialize();
333            Assert.IsTrue(testableCatalog.LoadCalled);
334            Assert.IsTrue(testableCatalog.ValidateCalled);
335            Assert.IsTrue(testableCatalog.LoadCalledFirst);
336        }
337
338        [TestMethod]
339        public void ShouldNotLoadAgainIfInitializedCalledMoreThanOnce()
340        {
341            var catalog = new TestableModuleCatalog();
342
343            var testableCatalog = new TestableModuleCatalog();
344            Assert.IsFalse(testableCatalog.LoadCalled);
345            Assert.IsFalse(testableCatalog.ValidateCalled);
346
347            testableCatalog.Initialize();
348            Assert.AreEqual<int>(1, testableCatalog.LoadCalledCount);
349            testableCatalog.Initialize();
350            Assert.AreEqual<int>(1, testableCatalog.LoadCalledCount);
351        }
352
353        [TestMethod]
354        public void ShouldNotLoadAgainDuringInitialize()
355        {
356            var catalog = new TestableModuleCatalog();
357
358            var testableCatalog = new TestableModuleCatalog();
359            Assert.IsFalse(testableCatalog.LoadCalled);
360            Assert.IsFalse(testableCatalog.ValidateCalled);
361
362            testableCatalog.Load();
363            Assert.AreEqual<int>(1, testableCatalog.LoadCalledCount);
364            testableCatalog.Initialize();
365            Assert.AreEqual<int>(1, testableCatalog.LoadCalledCount);
366        }
367
368
369        [TestMethod]
370        public void ShouldAllowLoadToBeInvokedTwice()
371        {
372            var catalog = new TestableModuleCatalog();
373
374            var testableCatalog = new TestableModuleCatalog();
375            testableCatalog.Load();
376            Assert.AreEqual<int>(1, testableCatalog.LoadCalledCount);
377            testableCatalog.Load();
378            Assert.AreEqual<int>(2, testableCatalog.LoadCalledCount);
379        }
380	
381        [TestMethod]
382        public void CanAddModule1()
383        {
384            ModuleCatalog catalog = new ModuleCatalog();
385
386            catalog.AddModule("Module", "ModuleType", InitializationMode.OnDemand, "DependsOn1", "DependsOn2");
387
388            Assert.AreEqual(1, catalog.Modules.Count());
389            Assert.AreEqual("Module", catalog.Modules.First().ModuleName);
390            Assert.AreEqual("ModuleType", catalog.Modules.First().ModuleType);
391            Assert.AreEqual(InitializationMode.OnDemand, catalog.Modules.First().InitializationMode);
392            Assert.AreEqual(2, catalog.Modules.First().DependsOn.Count);
393            Assert.AreEqual("DependsOn1", catalog.Modules.First().DependsOn[0]);
394            Assert.AreEqual("DependsOn2", catalog.Modules.First().DependsOn[1]);
395
396        }
397
398        [TestMethod]
399        public void CanAddModule2()
400        {
401            ModuleCatalog catalog = new ModuleCatalog();
402
403            catalog.AddModule("Module", "ModuleType", "DependsOn1", "DependsOn2");
404
405            Assert.AreEqual(1, catalog.Modules.Count());
406            Assert.AreEqual("Module", catalog.Modules.First().ModuleName);
407            Assert.AreEqual("ModuleType", catalog.Modules.First().ModuleType);
408            Assert.AreEqual(InitializationMode.WhenAvailable, catalog.Modules.First().InitializationMode);
409            Assert.AreEqual(2, catalog.Modules.First().DependsOn.Count);
410            Assert.AreEqual("DependsOn1", catalog.Modules.First().DependsOn[0]);
411            Assert.AreEqual("DependsOn2", catalog.Modules.First().DependsOn[1]);
412
413        }
414        [TestMethod]
415        public void CanAddModule3()
416        {
417            ModuleCatalog catalog = new ModuleCatalog();
418
419            catalog.AddModule(typeof (MockModule), InitializationMode.OnDemand, "DependsOn1", "DependsOn2");
420
421            Assert.AreEqual(1, catalog.Modules.Count());
422            Assert.AreEqual("MockModule", catalog.Modules.First().ModuleName);
423            Assert.AreEqual(typeof(MockModule).AssemblyQualifiedName, catalog.Modules.First().ModuleType);
424            Assert.AreEqual(InitializationMode.OnDemand, catalog.Modules.First().InitializationMode);
425            Assert.AreEqual(2, catalog.Modules.First().DependsOn.Count);
426            Assert.AreEqual("DependsOn1", catalog.Modules.First().DependsOn[0]);
427            Assert.AreEqual("DependsOn2", catalog.Modules.First().DependsOn[1]);
428
429        }
430
431        [TestMethod]
432        public void CanAddModule4()
433        {
434            ModuleCatalog catalog = new ModuleCatalog();
435
436            catalog.AddModule(typeof(MockModule), "DependsOn1", "DependsOn2");
437
438            Assert.AreEqual(1, catalog.Modules.Count());
439            Assert.AreEqual("MockModule", catalog.Modules.First().ModuleName);
440            Assert.AreEqual(typeof(MockModule).AssemblyQualifiedName, catalog.Modules.First().ModuleType);
441            Assert.AreEqual(InitializationMode.WhenAvailable, catalog.Modules.First().InitializationMode);
442            Assert.AreEqual(2, catalog.Modules.First().DependsOn.Count);
443            Assert.AreEqual("DependsOn1", catalog.Modules.First().DependsOn[0]);
444            Assert.AreEqual("DependsOn2", catalog.Modules.First().DependsOn[1]);
445
446        }
447
448        [TestMethod]
449        public void CanAddGroup()
450        {
451            ModuleCatalog catalog = new ModuleCatalog();
452
453            catalog.Items.Add(new ModuleInfoGroup());
454
455            catalog.AddGroup(InitializationMode.OnDemand, "Ref1",
456                             new ModuleInfo("M1", "T1"),
457                             new ModuleInfo("M2", "T2", "M1"));
458
459            Assert.AreEqual(2, catalog.Modules.Count());
460
461            var module1 = catalog.Modules.First();
462            var module2 = catalog.Modules.Skip(1).First();
463
464            
465            Assert.AreEqual("M1", module1.ModuleName);
466            Assert.AreEqual("T1", module1.ModuleType);
467            Assert.AreEqual("Ref1", module1.Ref);
468            Assert.AreEqual(InitializationMode.OnDemand, module1.InitializationMode);
469
470            Assert.AreEqual("M2", module2.ModuleName);
471            Assert.AreEqual("T2", module2.ModuleType);
472            Assert.AreEqual("Ref1", module2.Ref);
473            Assert.AreEqual(InitializationMode.OnDemand, module2.InitializationMode);
474        }
475
476
477        private class TestableModuleCatalog : ModuleCatalog
478        {
479            public bool ValidateCalled { get; set; }
480            public bool LoadCalledFirst { get; set; }
481            public bool LoadCalled
482            {
483                get { return LoadCalledCount > 0; }
484            }
485            public int LoadCalledCount { get; set; }
486
487            public override void Validate()
488            {
489                ValidateCalled = true;
490                Validated = true;
491            }
492            
493            protected override void InnerLoad()
494            {
495                if (ValidateCalled == false && !LoadCalled)
496                    LoadCalledFirst = true;
497
498                LoadCalledCount++;
499            }
500
501        }
502
503        private static ModuleInfo CreateModuleInfo(string name, params string[] dependsOn)
504        {
505            ModuleInfo moduleInfo = new ModuleInfo(name, name);
506            moduleInfo.DependsOn.AddRange(dependsOn);
507            return moduleInfo;
508        }
509    }
510}