Example #1
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();

            Exception retrieverException = new Exception();

            moduleTypeLoader.LoadCompletedError = retrieverException;

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

            try
            {
                manager.Run();
            }
            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.");
        }
Example #2
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();

            moduleTypeLoader.LoadCompletedError = new Exception();
            manager.ModuleTypeLoaders           = new List <IModuleTypeLoader> {
                moduleTypeLoader
            };

            try
            {
                manager.Run();
            }
            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);
        }
Example #3
0
        public void ShouldNotInitializeIfDependenciesAreNotMet()
        {
            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 = m => new[] { requiredModule };

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

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

            manager.Run();

            moduleTypeLoader.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(dependantModuleInfo, null));

            Assert.IsFalse(loader.InitializeCalled);
            Assert.AreEqual(0, loader.InitializedModules.Count);
        }
Example #4
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.InitializeCalled);
            Assert.AreEqual(0, moduleRetriever.LoadedModules.Count);

            manager.LoadModule("NeedsRetrieval");

            Assert.AreEqual(1, moduleRetriever.LoadedModules.Count);
            Assert.IsTrue(loader.InitializeCalled);
            Assert.AreEqual(1, loader.InitializedModules.Count);
            Assert.AreEqual(onDemandModule, loader.InitializedModules[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.LoadedModules.Contains(moduleInfo));
        }
Example #6
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.InitializeCalled = false;
            manager.LoadModule("MockModule");

            Assert.IsFalse(loader.InitializeCalled);
        }
        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 };            
            Assert.IsFalse(loader.InitializeCalled);

            manager.Run();

            Assert.IsTrue(loader.InitializeCalled);
            Assert.AreEqual(1, loader.InitializedModules.Count);
            Assert.AreEqual(backgroungModuleInfo, loader.InitializedModules[0]);
        }
Example #8
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();
        }
Example #9
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");
        }
Example #10
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.LoadedModules.Contains(moduleInfo));
        }
        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.InitializeCalled);
            Assert.AreEqual(0, moduleRetriever.LoadedModules.Count);

            manager.LoadModule("NeedsRetrieval");

            Assert.AreEqual(1, moduleRetriever.LoadedModules.Count);
            Assert.IsTrue(loader.InitializeCalled);
            Assert.AreEqual(1, loader.InitializedModules.Count);
            Assert.AreEqual(onDemandModule, loader.InitializedModules[0]);
        }
Example #12
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");
        }
Example #13
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
            };
            Assert.IsFalse(loader.InitializeCalled);

            manager.Run();

            Assert.IsTrue(loader.InitializeCalled);
            Assert.AreEqual(1, loader.InitializedModules.Count);
            Assert.AreEqual(backgroungModuleInfo, loader.InitializedModules[0]);
        }
Example #14
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.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(onDemandModule, null));
            loader.InitializeCalled = false;

            manager.LoadModule("ModuleThatNeedsRetrieval");

            Assert.IsFalse(loader.InitializeCalled);
        }
Example #15
0
        public void ShouldNotLoadTypeIfModuleInitialized()
        {
            var loader = new MockModuleInitializer();
            var alreadyPresentModule = CreateModuleInfo(typeof(MockModule), InitializationMode.WhenAvailable);

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

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

            manager.Run();

            Assert.IsFalse(moduleTypeLoader.LoadedModules.Contains(alreadyPresentModule));
            Assert.IsTrue(loader.InitializeCalled);
            Assert.AreEqual(1, loader.InitializedModules.Count);
            Assert.AreEqual(alreadyPresentModule, loader.InitializedModules[0]);
        }
Example #16
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);
        }
Example #17
0
        public void ShouldInitializeIfDependenciesAreMet()
        {
            var initializer    = 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(initializer, catalog, new MockLogger());
            var           moduleTypeLoader = new MockModuleTypeLoader();

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

            manager.Run();

            Assert.IsTrue(initializer.InitializeCalled);
            Assert.AreEqual(2, initializer.InitializedModules.Count);
        }
        public void ShouldNotLoadTypeIfModuleInitialized()
        {
            var loader = new MockModuleInitializer();
            var alreadyPresentModule = CreateModuleInfo(typeof(MockModule), InitializationMode.WhenAvailable);
            alreadyPresentModule.State = ModuleState.ReadyForInitialization;
            var catalog = new MockModuleCatalog { Modules = { alreadyPresentModule } };
            var manager = new ModuleManager(loader, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };

            manager.Run();

            Assert.IsFalse(moduleTypeLoader.LoadedModules.Contains(alreadyPresentModule));
            Assert.IsTrue(loader.InitializeCalled);
            Assert.AreEqual(1, loader.InitializedModules.Count);
            Assert.AreEqual(alreadyPresentModule, loader.InitializedModules[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.InitializeCalled = false;
            manager.LoadModule("MockModule");

            Assert.IsFalse(loader.InitializeCalled);
        }
 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();
 }
        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();
            moduleTypeLoader.LoadCompletedError = new Exception();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };

            try
            {
                manager.Run();
            }
            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);
        }
        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();

            Exception retrieverException = new Exception();
            moduleTypeLoader.LoadCompletedError = retrieverException;

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

            try
            {
                manager.Run();
            }
            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.");
        }
        public void ShouldInitializeIfDependenciesAreMet()
        {
            var initializer = 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(initializer, catalog, new MockLogger());
            var moduleTypeLoader = new MockModuleTypeLoader();
            manager.ModuleTypeLoaders = new List<IModuleTypeLoader> { moduleTypeLoader };

            manager.Run();

            Assert.IsTrue(initializer.InitializeCalled);
            Assert.AreEqual(2, initializer.InitializedModules.Count);
        }
        public void ShouldNotInitializeIfDependenciesAreNotMet()
        {
            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 = m => new[] { requiredModule };

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

            manager.Run();

            moduleTypeLoader.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(dependantModuleInfo, null));            

            Assert.IsFalse(loader.InitializeCalled);
            Assert.AreEqual(0, loader.InitializedModules.Count);
        }
        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);
        }
        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.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(onDemandModule, null));
            loader.InitializeCalled = false;

            manager.LoadModule("ModuleThatNeedsRetrieval");

            Assert.IsFalse(loader.InitializeCalled);
        }
        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");
        }
        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");
        }