public void LoaderDoesNotLoadModulesExcludedBasedOnRole()
        {
            IPrincipal    oldPrincipal = Thread.CurrentPrincipal;
            StringBuilder sb           = new StringBuilder();

            using (ConsoleHelper consoleHelper = new ConsoleHelper())
            {
                try
                {
                    GenericIdentity  identity  = new GenericIdentity("Bob");
                    GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "Bar", "Baz" });
                    Thread.CurrentPrincipal = principal;
                    WorkItem             wi  = new TestableRootWorkItem();
                    IModuleLoaderService svc = new DependentModuleLoaderService(null);
                    string moduleAFilename   = Guid.NewGuid().ToString() + ".dll";
                    string moduleBFilename   = ModuleCompilerHelper.GenerateDynamicModule("ModuleAAC", "ModuleB");

                    ModuleInfo[] modules = new ModuleInfo[2];
                    modules[0] = new ModuleInfo(moduleAFilename);
                    modules[0].AddRoles("Foo");
                    modules[1] = new ModuleInfo(moduleBFilename);
                    modules[1].AddRoles("Bar");
                    modules[1].AddRoles("Baz");

                    svc.Load(wi, modules);

                    Assert.AreEqual(1, svc.LoadedModules.Count);
                    Assert.AreEqual("ModuleB", svc.LoadedModules[0].Name);
                }
                finally
                {
                    Thread.CurrentPrincipal = oldPrincipal;
                }
            }
        }
        public void LoaderExcludesModulesBasedOnRoles()
        {
            IPrincipal    oldPrincipal       = Thread.CurrentPrincipal;
            TextWriter    originalConsoleOut = Console.Out;
            StringBuilder sb = new StringBuilder();

            Console.SetOut(new StringWriter(sb));

            try
            {
                GenericIdentity  identity  = new GenericIdentity("Bob");
                GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "Bar" });
                Thread.CurrentPrincipal = principal;
                WorkItem             wi  = new TestableRootWorkItem();
                IModuleLoaderService svc = new DependentModuleLoaderService(null);
                string moduleAFilename   = ModuleCompilerHelper.GenerateDynamicModule("ModuleAAA", "ModuleA");
                string moduleBFilename   = ModuleCompilerHelper.GenerateDynamicModule("ModuleAAB", "ModuleB");

                ModuleInfo[] modules = new ModuleInfo[2];
                modules[0] = new ModuleInfo(moduleAFilename);
                modules[0].AddRoles("Foo");
                modules[1] = new ModuleInfo(moduleBFilename);
                modules[1].AddRoles("Bar");

                svc.Load(wi, modules);

                Assert.AreEqual(1, svc.LoadedModules.Count);
                Assert.AreEqual("ModuleB", svc.LoadedModules[0].Name);
            }
            finally
            {
                Thread.CurrentPrincipal = oldPrincipal;
                Console.SetOut(originalConsoleOut);
            }
        }
        public void FailWhenDependingOnMissingModule()
        {
            ModuleInfo module = new ModuleInfo(ModuleCompilerHelper.GenerateDynamicModule("ModuleK", null, "ModuleL"));

            WorkItem mockContainer = new TestableRootWorkItem();
            DependentModuleLoaderService loader = new DependentModuleLoaderService(null);

            loader.Load(mockContainer, module);
        }
        public void LoadProfileWithAcyclicModuleDependencies()
        {
            List <string> assemblies = new List <string>();

            // Create several modules with this dependency graph (X->Y meaning Y depends on X)
            // a->b, b->c, b->d, c->e, d->e, f
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleA", "ModuleA"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleB", "ModuleB", "ModuleA"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleC", "ModuleC", "ModuleB"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleD", "ModuleD", "ModuleB"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleE", "ModuleE", "ModuleC", "ModuleD"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleF", "ModuleF"));

            ModuleInfo[] Modules = new ModuleInfo[assemblies.Count];
            for (int i = 0; i < assemblies.Count; i++)
            {
                Modules[i] = new ModuleInfo(assemblies[i]);
            }

            TextWriter consoleOut = Console.Out;

            StringBuilder sb = new StringBuilder();

            Console.SetOut(new StringWriter(sb));
            WorkItem mockContainer = new TestableRootWorkItem();
            DependentModuleLoaderService loader = new DependentModuleLoaderService(null);

            loader.Load(mockContainer, Modules);

            List <string> trace =
                new List <string>(sb.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

            Assert.AreEqual(12, trace.Count);
            Assert.IsTrue(trace.IndexOf("ModuleE.AddServices") > trace.IndexOf("ModuleC.AddServices"),
                          "ModuleC must precede ModuleE");
            Assert.IsTrue(trace.IndexOf("ModuleE.AddServices") > trace.IndexOf("ModuleD.AddServices"),
                          "ModuleD must precede ModuleE");
            Assert.IsTrue(trace.IndexOf("ModuleD.AddServices") > trace.IndexOf("ModuleB.AddServices"),
                          "ModuleB must precede ModuleD");
            Assert.IsTrue(trace.IndexOf("ModuleC.AddServices") > trace.IndexOf("ModuleB.AddServices"),
                          "ModuleB must precede ModuleC");
            Assert.IsTrue(trace.IndexOf("ModuleB.AddServices") > trace.IndexOf("ModuleA.AddServices"),
                          "ModuleA must precede ModuleB");
            Assert.IsTrue(trace.Contains("ModuleF.AddServices"), "ModuleF must be loaded");
            Console.SetOut(consoleOut);
        }
        public void FailWhenLoadingModulesWithCyclicDependencies()
        {
            List <string> assemblies = new List <string>();

            // Create several modules with this dependency graph (X->Y meaning Y depends on X)
            // 1->2, 2->3, 3->4, 4->5, 4->2
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("Module1", "Module1"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("Module2", "Module2", "Module1", "Module4"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("Module3", "Module3", "Module2"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("Module4", "Module4", "Module3"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("Module5", "Module5", "Module4"));

            ModuleInfo[] modules = new ModuleInfo[assemblies.Count];
            for (int i = 0; i < assemblies.Count; i++)
            {
                modules[i] = new ModuleInfo(assemblies[i]);
            }
            WorkItem mockContainer = new TestableRootWorkItem();
            DependentModuleLoaderService loader = new DependentModuleLoaderService(null);

            loader.Load(mockContainer, modules);
        }
        public void CanLoadAnonymousModulesWithDepedencies()
        {
            List <string> assemblies = new List <string>();

            // Create several modules with this dependency graph (X->Y meaning Y depends on X)
            // a->b, b->c, b->d, c->e, d->e, f
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleX", "ModuleX"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleY", null, "ModuleX"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleP", "ModuleP"));
            assemblies.Add(ModuleCompilerHelper.GenerateDynamicModule("ModuleQ", null, "ModuleP"));

            ModuleInfo[] modules = new ModuleInfo[assemblies.Count];
            for (int i = 0; i < assemblies.Count; i++)
            {
                modules[i] = new ModuleInfo(assemblies[i]);
            }

            TextWriter consoleOut = Console.Out;

            StringBuilder sb = new StringBuilder();

            Console.SetOut(new StringWriter(sb));
            WorkItem mockContainer = new TestableRootWorkItem();
            DependentModuleLoaderService loader = new DependentModuleLoaderService(null);

            loader.Load(mockContainer, modules);

            List <string> trace =
                new List <string>(sb.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

            Assert.AreEqual(8, trace.Count);
            Assert.IsTrue(trace.IndexOf("ModuleX.AddServices") < trace.IndexOf("ModuleY.AddServices"),
                          "ModuleX must precede ModuleY");
            Assert.IsTrue(trace.IndexOf("ModuleP.AddServices") < trace.IndexOf("ModuleQ.AddServices"),
                          "ModuleP must precede ModuleQ");
            Console.SetOut(consoleOut);
        }
        public void LoadSampleModule()
        {
            WorkItem             container = new TestableRootWorkItem();
            IModuleLoaderService loader    = new DependentModuleLoaderService(null);

            container.Services.Add(typeof(IModuleLoaderService), loader);
            int originalCount = container.Items.Count;

            ModuleInfo info = new ModuleInfo();

            info.SetAssemblyFile(ModuleCompilerHelper.GenerateDynamicModule("SampleModule", "SampleModule"));

            TextWriter    consoleOut = Console.Out;
            StringBuilder sb         = new StringBuilder();

            Console.SetOut(new StringWriter(sb));

            loader.Load(container, info);

            Assert.AreEqual(1, container.Items.Count - originalCount);

            bool foundUs = false;

            foreach (KeyValuePair <string, object> pair in container.Items)
            {
                if (pair.Value.GetType().FullName == "TestModules.SampleModuleClass")
                {
                    foundUs = true;
                    break;
                }
            }

            Assert.IsTrue(foundUs);

            Console.SetOut(consoleOut);
        }