PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/CAL/Composite.Tests/Modularity/ConfigurationModuleEnumeratorFixture.cs

#
C# | 182 lines | 132 code | 34 blank | 16 comment | 3 complexity | 9f3ad61ea88537345f557712fa65608a MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation
  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.Configuration;
 21using Microsoft.Practices.Composite.Modularity;
 22using Microsoft.Practices.Composite.Tests.Mocks;
 23using Microsoft.VisualStudio.TestTools.UnitTesting;
 24
 25namespace Microsoft.Practices.Composite.Tests.Modularity
 26{
 27    [TestClass]
 28    public class ConfigurationModuleEnumeratorFixture
 29    {
 30        [TestMethod]
 31        public void CanInitConfigModuleEnumerator()
 32        {
 33            MockConfigurationStore store = new MockConfigurationStore();
 34            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
 35            Assert.IsNotNull(enumerator);
 36        }
 37
 38        [TestMethod]
 39        [ExpectedException(typeof(ArgumentNullException))]
 40        public void NullConfigurationStoreThrows()
 41        {
 42            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(null);
 43        }
 44
 45        [TestMethod]
 46        public void CanInitWithDefaultConstructor()
 47        {
 48            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator();
 49            Assert.IsNotNull(enumerator);
 50        }
 51
 52
 53        [TestMethod]
 54        public void ShouldReturnAListOfModuleInfo()
 55        {
 56            MockConfigurationStore store = new MockConfigurationStore();
 57            store.Modules = new[] { new ModuleConfigurationElement(@"MocksModules\MockModuleA.dll", "Microsoft.Composite.Tests.Mocks.Modules.MockModuleA", "MockModuleA", false) };
 58
 59            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
 60
 61            ModuleInfo[] modules = enumerator.GetModules();
 62
 63            Assert.IsNotNull(modules);
 64            Assert.AreEqual(1, modules.Length);
 65            Assert.IsNotNull(modules[0].AssemblyFile);
 66            Assert.IsTrue(modules[0].AssemblyFile.Contains(@"MocksModules\MockModuleA.dll"));
 67            Assert.IsNotNull(modules[0].ModuleType);
 68            Assert.AreEqual("Microsoft.Composite.Tests.Mocks.Modules.MockModuleA", modules[0].ModuleType);
 69            Assert.IsFalse(modules[0].StartupLoaded);
 70        }
 71
 72        [TestMethod]
 73        public void GetZeroModules()
 74        {
 75            MockConfigurationStore store = new MockConfigurationStore();
 76            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
 77
 78            ModuleInfo[] modules = enumerator.GetModules();
 79
 80            Assert.AreEqual(0, modules.Length);
 81        }
 82
 83        [TestMethod]
 84        public void EnumeratesThreeModulesWithDependencies()
 85        {
 86            var store = new MockConfigurationStore();
 87            var module1 = new ModuleConfigurationElement("Module1.dll", "Test.Module1", "Module1", false);
 88            module1.Dependencies = new ModuleDependencyCollection(
 89                new[] { new ModuleDependencyConfigurationElement("Module2") });
 90
 91            var module2 = new ModuleConfigurationElement("Module2.dll", "Test.Module2", "Module2", false);
 92            module2.Dependencies = new ModuleDependencyCollection(
 93                new[] { new ModuleDependencyConfigurationElement("Module3") });
 94
 95            var module3 = new ModuleConfigurationElement("Module3.dll", "Test.Module3", "Module3", false);
 96            store.Modules = new[] { module3, module2, module1 };
 97
 98            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
 99
100            var modules = new List<ModuleInfo>(enumerator.GetModules());
101
102            Assert.AreEqual(3, modules.Count);
103            Assert.IsTrue(modules.Exists(module => module.ModuleName == "Module1"));
104            Assert.IsTrue(modules.Exists(module => module.ModuleName == "Module2"));
105            Assert.IsTrue(modules.Exists(module => module.ModuleName == "Module3"));
106        }
107
108        [TestMethod]
109        [ExpectedException(typeof(ConfigurationErrorsException))]
110        public void EnumerateThrowsIfDuplicateNames()
111        {
112            MockConfigurationStore store = new MockConfigurationStore();
113            var module1 = new ModuleConfigurationElement("Module1.dll", "Test.Module1", "Module1", false);
114            var module2 = new ModuleConfigurationElement("Module2.dll", "Test.Module2", "Module1", false);
115            store.Modules = new[] { module2, module1 };
116            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
117
118            ModuleInfo[] modules = enumerator.GetModules();
119        }
120
121        [TestMethod]
122        public void EnumerateNotThrowsIfDuplicateAssemblyFile()
123        {
124            MockConfigurationStore store = new MockConfigurationStore();
125            var module1 = new ModuleConfigurationElement("Module1.dll", "Test.Module1", "Module1", false);
126            var module2 = new ModuleConfigurationElement("Module1.dll", "Test.Module2", "Module2", false);
127            store.Modules = new[] { module2, module1 };
128            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
129
130            Assert.AreEqual(2, enumerator.GetModules().Length);
131        }
132
133        [TestMethod]
134        public void GetStartupLoadedModulesDoesntRetrieveOnDemandLoaded()
135        {
136            MockConfigurationStore store = new MockConfigurationStore();
137            var module1 = new ModuleConfigurationElement("Module1.dll", "Test.Module1", "Module1", false);
138
139            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
140            store.Modules = new[] { module1 };
141
142            Assert.AreEqual<int>(1, enumerator.GetModules().Length);
143            Assert.AreEqual<int>(0, enumerator.GetStartupLoadedModules().Length);
144        }
145
146        [TestMethod]
147        public void GetModuleReturnsOnlySpecifiedModule()
148        {
149            MockConfigurationStore store = new MockConfigurationStore();
150            var module1 = new ModuleConfigurationElement("Module1.dll", "Test.Module1", "Module1", false);
151
152            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
153            store.Modules = new[] { module1 };
154
155            var module = enumerator.GetModule("Module1");
156
157            Assert.IsNotNull(module);
158            Assert.AreEqual("Module1", module.ModuleName);
159        }
160
161        [TestMethod]
162        public void GetModulesNotThrownIfModuleSectionIsNotDeclared()
163        {
164            MockNullConfigurationStore store = new MockNullConfigurationStore();
165
166            IModuleEnumerator enumerator = new ConfigurationModuleEnumerator(store);
167
168            var modules = enumerator.GetModules();
169
170            Assert.IsNotNull(modules);
171            Assert.AreEqual(0, modules.Length);
172        }
173    }
174
175    internal class MockNullConfigurationStore : ConfigurationStore
176    {
177        public override ModulesConfigurationSection RetrieveModuleConfigurationSection()
178        {
179            return null;
180        }
181    }
182}