File: ApplicationPartRegistryTest.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (152 lines) | stat: -rw-r--r-- 6,040 bytes parent folder | download | duplicates (11)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Web.WebPages.ApplicationParts;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.WebPages.Test.ApplicationModule
{
    public class ApplicationPartRegistryTest
    {
        [Fact]
        public void ApplicationModuleGeneratesRootRelativePaths()
        {
            // Arrange
            var path1 = "foo/bar";
            var path2 = "~/xyz/pqr";
            var root1 = "~/myappmodule";
            var root2 = "~/myappmodule2/";

            // Act 
            var actualPath11 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root1, path1);
            var actualPath12 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root1, path2);
            var actualPath21 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root2, path1);
            var actualPath22 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root2, path2);

            // Assert
            Assert.Equal(actualPath11, root1 + "/" + path1);
            Assert.Equal(actualPath12, root1 + path2.TrimStart('~'));
            Assert.Equal(actualPath21, root2 + path1);
            Assert.Equal(actualPath22, root2 + path2.TrimStart('~', '/'));
        }

        [Fact]
        public void ApplicationPartRegistryLooksUpPartsByName()
        {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.Equal(registry["my-assembly"], part);
            Assert.Equal(registry["MY-aSSembly"], part);
        }

        [Fact]
        public void ApplicationPartRegistryLooksUpPartsByAssembly()
        {
            // Arrange
            var assembly = BuildAssembly();
            var part = new ApplicationPart(assembly, "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.Equal(registry[assembly], part);
        }

        [Fact]
        public void RegisterThrowsIfAssemblyAlreadyRegistered()
        {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.Throws<InvalidOperationException>(() => registry.Register(part, myFunc),
                                                              String.Format("The assembly \"{0}\" is already registered.", part.Assembly.ToString()));
        }

        [Fact]
        public void RegisterThrowsIfPathAlreadyRegistered()
        {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            var newPart = new ApplicationPart(BuildAssembly("different-assembly"), "~/mymodule");
            Assert.Throws<InvalidOperationException>(() => registry.Register(newPart, myFunc),
                                                              "An application module is already registered for virtual path \"~/mymodule/\".");
        }

        [Fact]
        public void RegisterCreatesRoutesForValidPages()
        {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.True(dictionary.Exists("~/mymodule/Page1"));
            Assert.Equal(dictionary.CreateInstance("~/mymodule/Page1"), "foo");
            Assert.False(dictionary.Exists("~/mymodule/Page2"));
            Assert.False(dictionary.Exists("~/mymodule/Page3"));
        }

        private static IResourceAssembly BuildAssembly(string name = "my-assembly")
        {
            Mock<TestResourceAssembly> assembly = new Mock<TestResourceAssembly>();
            assembly.SetupGet(c => c.Name).Returns(name);
            assembly.Setup(c => c.GetHashCode()).Returns(name.GetHashCode());
            assembly.Setup(c => c.Equals(It.IsAny<TestResourceAssembly>())).Returns((TestResourceAssembly c) => c.Name == name);

            assembly.Setup(c => c.GetTypes()).Returns(new[]
            {
                BuildPageType(inherits: true, virtualPath: "~/Page1"),
                BuildPageType(inherits: true, virtualPath: null),
                BuildPageType(inherits: false, virtualPath: "~/Page3"),
            });

            return assembly.Object;
        }

        private static Type BuildPageType(bool inherits, string virtualPath)
        {
            Mock<Type> type = new Mock<Type>();
            type.Setup(c => c.IsSubclassOf(typeof(WebPageRenderingBase))).Returns(inherits);

            if (virtualPath != null)
            {
                type.Setup(c => c.GetCustomAttributes(typeof(PageVirtualPathAttribute), false))
                    .Returns(new[] { new PageVirtualPathAttribute(virtualPath) });
            }
            return type.Object;
        }
    }
}