public void GetAssemblies_ReturnsAllControllerAssemblies()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();

            var assemblyFiles = new[] { "No Controllers", "No Controllers", "Controllers", "No Controllers", "Controllers" };

            initializer.RetrieveAssembliesFileNamesMock = () => assemblyFiles;
            initializer.IsControllerContainerMock = asmFileName => asmFileName.Equals("Controllers");

            var triedToLoad = new List<string>(2);
            initializer.LoadAssemblyMock = asmFileName =>
                {
                    triedToLoad.Add(asmFileName);
                    return Assembly.GetExecutingAssembly();
                };

            var triedToInitializeContainers = new List<string>(2);
            initializer.InitializeControllerContainerMock = assembly => triedToInitializeContainers.Add(assembly.FullName);

            // Act
            var result = initializer.GetAssembliesPublic();

            // Assert
            Assert.AreEqual(2, result.Count(), "Not all controller assemblies were returned.");
            Assert.AreEqual(2, triedToLoad.Count, "Not all controller assemblies were loaded.");
            Assert.AreEqual(2, triedToInitializeContainers.Count, "Not all controller assemblies were initialized.");
            Assert.IsFalse(triedToLoad.Any(asmFile => !asmFile.Equals("Controllers")), "Some assemblies were loaded that were not controller assemblies.");
        }
        public void GetLabel_DummyLocalizedController_EnsuresTheResourceStringIsFound()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();
            Type controller = typeof(DummyLocalizedController);

            var context = new ViewContext();
            context.Controller = new DummyLocalizedController();
            var urlHelper = new HtmlHelper(context, new DummyViewDataContainer());

            using (new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType<ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(), new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType<XmlConfigProvider, DummyConfigProvider>();
                Config.RegisterSection<ResourcesConfig>();
                Config.RegisterSection<ProjectConfig>();

                // Act
                initializer.RegisterControllerPublic(controller);

                var resourceString = urlHelper.Resource("DummyResource");

                // Assert
                var resourceRegistered = ObjectFactory.Container.IsRegistered(typeof(DummyLocalizationControllerResources), Res.GetResourceClassId(typeof(DummyLocalizationControllerResources)));
                Assert.IsTrue(resourceRegistered, "String resources were not registered for the controller.");
                Assert.IsFalse(resourceString.IsNullOrEmpty(), "The resource with the given key was not found");
                Assert.AreEqual("Dummy Resource", resourceString, "The returned resource is not as expected");
            }
        }
        public void GetControllers_CurrentAssembly_GetsControllers()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();

            // Act
            var types = initializer.GetControllersPublic(new[] { AssemblyLoaderHelper.GetTestUtilitiesAssembly() });

            // Assert
            Assert.IsNotNull(types, "GetControllers returned null.");

            // Don't check for exact count. They can change while this assembly grows.
            Assert.IsTrue(types.Any(), "No controllers were found.");
        }
        public void RegisterVirtualPaths_CurrentAssembly_VirtualPathAndRouteRegistered()
        {
            // Arrange
            Assembly assembly = Assembly.GetExecutingAssembly();
            PathDefinition definition = null;
            var initializer = new DummyControllerContainerInitializer();

            using (new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType<ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(), new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType<XmlConfigProvider, DummyConfigProvider>();
                ObjectFactory.Container.RegisterType<IResourceResolverStrategy, DummyResolverStrategy>(new ContainerControlledLifetimeManager());

                var strategy = (DummyResolverStrategy)ObjectFactory.Container.Resolve<IResourceResolverStrategy>();
                strategy.ExistsMock = (def, vp) =>
                    {
                        definition = def;
                        return true;
                    };

                Config.RegisterSection<VirtualPathSettingsConfig>();
                Config.RegisterSection<ControlsConfig>();

                // Act
                initializer.RegisterVirtualPathsPublic(new[] { assembly });

                VirtualPathManager.FileExists("~/" + FrontendManager.VirtualPathBuilder.GetVirtualPath(assembly));
            }

            // Assert
            Assert.AreNotEqual(0, RouteTable.Routes.Count, "No routes were registered.");
            Assert.IsNotNull(RouteTable.Routes.OfType<Route>().FirstOrDefault(r => r.Url == "Frontend-Assembly/Telerik.Sitefinity.Frontend.TestUnit/{*Params}"));
            Assert.IsNotNull(definition, "Virtual path definition was not found.");
            Assert.AreEqual(definition.ResourceLocation, assembly.CodeBase, "The resolved virtual path definition was not expected.");
        }
        public void RegisterController_DummyController_IsRegisteredInStore()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();
            Type controller = typeof(DummyController);

            using (new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType<ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(), new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType<XmlConfigProvider, DummyConfigProvider>();
                Config.RegisterSection<ResourcesConfig>();
                Config.RegisterSection<ProjectConfig>();

                // Act
                initializer.RegisterControllerPublic(controller);

                // Assert
                var resourceRegistered = ObjectFactory.Container.IsRegistered(typeof(DummyControllerResources), Res.GetResourceClassId(typeof(DummyControllerResources)));
                Assert.IsTrue(resourceRegistered, "String resources were not registered for the controller.");
            }

            ControllerInfo registration = ControllerStore.Controllers().SingleOrDefault(c => c.ControllerType == controller);
            Assert.IsNotNull(registration, "Controller was not registered.");

            var route = RouteTable.Routes[controller.Name];
            Assert.IsNull(route, "Route was registered for the controller.");
        }
        public void RegisterController_DesignerController_NotRegistersAnyRoutes()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();
            Type controller = typeof(DesignerController);

            using (new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType<ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(), new InjectionConstructor(typeof(XmlConfigProvider).Name));

                ObjectFactory.Container.RegisterType<XmlConfigProvider, DummyConfigProvider>();

                // Act
                initializer.RegisterControllerPublic(controller);
            }

            // Assert
            ControllerInfo registration = ControllerStore.Controllers().SingleOrDefault(c => c.ControllerType == controller);
            Assert.IsNotNull(registration, "DesignerController was not registered.");

            RouteBase route = RouteTable.Routes[controller.Name];
            Assert.IsNull(route, "Route was registered for the controller.");
        }
        public void IsControllerContainer_NullFileName_ReturnsFalse()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();

            // Act
            var result = initializer.IsControllerContainerPublic(null);

            // Assert
            Assert.IsFalse(result);
        }
        public void IsControllerContainer_NonExistingFileName_ReturnsFalse()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();

            // Act
            var result = initializer.IsControllerContainerPublic("C:\\NonExistingPath\\NonExistingAssembly.dll");

            // Assert
            Assert.IsFalse(result);
        }
        public void IsControllerContainer_DesignerAssembly_ReturnTrue()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();

            // Act
            var result = initializer.IsControllerContainerPublic(typeof(DesignerController).Assembly.CodeBase);

            // Assert
            Assert.IsTrue(result);
        }
        public void Initialize_CallsRegisterVirtualPathsAndInitializeControllers()
        {
            // Arrange
            var initializer = new DummyControllerContainerInitializer();

            IEnumerable<Assembly> assemblies = null;
            initializer.RetrieveAssembliesMock = () =>
                {
                    assemblies = new List<Assembly>();
                    return assemblies;
                };

            bool registerVirtualPathsCalled = false;
            initializer.RegisterVirtualPathsMock = asm =>
                {
                    registerVirtualPathsCalled = true;
                    Assert.AreSame(assemblies, asm, "RegisterVirtualPaths was not called with the expected arguments.");
                };

            IEnumerable<Type> controllerTypes = null;
            initializer.GetControllersMock = asm =>
                {
                    Assert.AreSame(assemblies, asm, "GetControllerTypes was not called with the expected arguments.");
                    controllerTypes = new List<Type>();
                    return controllerTypes;
                };

            bool initializeControllersCalled = false;
            initializer.InitializeControllersMock = types =>
                {
                    initializeControllersCalled = true;
                    Assert.AreSame(controllerTypes, types, "InitializeControllers was not called with the expected arguments");
                };

            // Act
            initializer.Initialize();

            // Assert
            Assert.IsTrue(registerVirtualPathsCalled, "RegisterVirtualPaths was not called.");
            Assert.IsTrue(initializeControllersCalled, "InitializeControllers was not called.");
        }
        public void InitializeControllers_TwoControllers_BothAreRegisteredAndControllerFactoryIsPrepared()
        {
            // Arrange
            var registeredControllers = new List<Type>(2);
            var initializer = new DummyControllerContainerInitializer();

            using (new ObjectFactoryContainerRegion())
            {
                initializer.RegisterControllerFactoryMock = () => ObjectFactory.Container.RegisterType<ISitefinityControllerFactory, DummyControllerFactory>();

                initializer.RegisterControllerMock = registeredControllers.Add;

                // Act
                initializer.InitializeControllersPublic(new[] { typeof(DummyController), typeof(DummyControllerContainerInitializer) });
            }

            // Assert
            Assert.IsInstanceOfType(ControllerBuilder.Current.GetControllerFactory(), typeof(DummyControllerFactory), "Controller factory was not set.");
            Assert.AreEqual(2, registeredControllers.Count, "Not all widgets were registered.");
            Assert.IsTrue(registeredControllers.Contains(typeof(DummyController)), "The first controller was not registered.");
            Assert.IsTrue(registeredControllers.Contains(typeof(DummyControllerContainerInitializer)), "The second controller was not registered.");
        }