Пример #1
0
        public void ShouldNotInitializeModulesSynchronously()
        {
            var loader = new MockModuleInitializer();
            var catalog = new MockModuleCatalog { Modules = { CreateModuleInfo("ModuleThatNeedsRetrieval", InitializationMode.WhenAvailable) } };
            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { new MockModuleTypeLoader() };
            manager.Run();

            Assert.IsFalse(loader.LoadCalled);
        }
Пример #2
0
        public void ShouldInvokeRetrieverForModules()
        {
            var loader = new MockModuleInitializer();
            var moduleInfo = CreateModuleInfo("needsRetrieval", InitializationMode.WhenAvailable);
            var catalog = new MockModuleCatalog { Modules = { moduleInfo } };
            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };

            manager.Run();

            Assert.IsTrue(moduleTypeLoader.beginLoadModuleTypeCalls.Contains(moduleInfo));
        }
Пример #3
0
        public void ShouldInitializeModulesOnRetrievalCompleted()
        {
            var loader = new MockModuleInitializer();
            var backgroungModuleInfo = CreateModuleInfo("NeedsRetrieval", InitializationMode.WhenAvailable);
            var catalog = new MockModuleCatalog { Modules = { backgroungModuleInfo } };
            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };
            manager.Run();
            Assert.IsFalse(loader.LoadCalled);

            moduleTypeLoader.RaiseCallbackForModule(backgroungModuleInfo);

            Assert.IsTrue(loader.LoadCalled);
            Assert.AreEqual(1, loader.LoadedModules.Count);
            Assert.AreEqual(backgroungModuleInfo, loader.LoadedModules[0]);
        }
Пример #4
0
        public void ShouldInitializeIfDependenciesAreMet()
        {
            var loader         = new MockModuleInitializer();
            var requiredModule = CreateModuleInfo("ModuleThatNeedsRetrieval1", InitializationMode.WhenAvailable);

            requiredModule.ModuleName = "RequiredModule";
            var dependantModuleInfo = CreateModuleInfo("ModuleThatNeedsRetrieval2", InitializationMode.WhenAvailable, "RequiredModule");

            var catalog = new MockModuleCatalog {
                Modules = { requiredModule, dependantModuleInfo }
            };

            catalog.GetDependentModules = delegate(ModuleInfo module)
            {
                if (module == dependantModuleInfo)
                {
                    return new[] { requiredModule }
                }
                ;
                else
                {
                    return(null);
                }
            };

            ModuleManager manager          = new ModuleManager(loader, catalog, new MockLogger());
            var           moduleTypeLoader = new MockModuleTypeLoader();

            manager.ModuleTypeLoaders = new List <IModuleTypeLoader> {
                moduleTypeLoader
            };

            manager.Run();

            moduleTypeLoader.RaiseCallbackForModule(dependantModuleInfo);
            moduleTypeLoader.RaiseCallbackForModule(requiredModule);

            Assert.IsTrue(loader.LoadCalled);
            Assert.AreEqual(2, loader.LoadedModules.Count);
        }
Пример #5
0
        public void ShouldNotLoadModuleThatNeedsRetrievalTwice()
        {
            var loader         = new MockModuleInitializer();
            var onDemandModule = CreateModuleInfo("ModuleThatNeedsRetrieval", InitializationMode.OnDemand);
            var catalog        = new MockModuleCatalog {
                Modules = { onDemandModule }
            };
            var manager          = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();

            manager.ModuleTypeLoaders = new List <IModuleTypeLoader> {
                moduleTypeLoader
            };
            manager.Run();
            manager.LoadModule("ModuleThatNeedsRetrieval");
            moduleTypeLoader.RaiseCallbackForModule(onDemandModule);
            loader.LoadCalled = false;

            manager.LoadModule("ModuleThatNeedsRetrieval");

            Assert.IsFalse(loader.LoadCalled);
        }
