public void CreateInstanceLooksThroughAllRegisteredFactoriesForExistence()
        {
            // Arrange
            var page     = Utils.CreatePage(null);
            var factory1 = new Mock <IVirtualPathFactory>();

            factory1.Setup(c => c.Exists(page.VirtualPath)).Returns(false).Verifiable();
            var factory2 = new Mock <IVirtualPathFactory>();

            factory2.Setup(c => c.Exists(page.VirtualPath)).Returns(true).Verifiable();
            factory2.Setup(c => c.CreateInstance(page.VirtualPath)).Returns(page).Verifiable();
            var factory3 = new Mock <IVirtualPathFactory>();

            factory3.Setup(c => c.Exists(page.VirtualPath)).Throws(new Exception("This factory should not be called since the page has already been found in 2"));
            var defaultFactory = new Mock <IVirtualPathFactory>();

            defaultFactory.Setup(c => c.Exists(page.VirtualPath)).Throws(new Exception("This factory should not be called since it always called last"));

            var vpfm = new VirtualPathFactoryManager(defaultFactory.Object);

            vpfm.RegisterVirtualPathFactoryInternal(factory1.Object);
            vpfm.RegisterVirtualPathFactoryInternal(factory2.Object);
            vpfm.RegisterVirtualPathFactoryInternal(factory3.Object);

            // Act
            var result = vpfm.CreateInstance(page.VirtualPath);

            // Assert
            Assert.Equal(page, result);

            factory1.Verify();
            factory2.Verify();
        }
        public void GenericCreateInstanceReturnsNullIfNoFactoryCanCreateVirtualPath()
        {
            // Arrange
            var factory1 = new HashVirtualPathFactory(Utils.CreatePage(_ => { }, "~/index.cshtml"));
            var factory2 = new HashVirtualPathFactory(Utils.CreatePage(null, "~/_admin/index.cshtml"));

            // Act
            var factoryManager = new VirtualPathFactoryManager(factory2);
            factoryManager.RegisterVirtualPathFactoryInternal(factory1);
            var page = factoryManager.CreateInstance<WebPageBase>("~/does-not-exist.cshtml");

            // Assert
            Assert.Null(page);
        }
Esempio n. 3
0
        public void GenericCreateInstanceReturnsNullIfNoFactoryCanCreateVirtualPath()
        {
            // Arrange
            var factory1 = new HashVirtualPathFactory(Utils.CreatePage(_ => { }, "~/index.cshtml"));
            var factory2 = new HashVirtualPathFactory(Utils.CreatePage(null, "~/_admin/index.cshtml"));

            // Act
            var factoryManager = new VirtualPathFactoryManager(factory2);

            factoryManager.RegisterVirtualPathFactoryInternal(factory1);
            var page = factoryManager.CreateInstance <WebPageBase>("~/does-not-exist.cshtml");

            // Assert
            Assert.Null(page);
        }
        public void GenericCreateInstanceLoopsOverAllRegisteredFactories()
        {
            // Arrange
            var virtualPath = "~/index.cshtml";
            var mockPage = Utils.CreatePage(_ => { }, virtualPath);
            var factory1 = new HashVirtualPathFactory(mockPage);
            var factory2 = new HashVirtualPathFactory(Utils.CreatePage(null, "~/_admin/index.cshtml"));

            // Act
            var factoryManager = new VirtualPathFactoryManager(factory2);
            factoryManager.RegisterVirtualPathFactoryInternal(factory1);
            var page = factoryManager.CreateInstance<WebPageBase>(virtualPath);

            // Assert
            Assert.Equal(mockPage, page);
        }
