public void Exists_SecondNodeExists_ReturnsTrue()
        {
            using (new ObjectFactoryContainerRegion())
            {
                //Arrange
                ObjectFactory.Container.RegisterType<IResourceResolverStrategy, ResourceResolverStrategy>(new ContainerControlledLifetimeManager());

                var firstNode = new DummyResourceResolverNode();
                firstNode.CurrentExistsMock = (pathDefinition, virtualPath) => false;

                var secondNode = new DummyResourceResolverNode();
                secondNode.CurrentExistsMock = (pathDefinition, virtualPath) => true;

                ObjectFactory.Resolve<IResourceResolverStrategy>()
                    .SetFirst(firstNode)
                    .SetNext(secondNode);

                //Act
                var resolver = new ResourceResolver();
                var result = resolver.Exists(new PathDefinition(), "~/Test");

                //Assert
                Assert.IsTrue(result, "Resolver returned false for Exists while there is one node for which the resource exits.");
            }
        }
        public void GetCacheDependency_ReturnsFirstNodesCacheDependency()
        {
            // Arrange
            var strategy = new ResourceResolverStrategy();
            var nodeCacheDependency = new CacheDependency(Directory.GetCurrentDirectory());
            var node = new DummyResourceResolverNode();

            strategy.SetFirst(node);
            node.GetCurrentCacheDependencyMock = (pathDefinition, virtualPath, virtualPathDependencies, utcStart) => nodeCacheDependency;

            // Act
            var resultCacheDependency = strategy.GetCacheDependency(new PathDefinition(), "~/Test", null, DateTime.UtcNow);

            // Assert
            Assert.AreSame(nodeCacheDependency, resultCacheDependency, "GetCacheDependency did not return the instance that is provided by the node in the strategy.");
        }
        public void GetCacheDependency_NoCurrentDependencyCurrentExistsFalse_ReturnsNextCacheDependency()
        {
            // Arrange
            var currentNode = new DummyResourceResolverNode();

            currentNode.GetCurrentCacheDependencyMock = (pathDefinition, virtualPath, virtualPathDependencies, utcDate) => null;
            currentNode.CurrentExistsMock = (pathDefinition, virtualPath) => false;

            var nextCacheDependency = this.SetNextNode(currentNode);

            // Act
            var result = currentNode.GetCacheDependency(new PathDefinition(), "~/Test", null, DateTime.UtcNow);

            // Assert
            Assert.IsNotInstanceOfType(result, typeof(AggregateCacheDependency), "The result of GetCacheDependency methods should be instance of AggregateCacheDependency");
            Assert.AreSame(nextCacheDependency, result, "The returned cache dependency is not that of the second node.");
        }
        public void GetCacheDependency_HasCurrentDependencyCurrentExistsFalse_AggregatesCurrentCacheDependencyWithNext()
        {
            // Arrange
            var currentNode = new DummyResourceResolverNode();

            var currentCacheDependency = new CacheDependency(Directory.GetCurrentDirectory());
            currentNode.GetCurrentCacheDependencyMock = (pathDefinition, virtualPath, virtualPathDependencies, utcDate) => currentCacheDependency;
            currentNode.CurrentExistsMock = (pathDefinition, virtualPath) => false;

            var nextCacheDependency = this.SetNextNode(currentNode);

            // Act
            var result = currentNode.GetCacheDependency(new PathDefinition(), "~/Test", null, DateTime.UtcNow);

            // Assert
            Assert.IsInstanceOfType(result, typeof(AggregateCacheDependency), "Resulting cache dependency is not aggregated.");

            var dependencies = this.GetAggregatedDependencies((AggregateCacheDependency)result);

            Assert.AreEqual(2, dependencies.Count, "The dependencies count is not correct.");
            Assert.IsTrue(dependencies.Contains(currentCacheDependency), "The resulting aggregated cache dependency does not contain the dependency of the first node.");
            Assert.IsTrue(dependencies.Contains(nextCacheDependency), "The resulting aggregated cache dependency does not contain the dependency of the second node.");
        }
        public void Open_CurrentExistsFalse_ReturnsNextOpenResult()
        {
            // Arrange
            var currentNode = new DummyResourceResolverNode();
            var nextNode = new DummyResourceResolverNode();

            currentNode.GetCurrentCacheDependencyMock = (pathDefinition, virtualPath, virtualPathDependencies, utcDate) => null;
            var currentStream = new MemoryStream();
            currentNode.CurrentOpenMock = (pathDefinition, virtualPath) => currentStream;
            currentNode.CurrentExistsMock = (pathDefinition, virtualPath) => false;

            var nextCacheDependency = new CacheDependency(Directory.GetCurrentDirectory());
            var nextStream = new MemoryStream();

            nextNode.CurrentOpenMock = (pathDefinition, virtualPath) => nextStream;
            nextNode.CurrentExistsMock = (pathDefinition, virtualPath) => true;
            currentNode.SetNext(nextNode);

            // Act
            var result = currentNode.Open(new PathDefinition(), "~/Test");

            // Assert
            Assert.AreSame(nextStream, result, "The returned stream is not that of the second node.");
        }
        /// <summary>
        ///     Sets the next node.
        /// </summary>
        /// <param name="currentNode">The current node.</param>
        /// <returns></returns>
        private CacheDependency SetNextNode(DummyResourceResolverNode currentNode)
        {
            var nextNode = new DummyResourceResolverNode();

            var nextCacheDependency = new CacheDependency(Directory.GetCurrentDirectory());
            nextNode.GetCurrentCacheDependencyMock = (pathDefinition, virtualPath, virtualPathDependencies, utcDate) => nextCacheDependency;
            nextNode.CurrentExistsMock = (pathDefinition, virtualPath) => true;

            currentNode.SetNext(nextNode);

            return nextCacheDependency;
        }
        public void Open_TwoResolvers_ReturnsFirstResolverStream()
        {
            //Arrange
            using (new ObjectFactoryContainerRegion())
            {
                using (var ms1 = new MemoryStream())
                {
                    using (var ms2 = new MemoryStream())
                    {
                        ObjectFactory.Container.RegisterType<IResourceResolverStrategy, ResourceResolverStrategy>(new ContainerControlledLifetimeManager());

                        var firstNode = new DummyResourceResolverNode();
                        firstNode.CurrentExistsMock = (pathDefinition, virtualPath) => true;
                        firstNode.CurrentOpenMock = (pathDefinition, virtualPath) => ms1;

                        var secondNode = new DummyResourceResolverNode();
                        secondNode.CurrentExistsMock = (pathDefinition, virtualPath) => true;
                        secondNode.CurrentOpenMock = (pathDefinition, virtualPath) => ms2;

                        ObjectFactory.Resolve<IResourceResolverStrategy>()
                            .SetFirst(firstNode)
                            .SetNext(secondNode);

                        //Act
                        var resolver = new ResourceResolver();
                        var result = resolver.Open(new PathDefinition(), "~/Test");

                        //Assert
                        Assert.AreNotSame(ms2, result, "The stream for the second node was returned while the first one was expected.");
                        Assert.AreSame(ms1, result, "The stream for the first node was not returned.");
                    }
                }
            }
        }