Пример #6
0
        public void ShouldInitializeModulesOnRetrievalCompleted()
        {
            var loader = new MockModuleInitializer();
            var backgroungModuleInfo = CreateModuleInfo("NeedsRetrieval", InitializationMode.WhenAvailable);
            var catalog = new MockModuleCatalog {
                Modules = { backgroungModuleInfo }
            };
            ModuleManager manager          = new ModuleManager(loader, catalog, new MockLogger());
            var           moduleTypeLoader = new MockModuleTypeLoader();

            manager.ModuleTypeLoaders = new List <IModuleTypeLoader> {
                moduleTypeLoader
            };
            manager.Run();
            Assert.IsFalse(loader.LoadCalled);

            moduleTypeLoader.RaiseCallbackForModule(backgroungModuleInfo);

            Assert.IsTrue(loader.LoadCalled);
            Assert.AreEqual(1, loader.LoadedModules.Count);
            Assert.AreEqual(backgroungModuleInfo, loader.LoadedModules[0]);
        }
Пример #7
0
        public void ShouldCallValidateCatalogBeforeGettingGroupsFromCatalog()
        {
            var  loader  = new MockModuleInitializer();
            var  catalog = new MockModuleCatalog();
            var  manager = new ModuleManager(loader, catalog, new MockLogger());
            bool validateCatalogCalled          = false;
            bool getModulesCalledBeforeValidate = false;

            catalog.ValidateCatalog = () => validateCatalogCalled = true;
            catalog.CompleteListWithDependencies = f =>
            {
                if (!validateCatalogCalled)
                {
                    getModulesCalledBeforeValidate = true;
                }

                return(null);
            };
            manager.Run();

            Assert.IsTrue(validateCatalogCalled);
            Assert.IsFalse(getModulesCalledBeforeValidate);
        }
Пример #8
0
        public void ShouldNotLoadTypeIfModuleInitialized()
        {
            var loader = new MockModuleInitializer();
            var alreadedPresentModule = CreateModuleInfo(typeof(MockModule), InitializationMode.WhenAvailable);

            alreadedPresentModule.State = ModuleState.ReadyForInitialization;
            var catalog = new MockModuleCatalog {
                Modules = { alreadedPresentModule }
            };
            var manager          = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();

            manager.ModuleTypeLoaders = new List <IModuleTypeLoader> {
                moduleTypeLoader
            };

            manager.Run();

            Assert.IsFalse(moduleTypeLoader.beginLoadModuleTypeCalls.Contains(alreadedPresentModule));
            Assert.IsTrue(loader.LoadCalled);
            Assert.AreEqual(1, loader.LoadedModules.Count);
            Assert.AreEqual(alreadedPresentModule, loader.LoadedModules[0]);
        }
Пример #9
0
        public void ShouldNotLoadModuleThatNeedsRetrievalTwice()
        {
            var loader = new MockModuleInitializer();
            var onDemandModule = CreateModuleInfo("ModuleThatNeedsRetrieval", InitializationMode.OnDemand);
            var catalog = new MockModuleCatalog { Modules = { onDemandModule } };
            var manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };
            manager.Run();
            manager.LoadModule("ModuleThatNeedsRetrieval");
            moduleTypeLoader.RaiseCallbackForModule(onDemandModule);
            loader.LoadCalled = false;

            manager.LoadModule("ModuleThatNeedsRetrieval");

            Assert.IsFalse(loader.LoadCalled);
        }
Пример #10
0
        public void ShouldWorkIfModuleLoadsAnotherOnDemandModuleWhenInitializing()
        {
            var initializer = new StubModuleInitializer();
            var onDemandModule = CreateModuleInfo(typeof(MockModule), InitializationMode.OnDemand);
            onDemandModule.ModuleName = "OnDemandModule";
            var moduleThatLoadsOtherModule = CreateModuleInfo(typeof(MockModule), InitializationMode.WhenAvailable);
            var catalog = new MockModuleCatalog { Modules = { moduleThatLoadsOtherModule, onDemandModule } };
            ModuleManager manager = new ModuleManager(initializer, catalog, new MockLogger());
            
            bool onDemandModuleWasInitialized = false;
            initializer.Initialize = m =>
                                     {
                                         if (m == moduleThatLoadsOtherModule)
                                         {
                                             manager.LoadModule("OnDemandModule");
                                         }
                                         else if (m == onDemandModule)
                                         {
                                             onDemandModuleWasInitialized = true;
                                         }
                                     };

            manager.Run();

            Assert.IsTrue(onDemandModuleWasInitialized);
        }