Esempio n. 5
0
        public void GenericCreateInstanceLoopsOverAllRegisteredFactories()
        {
            // Arrange
            var virtualPath = "~/index.cshtml";
            var mockPage    = Utils.CreatePage(_ => { }, virtualPath);
            var factory1    = new HashVirtualPathFactory(mockPage);
            var factory2    = new HashVirtualPathFactory(Utils.CreatePage(null, "~/_admin/index.cshtml"));

            // Act
            var factoryManager = new VirtualPathFactoryManager(factory2);

            factoryManager.RegisterVirtualPathFactoryInternal(factory1);
            var page = factoryManager.CreateInstance <WebPageBase>(virtualPath);

            // Assert
            Assert.Equal(mockPage, page);
        }
        public void VirtualPathFactoryExtensionsSpecialCasesVirtualPathFactoryManager()
        {
            // Arrange
            var virtualPath = "~/index.cshtml";
            var mockPage = Utils.CreatePage(_ => { }, virtualPath);
            var factory = new Mock<IVirtualPathFactory>();
            factory.Setup(c => c.Exists(virtualPath)).Returns(true).Verifiable();
            factory.Setup(c => c.CreateInstance(virtualPath)).Returns(mockPage);

            // Act
            var factoryManager = new VirtualPathFactoryManager(factory.Object);
            var page = factoryManager.CreateInstance<WebPageBase>(virtualPath);

            // Assert
            Assert.Equal(mockPage, page);
            factory.Verify();
        }
Esempio n. 7
0
        public void VirtualPathFactoryExtensionsSpecialCasesVirtualPathFactoryManager()
        {
            // Arrange
            var virtualPath = "~/index.cshtml";
            var mockPage    = Utils.CreatePage(_ => { }, virtualPath);
            var factory     = new Mock <IVirtualPathFactory>();

            factory.Setup(c => c.Exists(virtualPath)).Returns(true).Verifiable();
            factory.Setup(c => c.CreateInstance(virtualPath)).Returns(mockPage);

            // Act
            var factoryManager = new VirtualPathFactoryManager(factory.Object);
            var page           = factoryManager.CreateInstance <WebPageBase>(virtualPath);

            // Assert
            Assert.Equal(mockPage, page);
            factory.Verify();
        }
        internal static IHttpHandler CreateFromVirtualPath(string virtualPath, VirtualPathFactoryManager virtualPathFactoryManager) {
            // Instantiate the page from the virtual path
            object instance = virtualPathFactoryManager.CreateInstance<object>(virtualPath);

            WebPage page = instance as WebPage;

            // If it's not a page, assume it's a regular handler
            if (page == null) {
                return (IHttpHandler)instance;
            }

            // Mark it as a 'top level' page (as opposed to a user control or master)
            page.TopLevelPage = true;

            // Give it its virtual path
            page.VirtualPath = virtualPath;

            // Return a handler over it
            return new WebPageHttpHandler(page);
        }
Esempio n. 9
0
        internal static WebPageBase CreateInstanceFromVirtualPath(string virtualPath, VirtualPathFactoryManager virtualPathFactoryManager) {
            // Get the compiled object (through the VPP)
            try {
                WebPageBase webPage = virtualPathFactoryManager.CreateInstance<WebPageBase>(virtualPath);

                // Give it its virtual path
                webPage.VirtualPath = virtualPath;

                return webPage;
            }
            catch (HttpException e) {
                Util.ThrowIfUnsupportedExtension(virtualPath, e);
                throw;
            }
        }
        public void CreateInstanceLooksThroughAllRegisteredFactoriesForExistence()
        {
            // Arrange
            var page = Utils.CreatePage(null);
            var factory1 = new Mock<IVirtualPathFactory>();
            factory1.Setup(c => c.Exists(page.VirtualPath)).Returns(false).Verifiable();
            var factory2 = new Mock<IVirtualPathFactory>();
            factory2.Setup(c => c.Exists(page.VirtualPath)).Returns(true).Verifiable();
            factory2.Setup(c => c.CreateInstance(page.VirtualPath)).Returns(page).Verifiable();
            var factory3 = new Mock<IVirtualPathFactory>();
            factory3.Setup(c => c.Exists(page.VirtualPath)).Throws(new Exception("This factory should not be called since the page has already been found in 2"));
            var defaultFactory = new Mock<IVirtualPathFactory>();
            defaultFactory.Setup(c => c.Exists(page.VirtualPath)).Throws(new Exception("This factory should not be called since it always called last"));

            var vpfm = new VirtualPathFactoryManager(defaultFactory.Object);
            vpfm.RegisterVirtualPathFactoryInternal(factory1.Object);
            vpfm.RegisterVirtualPathFactoryInternal(factory2.Object);
            vpfm.RegisterVirtualPathFactoryInternal(factory3.Object);

            // Act
            var result = vpfm.CreateInstance(page.VirtualPath);

            // Assert
            Assert.Equal(page, result);

            factory1.Verify();
            factory2.Verify();
        }