public void GetCacheDependency_ExistingResource_ReturnsDependencyWithId()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";
            Guid presentationId = new Guid("4B9F6715-014E-4B1E-887F-428AF942EAC8");

            var resolver = new DummyDatabaseResourceResolver();
            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation(null, presentationId);

            CacheDependency result = null;
            using (var objFactory = new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType<ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(),
                    new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType<XmlConfigProvider, DummyConfigProvider>();
                Config.RegisterSection<SystemConfig>();

                result = resolver.GetCacheDependency(new PathDefinition(), virtualPath, null, DateTime.UtcNow);
            }
            Assert.IsInstanceOfType(result, typeof(ControlPresentationCacheDependency), "Returned cache dependency was not of the expected type.");

            var presentationDependency = (ControlPresentationCacheDependency)result;
            var itemId = presentationDependency.GetType().GetField("itemId", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(presentationDependency) as string;
            Assert.AreEqual(presentationId.ToString().ToLower(), itemId.ToLower(),
                "GetCacheDependency did not return dependency on the expected object.");
        }
        public void Exists_NonExistingResource_ReturnsFalse()
        {
            var resolver = new DummyDatabaseResourceResolver();
            var result = resolver.Exists(new PathDefinition(), "Test/MyTemplate.cshtml");

            Assert.IsFalse(result, "DatabaseResourceResolver.Exists returned True for a non-existing resource.");
        }
        public void GetCacheDependency_ExistingResource_ReturnsDependencyWithId()
        {
            const string virtualPath    = "Test/MyTemplate.cshtml";
            Guid         presentationId = new Guid("4B9F6715-014E-4B1E-887F-428AF942EAC8");

            var resolver = new DummyDatabaseResourceResolver();

            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation(null, presentationId);

            CacheDependency result = null;

            using (var objFactory = new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType <ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(),
                                                                                    new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType <XmlConfigProvider, DummyConfigProvider>();
                Config.RegisterSection <SystemConfig>();

                result = resolver.GetCacheDependency(new PathDefinition(), virtualPath, null, DateTime.UtcNow);
            }
            Assert.IsInstanceOfType(result, typeof(ControlPresentationCacheDependency), "Returned cache dependency was not of the expected type.");

            var presentationDependency = (ControlPresentationCacheDependency)result;
            var itemId = presentationDependency.GetType().GetField("itemId", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(presentationDependency) as string;

            Assert.AreEqual(presentationId.ToString().ToLower(), itemId.ToLower(),
                            "GetCacheDependency did not return dependency on the expected object.");
        }
        public void Exists_NonExistingResource_ReturnsFalse()
        {
            var resolver = new DummyDatabaseResourceResolver();
            var result   = resolver.Exists(new PathDefinition(), "Test/MyTemplate.cshtml");

            Assert.IsFalse(result, "DatabaseResourceResolver.Exists returned True for a non-existing resource.");
        }
        public void Exists_ResourceWithNullContent_ReturnsFalse()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";

            var resolver = new DummyDatabaseResourceResolver();
            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation() { Data = null };
            var result = resolver.Exists(new PathDefinition(), virtualPath);

            Assert.IsFalse(result, "DatabaseResourceResolver.Exists returned True for resource with null content.");
        }
        public void Exists_ExistingResource_ReturnsTrue()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";

            var resolver = new DummyDatabaseResourceResolver();
            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation() { Data = "<div>Content</div>" };
            var result = resolver.Exists(new PathDefinition(), virtualPath);

            Assert.IsTrue(result, "DatabaseResourceResolver.Exists returned False for an existing resource.");
        }
        public void GetCacheDependency_NonExistingResource_ReturnsDependencyWithoutId()
        {
            //Arrange
            const string virtualPath = "Test/MyTemplate.cshtml";
            var          resolver    = new DummyDatabaseResourceResolver();

            //Act
            var presentationDependency = this.GetCacheDependencyResult(resolver, virtualPath);

            //Assert
            var itemId = presentationDependency.GetType().GetField("itemId", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(presentationDependency) as string;

            Assert.IsNull(itemId, "The returned CacheDependency has an unexpected key.");
        }
        public void Exists_ResourceWithNullContent_ReturnsFalse()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";

            var resolver = new DummyDatabaseResourceResolver();

            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation()
            {
                Data = null
            };
            var result = resolver.Exists(new PathDefinition(), virtualPath);

            Assert.IsFalse(result, "DatabaseResourceResolver.Exists returned True for resource with null content.");
        }
        public void Exists_ExistingResource_ReturnsTrue()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";

            var resolver = new DummyDatabaseResourceResolver();

            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation()
            {
                Data = "<div>Content</div>"
            };
            var result = resolver.Exists(new PathDefinition(), virtualPath);

            Assert.IsTrue(result, "DatabaseResourceResolver.Exists returned False for an existing resource.");
        }
        public void GetCacheDependency_ExistingResource_ReturnsDependencyWithId()
        {
            // Arrange
            const string VirtualPath    = "Test/MyTemplate.cshtml";
            var          presentationId = new Guid("4B9F6715-014E-4B1E-887F-428AF942EAC8");

            var resolver = new DummyDatabaseResourceResolver();

            resolver.ControlPresentationResult[VirtualPath] = new ControlPresentation(null, presentationId);

            // Act
            var presentationDependency = this.GetCacheDependencyResult(resolver, VirtualPath);

            // Assert
            var itemId = presentationDependency.GetType().GetField("itemId", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(presentationDependency) as string;

            Assert.AreEqual(presentationId.ToString().ToLowerInvariant(), itemId.ToLowerInvariant(), "GetCacheDependency did not return dependency on the expected object.");
        }
        /// <summary>
        /// Gets the cache dependency result.
        /// </summary>
        /// <param name="resolver">The resolver.</param>
        /// <param name="virtualPath">The virtual path.</param>
        /// <returns></returns>
        private ControlPresentationCacheDependency GetCacheDependencyResult(DummyDatabaseResourceResolver resolver, string virtualPath)
        {
            CacheDependency result = null;

            using (var objFactory = new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType <ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(), new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType <XmlConfigProvider, DummyConfigProvider>();
                Config.RegisterSection <SystemConfig>();

                result = resolver.GetCacheDependency(new PathDefinition(), virtualPath, null, DateTime.UtcNow);
            }

            Assert.IsInstanceOfType(result, typeof(ControlPresentationCacheDependency), "Returned cache dependency was not of the expected type.");

            var presentationDependency = (ControlPresentationCacheDependency)result;

            return(presentationDependency);
        }
        public void Open_ExistingResource_StreamsResourceData()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";
            const string content     = "<div>Content</div>";

            var resolver = new DummyDatabaseResourceResolver();

            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation()
            {
                Data = content
            };
            var result = resolver.Open(new PathDefinition(), virtualPath);

            Assert.IsNotNull(result, "Open method returned null.");

            using (var reader = new StreamReader(result))
            {
                var returnedData = reader.ReadToEnd();
                Assert.AreEqual(content, returnedData, "The stream did not contain the expected data.");
            }
        }
        public void GetCacheDependency_NonExistingResource_ReturnsDependencyWithoutId()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";

            var             resolver = new DummyDatabaseResourceResolver();
            CacheDependency result   = null;

            using (var objFactory = new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType <ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(),
                                                                                    new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType <XmlConfigProvider, DummyConfigProvider>();
                Config.RegisterSection <SystemConfig>();

                result = resolver.GetCacheDependency(new PathDefinition(), virtualPath, null, DateTime.UtcNow);
            }
            Assert.IsInstanceOfType(result, typeof(ControlPresentationCacheDependency), "Returned cache dependency was not of the expected type.");

            var presentationDependency = (ControlPresentationCacheDependency)result;
            var itemId = presentationDependency.GetType().GetField("itemId", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(presentationDependency) as string;

            Assert.IsNull(itemId, "The returned CacheDependency has an unexpected key.");
        }
        public void Open_ExistingResource_StreamsResourceData()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";
            const string content = "<div>Content</div>";

            var resolver = new DummyDatabaseResourceResolver();
            resolver.GetControlPresentationResult[virtualPath] = new ControlPresentation() { Data = content };
            var result = resolver.Open(new PathDefinition(), virtualPath);

            Assert.IsNotNull(result, "Open method returned null.");

            using (var reader = new StreamReader(result))
            {
                var returnedData = reader.ReadToEnd();
                Assert.AreEqual(content, returnedData, "The stream did not contain the expected data.");
            }
        }
        public void GetCacheDependency_NonExistingResource_ReturnsDependencyWithoutId()
        {
            const string virtualPath = "Test/MyTemplate.cshtml";

            var resolver = new DummyDatabaseResourceResolver();
            CacheDependency result = null;
            using (var objFactory = new ObjectFactoryContainerRegion())
            {
                ObjectFactory.Container.RegisterType<ConfigManager, ConfigManager>(typeof(XmlConfigProvider).Name.ToUpperInvariant(),
                    new InjectionConstructor(typeof(XmlConfigProvider).Name));
                ObjectFactory.Container.RegisterType<XmlConfigProvider, DummyConfigProvider>();
                Config.RegisterSection<SystemConfig>();

                result = resolver.GetCacheDependency(new PathDefinition(), virtualPath, null, DateTime.UtcNow);
            }
            Assert.IsInstanceOfType(result, typeof(ControlPresentationCacheDependency), "Returned cache dependency was not of the expected type.");

            var presentationDependency = (ControlPresentationCacheDependency)result;
            var itemId = presentationDependency.GetType().GetField("itemId", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(presentationDependency) as string;
            Assert.IsNull(itemId, "The returned CacheDependency has an unexpected key.");
        }