Пример #11
0
        public void InvalidOnDemandModuleNameThrows()
        {
            var loader = new MockModuleInitializer();

            var catalog = new MockModuleCatalog { Modules = new List<ModuleInfo> { CreateModuleInfo("Missing", InitializationMode.OnDemand) } };

            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };
            manager.Run();

            manager.LoadModule("NonExistent");
        }
Пример #12
0
 public void ShouldThrowIfNoRetrieverCanRetrieveModule()
 {
     var loader = new MockModuleInitializer();
     var catalog = new MockModuleCatalog { Modules = { CreateModuleInfo("ModuleThatNeedsRetrieval", InitializationMode.WhenAvailable) } };
     ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
     manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { new MockModuleTypeLoader() { canLoadModuleTypeReturnValue = false } };
     manager.Run();
 }
Пример #13
0
        public void ShouldLogMessageOnModuleRetrievalError()
        {
            var loader = new MockModuleInitializer();
            var moduleInfo = CreateModuleInfo("ModuleThatNeedsRetrieval", InitializationMode.WhenAvailable);
            var catalog = new MockModuleCatalog { Modules = { moduleInfo } };
            var logger = new MockLogger();
            ModuleManager manager = new ModuleManager(loader, catalog, logger);
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };
            manager.Run();

            try
            {
                moduleTypeLoader.RaiseCallbackForModule(moduleInfo, new Exception());
            }
            catch
            {
                // Ignore all errors to make sure logger is called even if errors thrown.
            }

            Assert.IsNotNull(logger.LastMessage);
            StringAssert.Contains(logger.LastMessage, "ModuleThatNeedsRetrieval");
            Assert.AreEqual<Category>(Category.Exception, logger.LastMessageCategory);
        }
Пример #14
0
        public void ShouldThrowOnRetrieverErrorAndWrapException()
        {
            var loader = new MockModuleInitializer();
            var moduleInfo = CreateModuleInfo("NeedsRetrieval", InitializationMode.WhenAvailable);
            var catalog = new MockModuleCatalog { Modules = { moduleInfo } };
            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };
            manager.Run();
            Assert.IsFalse(loader.LoadCalled);

            Exception retrieverException = new Exception();

            try
            {
                moduleTypeLoader.RaiseCallbackForModule(moduleInfo, retrieverException);
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ModuleTypeLoadingException));
                Assert.AreEqual(moduleInfo.ModuleName, ((ModularityException)ex).ModuleName);
                StringAssert.Contains(ex.Message, moduleInfo.ModuleName);
                Assert.AreSame(retrieverException, ex.InnerException);
                return;
            }

            Assert.Fail("Exception not thrown.");
        }
Пример #15
0
        public void ShouldInitializeIfDependenciesAreMet()
        {
            var loader = new MockModuleInitializer();
            var requiredModule = CreateModuleInfo("ModuleThatNeedsRetrieval1", InitializationMode.WhenAvailable);
            requiredModule.ModuleName = "RequiredModule";
            var dependantModuleInfo = CreateModuleInfo("ModuleThatNeedsRetrieval2", InitializationMode.WhenAvailable, "RequiredModule");

            var catalog = new MockModuleCatalog { Modules = { requiredModule, dependantModuleInfo } };
            catalog.GetDependentModules = delegate(ModuleInfo module)
                                              {
                                                  if (module == dependantModuleInfo)
                                                      return new[] { requiredModule };
                                                  else
                                                      return null;
                                              };

            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };

            manager.Run();

            moduleTypeLoader.RaiseCallbackForModule(dependantModuleInfo);
            moduleTypeLoader.RaiseCallbackForModule(requiredModule);

            Assert.IsTrue(loader.LoadCalled);
            Assert.AreEqual(2, loader.LoadedModules.Count);
        }
