1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq;
using Moq;
using Xunit;
namespace System.Web.WebPages.Test
{
public class VirtualPathFactoryManagerTest
{
[Fact]
public void DefaultFactoryIsListedInRegisteredFactories()
{
// Arrange
var factory = new HashVirtualPathFactory();
// Act
var factoryManager = new VirtualPathFactoryManager(factory);
// Assert
Assert.Equal(factory, factoryManager.RegisteredFactories.Single());
}
[Fact]
public void RegisterFactoryEnsuresDefaultFactoryRemainsTheLast()
{
// Arrange
var defaultFactory = new HashVirtualPathFactory();
var factory1 = new HashVirtualPathFactory();
var factory2 = new HashVirtualPathFactory();
var factory3 = new HashVirtualPathFactory();
// Act
var factoryManager = new VirtualPathFactoryManager(defaultFactory);
factoryManager.RegisterVirtualPathFactoryInternal(factory1);
factoryManager.RegisterVirtualPathFactoryInternal(factory2);
factoryManager.RegisterVirtualPathFactoryInternal(factory3);
// Assert
Assert.Equal(factory1, factoryManager.RegisteredFactories.ElementAt(0));
Assert.Equal(factory2, factoryManager.RegisteredFactories.ElementAt(1));
Assert.Equal(factory3, factoryManager.RegisteredFactories.ElementAt(2));
Assert.Equal(defaultFactory, factoryManager.RegisteredFactories.Last());
}
[Fact]
public void CreateInstanceUsesRegisteredFactoriesForExistence()
{
// Arrange
var path = "~/index.cshtml";
var factory1 = new Mock<IVirtualPathFactory>();
factory1.Setup(c => c.Exists(path)).Returns(false).Verifiable();
var factory2 = new Mock<IVirtualPathFactory>();
factory2.Setup(c => c.Exists(path)).Returns(true).Verifiable();
var factory3 = new Mock<IVirtualPathFactory>();
factory3.Setup(c => c.Exists(path)).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(path)).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.Exists(path);
// Assert
Assert.True(result);
factory1.Verify();
factory2.Verify();
}
[Fact]
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();
}
}
}
|