Пример #16
0
        public void EmptyOnDemandModuleReturnedThrows()
        {
            var loader = new MockModuleInitializer();

            var catalog = new MockModuleCatalog { CompleteListWithDependencies = modules => new List<ModuleInfo>() };
            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleRetriever = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleRetriever };
            manager.Run();

            manager.LoadModule("NullModule");
        }
Пример #17
0
        public void ShouldNotLoadTypeIfModuleInitialized()
        {
            var loader = new MockModuleInitializer();
            var alreadedPresentModule = CreateModuleInfo(typeof(MockModule), InitializationMode.WhenAvailable);
            alreadedPresentModule.State = ModuleState.ReadyForInitialization;
            var catalog = new MockModuleCatalog { Modules = { alreadedPresentModule } };
            var manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };

            manager.Run();

            Assert.IsFalse(moduleTypeLoader.beginLoadModuleTypeCalls.Contains(alreadedPresentModule));
            Assert.IsTrue(loader.LoadCalled);
            Assert.AreEqual(1, loader.LoadedModules.Count);
            Assert.AreEqual(alreadedPresentModule, loader.LoadedModules[0]);
        }
Пример #18
0
        public void ShouldNotLoadSameModuleTwice()
        {
            var loader = new MockModuleInitializer();
            var onDemandModule = CreateModuleInfo(typeof(MockModule), InitializationMode.OnDemand);
            var catalog = new MockModuleCatalog { Modules = { onDemandModule } };
            var manager = new ModuleManager(loader, catalog, new MockLogger());
            manager.Run();
            manager.LoadModule("MockModule");
            loader.LoadCalled = false;
            manager.LoadModule("MockModule");

            Assert.IsFalse(loader.LoadCalled);
        }
Пример #19
0
        public void ShouldInitializeModuleOnDemand()
        {
            var loader = new MockModuleInitializer();
            var onDemandModule = CreateModuleInfo("NeedsRetrieval", InitializationMode.OnDemand);
            var catalog = new MockModuleCatalog { Modules = { onDemandModule } };
            ModuleManager manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleRetriever = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleRetriever };
            manager.Run();

            Assert.IsFalse(loader.LoadCalled);
            Assert.AreEqual(0, moduleRetriever.beginLoadModuleTypeCalls.Count);

            manager.LoadModule("NeedsRetrieval");

            Assert.AreEqual(1, moduleRetriever.beginLoadModuleTypeCalls.Count);

            moduleRetriever.RaiseCallbackForModule(onDemandModule);

            Assert.IsTrue(loader.LoadCalled);
            Assert.AreEqual(1, loader.LoadedModules.Count);
            Assert.AreEqual(onDemandModule, loader.LoadedModules[0]);
        }
Пример #20
0
        public void ShouldCallValidateCatalogBeforeGettingGroupsFromCatalog()
        {
            var loader = new MockModuleInitializer();
            var catalog = new MockModuleCatalog();
            var manager = new ModuleManager(loader, catalog, new MockLogger());
            bool validateCatalogCalled = false;
            bool getModulesCalledBeforeValidate = false;

            catalog.ValidateCatalog = () => validateCatalogCalled = true;
            catalog.CompleteListWithDependencies = f =>
                                                     {
                                                         if (!validateCatalogCalled)
                                                         {
                                                             getModulesCalledBeforeValidate = true;
                                                         }

                                                         return null;
                                                     };
            manager.Run();

            Assert.IsTrue(validateCatalogCalled);
            Assert.IsFalse(getModulesCalledBeforeValidate);
        }